From c127641188b547f74edbb76be3aba0702a97283d Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 20 Nov 2024 22:46:04 -0500 Subject: [PATCH] Use a simpler API to manipulate safe area insets Discussion here: https://publicobject.com/2024/11/20/testing-safe-area-insets-on-ios/ --- .../cash/redwood/widget/RedwoodUIViewTest.kt | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/redwood-widget/src/iosTest/kotlin/app/cash/redwood/widget/RedwoodUIViewTest.kt b/redwood-widget/src/iosTest/kotlin/app/cash/redwood/widget/RedwoodUIViewTest.kt index 43ba851aae..42dd65b136 100644 --- a/redwood-widget/src/iosTest/kotlin/app/cash/redwood/widget/RedwoodUIViewTest.kt +++ b/redwood-widget/src/iosTest/kotlin/app/cash/redwood/widget/RedwoodUIViewTest.kt @@ -22,13 +22,13 @@ import assertk.assertThat import assertk.assertions.containsExactly import assertk.assertions.isEqualTo import kotlin.test.Test -import platform.CoreGraphics.CGRectMake +import kotlinx.cinterop.cValue +import platform.CoreGraphics.CGRectZero +import platform.UIKit.UIEdgeInsets import platform.UIKit.UIEdgeInsetsMake +import platform.UIKit.UIEdgeInsetsZero import platform.UIKit.UILabel import platform.UIKit.UIView -import platform.UIKit.UIViewController -import platform.UIKit.UIWindow -import platform.UIKit.additionalSafeAreaInsets class RedwoodUIViewTest { @Test @@ -41,31 +41,17 @@ class RedwoodUIViewTest { assertThat(redwoodUIView.value.subviews).containsExactly(label) } - /** - * Confirm we accept and propagates insets through [RedwoodUIView.uiConfiguration]. - * - * Testing insets is tricky. We need both a [UIWindow] and a [UIViewController] to apply insets to - * a subject view. - */ + /** Confirm we accept and propagates insets through [RedwoodUIView.uiConfiguration]. */ @Test fun viewInsets() { val redwoodUIView = RedwoodUIView() - val viewController = object : UIViewController(null, null) { - override fun loadView() { - view = redwoodUIView.value - } - } - - val window = UIWindow( - CGRectMake(0.0, 0.0, 390.0, 844.0), // iPhone 14. - ) - window.setHidden(false) // Necessary to propagate additionalSafeAreaInsets. - window.rootViewController = viewController + val insetsContainer = InsetsContainer() + insetsContainer.addSubview(redwoodUIView.value) assertThat(redwoodUIView.uiConfiguration.value.viewInsets) .isEqualTo(Margin.Zero) - viewController.additionalSafeAreaInsets = UIEdgeInsetsMake(10.0, 20.0, 30.0, 40.0) + insetsContainer.subviewSafeAreaInsets = UIEdgeInsetsMake(10.0, 20.0, 30.0, 40.0) assertThat(redwoodUIView.uiConfiguration.value.viewInsets) .isEqualTo(Margin(top = 10.0.dp, start = 20.0.dp, bottom = 30.0.dp, end = 40.0.dp)) @@ -76,4 +62,16 @@ class RedwoodUIViewTest { ) : Widget { override var modifier: Modifier = Modifier } + + /** Override [safeAreaInsets] to propagate a test value to subviews on the next layout. */ + class InsetsContainer : UIView(cValue { CGRectZero }) { + var subviewSafeAreaInsets = cValue { UIEdgeInsetsZero } + set(value) { + field = value + setNeedsLayout() + layoutIfNeeded() + } + + override fun safeAreaInsets() = subviewSafeAreaInsets + } }