|
| 1 | +package com.digiventure.ventnote |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import androidx.compose.ui.test.* |
| 5 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 6 | +import androidx.test.espresso.intent.Intents |
| 7 | +import androidx.test.espresso.intent.Intents.intended |
| 8 | +import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction |
| 9 | +import androidx.test.espresso.intent.matcher.IntentMatchers.hasData |
| 10 | +import com.digiventure.utils.BaseAcceptanceTest |
| 11 | +import com.digiventure.ventnote.commons.TestTags |
| 12 | +import dagger.hilt.android.testing.HiltAndroidRule |
| 13 | +import dagger.hilt.android.testing.HiltAndroidTest |
| 14 | +import org.hamcrest.Matchers.allOf |
| 15 | +import org.junit.After |
| 16 | +import org.junit.Before |
| 17 | +import org.junit.Rule |
| 18 | +import org.junit.Test |
| 19 | + |
| 20 | +@HiltAndroidTest |
| 21 | +class NavDrawerFeature : BaseAcceptanceTest() { |
| 22 | + |
| 23 | + @get:Rule(order = 0) |
| 24 | + val hiltRule = HiltAndroidRule(this) |
| 25 | + |
| 26 | + @get:Rule(order = 1) |
| 27 | + val composeTestRule = createAndroidComposeRule<MainActivity>() |
| 28 | + |
| 29 | + @Before |
| 30 | + fun setUp() { |
| 31 | + hiltRule.inject() |
| 32 | + Intents.init() |
| 33 | + |
| 34 | + // Open the drawer |
| 35 | + composeTestRule.onNodeWithTag(TestTags.NOTES_PAGE).assertIsDisplayed() |
| 36 | + composeTestRule.onNodeWithTag(TestTags.MENU_ICON_BUTTON).performClick() |
| 37 | + composeTestRule.waitForIdle() |
| 38 | + composeTestRule.onNodeWithTag(TestTags.NAV_DRAWER, useUnmergedTree = true).assertIsDisplayed() |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + fun tearDown() { |
| 43 | + Intents.release() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun initialState_showsMenuItems() { |
| 48 | + composeTestRule.onNodeWithTag(TestTags.RATE_APP_TILE).assertIsDisplayed() |
| 49 | + composeTestRule.onNodeWithTag(TestTags.MORE_APPS_TILE).assertIsDisplayed() |
| 50 | + composeTestRule.onNodeWithTag(TestTags.APP_VERSION_TILE).assertIsDisplayed() |
| 51 | + composeTestRule.onNodeWithTag(TestTags.THEME_TILE).assertIsDisplayed() |
| 52 | + composeTestRule.onNodeWithTag(TestTags.COLOR_MODE_TILE).assertIsDisplayed() |
| 53 | + composeTestRule.onNodeWithTag(TestTags.BACKUP_TILE).assertIsDisplayed() |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun rateApp_launchesPlayStore() { |
| 58 | + composeTestRule.onNodeWithTag(TestTags.RATE_APP_TILE).performClick() |
| 59 | + |
| 60 | + intended(allOf( |
| 61 | + hasAction(Intent.ACTION_VIEW), |
| 62 | + hasData("https://play.google.com/store/apps/details?id=com.digiventure.ventnote") |
| 63 | + )) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun moreApps_launchesDeveloperPage() { |
| 68 | + composeTestRule.onNodeWithTag(TestTags.MORE_APPS_TILE).performClick() |
| 69 | + |
| 70 | + intended(allOf( |
| 71 | + hasAction(Intent.ACTION_VIEW), |
| 72 | + hasData("https://play.google.com/store/apps/developer?id=Mattrmost") |
| 73 | + )) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun backupNavigation_navigatesToBackupPage() { |
| 78 | + composeTestRule.onNodeWithTag(TestTags.BACKUP_TILE).performClick() |
| 79 | + composeTestRule.waitForIdle() |
| 80 | + |
| 81 | + // Use text or tag to verify backup page (usually has "Backup" title in header) |
| 82 | + composeTestRule.onNodeWithText("Backup Notes").assertIsDisplayed() |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun themeColorChange_updatesTheme() { |
| 87 | + // Verify we can click all theme colors |
| 88 | + composeTestRule.onNodeWithTag(TestTags.THEME_COLOR_PURPLE).performClick() |
| 89 | + composeTestRule.onNodeWithTag(TestTags.THEME_COLOR_CRIMSON).performClick() |
| 90 | + composeTestRule.onNodeWithTag(TestTags.THEME_COLOR_CADMIUM_GREEN).performClick() |
| 91 | + composeTestRule.onNodeWithTag(TestTags.THEME_COLOR_COBALT_BLUE).performClick() |
| 92 | + |
| 93 | + // Clicks should not crash and should update internal state (hard to verify without custom matchers) |
| 94 | + composeTestRule.onNodeWithTag(TestTags.THEME_TILE).assertIsDisplayed() |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun colorModeToggle_updatesMode() { |
| 99 | + // Find the current mode by checking the subtitle text |
| 100 | + val lightModeText = "switch to light mode" |
| 101 | + val darkModeText = "switch to dark mode" |
| 102 | + |
| 103 | + // Initial click to toggle (assuming default is light mode or whatever) |
| 104 | + // We look for either text to be sure |
| 105 | + val initialNode = composeTestRule.onNode( |
| 106 | + hasText(lightModeText, ignoreCase = true) or hasText(darkModeText, ignoreCase = true) |
| 107 | + ) |
| 108 | + |
| 109 | + initialNode.assertIsDisplayed() |
| 110 | + val isInitiallyLight = try { |
| 111 | + composeTestRule.onNodeWithText(darkModeText, ignoreCase = true).assertIsDisplayed() |
| 112 | + true // It says "switch to dark", so it is currently light |
| 113 | + } catch (_: Throwable) { |
| 114 | + false |
| 115 | + } |
| 116 | + |
| 117 | + // Toggle |
| 118 | + composeTestRule.onNodeWithTag(TestTags.COLOR_MODE_TILE).performClick() |
| 119 | + composeTestRule.waitForIdle() |
| 120 | + |
| 121 | + // Verify text swapped |
| 122 | + if (isInitiallyLight) { |
| 123 | + composeTestRule.onNodeWithText(lightModeText, ignoreCase = true).assertIsDisplayed() |
| 124 | + } else { |
| 125 | + composeTestRule.onNodeWithText(darkModeText, ignoreCase = true).assertIsDisplayed() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun drawer_canBeClosed() { |
| 131 | + // Click outside or use a specific close mechanism if available, |
| 132 | + // but here we can just swipe or click the content area if accessible. |
| 133 | + // Easiest is to just verify it closes on back press |
| 134 | + androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(android.view.KeyEvent.KEYCODE_BACK) |
| 135 | + composeTestRule.waitForIdle() |
| 136 | + composeTestRule.onNodeWithTag(TestTags.NAV_DRAWER).assertDoesNotExist() |
| 137 | + } |
| 138 | +} |
0 commit comments