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 |
Anastasia Soboleva | 637055e | 2020-02-27 14:26:28 +0000 | [diff] [blame] | 23 | import androidx.ui.layout.Stack |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 24 | import androidx.ui.layout.fillMaxSize |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 25 | import androidx.ui.layout.wrapContentSize |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 26 | import androidx.ui.test.createComposeRule |
| 27 | import androidx.ui.test.doGesture |
| 28 | import androidx.ui.test.findByTag |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 29 | import androidx.ui.test.sendLongClick |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 30 | import androidx.ui.test.util.ClickableTestBox |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 31 | import androidx.ui.test.util.ClickableTestBox.defaultSize |
| 32 | import androidx.ui.test.util.ClickableTestBox.defaultTag |
| 33 | import androidx.ui.test.util.isAlmostEqualTo |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 34 | import androidx.ui.unit.PxPosition |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 35 | import com.google.common.truth.Truth.assertThat |
| 36 | import org.junit.Rule |
| 37 | import org.junit.Test |
| 38 | import org.junit.runner.RunWith |
| 39 | import org.junit.runners.JUnit4 |
| 40 | import org.junit.runners.Parameterized |
| 41 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 42 | /** |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 43 | * Tests [sendLongClick] without arguments. Verifies that the click is in the middle |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 44 | * of the component, that the gesture has a duration of 600 milliseconds and that all input |
| 45 | * events were on the same location. |
| 46 | */ |
| 47 | @MediumTest |
| 48 | @RunWith(JUnit4::class) |
| 49 | class SendLongClickWithoutArgumentsTest { |
| 50 | |
| 51 | @get:Rule |
| 52 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 53 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 54 | private val recordedLongClicks = mutableListOf<PxPosition>() |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 55 | private val expectedPosition = PxPosition(defaultSize / 2, defaultSize / 2) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 56 | |
| 57 | private fun recordLongPress(position: PxPosition) { |
| 58 | recordedLongClicks.add(position) |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun testLongClick() { |
| 63 | // Given some content |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 64 | composeTestRule.setContent { |
| 65 | Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) { |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 66 | ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress)) |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 69 | |
| 70 | // When we inject a long click |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 71 | findByTag(defaultTag).doGesture { sendLongClick() } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 72 | |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 73 | // Then we record 1 long click at the expected position |
| 74 | assertThat(recordedLongClicks).isEqualTo(listOf(expectedPosition)) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 79 | * Tests [sendLongClick] with arguments. Verifies that the click is in the middle |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 80 | * of the component, that the gesture has a duration of 600 milliseconds and that all input |
| 81 | * events were on the same location. |
| 82 | */ |
| 83 | @MediumTest |
| 84 | @RunWith(Parameterized::class) |
| 85 | class SendLongClickWithArgumentsTest(private val config: TestConfig) { |
| 86 | data class TestConfig( |
| 87 | val position: PxPosition |
| 88 | ) |
| 89 | |
| 90 | companion object { |
| 91 | @JvmStatic |
| 92 | @Parameterized.Parameters(name = "{0}") |
| 93 | fun createTestSet(): List<TestConfig> { |
| 94 | return mutableListOf<TestConfig>().apply { |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 95 | for (x in listOf(1.0f, defaultSize / 4)) { |
| 96 | for (y in listOf(1.0f, defaultSize / 3)) { |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 97 | add(TestConfig(PxPosition(x, y))) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @get:Rule |
| 105 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 106 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 107 | private val recordedLongClicks = mutableListOf<PxPosition>() |
| 108 | |
| 109 | private fun recordLongPress(position: PxPosition) { |
| 110 | recordedLongClicks.add(position) |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | fun testLongClick() { |
| 115 | // Given some content |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 116 | composeTestRule.setContent { |
| 117 | Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) { |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 118 | ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress)) |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 121 | |
| 122 | // When we inject a long click |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 123 | findByTag(defaultTag).doGesture { sendLongClick(config.position) } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 124 | |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 125 | // Then we record 1 long click at the expected position |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 126 | assertThat(recordedLongClicks).hasSize(1) |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame^] | 127 | recordedLongClicks[0].isAlmostEqualTo(config.position) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 128 | } |
| 129 | } |