[go: nahoru, domu]

blob: 984308370969d480b3b43266bc7f59595eb60808 [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
Anastasia Soboleva637055e2020-02-27 14:26:28 +000023import androidx.ui.layout.Stack
Adam Powell999a89b2020-03-11 09:08:07 -070024import androidx.ui.layout.fillMaxSize
Adam Powell999a89b2020-03-11 09:08:07 -070025import androidx.ui.layout.wrapContentSize
Jelle Fresen3d3c4482020-04-17 18:43:02 +010026import androidx.ui.test.createComposeRule
27import androidx.ui.test.doGesture
28import androidx.ui.test.findByTag
Jelle Fresen3d3c4482020-04-17 18:43:02 +010029import androidx.ui.test.sendLongClick
Jelle Fresen3b1a5262020-04-30 18:30:28 +010030import androidx.ui.test.util.ClickableTestBox
Jelle Fresen5b1db502020-05-28 16:44:47 +010031import androidx.ui.test.util.ClickableTestBox.defaultSize
32import androidx.ui.test.util.ClickableTestBox.defaultTag
33import androidx.ui.test.util.isAlmostEqualTo
George Mount842c8c12020-01-08 16:03:42 -080034import androidx.ui.unit.PxPosition
Jelle Fresen208d53f2020-01-10 16:03:36 +000035import com.google.common.truth.Truth.assertThat
36import org.junit.Rule
37import org.junit.Test
38import org.junit.runner.RunWith
39import org.junit.runners.JUnit4
40import org.junit.runners.Parameterized
41
Jelle Fresen208d53f2020-01-10 16:03:36 +000042/**
Jelle Fresen3d3c4482020-04-17 18:43:02 +010043 * Tests [sendLongClick] without arguments. Verifies that the click is in the middle
Jelle Fresen208d53f2020-01-10 16:03:36 +000044 * 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)
49class SendLongClickWithoutArgumentsTest {
50
51 @get:Rule
52 val composeTestRule = createComposeRule(disableTransitions = true)
53
Jelle Fresen208d53f2020-01-10 16:03:36 +000054 private val recordedLongClicks = mutableListOf<PxPosition>()
Jelle Fresen5b1db502020-05-28 16:44:47 +010055 private val expectedPosition = PxPosition(defaultSize / 2, defaultSize / 2)
Jelle Fresen208d53f2020-01-10 16:03:36 +000056
57 private fun recordLongPress(position: PxPosition) {
58 recordedLongClicks.add(position)
59 }
60
61 @Test
62 fun testLongClick() {
63 // Given some content
Jelle Fresen3b1a5262020-04-30 18:30:28 +010064 composeTestRule.setContent {
65 Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) {
Jelle Fresen5b1db502020-05-28 16:44:47 +010066 ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress))
Jelle Fresen3b1a5262020-04-30 18:30:28 +010067 }
68 }
Jelle Fresen208d53f2020-01-10 16:03:36 +000069
70 // When we inject a long click
Jelle Fresen5b1db502020-05-28 16:44:47 +010071 findByTag(defaultTag).doGesture { sendLongClick() }
Jelle Fresen208d53f2020-01-10 16:03:36 +000072
Jelle Fresen5b1db502020-05-28 16:44:47 +010073 // Then we record 1 long click at the expected position
74 assertThat(recordedLongClicks).isEqualTo(listOf(expectedPosition))
Jelle Fresen208d53f2020-01-10 16:03:36 +000075 }
76}
77
78/**
Jelle Fresen3d3c4482020-04-17 18:43:02 +010079 * Tests [sendLongClick] with arguments. Verifies that the click is in the middle
Jelle Fresen208d53f2020-01-10 16:03:36 +000080 * 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)
85class 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 Fresen5b1db502020-05-28 16:44:47 +010095 for (x in listOf(1.0f, defaultSize / 4)) {
96 for (y in listOf(1.0f, defaultSize / 3)) {
Jelle Fresen208d53f2020-01-10 16:03:36 +000097 add(TestConfig(PxPosition(x, y)))
98 }
99 }
100 }
101 }
102 }
103
104 @get:Rule
105 val composeTestRule = createComposeRule(disableTransitions = true)
106
Jelle Fresen208d53f2020-01-10 16:03:36 +0000107 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 Fresen3b1a5262020-04-30 18:30:28 +0100116 composeTestRule.setContent {
117 Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) {
Jelle Fresen5b1db502020-05-28 16:44:47 +0100118 ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress))
Jelle Fresen3b1a5262020-04-30 18:30:28 +0100119 }
120 }
Jelle Fresen208d53f2020-01-10 16:03:36 +0000121
122 // When we inject a long click
Jelle Fresen5b1db502020-05-28 16:44:47 +0100123 findByTag(defaultTag).doGesture { sendLongClick(config.position) }
Jelle Fresen208d53f2020-01-10 16:03:36 +0000124
Jelle Fresen5b1db502020-05-28 16:44:47 +0100125 // Then we record 1 long click at the expected position
Jelle Fresen208d53f2020-01-10 16:03:36 +0000126 assertThat(recordedLongClicks).hasSize(1)
Jelle Fresen5b1db502020-05-28 16:44:47 +0100127 recordedLongClicks[0].isAlmostEqualTo(config.position)
Jelle Fresen208d53f2020-01-10 16:03:36 +0000128 }
129}