[go: nahoru, domu]

blob: def81a71891984547a7325a7d96323bf35364b2b [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
Nader Jawad6df06122020-06-03 15:27:08 -070034import androidx.ui.geometry.Offset
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
Jelle Fresen208d53f2020-01-10 16:03:36 +000039import org.junit.runners.Parameterized
40
Jelle Fresen208d53f2020-01-10 16:03:36 +000041/**
Jelle Fresen3d3c4482020-04-17 18:43:02 +010042 * Tests [sendLongClick] with arguments. Verifies that the click is in the middle
Jelle Fresen208d53f2020-01-10 16:03:36 +000043 * 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 Fresenae8fbfc2020-06-02 17:54:26 +010048class SendLongClickTest(private val config: TestConfig) {
Nader Jawad6df06122020-06-03 15:27:08 -070049 data class TestConfig(val position: Offset?)
Jelle Fresen208d53f2020-01-10 16:03:36 +000050
51 companion object {
52 @JvmStatic
53 @Parameterized.Parameters(name = "{0}")
54 fun createTestSet(): List<TestConfig> {
55 return mutableListOf<TestConfig>().apply {
Jelle Fresen5b1db502020-05-28 16:44:47 +010056 for (x in listOf(1.0f, defaultSize / 4)) {
57 for (y in listOf(1.0f, defaultSize / 3)) {
Nader Jawad6df06122020-06-03 15:27:08 -070058 add(TestConfig(Offset(x, y)))
Jelle Fresen208d53f2020-01-10 16:03:36 +000059 }
60 }
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010061 add(TestConfig(null))
Jelle Fresen208d53f2020-01-10 16:03:36 +000062 }
63 }
64 }
65
66 @get:Rule
67 val composeTestRule = createComposeRule(disableTransitions = true)
68
Nader Jawad6df06122020-06-03 15:27:08 -070069 private val recordedLongClicks = mutableListOf<Offset>()
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010070 private val expectedClickPosition =
Nader Jawad6df06122020-06-03 15:27:08 -070071 config.position ?: Offset(defaultSize / 2, defaultSize / 2)
Jelle Fresen208d53f2020-01-10 16:03:36 +000072
Nader Jawad6df06122020-06-03 15:27:08 -070073 private fun recordLongPress(position: Offset) {
Jelle Fresen208d53f2020-01-10 16:03:36 +000074 recordedLongClicks.add(position)
75 }
76
77 @Test
78 fun testLongClick() {
79 // Given some content
Jelle Fresen3b1a5262020-04-30 18:30:28 +010080 composeTestRule.setContent {
81 Stack(Modifier.fillMaxSize().wrapContentSize(Alignment.BottomEnd)) {
Jelle Fresen5b1db502020-05-28 16:44:47 +010082 ClickableTestBox(Modifier.longPressGestureFilter(::recordLongPress))
Jelle Fresen3b1a5262020-04-30 18:30:28 +010083 }
84 }
Jelle Fresen208d53f2020-01-10 16:03:36 +000085
86 // When we inject a long click
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010087 findByTag(defaultTag).doGesture {
88 if (config.position != null) {
89 sendLongClick(config.position)
90 } else {
91 sendLongClick()
92 }
93 }
Jelle Fresen208d53f2020-01-10 16:03:36 +000094
Jelle Fresen5b1db502020-05-28 16:44:47 +010095 // Then we record 1 long click at the expected position
Jelle Fresen208d53f2020-01-10 16:03:36 +000096 assertThat(recordedLongClicks).hasSize(1)
Jelle Fresenae8fbfc2020-06-02 17:54:26 +010097 recordedLongClicks[0].isAlmostEqualTo(expectedClickPosition)
Jelle Fresen208d53f2020-01-10 16:03:36 +000098 }
99}