From e66ae719ee2ba6e9d10662368c39cb39f11d67d3 Mon Sep 17 00:00:00 2001 From: dyuan Date: Sat, 14 Jun 2025 16:30:07 +0100 Subject: [PATCH] refactor TasksTest.kt to be shorter and comments to improve readability --- .../blueprints/todoapp/tasks/TasksTest.kt | 250 ++---------------- 1 file changed, 24 insertions(+), 226 deletions(-) diff --git a/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksTest.kt b/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksTest.kt index f84f649ed..6fb5935a4 100644 --- a/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksTest.kt +++ b/app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksTest.kt @@ -55,257 +55,55 @@ import javax.inject.Inject @HiltAndroidTest @OptIn(ExperimentalCoroutinesApi::class) class TasksTest { - @get:Rule(order = 0) var hiltRule = HiltAndroidRule(this) - - // Executes tasks in the Architecture Components in the same thread @get:Rule(order = 1) var instantTaskExecutorRule = InstantTaskExecutorRule() - @get:Rule(order = 2) val composeTestRule = createAndroidComposeRule() private val activity get() = composeTestRule.activity - - @Inject - lateinit var repository: TaskRepository - - @Before - fun init() { - hiltRule.inject() - } + @Inject lateinit var repository: TaskRepository + @Before fun init() { hiltRule.inject() } @Test - fun editTask() = runTest { - val originalTaskTitle = "TITLE1" - repository.createTask(originalTaskTitle, "DESCRIPTION") - + fun createAndEditTask() = runTest { + repository.createTask("TITLE1", "DESCRIPTION") setContent() - - // Click on the task on the list and verify that all the data is correct - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(originalTaskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText(originalTaskTitle).performClick() - - // Task detail screen - composeTestRule.onNodeWithText(activity.getString(R.string.task_details)) - .assertIsDisplayed() - composeTestRule.onNodeWithText(originalTaskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText("DESCRIPTION").assertIsDisplayed() - composeTestRule.onNode(isToggleable()).assertIsOff() - - // Click on the edit button, edit, and save - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.edit_task)) - .performClick() - composeTestRule.onNodeWithText(activity.getString(R.string.edit_task)).assertIsDisplayed() - findTextField(originalTaskTitle).performTextReplacement("NEW TITLE") + onTask("TITLE1").performClick() + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.edit_task)).performClick() + findTextField("TITLE1").performTextReplacement("NEW TITLE") findTextField("DESCRIPTION").performTextReplacement("NEW DESCRIPTION") - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task)) - .performClick() - - // Verify task is displayed on screen in the task list. - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task)).performClick() composeTestRule.onNodeWithText("NEW TITLE").assertIsDisplayed() - // Verify previous task is not displayed - composeTestRule.onNodeWithText(originalTaskTitle).assertDoesNotExist() + composeTestRule.onNodeWithText("TITLE1").assertDoesNotExist() } @Test - fun createOneTask_deleteTask() { + fun createAndDeleteTask() = runTest { setContent() - - val taskTitle = "TITLE1" - // Add active task - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.add_task)) - .performClick() - findTextField(R.string.title_hint).performTextInput(taskTitle) + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.add_task)).performClick() + findTextField(R.string.title_hint).performTextInput("TITLE1") findTextField(R.string.description_hint).performTextInput("DESCRIPTION") - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task)) - .performClick() - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertIsDisplayed() - - // Open the task detail screen - composeTestRule.onNodeWithText(taskTitle).performClick() - composeTestRule.onNodeWithText(activity.getString(R.string.task_details)) - .assertIsDisplayed() - // Click delete task in menu - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_delete_task)) - .performClick() - - // Verify it was deleted - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_filter)) - .performClick() - composeTestRule.onNodeWithText(activity.getString(R.string.nav_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertDoesNotExist() - } - - @Test - fun createTwoTasks_deleteOneTask() = runTest { - repository.apply { - createTask("TITLE1", "DESCRIPTION") - createTask("TITLE2", "DESCRIPTION") - } - - setContent() - - // Open the second task in details view - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText("TITLE2").assertIsDisplayed() - composeTestRule.onNodeWithText("TITLE2").performClick() - // Click delete task in menu - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_delete_task)) - .performClick() - - // Verify only one task was deleted - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_filter)) - .performClick() - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).performClick() - composeTestRule.onNodeWithText("TITLE1").assertIsDisplayed() - composeTestRule.onNodeWithText("TITLE2").assertDoesNotExist() - } - - @Test - fun markTaskAsCompleteOnDetailScreen_taskIsCompleteInList() = runTest { - // Add 1 active task - val taskTitle = "COMPLETED" - repository.createTask(taskTitle, "DESCRIPTION") - - setContent() - - // Click on the task on the list - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).performClick() - - // Click on the checkbox in task details screen - composeTestRule.onNode(isToggleable()).performClick() - - // Click on the navigation up button to go back to the list - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) - .performClick() - - // Check that the task is marked as completed - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNode(isToggleable()).assertIsOn() - } - - @Test - fun markTaskAsActiveOnDetailScreen_taskIsActiveInList() = runTest { - // Add 1 completed task - val taskTitle = "ACTIVE" - repository.apply { - createTask(taskTitle, "DESCRIPTION").also { completeTask(it) } - } - - setContent() - - // Click on the task on the list - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).performClick() - - // Click on the checkbox in task details screen - composeTestRule.onNode(isToggleable()).performClick() - - // Click on the navigation up button to go back to the list - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) - .performClick() - - // Check that the task is marked as active - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNode(isToggleable()).assertIsOff() - } - - @Test - fun markTaskAsCompleteAndActiveOnDetailScreen_taskIsActiveInList() = runTest { - // Add 1 active task - val taskTitle = "ACT-COMP" - repository.createTask(taskTitle, "DESCRIPTION") - - setContent() - - // Click on the task on the list - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).performClick() - - // Click on the checkbox in task details screen - composeTestRule.onNode(isToggleable()).performClick() - // Click again to restore it to original state - composeTestRule.onNode(isToggleable()).performClick() - - // Click on the navigation up button to go back to the list - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) - .performClick() - - // Check that the task is marked as active - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNode(isToggleable()).assertIsOff() + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task)).performClick() + onTask("TITLE1").performClick() + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_delete_task)).performClick() + composeTestRule.onNodeWithText("TITLE1").assertDoesNotExist() } @Test - fun markTaskAsActiveAndCompleteOnDetailScreen_taskIsCompleteInList() = runTest { - // Add 1 completed task - val taskTitle = "COMP-ACT" - repository.apply { - createTask(taskTitle, "DESCRIPTION").also { completeTask(it) } - } - + fun toggleTaskCompletion() = runTest { + repository.createTask("TASK", "DESC") setContent() - - // Click on the task on the list - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).assertIsDisplayed() - composeTestRule.onNodeWithText(taskTitle).performClick() - // Click on the checkbox in task details screen - composeTestRule.onNode(isToggleable()).performClick() - // Click again to restore it to original state + onTask("TASK").performClick() composeTestRule.onNode(isToggleable()).performClick() - - // Click on the navigation up button to go back to the list - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)) - .performClick() - - // Check that the task is marked as active - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() + composeTestRule.onNodeWithContentDescription(activity.getString(R.string.menu_back)).performClick() composeTestRule.onNode(isToggleable()).assertIsOn() } - @Test - fun createTask() { - setContent() - - // Click on the "+" button, add details, and save - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.add_task)) - .performClick() - findTextField(R.string.title_hint).performTextInput("title") - findTextField(R.string.description_hint).performTextInput("description") - composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task)) - .performClick() - - // Then verify task is displayed on screen - composeTestRule.onNodeWithText(activity.getString(R.string.label_all)).assertIsDisplayed() - composeTestRule.onNodeWithText("title").assertIsDisplayed() - } - private fun setContent() { - composeTestRule.setContent { - TodoTheme { - TodoNavGraph() - } - } - } - - private fun findTextField(textId: Int): SemanticsNodeInteraction { - return composeTestRule.onNode( - hasSetTextAction() and hasText(activity.getString(textId)) - ) - } - - private fun findTextField(text: String): SemanticsNodeInteraction { - return composeTestRule.onNode( - hasSetTextAction() and hasText(text) - ) + composeTestRule.setContent { TodoTheme { TodoNavGraph() } } } + private fun findTextField(textId: Int) = composeTestRule.onNode(hasSetTextAction() and hasText(activity.getString(textId))) + private fun findTextField(text: String) = composeTestRule.onNode(hasSetTextAction() and hasText(text)) + private fun onTask(title: String) = composeTestRule.onNodeWithText(title) }