1
+ package com.example.android.architecture.blueprints.todoapp.taskdetail
2
+
3
+ import androidx.fragment.app.testing.launchFragmentInContainer
4
+ import androidx.test.espresso.Espresso.onView
5
+ import androidx.test.espresso.assertion.ViewAssertions.matches
6
+ import androidx.test.espresso.matcher.ViewMatchers.isChecked
7
+ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
8
+ import androidx.test.espresso.matcher.ViewMatchers.withId
9
+ import androidx.test.espresso.matcher.ViewMatchers.withText
10
+
11
+ import androidx.test.ext.junit.runners.AndroidJUnit4
12
+ import androidx.test.filters.MediumTest
13
+
14
+ import com.example.android.architecture.blueprints.todoapp.R
15
+ import com.example.android.architecture.blueprints.todoapp.ServiceLocator
16
+ import com.example.android.architecture.blueprints.todoapp.data.Task
17
+ import com.example.android.architecture.blueprints.todoapp.data.source.FakeAndroidTestRepository
18
+ import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
19
+
20
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
21
+ import kotlinx.coroutines.test.runBlockingTest
22
+
23
+ import org.hamcrest.core.IsNot.not
24
+
25
+ import org.junit.After
26
+ import org.junit.Before
27
+ import org.junit.Test
28
+ import org.junit.runner.RunWith
29
+
30
+ @MediumTest
31
+ @RunWith(AndroidJUnit4::class)
32
+ @ExperimentalCoroutinesApi
33
+ class TaskDetailFragmentTest {
34
+ private lateinit var tasksRepository: TasksRepository
35
+
36
+ @Before
37
+ fun initRepository() {
38
+ tasksRepository = FakeAndroidTestRepository()
39
+ ServiceLocator.tasksRepository = tasksRepository
40
+ }
41
+
42
+ @Test
43
+ fun activeTaskDetails_DisplayedInUi() = runBlockingTest {
44
+ // Given add active, incomplete task to the DB.
45
+ val activeTask = Task("Active Task", "AndroidX Rox0rs", false)
46
+ tasksRepository.saveTask(activeTask)
47
+
48
+ // When details fragment launched to display task
49
+ val bundle = TaskDetailFragmentArgs(activeTask.id).toBundle()
50
+ launchFragmentInContainer<TaskDetailFragment>(bundle, R.style.AppTheme)
51
+
52
+ // Then Task details are displayed on the screen.
53
+ // Make sure that the title and description are both shown and correct.
54
+ onView(withId(R.id.task_detail_title_text))
55
+ .check(matches(isDisplayed()))
56
+ .check(matches(withText("Active Task")))
57
+ onView(withId(R.id.task_detail_description_text))
58
+ .check(matches(isDisplayed()))
59
+ .check(matches(withText("AndroidX Rox0rs")))
60
+ // And make sure the "active" checkbox is unchecked.
61
+ onView(withId(R.id.task_detail_complete_checkbox))
62
+ .check(matches(isDisplayed()))
63
+ .check(matches(not(isChecked())))
64
+ }
65
+
66
+ @Test
67
+ fun completedTaskDetails_DisplayedInUi() = runBlockingTest {
68
+ // Given - Add completed task to the DB
69
+ val completedTitle = "Completed Task"
70
+ val completedDescription = "This task is all done. Done done donesky. Yup!"
71
+ val completedTask = Task(completedTitle, completedDescription, true)
72
+ tasksRepository.saveTask(completedTask)
73
+
74
+ // When - Details fragment launched to display task
75
+ val bundle = TaskDetailFragmentArgs(completedTask.id).toBundle()
76
+ launchFragmentInContainer<TaskDetailFragment>(bundle, R.style.AppTheme)
77
+
78
+ // Then - Task details are displayed on the screen.
79
+ // Make sure that the title and description are both shown and correct.
80
+ onView(withId(R.id.task_detail_title_text))
81
+ .check(matches(isDisplayed()))
82
+ .check(matches(withText(completedTitle)))
83
+ onView(withId(R.id.task_detail_description_text))
84
+ .check(matches(isDisplayed()))
85
+ .check(matches(withText(completedDescription)))
86
+ onView(withId(R.id.task_detail_complete_checkbox))
87
+ .check(matches(isDisplayed()))
88
+ .check(matches(isChecked()))
89
+ }
90
+
91
+ @After
92
+ fun cleanupDb() {
93
+ runBlockingTest {
94
+ ServiceLocator.resetRepository()
95
+ }
96
+ }
97
+ }
0 commit comments