Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 1 | /* |
| 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 Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 17 | package androidx.ui.test.gesturescope |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 18 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 19 | import androidx.test.filters.MediumTest |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 20 | import androidx.ui.core.Alignment |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 21 | import androidx.ui.core.Modifier |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 22 | import androidx.ui.core.gesture.longPressGestureFilter |
Jelle Fresen | eedc83d | 2020-06-09 17:57:32 +0100 | [diff] [blame] | 23 | import androidx.ui.geometry.Offset |
Anastasia Soboleva | 637055e | 2020-02-27 14:26:28 +0000 | [diff] [blame] | 24 | import androidx.ui.layout.Stack |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 25 | import androidx.ui.layout.fillMaxSize |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 26 | import androidx.ui.layout.wrapContentSize |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 27 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 28 | import androidx.ui.test.performGesture |
| 29 | import androidx.ui.test.onNodeWithTag |
| 30 | import androidx.ui.test.longClick |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 31 | import androidx.ui.test.util.ClickableTestBox |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 32 | import androidx.ui.test.util.ClickableTestBox.defaultSize |
| 33 | import androidx.ui.test.util.ClickableTestBox.defaultTag |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 34 | import androidx.ui.test.util.SinglePointerInputRecorder |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 35 | import androidx.ui.test.util.isAlmostEqualTo |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 36 | import androidx.ui.test.util.recordedDuration |
| 37 | import androidx.ui.test.waitForIdle |
| 38 | import androidx.ui.unit.Duration |
| 39 | import androidx.ui.unit.milliseconds |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 40 | import com.google.common.truth.Truth.assertThat |
| 41 | import org.junit.Rule |
| 42 | import org.junit.Test |
| 43 | import org.junit.runner.RunWith |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 44 | import org.junit.runners.Parameterized |
| 45 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 46 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 47 | * Tests [longClick] with arguments. Verifies that the click is in the middle |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 48 | * 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 Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 53 | class SendLongClickTest(private val config: TestConfig) { |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 54 | data class TestConfig(val position: Offset?, val duration: Duration?) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 55 | |
| 56 | companion object { |
| 57 | @JvmStatic |
| 58 | @Parameterized.Parameters(name = "{0}") |
| 59 | fun createTestSet(): List<TestConfig> { |
| 60 | return mutableListOf<TestConfig>().apply { |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 61 | 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 Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 66 | } |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 67 | add(TestConfig(null, duration)) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @get:Rule |
| 74 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 75 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 76 | private val recordedLongClicks = mutableListOf<Offset>() |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 77 | private val expectedClickPosition = |
Jelle Fresen | d1e889d | 2020-06-16 17:38:45 +0100 | [diff] [blame] | 78 | config.position ?: Offset(defaultSize / 2, defaultSize / 2) |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 79 | private val expectedDuration = config.duration ?: 600.milliseconds |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 80 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 81 | private fun recordLongPress(position: Offset) { |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 82 | recordedLongClicks.add(position) |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | fun testLongClick() { |
| 87 | // Given some content |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 88 | val recorder = SinglePointerInputRecorder() |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 89 | composeTestRule.setContent { |
| 90 | Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) { |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 91 | ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress) + recorder) |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 94 | |
| 95 | // When we inject a long click |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 96 | onNodeWithTag(defaultTag).performGesture { |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 97 | if (config.position != null && config.duration != null) { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 98 | longClick(config.position, config.duration) |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 99 | } else if (config.position != null) { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 100 | longClick(config.position) |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 101 | } else if (config.duration != null) { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 102 | longClick(duration = config.duration) |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 103 | } else { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 104 | longClick() |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 107 | |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 108 | waitForIdle() |
| 109 | |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 110 | // Then we record 1 long click at the expected position |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 111 | assertThat(recordedLongClicks).hasSize(1) |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 112 | recordedLongClicks[0].isAlmostEqualTo(expectedClickPosition) |
Jelle Fresen | 3d98faa | 2020-06-12 18:39:06 +0100 | [diff] [blame] | 113 | |
| 114 | // And that the duration was as expected |
| 115 | assertThat(recorder.recordedDuration).isEqualTo(expectedDuration) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 116 | } |
| 117 | } |