|
15 | 15 | */
|
16 | 16 | package com.example.android.architecture.blueprints.todoapp.tasks
|
17 | 17 |
|
18 |
| -import android.view.Gravity |
| 18 | +import androidx.activity.ComponentActivity |
| 19 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
19 | 20 | import androidx.compose.ui.test.assertIsDisplayed
|
| 21 | +import androidx.compose.ui.test.assertIsNotDisplayed |
20 | 22 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
|
| 23 | +import androidx.compose.ui.test.onAllNodesWithText |
21 | 24 | import androidx.compose.ui.test.onNodeWithContentDescription
|
22 | 25 | import androidx.compose.ui.test.onNodeWithText
|
23 | 26 | import androidx.compose.ui.test.performClick
|
24 |
| -import androidx.drawerlayout.widget.DrawerLayout |
25 |
| -import androidx.navigation.findNavController |
26 | 27 | import androidx.test.core.app.ApplicationProvider.getApplicationContext
|
27 |
| -import androidx.test.espresso.Espresso.onView |
28 | 28 | import androidx.test.espresso.Espresso.pressBack
|
29 |
| -import androidx.test.espresso.IdlingRegistry |
30 |
| -import androidx.test.espresso.action.ViewActions.click |
31 |
| -import androidx.test.espresso.assertion.ViewAssertions.matches |
32 |
| -import androidx.test.espresso.contrib.DrawerActions.open |
33 |
| -import androidx.test.espresso.contrib.DrawerMatchers.isClosed |
34 |
| -import androidx.test.espresso.contrib.DrawerMatchers.isOpen |
35 |
| -import androidx.test.espresso.contrib.NavigationViewActions.navigateTo |
36 |
| -import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
37 |
| -import androidx.test.espresso.matcher.ViewMatchers.withContentDescription |
38 |
| -import androidx.test.espresso.matcher.ViewMatchers.withId |
39 |
| -import androidx.test.espresso.matcher.ViewMatchers.withText |
40 | 29 | import androidx.test.ext.junit.runners.AndroidJUnit4
|
41 | 30 | import androidx.test.filters.LargeTest
|
| 31 | +import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread |
42 | 32 | import com.example.android.architecture.blueprints.todoapp.R
|
43 | 33 | import com.example.android.architecture.blueprints.todoapp.ServiceLocator
|
| 34 | +import com.example.android.architecture.blueprints.todoapp.TodoNavGraph |
44 | 35 | import com.example.android.architecture.blueprints.todoapp.data.Task
|
45 | 36 | import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
|
46 |
| -import com.example.android.architecture.blueprints.todoapp.util.DataBindingIdlingResource |
47 |
| -import com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource |
48 |
| -import com.example.android.architecture.blueprints.todoapp.util.monitorActivity |
49 | 37 | import com.example.android.architecture.blueprints.todoapp.util.saveTaskBlocking
|
| 38 | +import com.google.accompanist.appcompattheme.AppCompatTheme |
| 39 | +import kotlinx.coroutines.Dispatchers |
50 | 40 | import org.junit.After
|
| 41 | +import org.junit.Assert.assertTrue |
51 | 42 | import org.junit.Before
|
52 | 43 | import org.junit.Rule
|
53 | 44 | import org.junit.Test
|
54 | 45 | import org.junit.runner.RunWith
|
55 | 46 |
|
56 | 47 | /**
|
57 |
| - * Tests for the [DrawerLayout] layout component in [TasksActivity] which manages |
58 |
| - * navigation within the app. |
| 48 | + * Tests for scenarios that requires navigating within the app. |
59 | 49 | */
|
60 | 50 | @RunWith(AndroidJUnit4::class)
|
61 | 51 | @LargeTest
|
62 | 52 | class AppNavigationTest {
|
63 | 53 |
|
64 | 54 | private lateinit var tasksRepository: TasksRepository
|
65 | 55 |
|
66 |
| - // An Idling Resource that waits for Data Binding to have no pending bindings |
67 |
| - private val dataBindingIdlingResource = DataBindingIdlingResource() |
68 |
| - |
69 | 56 | @get:Rule
|
70 |
| - val composeTestRule = createAndroidComposeRule<TasksActivity>() |
| 57 | + val composeTestRule = createAndroidComposeRule<ComponentActivity>() |
71 | 58 | private val activity get() = composeTestRule.activity
|
72 | 59 |
|
| 60 | + // Executes tasks in the Architecture Components in the same thread |
| 61 | + @get:Rule |
| 62 | + var instantTaskExecutorRule = InstantTaskExecutorRule() |
| 63 | + |
73 | 64 | @Before
|
74 | 65 | fun init() {
|
75 |
| - tasksRepository = ServiceLocator.provideTasksRepository(getApplicationContext()) |
| 66 | + // Run on UI thread to make sure the same instance of the SL is used. |
| 67 | + runOnUiThread { |
| 68 | + ServiceLocator.createDataBase(getApplicationContext(), inMemory = true) |
| 69 | + ServiceLocator.ioDispatcher = Dispatchers.Unconfined |
| 70 | + tasksRepository = ServiceLocator.provideTasksRepository(getApplicationContext()) |
| 71 | + } |
76 | 72 | }
|
77 | 73 |
|
78 | 74 | @After
|
79 | 75 | fun reset() {
|
80 |
| - ServiceLocator.resetRepository() |
81 |
| - } |
82 |
| - |
83 |
| - /** |
84 |
| - * Idling resources tell Espresso that the app is idle or busy. This is needed when operations |
85 |
| - * are not scheduled in the main Looper (for example when executed on a different thread). |
86 |
| - */ |
87 |
| - @Before |
88 |
| - fun registerIdlingResource() { |
89 |
| - IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource) |
90 |
| - IdlingRegistry.getInstance().register(dataBindingIdlingResource) |
91 |
| - } |
92 |
| - |
93 |
| - /** |
94 |
| - * Unregister your Idling Resource so it can be garbage collected and does not leak any memory. |
95 |
| - */ |
96 |
| - @After |
97 |
| - fun unregisterIdlingResource() { |
98 |
| - IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource) |
99 |
| - IdlingRegistry.getInstance().unregister(dataBindingIdlingResource) |
| 76 | + runOnUiThread { |
| 77 | + ServiceLocator.resetRepository() |
| 78 | + ServiceLocator.resetIODispatcher() |
| 79 | + } |
100 | 80 | }
|
101 | 81 |
|
102 | 82 | @Test
|
103 | 83 | fun drawerNavigationFromTasksToStatistics() {
|
104 |
| - // start up Tasks screen |
105 |
| - dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario) |
106 |
| - |
107 |
| - onView(withId(R.id.drawer_layout)) |
108 |
| - .check(matches(isClosed(Gravity.START))) // Left Drawer should be closed. |
109 |
| - .perform(open()) // Open Drawer |
| 84 | + setContent() |
110 | 85 |
|
| 86 | + openDrawer() |
111 | 87 | // Start statistics screen.
|
112 |
| - onView(withId(R.id.nav_view)) |
113 |
| - .perform(navigateTo(R.id.statistics_fragment_dest)) |
114 |
| - |
| 88 | + composeTestRule.onNodeWithText(activity.getString(R.string.statistics_title)).performClick() |
115 | 89 | // Check that statistics screen was opened.
|
116 |
| - composeTestRule.onNodeWithText("You have no tasks.").assertIsDisplayed() |
117 |
| - composeTestRule.waitForIdle() |
118 |
| - |
119 |
| - onView(withId(R.id.drawer_layout)) |
120 |
| - .check(matches(isClosed(Gravity.START))) // Left Drawer should be closed. |
121 |
| - .perform(open()) // Open Drawer |
| 90 | + composeTestRule.onNodeWithText(activity.getString(R.string.statistics_no_tasks)) |
| 91 | + .assertIsDisplayed() |
122 | 92 |
|
| 93 | + openDrawer() |
123 | 94 | // Start tasks screen.
|
124 |
| - onView(withId(R.id.nav_view)) |
125 |
| - .perform(navigateTo(R.id.tasks_fragment_dest)) |
126 |
| - |
| 95 | + composeTestRule.onNodeWithText(activity.getString(R.string.list_title)).performClick() |
127 | 96 | // Check that tasks screen was opened.
|
128 |
| - composeTestRule.onNodeWithText("You have no tasks!").assertIsDisplayed() |
| 97 | + composeTestRule.onNodeWithText(activity.getString(R.string.no_tasks_all)) |
| 98 | + .assertIsDisplayed() |
129 | 99 | }
|
130 | 100 |
|
131 | 101 | @Test
|
132 | 102 | fun tasksScreen_clickOnAndroidHomeIcon_OpensNavigation() {
|
133 |
| - // start up Tasks screen |
134 |
| - dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario) |
| 103 | + setContent() |
135 | 104 |
|
136 | 105 | // Check that left drawer is closed at startup
|
137 |
| - onView(withId(R.id.drawer_layout)) |
138 |
| - .check(matches(isClosed(Gravity.START))) // Left Drawer should be closed. |
| 106 | + composeTestRule.onNodeWithText(activity.getString(R.string.list_title)) |
| 107 | + .assertIsNotDisplayed() |
| 108 | + composeTestRule.onNodeWithText(activity.getString(R.string.statistics_title)) |
| 109 | + .assertIsNotDisplayed() |
139 | 110 |
|
140 |
| - // Open Drawer |
141 |
| - onView( |
142 |
| - withContentDescription( |
143 |
| - composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription() |
144 |
| - ) |
145 |
| - ).perform(click()) |
| 111 | + openDrawer() |
146 | 112 |
|
147 | 113 | // Check if drawer is open
|
148 |
| - onView(withId(R.id.drawer_layout)) |
149 |
| - .check(matches(isOpen(Gravity.START))) // Left drawer is open open. |
| 114 | + composeTestRule.onNodeWithText(activity.getString(R.string.list_title)).assertIsDisplayed() |
| 115 | + composeTestRule.onNodeWithText(activity.getString(R.string.statistics_title)) |
| 116 | + .assertIsDisplayed() |
150 | 117 | }
|
151 | 118 |
|
152 | 119 | @Test
|
153 | 120 | fun statsScreen_clickOnAndroidHomeIcon_OpensNavigation() {
|
154 |
| - dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario) |
| 121 | + setContent() |
155 | 122 |
|
156 | 123 | // When the user navigates to the stats screen
|
157 |
| - composeTestRule.activityRule.scenario.onActivity { |
158 |
| - it.findNavController(R.id.nav_host_fragment).navigate(R.id.statistics_fragment_dest) |
159 |
| - } |
160 |
| - composeTestRule.waitForIdle() |
161 |
| - |
162 |
| - // Then check that left drawer is closed at startup |
163 |
| - onView(withId(R.id.drawer_layout)) |
164 |
| - .check(matches(isClosed(Gravity.START))) // Left Drawer should be closed. |
165 |
| - |
166 |
| - // When the drawer is opened |
167 |
| - onView( |
168 |
| - withContentDescription( |
169 |
| - composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription() |
170 |
| - ) |
171 |
| - ).perform(click()) |
172 |
| - |
173 |
| - // Then check that the drawer is open |
174 |
| - onView(withId(R.id.drawer_layout)) |
175 |
| - .check(matches(isOpen(Gravity.START))) // Left drawer is open open. |
| 124 | + openDrawer() |
| 125 | + composeTestRule.onNodeWithText(activity.getString(R.string.statistics_title)).performClick() |
| 126 | + |
| 127 | + composeTestRule.onNodeWithText(activity.getString(R.string.list_title)) |
| 128 | + .assertIsNotDisplayed() |
| 129 | + |
| 130 | + openDrawer() |
| 131 | + |
| 132 | + // Check if drawer is open |
| 133 | + composeTestRule.onNodeWithText(activity.getString(R.string.list_title)).assertIsDisplayed() |
| 134 | + assertTrue( |
| 135 | + composeTestRule.onAllNodesWithText(activity.getString(R.string.statistics_title)) |
| 136 | + .fetchSemanticsNodes().isNotEmpty() |
| 137 | + ) |
176 | 138 | }
|
177 | 139 |
|
178 | 140 | @Test
|
179 | 141 | fun taskDetailScreen_doubleUIBackButton() {
|
180 |
| - dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario) |
181 |
| - |
182 |
| - val task = Task("UI <- button", "Description") |
| 142 | + val taskName = "UI <- button" |
| 143 | + val task = Task(taskName, "Description") |
183 | 144 | tasksRepository.saveTaskBlocking(task)
|
184 |
| - composeTestRule.waitForIdle() |
| 145 | + |
| 146 | + setContent() |
185 | 147 |
|
186 | 148 | // Click on the task on the list
|
187 |
| - onView(withText("UI <- button")).perform(click()) |
| 149 | + composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() |
| 150 | + composeTestRule.onNodeWithText(taskName).assertIsDisplayed() |
| 151 | + composeTestRule.onNodeWithText(taskName).performClick() |
| 152 | + |
188 | 153 | // Click on the edit task button
|
| 154 | + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.edit_task)) |
| 155 | + .assertIsDisplayed() |
189 | 156 | composeTestRule.onNodeWithContentDescription(activity.getString(R.string.edit_task))
|
190 | 157 | .performClick()
|
191 | 158 |
|
192 | 159 | // Confirm that if we click "<-" once, we end up back at the task details page
|
193 |
| - onView( |
194 |
| - withContentDescription( |
195 |
| - composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription() |
196 |
| - ) |
197 |
| - ).perform(click()) |
198 |
| - composeTestRule.onNodeWithText("UI <- button").assertIsDisplayed() |
| 160 | + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) |
| 161 | + .performClick() |
| 162 | + composeTestRule.onNodeWithText(taskName).assertIsDisplayed() |
199 | 163 |
|
200 | 164 | // Confirm that if we click "<-" a second time, we end up back at the home screen
|
201 |
| - onView( |
202 |
| - withContentDescription( |
203 |
| - composeTestRule.activityRule.scenario.getToolbarNavigationContentDescription() |
204 |
| - ) |
205 |
| - ).perform(click()) |
206 |
| - composeTestRule.onNodeWithText("All tasks").assertIsDisplayed() |
| 165 | + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) |
| 166 | + .performClick() |
| 167 | + composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() |
207 | 168 | }
|
208 | 169 |
|
209 | 170 | @Test
|
210 | 171 | fun taskDetailScreen_doubleBackButton() {
|
211 |
| - dataBindingIdlingResource.monitorActivity(composeTestRule.activityRule.scenario) |
212 |
| - |
213 |
| - val task = Task("Back button", "Description") |
| 172 | + val taskName = "Back button" |
| 173 | + val task = Task(taskName, "Description") |
214 | 174 | tasksRepository.saveTaskBlocking(task)
|
215 |
| - composeTestRule.waitForIdle() |
| 175 | + |
| 176 | + setContent() |
216 | 177 |
|
217 | 178 | // Click on the task on the list
|
218 |
| - onView(withText("Back button")).perform(click()) |
| 179 | + composeTestRule.onNodeWithText(taskName).assertIsDisplayed() |
| 180 | + composeTestRule.onNodeWithText(taskName).performClick() |
219 | 181 | // Click on the edit task button
|
220 | 182 | composeTestRule.onNodeWithContentDescription(activity.getString(R.string.edit_task))
|
221 | 183 | .performClick()
|
222 | 184 |
|
223 | 185 | // Confirm that if we click back once, we end up back at the task details page
|
224 | 186 | pressBack()
|
225 |
| - composeTestRule.onNodeWithText("Back button").assertIsDisplayed() |
| 187 | + composeTestRule.onNodeWithText(taskName).assertIsDisplayed() |
226 | 188 |
|
227 | 189 | // Confirm that if we click back a second time, we end up back at the home screen
|
228 | 190 | pressBack()
|
229 |
| - composeTestRule.onNodeWithText("All tasks").assertIsDisplayed() |
| 191 | + composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() |
| 192 | + } |
| 193 | + |
| 194 | + private fun setContent() { |
| 195 | + composeTestRule.setContent { |
| 196 | + AppCompatTheme { |
| 197 | + TodoNavGraph() |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private fun openDrawer() { |
| 203 | + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.open_drawer)) |
| 204 | + .performClick() |
230 | 205 | }
|
231 | 206 | }
|
0 commit comments