android6-2 Exercise 2: Compose UI tests ==================================== // app/src/androidTest/.../NotesScreenTest.kt (a Compose UI test) class NotesScreenTest { @get:Rule val composeTestRule = createComposeRule() private fun show(state: NotesUiState, onAddNote: (String) -> Unit = {}) { composeTestRule.setContent { NotesScreen( state = state, onAddNote = onAddNote, onDeleteNote = {}, onRefresh = {}, onNewestFirstChange = {}, onMessageShown = {} ) } } @Test fun showsTheNotes() { show(NotesUiState(notes = listOf(Note(1, "Buy milk", 0L)))) composeTestRule.onNodeWithText("Buy milk").assertIsDisplayed() } @Test fun addButtonSendsTheTypedText() { var added: String? = null show(NotesUiState(), onAddNote = { added = it }) composeTestRule.onNodeWithTag("noteInput").performTextInput("Call Ada") composeTestRule.onNodeWithText("Add").performClick() assertEquals("Call Ada", added) } } // In the screen: OutlinedTextField(..., modifier = Modifier.testTag("noteInput")) Third test: @Test fun refreshIsDisabledWhileRefreshing() { show(NotesUiState(isRefreshing = true)) composeTestRule.onNodeWithText("Refresh").assertIsNotEnabled() } Needed (docs): androidTestImplementation ui-test-junit4 and debugImplementation ui-test-manifest. Needed (general knowledge): the testTag on the input, imports for the test APIs, and an emulator or device. Run from Android Studio with the test's green arrow. Report what you added. Code unrun.