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 |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 34 | import androidx.ui.geometry.Offset |
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 |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 39 | import org.junit.runners.Parameterized |
| 40 | |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 41 | /** |
Jelle Fresen | 3d3c448 | 2020-04-17 18:43:02 +0100 | [diff] [blame] | 42 | * Tests [sendLongClick] with arguments. Verifies that the click is in the middle |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 43 | * of the component, that the gesture has a duration of 600 milliseconds and that all input |
| 44 | * events were on the same location. |
| 45 | */ |
| 46 | @MediumTest |
| 47 | @RunWith(Parameterized::class) |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 48 | class SendLongClickTest(private val config: TestConfig) { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 49 | data class TestConfig(val position: Offset?) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 50 | |
| 51 | companion object { |
| 52 | @JvmStatic |
| 53 | @Parameterized.Parameters(name = "{0}") |
| 54 | fun createTestSet(): List<TestConfig> { |
| 55 | return mutableListOf<TestConfig>().apply { |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 56 | for (x in listOf(1.0f, defaultSize / 4)) { |
| 57 | for (y in listOf(1.0f, defaultSize / 3)) { |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 58 | add(TestConfig(Offset(x, y))) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 61 | add(TestConfig(null)) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @get:Rule |
| 67 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 68 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 69 | private val recordedLongClicks = mutableListOf<Offset>() |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 70 | private val expectedClickPosition = |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 71 | config.position ?: Offset(defaultSize / 2, defaultSize / 2) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 72 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame^] | 73 | private fun recordLongPress(position: Offset) { |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 74 | recordedLongClicks.add(position) |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun testLongClick() { |
| 79 | // Given some content |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 80 | composeTestRule.setContent { |
| 81 | Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) { |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 82 | ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress)) |
Jelle Fresen | 3b1a526 | 2020-04-30 18:30:28 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 85 | |
| 86 | // When we inject a long click |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 87 | findByTag(defaultTag).doGesture { |
| 88 | if (config.position != null) { |
| 89 | sendLongClick(config.position) |
| 90 | } else { |
| 91 | sendLongClick() |
| 92 | } |
| 93 | } |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 94 | |
Jelle Fresen | 5b1db50 | 2020-05-28 16:44:47 +0100 | [diff] [blame] | 95 | // Then we record 1 long click at the expected position |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 96 | assertThat(recordedLongClicks).hasSize(1) |
Jelle Fresen | ae8fbfc | 2020-06-02 17:54:26 +0100 | [diff] [blame] | 97 | recordedLongClicks[0].isAlmostEqualTo(expectedClickPosition) |
Jelle Fresen | 208d53f | 2020-01-10 16:03:36 +0000 | [diff] [blame] | 98 | } |
| 99 | } |