forked from android/architecture-samples
/
AddEditTaskScreenTest.kt
113 lines (103 loc) · 4.32 KB
/
AddEditTaskScreenTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.architecture.blueprints.todoapp.addedittask
import androidx.activity.ComponentActivity
import androidx.compose.material.Surface
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasSetTextAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.navigation.Navigation.findNavController
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.data.Result
import com.example.android.architecture.blueprints.todoapp.data.source.FakeRepository
import com.example.android.architecture.blueprints.todoapp.util.getTasksBlocking
import com.google.accompanist.appcompattheme.AppCompatTheme
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.LooperMode
import org.robolectric.annotation.TextLayoutMode
/**
* Integration test for the Add Task screen.
*/
@RunWith(AndroidJUnit4::class)
@MediumTest
@LooperMode(LooperMode.Mode.PAUSED)
@TextLayoutMode(TextLayoutMode.Mode.REALISTIC)
@ExperimentalCoroutinesApi
class AddEditTaskScreenTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
private val activity by lazy { composeTestRule.activity }
private val repository = FakeRepository()
@Before
fun setup() {
// GIVEN - On the "Add Task" screen.
composeTestRule.setContent {
AppCompatTheme {
Surface {
AddEditTaskScreen(
viewModel = AddEditTaskViewModel(repository),
taskId = null,
onTaskUpdate = { }
)
}
}
}
}
@Test
fun emptyTask_isNotSaved() {
// WHEN - Enter invalid title and description combination and click save
findTextField(R.string.title_hint).performTextClearance()
findTextField(R.string.description_hint).performTextClearance()
composeTestRule.onNodeWithContentDescription(activity.getString(R.string.cd_save_task))
.performClick()
// THEN - Entered Task is still displayed (a correct task would close it).
composeTestRule
.onNodeWithText(activity.getString(R.string.empty_task_message))
.assertIsDisplayed()
}
@Test
fun validTask_isSaved() {
// WHEN - Valid title and description combination and click save
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 that the repository saved the task
val tasks = (repository.getTasksBlocking(true) as Result.Success).data
assertEquals(tasks.size, 1)
assertEquals(tasks[0].title, "title")
assertEquals(tasks[0].description, "description")
}
private fun findTextField(text: Int): SemanticsNodeInteraction {
return composeTestRule.onNode(
hasSetTextAction() and hasText(activity.getString(text))
)
}
}