Add tests for CurvedModifier.padding
Also, fix bug found adding the tests,
Relnote: N/A
Test: This is it
Change-Id: I246540179debe5ada7c0d9ca4b157562551305bc
diff --git a/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/CurvedPaddingTest.kt b/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/CurvedPaddingTest.kt
new file mode 100644
index 0000000..44e155e
--- /dev/null
+++ b/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/CurvedPaddingTest.kt
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.wear.compose.foundation
+
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.test.junit4.createComposeRule
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+import org.junit.Rule
+import org.junit.Test
+
+class CurvedPaddingTest {
+
+ @get:Rule
+ val rule = createComposeRule()
+
+ @Test
+ fun padding_all_works() =
+ check_padding_result(
+ 3.dp,
+ 3.dp,
+ 3.dp,
+ 3.dp,
+ CurvedModifier.padding(3.dp)
+ )
+
+ @Test
+ fun padding_angular_and_radial_works() =
+ check_padding_result(
+ outerPadding = 4.dp,
+ innerPadding = 4.dp,
+ beforePadding = 6.dp,
+ afterPadding = 6.dp,
+ CurvedModifier.padding(radial = 4.dp, angular = 6.dp)
+ )
+
+ @Test
+ fun basic_padding_works() =
+ check_padding_result(
+ outerPadding = 3.dp,
+ innerPadding = 4.dp,
+ beforePadding = 5.dp,
+ afterPadding = 6.dp,
+ CurvedModifier.padding(
+ outer = 3.dp,
+ inner = 4.dp,
+ before = 5.dp,
+ after = 6.dp
+ )
+ )
+
+ @Test
+ fun nested_padding_works() =
+ check_padding_result(
+ 11.dp,
+ 14.dp,
+ 18.dp,
+ 25.dp,
+ CurvedModifier
+ .padding(3.dp, 4.dp, 5.dp, 6.dp)
+ .padding(8.dp, 10.dp, 13.dp, 19.dp)
+ )
+
+ private fun check_padding_result(
+ outerPadding: Dp,
+ innerPadding: Dp,
+ beforePadding: Dp,
+ afterPadding: Dp,
+ modifier: CurvedModifier
+ ) {
+
+ val paddedCapturedInfo = CapturedInfo()
+ val componentCapturedInfo = CapturedInfo()
+
+ val componentThickness = 10.dp
+ val componentSweepDegrees = 90f
+
+ var outerPaddingPx = 0f
+ var innerPaddingPx = 0f
+ var beforePaddingPx = 0f
+ var afterPaddingPx = 0f
+ var componentThicknessPx = 0f
+
+ rule.setContent {
+ with(LocalDensity.current) {
+ outerPaddingPx = outerPadding.toPx()
+ innerPaddingPx = innerPadding.toPx()
+ beforePaddingPx = beforePadding.toPx()
+ afterPaddingPx = afterPadding.toPx()
+ componentThicknessPx = componentThickness.toPx()
+ }
+ CurvedLayout {
+ curvedRow(modifier = CurvedModifier
+ .spy(paddedCapturedInfo)
+ .then(modifier)
+ .spy(componentCapturedInfo)
+ .size(
+ sweepDegrees = componentSweepDegrees,
+ thickness = componentThickness
+ )
+ ) { }
+ }
+ }
+
+ rule.runOnIdle {
+ val measureRadius = componentCapturedInfo.lastLayoutInfo!!.measureRadius
+ val beforePaddingAsAngle = beforePaddingPx / measureRadius
+ val afterPaddingAsAngle = afterPaddingPx / measureRadius
+
+ // Check sizes.
+ val paddingAsAngle = (beforePaddingAsAngle + afterPaddingAsAngle).toDegrees()
+ paddedCapturedInfo.checkDimensions(
+ componentSweepDegrees + paddingAsAngle,
+ componentThicknessPx + outerPaddingPx + innerPaddingPx
+ )
+ componentCapturedInfo.checkDimensions(componentSweepDegrees, componentThicknessPx)
+
+ // Check its position.
+ componentCapturedInfo.checkPositionRelativeTo(
+ paddedCapturedInfo,
+ expectedAngularPositionDegrees = beforePaddingAsAngle,
+ expectedRadialPositionPx = outerPaddingPx,
+ )
+ }
+ }
+}
diff --git a/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/SpyModifier.kt b/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/SpyModifier.kt
index 45360e3..ce5b3a8 100644
--- a/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/SpyModifier.kt
+++ b/wear/compose/compose-foundation/src/androidAndroidTest/kotlin/androidx/wear/compose/foundation/SpyModifier.kt
@@ -112,6 +112,23 @@
)
}
+internal fun CapturedInfo.checkPositionRelativeTo(
+ target: CapturedInfo,
+ expectedAngularPositionDegrees: Float,
+ expectedRadialPositionPx: Float
+) {
+ Assert.assertEquals(
+ expectedAngularPositionDegrees,
+ lastLayoutInfo!!.startAngleRadians - target.lastLayoutInfo!!.startAngleRadians,
+ FINE_FLOAT_TOLERANCE
+ )
+ Assert.assertEquals(
+ expectedRadialPositionPx,
+ target.lastLayoutInfo!!.outerRadius - lastLayoutInfo!!.outerRadius,
+ FINE_FLOAT_TOLERANCE
+ )
+}
+
internal fun CurvedModifier.spy(capturedInfo: CapturedInfo) =
this.then { wrapped -> SpyCurvedChildWrapper(capturedInfo, wrapped) }
diff --git a/wear/compose/compose-foundation/src/commonMain/kotlin/androidx/wear/compose/foundation/CurvedPadding.kt b/wear/compose/compose-foundation/src/commonMain/kotlin/androidx/wear/compose/foundation/CurvedPadding.kt
index 43be9c2..44024d7 100644
--- a/wear/compose/compose-foundation/src/commonMain/kotlin/androidx/wear/compose/foundation/CurvedPadding.kt
+++ b/wear/compose/compose-foundation/src/commonMain/kotlin/androidx/wear/compose/foundation/CurvedPadding.kt
@@ -207,7 +207,7 @@
partialLayoutInfo.sweepRadians + angularPadding,
partialLayoutInfo.outerRadius + outerPx,
partialLayoutInfo.thickness + innerPx + outerPx,
- partialLayoutInfo.measureRadius + outerPx
+ partialLayoutInfo.measureRadius
)
}