Skip to content

Commit fe0c81e

Browse files
committed
Add tests for window insets padding
1 parent ea5e112 commit fe0c81e

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

compose/ui/ui/src/uikitInstrumentedTest/kotlin/androidx/compose/ui/layout/WindowInsetsPaddingTest.kt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import androidx.compose.foundation.layout.displayCutout
2525
import androidx.compose.foundation.layout.fillMaxSize
2626
import androidx.compose.foundation.layout.imePadding
2727
import androidx.compose.foundation.layout.safeDrawingPadding
28+
import androidx.compose.foundation.layout.size
29+
import androidx.compose.foundation.layout.statusBars
2830
import androidx.compose.foundation.layout.systemGesturesPadding
2931
import androidx.compose.foundation.layout.windowInsetsPadding
3032
import androidx.compose.material.Text
@@ -36,6 +38,7 @@ import androidx.compose.runtime.getValue
3638
import androidx.compose.runtime.mutableStateOf
3739
import androidx.compose.runtime.remember
3840
import androidx.compose.runtime.setValue
41+
import androidx.compose.ui.Alignment
3942
import androidx.compose.ui.Modifier
4043
import androidx.compose.ui.focus.FocusRequester
4144
import androidx.compose.ui.focus.focusRequester
@@ -47,13 +50,16 @@ import androidx.compose.ui.unit.DpRect
4750
import androidx.compose.ui.unit.DpSize
4851
import androidx.compose.ui.unit.dp
4952
import androidx.compose.ui.unit.toDpRect
53+
import androidx.compose.ui.viewinterop.UIKitView
54+
import androidx.compose.ui.window.ComposeUIView
5055
import kotlin.test.Test
5156
import kotlin.test.assertEquals
5257
import kotlinx.cinterop.ExperimentalForeignApi
5358
import kotlinx.cinterop.useContents
5459
import org.jetbrains.skiko.OS
5560
import org.jetbrains.skiko.OSVersion
5661
import org.jetbrains.skiko.available
62+
import platform.UIKit.UIColor
5763
import platform.UIKit.UIDevice
5864
import platform.UIKit.UIInterfaceOrientationMaskLandscapeLeft
5965
import platform.UIKit.UIInterfaceOrientationMaskLandscapeRight
@@ -164,6 +170,98 @@ class WindowInsetsPaddingTest {
164170

165171
assertEquals(false, recomposed.value)
166172
}
173+
174+
@OptIn(ExperimentalForeignApi::class)
175+
@Test
176+
fun testWindowInsetsPaddingAppliedToNonFullscreenContent() = runUIKitInstrumentedTest {
177+
var innerBoxRect = DpRectZero()
178+
var outerBoxRect = DpRectZero()
179+
180+
setContent {
181+
Box(modifier = Modifier.background(Color.Red).fillMaxSize()) {
182+
Box(
183+
modifier = Modifier
184+
.align(Alignment.Center)
185+
.background(Color.Blue)
186+
.size(200.dp, 200.dp)
187+
.onGloballyPositioned {
188+
outerBoxRect = it.boundsInWindow().toDpRect(density)
189+
},
190+
) {
191+
Box(modifier = Modifier
192+
.windowInsetsPadding(WindowInsets.statusBars)
193+
.background(Color.Green)
194+
.fillMaxSize()
195+
.onGloballyPositioned {
196+
innerBoxRect = it.boundsInWindow().toDpRect(density)
197+
}
198+
)
199+
}
200+
}
201+
}
202+
203+
// WindowInsets.statusBars should only represent the insets at the top in portrait orientation
204+
val topSafeAreaInsetsDp = viewController.view.safeAreaInsets.useContents { top }.dp
205+
206+
assertEquals(
207+
DpRect(
208+
left = outerBoxRect.left,
209+
top = outerBoxRect.top + topSafeAreaInsetsDp,
210+
right = outerBoxRect.right,
211+
bottom = outerBoxRect.bottom
212+
),
213+
innerBoxRect
214+
)
215+
}
216+
217+
@OptIn(ExperimentalForeignApi::class)
218+
@Test
219+
fun testWindowInsetsPaddingAppliedToNonFullscreenComposeUIViewContent() = runUIKitInstrumentedTest {
220+
var innerBoxRect = DpRectZero()
221+
var outerBoxRect = DpRectZero()
222+
223+
setContent {
224+
Box(modifier = Modifier.background(Color.Red).fillMaxSize()) {
225+
UIKitView(
226+
factory = {
227+
ComposeUIView(
228+
configure = { opaque = false }
229+
) {
230+
Box(modifier = Modifier
231+
.windowInsetsPadding(WindowInsets.statusBars)
232+
.background(Color.Green)
233+
.fillMaxSize()
234+
.onGloballyPositioned {
235+
innerBoxRect = it.boundsInWindow().toDpRect(density)
236+
}
237+
)
238+
}.apply {
239+
backgroundColor = UIColor.blueColor
240+
}
241+
},
242+
modifier = Modifier
243+
.align(Alignment.Center)
244+
.size(200.dp, 200.dp)
245+
.onGloballyPositioned {
246+
outerBoxRect = it.boundsInWindow().toDpRect(density)
247+
}
248+
)
249+
}
250+
}
251+
252+
// WindowInsets.statusBars should only represent the insets at the top in portrait orientation
253+
val topSafeAreaInsetsDp = viewController.view.safeAreaInsets.useContents { top }.dp
254+
255+
assertEquals(
256+
DpRect(
257+
left = outerBoxRect.left,
258+
top = outerBoxRect.top + topSafeAreaInsetsDp,
259+
right = outerBoxRect.right,
260+
bottom = outerBoxRect.bottom
261+
),
262+
innerBoxRect
263+
)
264+
}
167265
}
168266

169267
@Composable

compose/ui/ui/src/uikitInstrumentedTest/kotlin/androidx/compose/ui/test/UIKitInstrumentedTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import androidx.compose.ui.unit.asDpOffset
4343
import androidx.compose.ui.unit.asDpRect
4444
import androidx.compose.ui.unit.dp
4545
import androidx.compose.ui.unit.lerp
46-
import androidx.compose.ui.unit.toOffset
46+
import androidx.compose.ui.unit.size
4747
import androidx.compose.ui.window.KeyboardVisibilityListener
4848
import androidx.compose.ui.window.MetalRedrawer
4949
import kotlin.coroutines.cancellation.CancellationException
@@ -212,8 +212,9 @@ internal class UIKitInstrumentedTest(
212212
val appDelegate = MockAppDelegate()
213213
val keyboardHeight: Dp get() =
214214
KeyboardVisibilityListener.keyboardFrame.useContents { size.height.dp }
215-
val screenSize: DpSize get() = screen.bounds().useContents { DpSize(size.width.dp, size.height.dp) }
216-
val safeDrawingRect: DpRect get() = screen.bounds().asDpRect().let { rect ->
215+
val screenBounds: DpRect get() = screen.bounds().asDpRect()
216+
val screenSize: DpSize get() = screenBounds.size
217+
val safeDrawingRect: DpRect get() = screenBounds.let { rect ->
217218
viewController.view.safeAreaInsets.useContents {
218219
DpRect(
219220
left = rect.left + Dp(this.left.toFloat()),

0 commit comments

Comments
 (0)