@@ -23,8 +23,17 @@ import androidx.compose.foundation.lazy.LazyColumn
23
23
import androidx.compose.runtime.*
24
24
import androidx.compose.runtime.snapshots.Snapshot
25
25
import androidx.compose.ui.Modifier
26
+ import androidx.compose.ui.geometry.Size
27
+ import androidx.compose.ui.input.key.Key
28
+ import androidx.compose.ui.input.key.KeyEvent
29
+ import androidx.compose.ui.input.key.KeyEventType
30
+ import androidx.compose.ui.input.key.keyEvent
26
31
import androidx.compose.ui.layout.Layout
32
+ import androidx.compose.ui.test.isPopup
27
33
import androidx.compose.ui.test.junit4.createComposeRule
34
+ import androidx.compose.ui.test.onNodeWithTag
35
+ import androidx.compose.ui.test.performKeyPress
36
+ import androidx.compose.ui.test.runSkikoComposeUiTest
28
37
import androidx.compose.ui.unit.dp
29
38
import com.google.common.truth.Truth.assertThat
30
39
import org.junit.Rule
@@ -147,4 +156,100 @@ class DesktopPopupTest {
147
156
148
157
rule.waitForIdle()
149
158
}
159
+
160
+ @Test
161
+ fun dismissPopupByEscWithBackPressProperty () {
162
+ var onDismissRequestCallCount = 0
163
+ rule.setContent {
164
+ Popup (
165
+ onDismissRequest = { onDismissRequestCallCount++ },
166
+ properties = PopupProperties (
167
+ focusable = true ,
168
+ dismissOnBackPress = true
169
+ )
170
+ ) {
171
+ Box (Modifier )
172
+ }
173
+ }
174
+
175
+ rule.onNode(isPopup())
176
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyDown ))
177
+ rule.waitForIdle()
178
+ assertThat(onDismissRequestCallCount).isEqualTo(1 )
179
+ rule.onNode(isPopup())
180
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyUp ))
181
+ rule.waitForIdle()
182
+ assertThat(onDismissRequestCallCount).isEqualTo(1 )
183
+ }
184
+
185
+ @Test
186
+ fun doNotDismissPopupByEscWithoutBackPressProperty () {
187
+ var onDismissRequestCallCount = 0
188
+ rule.setContent {
189
+ Popup (
190
+ onDismissRequest = { onDismissRequestCallCount++ },
191
+ properties = PopupProperties (
192
+ focusable = true ,
193
+ dismissOnBackPress = false
194
+ )
195
+ ) {
196
+ Box (Modifier )
197
+ }
198
+ }
199
+
200
+ rule.onNode(isPopup())
201
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyDown ))
202
+ rule.waitForIdle()
203
+ assertThat(onDismissRequestCallCount).isEqualTo(0 )
204
+ rule.onNode(isPopup())
205
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyUp ))
206
+ rule.waitForIdle()
207
+ assertThat(onDismissRequestCallCount).isEqualTo(0 )
208
+ }
209
+
210
+ @Test
211
+ fun dismissPopupByEscOnNotConsumedKeyEvent () {
212
+ var onDismissRequestCallCount = 0
213
+ rule.setContent {
214
+ Popup (
215
+ focusable = true ,
216
+ onDismissRequest = { onDismissRequestCallCount++ },
217
+ onKeyEvent = { false }
218
+ ) {
219
+ Box (Modifier )
220
+ }
221
+ }
222
+
223
+ rule.onNode(isPopup())
224
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyDown ))
225
+ rule.waitForIdle()
226
+ assertThat(onDismissRequestCallCount).isEqualTo(1 )
227
+ rule.onNode(isPopup())
228
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyUp ))
229
+ rule.waitForIdle()
230
+ assertThat(onDismissRequestCallCount).isEqualTo(1 )
231
+ }
232
+
233
+ @Test
234
+ fun doNotDismissPopupByEscOnConsumedKeyEvent () {
235
+ var onDismissRequestCallCount = 0
236
+ rule.setContent {
237
+ Popup (
238
+ focusable = true ,
239
+ onDismissRequest = { onDismissRequestCallCount++ },
240
+ onKeyEvent = { true }
241
+ ) {
242
+ Box (Modifier )
243
+ }
244
+ }
245
+
246
+ rule.onNode(isPopup())
247
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyDown ))
248
+ rule.waitForIdle()
249
+ assertThat(onDismissRequestCallCount).isEqualTo(0 )
250
+ rule.onNode(isPopup())
251
+ .performKeyPress(keyEvent(Key .Escape , KeyEventType .KeyUp ))
252
+ rule.waitForIdle()
253
+ assertThat(onDismissRequestCallCount).isEqualTo(0 )
254
+ }
150
255
}
0 commit comments