[go: nahoru, domu]

blob: 2f920874b9de888bcd641c681d366d413a78101c [file] [log] [blame]
Jelle Fresen208d53f2020-01-10 16:03:36 +00001/*
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 */
16
Jelle Fresen3d3c4482020-04-17 18:43:02 +010017package androidx.ui.test.gesturescope
Jelle Fresen208d53f2020-01-10 16:03:36 +000018
Jelle Fresen208d53f2020-01-10 16:03:36 +000019import androidx.test.filters.MediumTest
Adam Powell999a89b2020-03-11 09:08:07 -070020import androidx.ui.core.Alignment
Adam Powell999a89b2020-03-11 09:08:07 -070021import androidx.ui.core.Modifier
Jelle Fresen3d3c4482020-04-17 18:43:02 +010022import androidx.ui.core.gesture.longPressGestureFilter
Jelle Freseneedc83d2020-06-09 17:57:32 +010023import androidx.ui.geometry.Offset
Anastasia Soboleva637055e2020-02-27 14:26:28 +000024import androidx.ui.layout.Stack
Adam Powell999a89b2020-03-11 09:08:07 -070025import androidx.ui.layout.fillMaxSize
Adam Powell999a89b2020-03-11 09:08:07 -070026import androidx.ui.layout.wrapContentSize
Jelle Fresen3d3c4482020-04-17 18:43:02 +010027import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010028import androidx.ui.test.performGesture
29import androidx.ui.test.onNodeWithTag
30import androidx.ui.test.longClick
Jelle Fresen3b1a5262020-04-30 18:30:28 +010031import androidx.ui.test.util.ClickableTestBox
Jelle Fresen5b1db502020-05-28 16:44:47 +010032import androidx.ui.test.util.ClickableTestBox.defaultSize
33import androidx.ui.test.util.ClickableTestBox.defaultTag
Jelle Fresen3d98faa2020-06-12 18:39:06 +010034import androidx.ui.test.util.SinglePointerInputRecorder
Jelle Fresen5b1db502020-05-28 16:44:47 +010035import androidx.ui.test.util.isAlmostEqualTo
Jelle Fresen3d98faa2020-06-12 18:39:06 +010036import androidx.ui.test.util.recordedDuration
37import androidx.ui.test.waitForIdle
38import androidx.ui.unit.Duration
39import androidx.ui.unit.milliseconds
Jelle Fresen208d53f2020-01-10 16:03:36 +000040import com.google.common.truth.Truth.assertThat
41import org.junit.Rule
42import org.junit.Test
43import org.junit.runner.RunWith
Jelle Fresen208d53f2020-01-10 16:03:36 +000044import org.junit.runners.Parameterized
45
Jelle Fresen208d53f2020-01-10 16:03:36 +000046/**
Filip Pavlis659ea722020-07-13 14:14:32 +010047 * Tests [longClick] with arguments. Verifies that the click is in the middle
Jelle Fresen208d53f2020-01-10 16:03:36 +000048 * of the component, that the gesture has a duration of 600 milliseconds and that all input
49 * events were on the same location.
50 */
51@MediumTest
52@RunWith(Parameterized::class)
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010053class SendLongClickTest(private val config: TestConfig) {
Jelle Fresen3d98faa2020-06-12 18:39:06 +010054 data class TestConfig(val position: Offset?, val duration: Duration?)
Jelle Fresen208d53f2020-01-10 16:03:36 +000055
56 companion object {
57 @JvmStatic
58 @Parameterized.Parameters(name = "{0}")
59 fun createTestSet(): List<TestConfig> {
60 return mutableListOf<TestConfig>().apply {
Jelle Fresen3d98faa2020-06-12 18:39:06 +010061 for (duration in listOf(null, 700.milliseconds)) {
62 for (x in listOf(1.0f, defaultSize / 4)) {
63 for (y in listOf(1.0f, defaultSize / 3)) {
64 add(TestConfig(Offset(x, y), duration))
65 }
Jelle Fresen208d53f2020-01-10 16:03:36 +000066 }
Jelle Fresen3d98faa2020-06-12 18:39:06 +010067 add(TestConfig(null, duration))
Jelle Fresen208d53f2020-01-10 16:03:36 +000068 }
69 }
70 }
71 }
72
73 @get:Rule
74 val composeTestRule = createComposeRule(disableTransitions = true)
75
Nader Jawad6df06122020-06-03 15:27:08 -070076 private val recordedLongClicks = mutableListOf<Offset>()
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010077 private val expectedClickPosition =
Jelle Fresend1e889d2020-06-16 17:38:45 +010078 config.position ?: Offset(defaultSize / 2, defaultSize / 2)
Jelle Fresen3d98faa2020-06-12 18:39:06 +010079 private val expectedDuration = config.duration ?: 600.milliseconds
Jelle Fresen208d53f2020-01-10 16:03:36 +000080
Nader Jawad6df06122020-06-03 15:27:08 -070081 private fun recordLongPress(position: Offset) {
Jelle Fresen208d53f2020-01-10 16:03:36 +000082 recordedLongClicks.add(position)
83 }
84
85 @Test
86 fun testLongClick() {
87 // Given some content
Jelle Fresen3d98faa2020-06-12 18:39:06 +010088 val recorder = SinglePointerInputRecorder()
Jelle Fresen3b1a5262020-04-30 18:30:28 +010089 composeTestRule.setContent {
90 Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) {
Jelle Fresen3d98faa2020-06-12 18:39:06 +010091 ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress) + recorder)
Jelle Fresen3b1a5262020-04-30 18:30:28 +010092 }
93 }
Jelle Fresen208d53f2020-01-10 16:03:36 +000094
95 // When we inject a long click
Filip Pavlis659ea722020-07-13 14:14:32 +010096 onNodeWithTag(defaultTag).performGesture {
Jelle Fresen3d98faa2020-06-12 18:39:06 +010097 if (config.position != null && config.duration != null) {
Filip Pavlis659ea722020-07-13 14:14:32 +010098 longClick(config.position, config.duration)
Jelle Fresen3d98faa2020-06-12 18:39:06 +010099 } else if (config.position != null) {
Filip Pavlis659ea722020-07-13 14:14:32 +0100100 longClick(config.position)
Jelle Fresen3d98faa2020-06-12 18:39:06 +0100101 } else if (config.duration != null) {
Filip Pavlis659ea722020-07-13 14:14:32 +0100102 longClick(duration = config.duration)
Jelle Fresenae8fbfc2020-06-02 17:54:26 +0100103 } else {
Filip Pavlis659ea722020-07-13 14:14:32 +0100104 longClick()
Jelle Fresenae8fbfc2020-06-02 17:54:26 +0100105 }
106 }
Jelle Fresen208d53f2020-01-10 16:03:36 +0000107
Jelle Fresen3d98faa2020-06-12 18:39:06 +0100108 waitForIdle()
109
Jelle Fresen5b1db502020-05-28 16:44:47 +0100110 // Then we record 1 long click at the expected position
Jelle Fresen208d53f2020-01-10 16:03:36 +0000111 assertThat(recordedLongClicks).hasSize(1)
Jelle Fresenae8fbfc2020-06-02 17:54:26 +0100112 recordedLongClicks[0].isAlmostEqualTo(expectedClickPosition)
Jelle Fresen3d98faa2020-06-12 18:39:06 +0100113
114 // And that the duration was as expected
115 assertThat(recorder.recordedDuration).isEqualTo(expectedDuration)
Jelle Fresen208d53f2020-01-10 16:03:36 +0000116 }
117}