[go: nahoru, domu]

blob: 0b5d60391e0edf788dca87e09e46941a088e30e0 [file] [log] [blame]
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +01001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010016package androidx.compose.foundation
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010017
Mihai Popab300d2c2020-06-23 16:25:50 +010018import androidx.compose.Providers
19import androidx.compose.ambientOf
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010020import androidx.compose.state
Matvei Malkov28d8d5d2020-07-14 20:04:44 +010021import androidx.test.filters.FlakyTest
Ryan Mentley7865a632019-08-20 20:10:29 -070022import androidx.test.filters.MediumTest
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010023import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
24import androidx.test.uiautomator.UiDevice
Matvei Malkov323b7452020-05-01 16:57:52 +010025import androidx.ui.core.Modifier
Filip Pavlis8b8a8622020-02-13 16:32:04 +000026import androidx.ui.test.assertIsDisplayed
Ryan Mentley7865a632019-08-20 20:10:29 -070027import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010028import androidx.ui.test.performClick
29import androidx.ui.test.onNodeWithText
Filip Pavlisa4d73a52020-07-14 11:30:00 +010030import androidx.ui.test.runOnIdle
Mihai Popab300d2c2020-06-23 16:25:50 +010031import org.junit.Assert.assertEquals
Filip Pavlis2b161e42019-11-22 17:40:08 +000032import org.junit.Ignore
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010033import org.junit.Rule
34import org.junit.Test
35import org.junit.runner.RunWith
36import org.junit.runners.JUnit4
37
38@MediumTest
39@RunWith(JUnit4::class)
40class DialogUiTest {
41 @get:Rule
42 val composeTestRule = createComposeRule(disableTransitions = true)
43
44 private val defaultText = "dialogText"
45
46 @Test
47 fun dialogTest_isShowingContent() {
48 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -080049 val showDialog = state { true }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010050
51 if (showDialog.value) {
52 Dialog(onCloseRequest = {}) {
53 Text(defaultText)
54 }
55 }
56 }
57
Filip Pavlis659ea722020-07-13 14:14:32 +010058 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010059 }
60
61 @Test
Matvei Malkov28d8d5d2020-07-14 20:04:44 +010062 @FlakyTest(bugId = 159364185)
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010063 fun dialogTest_isNotDismissed_whenClicked() {
64 val textBeforeClick = "textBeforeClick"
65 val textAfterClick = "textAfterClick"
66
67 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -080068 val showDialog = state { true }
69 val text = state { textBeforeClick }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010070
71 if (showDialog.value) {
72 Dialog(onCloseRequest = {
73 showDialog.value = false
74 }) {
Matvei Malkov323b7452020-05-01 16:57:52 +010075 Text(
76 text = text.value,
77 modifier = Modifier.clickable {
78 text.value = textAfterClick
79 }
80 )
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010081 }
82 }
83 }
84
Filip Pavlis659ea722020-07-13 14:14:32 +010085 onNodeWithText(textBeforeClick)
Filip Pavlis8b8a8622020-02-13 16:32:04 +000086 .assertIsDisplayed()
Ryan Mentley7865a632019-08-20 20:10:29 -070087 // Click inside the dialog
Filip Pavlis659ea722020-07-13 14:14:32 +010088 .performClick()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010089
Ryan Mentley7865a632019-08-20 20:10:29 -070090 // Check that the Clickable was pressed and that the Dialog is still visible, but with
91 // the new text
Filip Pavlis659ea722020-07-13 14:14:32 +010092 onNodeWithText(textBeforeClick).assertDoesNotExist()
93 onNodeWithText(textAfterClick).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +010094 }
95
96 @Test
97 fun dialogTest_isDismissed_whenSpecified() {
98 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -080099 val showDialog = state { true }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100100
101 if (showDialog.value) {
102 Dialog(onCloseRequest = { showDialog.value = false }) {
103 Text(defaultText)
104 }
105 }
106 }
107
Filip Pavlis659ea722020-07-13 14:14:32 +0100108 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100109
110 // Click outside the dialog to dismiss it
111 val outsideX = 0
112 val outsideY = composeTestRule.displayMetrics.heightPixels / 2
113 UiDevice.getInstance(getInstrumentation()).click(outsideX, outsideY)
114
Filip Pavlis659ea722020-07-13 14:14:32 +0100115 onNodeWithText(defaultText).assertDoesNotExist()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100116 }
117
118 @Test
119 fun dialogTest_isNotDismissed_whenNotSpecified() {
120 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -0800121 val showDialog = state { true }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100122
123 if (showDialog.value) {
124 Dialog(onCloseRequest = {}) {
125 Text(defaultText)
126 }
127 }
128 }
129
Filip Pavlis659ea722020-07-13 14:14:32 +0100130 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100131
132 // Click outside the dialog to try to dismiss it
133 val outsideX = 0
134 val outsideY = composeTestRule.displayMetrics.heightPixels / 2
135 UiDevice.getInstance(getInstrumentation()).click(outsideX, outsideY)
136
137 // The Dialog should still be visible
Filip Pavlis659ea722020-07-13 14:14:32 +0100138 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100139 }
140
141 @Test
142 fun dialogTest_isDismissed_whenSpecified_backButtonPressed() {
143 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -0800144 val showDialog = state { true }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100145
146 if (showDialog.value) {
147 Dialog(onCloseRequest = { showDialog.value = false }) {
148 Text(defaultText)
149 }
150 }
151 }
152
Filip Pavlis659ea722020-07-13 14:14:32 +0100153 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100154
155 // Click the back button to dismiss the Dialog
156 UiDevice.getInstance(getInstrumentation()).pressBack()
157
Filip Pavlis659ea722020-07-13 14:14:32 +0100158 onNodeWithText(defaultText).assertDoesNotExist()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100159 }
160
Filip Pavlis2b161e42019-11-22 17:40:08 +0000161 // TODO(pavlis): Espresso loses focus on the dialog after back press. That makes the
162 // subsequent query to fails.
163 @Ignore
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100164 @Test
165 fun dialogTest_isNotDismissed_whenNotSpecified_backButtonPressed() {
166 composeTestRule.setContent {
Leland Richardson7f848ab2019-12-12 13:43:41 -0800167 val showDialog = state { true }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100168
169 if (showDialog.value) {
170 Dialog(onCloseRequest = {}) {
171 Text(defaultText)
172 }
173 }
174 }
175
Filip Pavlis659ea722020-07-13 14:14:32 +0100176 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100177
178 // Click the back button to try to dismiss the dialog
179 UiDevice.getInstance(getInstrumentation()).pressBack()
180
181 // The Dialog should still be visible
Filip Pavlis659ea722020-07-13 14:14:32 +0100182 onNodeWithText(defaultText).assertIsDisplayed()
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100183 }
Mihai Popab300d2c2020-06-23 16:25:50 +0100184
185 @Test
186 fun dialog_preservesAmbients() {
187 val ambient = ambientOf<Float>()
188 var value = 0f
189 composeTestRule.setContent {
190 Providers(ambient provides 1f) {
191 Dialog(onCloseRequest = {}) {
192 value = ambient.current
193 }
194 }
195 }
Filip Pavlisa4d73a52020-07-14 11:30:00 +0100196 runOnIdle {
Mihai Popab300d2c2020-06-23 16:25:50 +0100197 assertEquals(1f, value)
198 }
199 }
Mihai Burlaciuce882b3c2019-07-16 18:58:04 +0100200}