|
| 1 | +/* |
| 2 | + * Copyright 2026 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 | + |
| 17 | +package androidx.compose.ui.interop |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Box |
| 20 | +import androidx.compose.foundation.layout.Column |
| 21 | +import androidx.compose.foundation.layout.fillMaxSize |
| 22 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 23 | +import androidx.compose.foundation.layout.height |
| 24 | +import androidx.compose.foundation.layout.systemBarsPadding |
| 25 | +import androidx.compose.foundation.pager.HorizontalPager |
| 26 | +import androidx.compose.foundation.pager.rememberPagerState |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.MutableIntState |
| 29 | +import androidx.compose.runtime.mutableIntStateOf |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.background |
| 32 | +import androidx.compose.ui.graphics.Color |
| 33 | +import androidx.compose.ui.platform.testTag |
| 34 | +import androidx.compose.ui.test.AccessibilityTestNode |
| 35 | +import androidx.compose.ui.test.UIKitInstrumentedTest |
| 36 | +import androidx.compose.ui.test.UIKitInstrumentedTestBlock |
| 37 | +import androidx.compose.ui.test.findNodeWithTag |
| 38 | +import androidx.compose.ui.test.findNodeWithTagOrNull |
| 39 | +import androidx.compose.ui.test.runUIKitInstrumentedTestInHostingView |
| 40 | +import androidx.compose.ui.test.runUIKitInstrumentedTestInHostingViewController |
| 41 | +import androidx.compose.ui.test.utils.up |
| 42 | +import androidx.compose.ui.uikit.embedSubview |
| 43 | +import androidx.compose.ui.unit.dp |
| 44 | +import kotlin.test.Test |
| 45 | +import kotlin.test.assertEquals |
| 46 | +import kotlin.test.assertNotNull |
| 47 | +import kotlin.test.assertNull |
| 48 | +import kotlin.time.Duration.Companion.milliseconds |
| 49 | +import kotlinx.cinterop.ExperimentalForeignApi |
| 50 | +import platform.UIKit.UINavigationController |
| 51 | +import platform.UIKit.UIViewController |
| 52 | + |
| 53 | +@OptIn(ExperimentalForeignApi::class) |
| 54 | +internal abstract class UIKitNavigationContentSwipeTest( |
| 55 | + private val runUIKitInstrumentedTest: (UIKitInstrumentedTestBlock) -> Unit |
| 56 | +) { |
| 57 | + private val SwipeDuration = 100.milliseconds |
| 58 | + |
| 59 | + @Test |
| 60 | + fun testSwipeRightOnPagerDoesNotPopController() = runUIKitInstrumentedTest { |
| 61 | + val currentPage = mutableIntStateOf(0) |
| 62 | + |
| 63 | + setNavigationControllerContent { |
| 64 | + TestContent(currentPage = currentPage) |
| 65 | + } |
| 66 | + |
| 67 | + delay(10) |
| 68 | + |
| 69 | + swipeRight(fromNode = findNodeWithTag("pager")) |
| 70 | + |
| 71 | + assertEquals(0, currentPage.value) |
| 72 | + assertEquals(2, navigationController.viewControllers.size) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testSwipeLeftOnPagerChangesPage() = runUIKitInstrumentedTest { |
| 77 | + val currentPage = mutableIntStateOf(0) |
| 78 | + |
| 79 | + setNavigationControllerContent { |
| 80 | + TestContent(currentPage = currentPage) |
| 81 | + } |
| 82 | + |
| 83 | + delay(10) |
| 84 | + |
| 85 | + swipeLeft(fromNode = findNodeWithTag("pager")) |
| 86 | + |
| 87 | + assertEquals(1, currentPage.value) |
| 88 | + assertEquals(2, navigationController.viewControllers.size) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun testSwipeLeftOutsidePagerNoChanges() = runUIKitInstrumentedTest { |
| 93 | + val initialPage = 1 |
| 94 | + val currentPage = mutableIntStateOf(initialPage) |
| 95 | + |
| 96 | + setNavigationControllerContent { |
| 97 | + TestContent(currentPage = currentPage) |
| 98 | + } |
| 99 | + |
| 100 | + delay(10) |
| 101 | + |
| 102 | + swipeLeft(fromNode = findNodeWithTag("outsideBox")) |
| 103 | + |
| 104 | + assertEquals(initialPage, currentPage.value) |
| 105 | + assertEquals(2, navigationController.viewControllers.size) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun testSwipeRightOutsidePagerPopsController() = runUIKitInstrumentedTest { |
| 110 | + val initialPage = 1 |
| 111 | + val currentPage = mutableIntStateOf(initialPage) |
| 112 | + |
| 113 | + setNavigationControllerContent { |
| 114 | + TestContent(currentPage = currentPage) |
| 115 | + } |
| 116 | + |
| 117 | + delay(10) |
| 118 | + |
| 119 | + swipeRight(fromNode = findNodeWithTag("outsideBox")) |
| 120 | + |
| 121 | + // wait for pop animation to finish |
| 122 | + delay(500) |
| 123 | + |
| 124 | + assertNull(findNodeWithTagOrNull("pager")) |
| 125 | + assertNull(findNodeWithTagOrNull("outsideBox")) |
| 126 | + assertEquals(1, navigationController.viewControllers.size) |
| 127 | + } |
| 128 | + |
| 129 | + private fun UIKitInstrumentedTest.swipeRight(fromNode: AccessibilityTestNode) { |
| 130 | + fromNode.touchDown() |
| 131 | + .dragTo(x = screenSize.width - 16.dp, duration = SwipeDuration) |
| 132 | + .up() |
| 133 | + |
| 134 | + waitForIdle() |
| 135 | + } |
| 136 | + |
| 137 | + private fun UIKitInstrumentedTest.swipeLeft(fromNode: AccessibilityTestNode) { |
| 138 | + fromNode.touchDown() |
| 139 | + .dragTo(x = 16.dp, duration = SwipeDuration) |
| 140 | + .up() |
| 141 | + |
| 142 | + waitForIdle() |
| 143 | + } |
| 144 | + |
| 145 | + private val UIKitInstrumentedTest.navigationController: UINavigationController get() { |
| 146 | + return assertNotNull(appDelegate.window?.rootViewController as? UINavigationController) |
| 147 | + } |
| 148 | + |
| 149 | + private fun UIKitInstrumentedTest.setNavigationControllerContent( |
| 150 | + content: @Composable () -> Unit = {} |
| 151 | + ) { |
| 152 | + val firstViewController = UIViewController() |
| 153 | + val secondViewController = |
| 154 | + if (useHostingView) { |
| 155 | + UIViewController().also { |
| 156 | + it.view.embedSubview(createHostingView(content = content)) |
| 157 | + } |
| 158 | + } else { |
| 159 | + createHostingViewController(content = content) |
| 160 | + } |
| 161 | + val navigationController = UINavigationController() |
| 162 | + |
| 163 | + navigationController.setViewControllers( |
| 164 | + listOf(firstViewController, secondViewController), false |
| 165 | + ) |
| 166 | + |
| 167 | + appDelegate.setUpWindow(navigationController) |
| 168 | + |
| 169 | + waitForIdle() |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +@Composable |
| 174 | +private fun TestContent( |
| 175 | + currentPage: MutableIntState |
| 176 | +) { |
| 177 | + val pagerColors = listOf(Color.Red, Color.Green, Color.Blue) |
| 178 | + val pagerState = rememberPagerState(initialPage = currentPage.value) { 3 } |
| 179 | + |
| 180 | + Column( |
| 181 | + modifier = Modifier |
| 182 | + .fillMaxSize() |
| 183 | + .systemBarsPadding() |
| 184 | + ) { |
| 185 | + HorizontalPager( |
| 186 | + state = pagerState, |
| 187 | + modifier = Modifier |
| 188 | + .fillMaxWidth() |
| 189 | + .weight(1f) |
| 190 | + .testTag("pager") |
| 191 | + ) { page -> |
| 192 | + currentPage.value = page |
| 193 | + Box(modifier = Modifier |
| 194 | + .fillMaxSize() |
| 195 | + .background(pagerColors[page]) |
| 196 | + ) |
| 197 | + } |
| 198 | + |
| 199 | + Box( |
| 200 | + modifier = Modifier |
| 201 | + .fillMaxWidth() |
| 202 | + .height(150.dp) |
| 203 | + .testTag("outsideBox") |
| 204 | + ) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +internal class UIKitNavigationContentSwipeInHostingViewTest : UIKitNavigationContentSwipeTest( |
| 209 | + runUIKitInstrumentedTest = ::runUIKitInstrumentedTestInHostingView |
| 210 | +) |
| 211 | + |
| 212 | +internal class UIKitNavigationContentSwipeInHostingViewControllerTest : UIKitNavigationContentSwipeTest( |
| 213 | + runUIKitInstrumentedTest = ::runUIKitInstrumentedTestInHostingViewController |
| 214 | +) |
0 commit comments