8000 refactor TasksTest.kt to be shorter and comments to improve readability by derekyuan1000 · Pull Request #1040 · android/architecture-samples · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor TasksTest.kt to be shorter and comments to improve readability #1040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<HiltTestActivity>()
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.stri 10000 ng.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)
}
0