@momo-kits/native-kits 0.169.13-beta.2 → 0.169.13-beta.3-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +4 -1
- package/build.gradle.kts +11 -0
- package/compose/build.gradle.kts +202 -0
- package/compose/compose.podspec +47 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +154 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReduceMotion.android.kt +17 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +6 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiActionsTest.kt +647 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiExprTest.kt +1411 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiHostTest.kt +130 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitArgsTest.kt +524 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitNodesTest.kt +559 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderPlanTest.kt +741 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderReuseTest.kt +221 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRendererPropsTest.kt +270 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStylePlanTest.kt +849 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStyleTest.kt +212 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiTrackingTest.kt +239 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiViewTest.kt +252 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +110 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +61 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +1067 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +268 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +81 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +354 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +115 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +130 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +482 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +49 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +224 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +137 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +473 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +113 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +262 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +237 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +228 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +280 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +183 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +342 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +40 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +82 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +362 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +304 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +88 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +308 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +218 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +456 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +205 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +542 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +201 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +176 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +180 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +194 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +291 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/HeaderLayout.kt +71 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +50 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ModalLayout.kt +184 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +315 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +41 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +397 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +58 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +153 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +118 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +206 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +421 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +750 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +152 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +35 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +242 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +398 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +103 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +93 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +167 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +446 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +105 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +106 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +74 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +400 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +363 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +197 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +127 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +114 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +76 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReduceMotion.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiActions.kt +154 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiExpr.kt +1038 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiKitNodes.kt +848 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiLog.kt +47 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiModel.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiPropsRenderer.kt +463 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiRenderer.kt +1146 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiStyle.kt +667 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiTracking.kt +240 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiValidate.kt +185 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiView.kt +200 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +117 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +42 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +45 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +32 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +38 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +65 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +217 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReduceMotion.ios.kt +48 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +143 -0
- package/gradle/libs.versions.toml +69 -0
- package/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/gradle.properties +24 -0
- package/gradlew +252 -0
- package/gradlew.bat +94 -0
- package/ios/Navigation/BottomTab/BottomTabBar.swift +4 -14
- package/ios/native-kits.podspec +1 -1
- package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
- package/ios-demo/README.md +120 -0
- package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/BottomTabDemo.swift +3 -16
- package/package.json +1 -1
- package/scripts/sdui-visual/README.md +64 -0
- package/scripts/sdui-visual/diff.py +65 -0
- package/scripts/sdui-visual/drive.py +234 -0
- package/scripts/sdui-visual/plan.json +713 -0
- package/scripts/sdui-visual/run.sh +92 -0
- package/settings.gradle.kts +64 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
// File: compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStylePlanTest.kt
|
|
2
|
+
// Created At: 2026-11-10 12:00:00 +07:00
|
|
3
|
+
// Created By: AI
|
|
4
|
+
// AI Agent: Claude Code
|
|
5
|
+
// Model: claude-sonnet-5-5
|
|
6
|
+
|
|
7
|
+
package vn.momo.kits.sdui
|
|
8
|
+
|
|
9
|
+
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
10
|
+
import androidx.compose.foundation.background
|
|
11
|
+
import androidx.compose.foundation.layout.fillMaxHeight
|
|
12
|
+
import androidx.compose.foundation.layout.fillMaxWidth
|
|
13
|
+
import androidx.compose.foundation.layout.height
|
|
14
|
+
import androidx.compose.foundation.layout.heightIn
|
|
15
|
+
import androidx.compose.foundation.layout.padding
|
|
16
|
+
import androidx.compose.foundation.layout.width
|
|
17
|
+
import androidx.compose.foundation.layout.widthIn
|
|
18
|
+
import androidx.compose.ui.Modifier
|
|
19
|
+
import androidx.compose.ui.draw.alpha
|
|
20
|
+
import androidx.compose.ui.graphics.RectangleShape
|
|
21
|
+
import androidx.compose.ui.unit.Dp
|
|
22
|
+
import androidx.compose.ui.zIndex
|
|
23
|
+
import androidx.compose.ui.graphics.Color
|
|
24
|
+
import androidx.compose.ui.unit.dp
|
|
25
|
+
import androidx.compose.ui.layout.IntrinsicMeasurable
|
|
26
|
+
import androidx.compose.ui.layout.IntrinsicMeasureScope
|
|
27
|
+
import androidx.compose.ui.unit.Constraints
|
|
28
|
+
import androidx.compose.ui.unit.LayoutDirection
|
|
29
|
+
import kotlin.math.roundToInt
|
|
30
|
+
import org.junit.Assert.assertEquals
|
|
31
|
+
import org.junit.Assert.assertFalse
|
|
32
|
+
import org.junit.Assert.assertNotNull
|
|
33
|
+
import org.junit.Assert.assertNotSame
|
|
34
|
+
import org.junit.Assert.assertNull
|
|
35
|
+
import org.junit.Assert.assertSame
|
|
36
|
+
import org.junit.Assert.assertTrue
|
|
37
|
+
import org.junit.Test
|
|
38
|
+
import vn.momo.kits.const.Colors
|
|
39
|
+
import vn.momo.kits.const.Radius
|
|
40
|
+
import vn.momo.kits.const.Spacing
|
|
41
|
+
import vn.momo.kits.const.Typography
|
|
42
|
+
import vn.momo.kits.const.defaultTheme
|
|
43
|
+
|
|
44
|
+
// AI-GENERATED START: unit tests for the pure style -> StylePlan decisions behind Sty.toModifier
|
|
45
|
+
class SduiStylePlanTest {
|
|
46
|
+
private fun sty(vararg kv: Pair<String, Any?>) = Sty(mapOf(*kv))
|
|
47
|
+
private fun plan(vararg kv: Pair<String, Any?>, stretch: Boolean = false, weight: Boolean = false, isImage: Boolean = false, hasBase: Boolean = false) =
|
|
48
|
+
stylePlanOf(sty(*kv), stretch, weight, isImage, hasBase)
|
|
49
|
+
|
|
50
|
+
private val empty = plan()
|
|
51
|
+
|
|
52
|
+
// ---- padding precedence ----
|
|
53
|
+
|
|
54
|
+
@Test fun paddingShorthandAppliesToAllSides() {
|
|
55
|
+
val p = plan("padding" to 8)
|
|
56
|
+
assertEquals(listOf(8.dp, 8.dp, 8.dp, 8.dp), listOf(p.paddingStart, p.paddingTop, p.paddingEnd, p.paddingBottom))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test fun paddingHorizontalVerticalBeatPadding() {
|
|
60
|
+
val p = plan("padding" to 4, "paddingHorizontal" to 10, "paddingVertical" to 6)
|
|
61
|
+
assertEquals(10.dp, p.paddingStart); assertEquals(10.dp, p.paddingEnd)
|
|
62
|
+
assertEquals(6.dp, p.paddingTop); assertEquals(6.dp, p.paddingBottom)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test fun paddingPerSideBeatsAxisAndShorthand() {
|
|
66
|
+
val p = plan(
|
|
67
|
+
"padding" to 1, "paddingHorizontal" to 2, "paddingVertical" to 3,
|
|
68
|
+
"paddingLeft" to 20, "paddingRight" to 30, "paddingTop" to 40, "paddingBottom" to 50,
|
|
69
|
+
)
|
|
70
|
+
assertEquals(20.dp, p.paddingStart); assertEquals(30.dp, p.paddingEnd)
|
|
71
|
+
assertEquals(40.dp, p.paddingTop); assertEquals(50.dp, p.paddingBottom)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Test fun paddingSidesResolveIndependently() {
|
|
75
|
+
val p = plan("padding" to 5, "paddingTop" to 9)
|
|
76
|
+
assertEquals(9.dp, p.paddingTop); assertEquals(5.dp, p.paddingBottom)
|
|
77
|
+
assertEquals(5.dp, p.paddingStart); assertEquals(5.dp, p.paddingEnd)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Test fun paddingStartEndAreLeftAndRight() {
|
|
81
|
+
val p = plan("paddingStart" to 12, "paddingEnd" to 7)
|
|
82
|
+
assertEquals(12.dp, p.paddingStart); assertEquals(7.dp, p.paddingEnd)
|
|
83
|
+
assertEquals(0.dp, p.paddingTop); assertEquals(0.dp, p.paddingBottom)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Test fun paddingStartEndBeatLeftRightAxisAndShorthand() {
|
|
87
|
+
val p = plan("padding" to 1, "paddingHorizontal" to 2, "paddingLeft" to 3, "paddingRight" to 4, "paddingStart" to 5, "paddingEnd" to 6)
|
|
88
|
+
assertEquals(5.dp, p.paddingStart); assertEquals(6.dp, p.paddingEnd)
|
|
89
|
+
// without Start/End the next key in line still wins
|
|
90
|
+
val q = plan("padding" to 1, "paddingHorizontal" to 2, "paddingLeft" to 3, "paddingEnd" to 6)
|
|
91
|
+
assertEquals(3.dp, q.paddingStart); assertEquals(6.dp, q.paddingEnd)
|
|
92
|
+
val r = plan("padding" to 1, "paddingStart" to "x") // invalid Start falls through
|
|
93
|
+
assertEquals(1.dp, r.paddingStart)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Test fun negativePaddingStartEndIsClampedToZero() {
|
|
97
|
+
val p = plan("paddingStart" to -4, "paddingEnd" to -1, "padding" to 9)
|
|
98
|
+
assertEquals(0.dp, p.paddingStart); assertEquals(0.dp, p.paddingEnd); assertEquals(9.dp, p.paddingTop)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@Test fun negativePaddingIsClampedToZero() {
|
|
102
|
+
val p = plan("padding" to -8, "paddingTop" to -1)
|
|
103
|
+
assertEquals(0.dp, p.paddingTop); assertEquals(0.dp, p.paddingStart)
|
|
104
|
+
assertEquals(0.dp, p.paddingEnd); assertEquals(0.dp, p.paddingBottom)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@Test fun invalidPaddingValueFallsThroughToNextKey() {
|
|
108
|
+
val p = plan("paddingTop" to "abc", "paddingVertical" to 7)
|
|
109
|
+
assertEquals(7.dp, p.paddingTop)
|
|
110
|
+
assertEquals(0.dp, plan("padding" to "8dp").paddingTop)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---- margin precedence ----
|
|
114
|
+
|
|
115
|
+
@Test fun marginPrecedenceMatchesPadding() {
|
|
116
|
+
val p = plan(
|
|
117
|
+
"margin" to 1, "marginHorizontal" to 2, "marginVertical" to 3,
|
|
118
|
+
"marginLeft" to 20, "marginTop" to 40,
|
|
119
|
+
)
|
|
120
|
+
assertEquals(20.dp, p.marginStart)
|
|
121
|
+
assertEquals(2.dp, p.marginEnd)
|
|
122
|
+
assertEquals(40.dp, p.marginTop)
|
|
123
|
+
assertEquals(3.dp, p.marginBottom)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@Test fun marginShorthandAndAccessors() {
|
|
127
|
+
val s = sty("margin" to 6)
|
|
128
|
+
assertEquals(listOf(6.dp, 6.dp, 6.dp, 6.dp), listOf(s.marTop(), s.marBottom(), s.marStart(), s.marEnd()))
|
|
129
|
+
val t = sty("marginRight" to 3, "marginBottom" to 4)
|
|
130
|
+
assertEquals(3.dp, t.marEnd()); assertEquals(4.dp, t.marBottom())
|
|
131
|
+
assertEquals(0.dp, t.marStart()); assertEquals(0.dp, t.marTop())
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@Test fun negativeMarginIsKept() {
|
|
135
|
+
val p = plan("margin" to -8, "marginTop" to -2)
|
|
136
|
+
assertEquals(-2.dp, p.marginTop); assertEquals(-8.dp, p.marginStart)
|
|
137
|
+
assertEquals(-8.dp, p.marginEnd); assertEquals(-8.dp, p.marginBottom)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@Test fun marginStartEndAreLeftAndRightWithPrecedence() {
|
|
141
|
+
val p = plan("marginStart" to 9, "marginEnd" to 3)
|
|
142
|
+
assertEquals(9.dp, p.marginStart); assertEquals(3.dp, p.marginEnd)
|
|
143
|
+
val q = plan("margin" to 1, "marginHorizontal" to 2, "marginLeft" to 3, "marginRight" to 4, "marginStart" to 5, "marginEnd" to 6)
|
|
144
|
+
assertEquals(5.dp, q.marginStart); assertEquals(6.dp, q.marginEnd); assertEquals(1.dp, q.marginTop)
|
|
145
|
+
val r = plan("marginHorizontal" to 2, "marginEnd" to 6)
|
|
146
|
+
assertEquals(2.dp, r.marginStart); assertEquals(6.dp, r.marginEnd)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@Test fun negativeMarginStartEndIsKept() {
|
|
150
|
+
val p = plan("marginStart" to -6, "marginEnd" to -2)
|
|
151
|
+
assertEquals(-6.dp, p.marginStart); assertEquals(-2.dp, p.marginEnd)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---- value parsing ----
|
|
155
|
+
|
|
156
|
+
@Test fun parseDpAcceptsNumbersAndNumericStrings() {
|
|
157
|
+
assertEquals(8.dp, parseDp(8)); assertEquals(8.dp, parseDp(8L)); assertEquals(2.5.dp, parseDp(2.5))
|
|
158
|
+
assertEquals(12.dp, parseDp("12")); assertEquals(1.5.dp, parseDp("1.5")); assertEquals(8.dp, parseDp(8.dp))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@Test fun parseDpIgnoresInvalid() {
|
|
162
|
+
assertNull(parseDp("abc")); assertNull(parseDp("12dp")); assertNull(parseDp("50%")); assertNull(parseDp(null))
|
|
163
|
+
assertNull(parseDp(true)); assertNull(parseDp(""))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@Test fun resolvedTokensFeedStylesAsDp() {
|
|
167
|
+
val t = defaultTheme
|
|
168
|
+
val p = plan("padding" to resolveToken("Spacing.M", t), "borderRadius" to resolveToken("Radius.L", t))
|
|
169
|
+
assertEquals(Spacing.M, p.paddingTop)
|
|
170
|
+
assertEquals(RoundedCornerShape(Radius.L, Radius.L, Radius.L, Radius.L), p.shape)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@Test fun unresolvedTokenStringIsIgnored() {
|
|
174
|
+
assertEquals(0.dp, plan("padding" to "Spacing.NOPE").paddingTop)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ---- width / height ----
|
|
178
|
+
|
|
179
|
+
@Test fun fixedWidthAndHeight() {
|
|
180
|
+
val p = plan("width" to 120, "height" to "48")
|
|
181
|
+
assertEquals(WidthMode.FIXED, p.widthMode); assertEquals(120.dp, p.width); assertEquals(48.dp, p.height)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
@Test fun width100PercentFillsEvenWithoutStretch() {
|
|
185
|
+
assertEquals(WidthMode.FILL, plan("width" to "100%").widthMode)
|
|
186
|
+
assertNull(plan("width" to "100%").width)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
@Test fun stretchFillsWidthUnlessWeightedOrExplicit() {
|
|
190
|
+
assertEquals(WidthMode.FILL, plan(stretch = true).widthMode)
|
|
191
|
+
assertEquals(WidthMode.NONE, plan(stretch = false).widthMode)
|
|
192
|
+
assertEquals(WidthMode.NONE, plan(stretch = true, weight = true).widthMode)
|
|
193
|
+
assertEquals(WidthMode.FIXED, plan("width" to 50, stretch = true).widthMode)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
@Test fun percentWidthFillsThatFraction() {
|
|
197
|
+
val half = plan("width" to "50%")
|
|
198
|
+
assertEquals(WidthMode.FILL, half.widthMode); assertEquals(0.5f, half.widthFraction, 1e-6f); assertNull(half.width)
|
|
199
|
+
assertEquals(0.335f, plan("width" to "33.5%").widthFraction, 1e-6f)
|
|
200
|
+
// a percent beats stretch and weight, like "100%" always did
|
|
201
|
+
assertEquals(0.5f, plan("width" to "50%", stretch = true, weight = true).widthFraction, 1e-6f)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
@Test fun width100PercentAndStretchKeepFractionOne() {
|
|
205
|
+
assertEquals(1f, plan("width" to "100%").widthFraction, 0f)
|
|
206
|
+
assertEquals(1f, plan(stretch = true).widthFraction, 0f)
|
|
207
|
+
assertEquals(1f, empty.widthFraction, 0f)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@Test fun percentWidthOutOfRangeIsIgnoredLikeBefore() {
|
|
211
|
+
listOf("0%", "-5%", "101%", "%", "abc%", "50 %x", "NaN%").forEach { v ->
|
|
212
|
+
assertEquals(v, WidthMode.NONE, plan("width" to v).widthMode)
|
|
213
|
+
// falls through to the stretch logic
|
|
214
|
+
assertEquals(v, WidthMode.FILL, plan("width" to v, stretch = true).widthMode)
|
|
215
|
+
assertEquals(v, 1f, plan("width" to v, stretch = true).widthFraction, 0f)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
@Test fun parsePercentTable() {
|
|
220
|
+
assertEquals(1f, parsePercent("100%")!!, 0f)
|
|
221
|
+
assertEquals(0.5f, parsePercent("50%")!!, 0f)
|
|
222
|
+
assertEquals(0.005f, parsePercent(".5%")!!, 1e-6f)
|
|
223
|
+
assertEquals(0.25f, parsePercent(" 25 % ")!!, 1e-6f)
|
|
224
|
+
assertNull(parsePercent("0%")); assertNull(parsePercent("101%")); assertNull(parsePercent("100.5%"))
|
|
225
|
+
assertNull(parsePercent(50)); assertNull(parsePercent("50")); assertNull(parsePercent(null)); assertNull(parsePercent("1e2%"))
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
@Test fun negativeWidthIsIgnoredAndDoesNotStretch() {
|
|
229
|
+
val p = plan("width" to -10, stretch = true)
|
|
230
|
+
assertEquals(WidthMode.NONE, p.widthMode); assertNull(p.width)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
@Test fun zeroWidthIsFixed() {
|
|
234
|
+
assertEquals(WidthMode.FIXED, plan("width" to 0).widthMode)
|
|
235
|
+
assertEquals(0.dp, plan("width" to 0).width)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
@Test fun invalidWidthFallsBackToStretch() {
|
|
239
|
+
assertEquals(WidthMode.FILL, plan("width" to "auto", stretch = true).widthMode)
|
|
240
|
+
assertEquals(WidthMode.NONE, plan("width" to "auto").widthMode)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
@Test fun heightIgnoresNegativeAndInvalid() {
|
|
244
|
+
assertNull(plan("height" to -4).height)
|
|
245
|
+
assertNull(plan("height" to "tall").height)
|
|
246
|
+
assertNull(plan("height" to "tall").heightFraction)
|
|
247
|
+
assertEquals(0.dp, plan("height" to 0).height)
|
|
248
|
+
assertNull(plan("height" to 0).heightFraction)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
@Test fun percentHeightFillsThatFraction() {
|
|
252
|
+
assertEquals(0.5f, plan("height" to "50%").heightFraction!!, 1e-6f)
|
|
253
|
+
assertNull(plan("height" to "50%").height)
|
|
254
|
+
assertEquals(1f, plan("height" to "100%").heightFraction!!, 0f)
|
|
255
|
+
assertNull(plan("height" to "100%").height)
|
|
256
|
+
assertNull(plan("height" to "0%").heightFraction)
|
|
257
|
+
assertNull(plan("height" to "101%").heightFraction)
|
|
258
|
+
assertNull(empty.heightFraction)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
@Test fun flexAndPositionDoNotAffectTheModifierPlan() {
|
|
262
|
+
// NOTE: toModifier does not handle these; the parent layout consumes them
|
|
263
|
+
assertEquals(empty, plan("flex" to 1, "position" to "absolute", "top" to 4, "left" to 3))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ---- opacity ----
|
|
267
|
+
|
|
268
|
+
@Test fun opacityIsClampedAndOnlyKeptBelowOne() {
|
|
269
|
+
assertEquals(0.5f, plan("opacity" to 0.5).opacity!!, 1e-6f)
|
|
270
|
+
assertEquals(0f, plan("opacity" to 0).opacity!!, 0f)
|
|
271
|
+
assertEquals(0f, plan("opacity" to -3).opacity!!, 0f)
|
|
272
|
+
assertNull(plan("opacity" to 1).opacity)
|
|
273
|
+
assertNull(plan("opacity" to 1.5).opacity)
|
|
274
|
+
assertNull(plan("opacity" to 7L).opacity)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
@Test fun opacityInvalidIsIgnored() {
|
|
278
|
+
assertNull(plan("opacity" to Double.NaN).opacity)
|
|
279
|
+
assertNull(plan("opacity" to Double.POSITIVE_INFINITY).opacity)
|
|
280
|
+
assertNull(plan("opacity" to "0.5").opacity)
|
|
281
|
+
assertNull(plan("opacity" to null).opacity)
|
|
282
|
+
assertNull(plan("opacity" to true).opacity)
|
|
283
|
+
assertNull(empty.opacity)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// ---- min / max size ----
|
|
287
|
+
|
|
288
|
+
@Test fun minMaxWidthAndHeight() {
|
|
289
|
+
val p = plan("minWidth" to 10, "maxWidth" to 20, "minHeight" to 5, "maxHeight" to "9")
|
|
290
|
+
assertEquals(SizeBounds(10.dp, 20.dp), p.widthBounds)
|
|
291
|
+
assertEquals(SizeBounds(5.dp, 9.dp), p.heightBounds)
|
|
292
|
+
assertEquals(SizeBounds(null, 20.dp), plan("maxWidth" to 20).widthBounds)
|
|
293
|
+
assertEquals(SizeBounds(4.dp, null), plan("minHeight" to 4).heightBounds)
|
|
294
|
+
assertNull(empty.widthBounds); assertNull(empty.heightBounds)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
@Test fun minAboveMaxFollowsCssMinWins() {
|
|
298
|
+
assertEquals(SizeBounds(30.dp, 30.dp), plan("minWidth" to 30, "maxWidth" to 20).widthBounds)
|
|
299
|
+
assertEquals(SizeBounds(12.dp, 12.dp), plan("minHeight" to 12, "maxHeight" to 0).heightBounds)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
@Test fun minMaxInvalidOrNegativeIsIgnored() {
|
|
303
|
+
assertNull(plan("minWidth" to -1, "maxWidth" to "abc").widthBounds)
|
|
304
|
+
assertNull(plan("maxHeight" to Double.NaN, "minHeight" to 1e9).heightBounds)
|
|
305
|
+
assertEquals(SizeBounds(null, 8.dp), plan("minWidth" to -5, "maxWidth" to 8).widthBounds)
|
|
306
|
+
assertEquals(SizeBounds(0.dp, null), plan("minWidth" to 0).widthBounds)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
@Test fun minMaxIsKeptAlongsideFixedAndFillWidth() {
|
|
310
|
+
// the plan keeps both: toModifier applies widthIn BEFORE width, so the bounds win over the width
|
|
311
|
+
val fixed = plan("width" to 300, "maxWidth" to 200)
|
|
312
|
+
assertEquals(WidthMode.FIXED, fixed.widthMode); assertEquals(300.dp, fixed.width)
|
|
313
|
+
assertEquals(SizeBounds(null, 200.dp), fixed.widthBounds)
|
|
314
|
+
val fill = plan("width" to "100%", "minWidth" to 50)
|
|
315
|
+
assertEquals(WidthMode.FILL, fill.widthMode); assertEquals(SizeBounds(50.dp, null), fill.widthBounds)
|
|
316
|
+
val h = plan("height" to 10, "minHeight" to 40)
|
|
317
|
+
assertEquals(10.dp, h.height); assertEquals(SizeBounds(40.dp, null), h.heightBounds)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ---- zIndex ----
|
|
321
|
+
|
|
322
|
+
@Test fun zIndexHelperReadsFiniteNonZeroNumbers() {
|
|
323
|
+
assertEquals(2f, sty("zIndex" to 2).zIndexOrNull()!!, 0f)
|
|
324
|
+
assertEquals(-1.5f, sty("zIndex" to -1.5).zIndexOrNull()!!, 0f)
|
|
325
|
+
assertEquals(3f, sty("zIndex" to 3L).zIndexOrNull()!!, 0f)
|
|
326
|
+
assertNull(sty("zIndex" to 0).zIndexOrNull())
|
|
327
|
+
assertNull(sty("zIndex" to "2").zIndexOrNull())
|
|
328
|
+
assertNull(sty("zIndex" to Double.NaN).zIndexOrNull())
|
|
329
|
+
assertNull(sty("zIndex" to Double.POSITIVE_INFINITY).zIndexOrNull())
|
|
330
|
+
assertNull(sty().zIndexOrNull())
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
@Test fun zIndexGoesIntoThePlan() {
|
|
334
|
+
assertEquals(4f, plan("zIndex" to 4).zIndex!!, 0f)
|
|
335
|
+
assertNull(empty.zIndex)
|
|
336
|
+
// only the zIndex field differs from an empty plan
|
|
337
|
+
assertEquals(empty, plan("zIndex" to 4).copy(zIndex = null))
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
@Test fun specZIndexLeavesTheModifierUntouchedWithoutZIndex() {
|
|
341
|
+
assertSame(Modifier, Modifier.specZIndex(sty()))
|
|
342
|
+
assertSame(Modifier, Modifier.specZIndex(sty("zIndex" to 0)))
|
|
343
|
+
assertNotSame(Modifier, Modifier.specZIndex(sty("zIndex" to 1)))
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
@Test fun toModifierChainOrder() {
|
|
347
|
+
// zIndex first, min/max before the size, opacity after the size and before background, padding last
|
|
348
|
+
val actual = sty(
|
|
349
|
+
"zIndex" to 2, "minWidth" to 10, "maxWidth" to 20, "width" to 15, "minHeight" to 1, "height" to 30,
|
|
350
|
+
"opacity" to 0.5, "backgroundColor" to "#fff", "padding" to 4,
|
|
351
|
+
).toModifier(stretch = false)
|
|
352
|
+
val expected = Modifier.zIndex(2f).widthIn(10.dp, 20.dp).heightIn(1.dp, Dp.Unspecified).width(15.dp).height(30.dp)
|
|
353
|
+
.alpha(0.5f).background(Color.White, RectangleShape).padding(start = 4.dp, top = 4.dp, end = 4.dp, bottom = 4.dp)
|
|
354
|
+
assertEquals(expected, actual)
|
|
355
|
+
val pct = sty("width" to "50%", "height" to "25%").toModifier(stretch = true)
|
|
356
|
+
assertEquals(Modifier.fillMaxWidth(0.5f).fillMaxHeight(0.25f).padding(0.dp), pct)
|
|
357
|
+
// no new key: the exact old chain
|
|
358
|
+
assertEquals(Modifier.fillMaxWidth().padding(0.dp), sty().toModifier(stretch = true))
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@Test fun newKeysAbsentLeaveThePlanAsBefore() {
|
|
362
|
+
// a style without any of the new keys decides exactly the old fields; the new ones stay at their neutral values
|
|
363
|
+
val p = plan("width" to "100%", "padding" to 4, "margin" to 2, "backgroundColor" to "#fff", "height" to 8)
|
|
364
|
+
assertNull(p.zIndex); assertNull(p.widthBounds); assertNull(p.heightBounds); assertNull(p.heightFraction); assertNull(p.opacity)
|
|
365
|
+
assertEquals(1f, p.widthFraction, 0f)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ---- aspect ratio ----
|
|
369
|
+
|
|
370
|
+
@Test fun aspectRatioNumberAndString() {
|
|
371
|
+
assertEquals(1.5f, plan("aspectRatio" to 1.5).aspectRatio!!, 1e-6f)
|
|
372
|
+
assertEquals(2f, plan("aspectRatio" to "2").aspectRatio!!, 1e-6f)
|
|
373
|
+
assertEquals(16f / 9f, plan("aspectRatio" to "16/9").aspectRatio!!, 1e-6f)
|
|
374
|
+
assertEquals(16f / 9f, plan("aspectRatio" to " 16 / 9 ").aspectRatio!!, 1e-6f)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
@Test fun aspectRatioNonPositiveOrInvalidIsDropped() {
|
|
378
|
+
assertNull(plan("aspectRatio" to 0).aspectRatio)
|
|
379
|
+
assertNull(plan("aspectRatio" to -1.5).aspectRatio)
|
|
380
|
+
assertNull(plan("aspectRatio" to "16:9").aspectRatio)
|
|
381
|
+
assertNull(plan("aspectRatio" to "abc").aspectRatio)
|
|
382
|
+
assertNull(plan("aspectRatio" to "x/2").aspectRatio)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
@Test fun parseRatioTable() {
|
|
386
|
+
assertEquals(1f, parseRatio(1)!!, 0f)
|
|
387
|
+
assertEquals(0.5f, parseRatio("1/2")!!, 1e-6f)
|
|
388
|
+
assertEquals(16f, parseRatio("16/x")!!, 1e-6f) // unparsable denominator counts as 1
|
|
389
|
+
assertEquals(0f, parseRatio("x/2")!!, 0f) // unparsable numerator counts as 0
|
|
390
|
+
assertNull(parseRatio("1/2/3"))
|
|
391
|
+
assertNull(parseRatio("nope")); assertNull(parseRatio(null)); assertNull(parseRatio(true))
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// ---- values that would overflow layout (Compose throws while measuring): dropped like unreadable ones ----
|
|
395
|
+
|
|
396
|
+
@Test fun aspectRatioThatWouldCrashLayoutIsDropped() {
|
|
397
|
+
assertNull(plan("aspectRatio" to "16/0").aspectRatio) // +Infinity
|
|
398
|
+
assertNull(plan("aspectRatio" to "-16/0").aspectRatio) // -Infinity
|
|
399
|
+
assertNull(plan("aspectRatio" to "0/0").aspectRatio) // NaN
|
|
400
|
+
assertNull(plan("aspectRatio" to 1e9).aspectRatio)
|
|
401
|
+
assertNull(plan("aspectRatio" to 1e-9).aspectRatio)
|
|
402
|
+
assertNull(plan("aspectRatio" to "Infinity").aspectRatio)
|
|
403
|
+
assertNull(parseRatio(MAX_ASPECT_RATIO * 2)); assertNull(parseRatio(MIN_ASPECT_RATIO / 2))
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
@Test fun aspectRatioRangeEdgesAreKept() {
|
|
407
|
+
assertEquals(MAX_ASPECT_RATIO, plan("aspectRatio" to MAX_ASPECT_RATIO).aspectRatio!!, 0f)
|
|
408
|
+
assertEquals(MIN_ASPECT_RATIO, plan("aspectRatio" to MIN_ASPECT_RATIO).aspectRatio!!, 0f)
|
|
409
|
+
assertEquals(16f / 9f, plan("aspectRatio" to "16/9").aspectRatio!!, 1e-6f)
|
|
410
|
+
assertEquals(0f, parseRatio(0)!!, 0f) // 0 / negative keep their meaning: no aspect ratio (plan filters > 0)
|
|
411
|
+
assertNull(plan("aspectRatio" to 0).aspectRatio)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
@Test fun sizesThatWouldCrashLayoutAreDropped() {
|
|
415
|
+
listOf<Any>("Infinity", "-Infinity", "NaN", Double.POSITIVE_INFINITY, Float.NaN, 1e9, "1e9", MAX_SPEC_DP + 1, -(MAX_SPEC_DP + 1)).forEach { bad ->
|
|
416
|
+
assertNull("parseDp($bad)", parseDp(bad))
|
|
417
|
+
}
|
|
418
|
+
assertEquals(MAX_SPEC_DP.dp, parseDp(MAX_SPEC_DP)); assertEquals((-MAX_SPEC_DP).dp, parseDp(-MAX_SPEC_DP))
|
|
419
|
+
assertEquals(12.dp, parseDp("12")); assertEquals(12.dp, parseDp(12.dp))
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
@Test fun hugeSizesInStylesFallBackAsIfAbsent() {
|
|
423
|
+
val p = plan("width" to "Infinity", "height" to 1e9, "padding" to "Infinity", "margin" to 1e12, "borderWidth" to "Infinity")
|
|
424
|
+
assertEquals(plan(), p)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ---- radius / shape / clip ----
|
|
428
|
+
|
|
429
|
+
@Test fun noRadiusMeansNoShapeAndNoClip() {
|
|
430
|
+
val p = plan("overflow" to "hidden", isImage = true)
|
|
431
|
+
assertNull(p.shape); assertFalse(p.clip)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
@Test fun uniformRadiusBuildsRoundedShape() {
|
|
435
|
+
assertEquals(RoundedCornerShape(8.dp, 8.dp, 8.dp, 8.dp), plan("borderRadius" to 8).shape)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
@Test fun perCornerRadiusOverridesAndMissingCornersAreZero() {
|
|
439
|
+
assertEquals(RoundedCornerShape(12.dp, 0.dp, 0.dp, 0.dp), plan("borderTopLeftRadius" to 12).shape)
|
|
440
|
+
val mixed = plan("borderRadius" to 4, "borderTopRightRadius" to 16, "borderBottomLeftRadius" to 0).shape
|
|
441
|
+
// constructor order is (topStart, topEnd, bottomEnd, bottomStart)
|
|
442
|
+
assertEquals(RoundedCornerShape(4.dp, 16.dp, 4.dp, 0.dp), mixed)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
@Test fun containerClipsOnlyWithOverflowHidden() {
|
|
446
|
+
assertFalse(plan("borderRadius" to 8).clip)
|
|
447
|
+
assertFalse(plan("borderRadius" to 8, "overflow" to "visible").clip)
|
|
448
|
+
assertTrue(plan("borderRadius" to 8, "overflow" to "hidden").clip)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
@Test fun imageAlwaysClipsWhenRadiusSet() {
|
|
452
|
+
assertTrue(plan("borderRadius" to 8, isImage = true).clip)
|
|
453
|
+
assertTrue(sty("borderRadius" to 8).shouldClip(true))
|
|
454
|
+
assertFalse(sty("borderRadius" to 8).shouldClip(false))
|
|
455
|
+
assertFalse(sty().shouldClip(true))
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// ---- border ----
|
|
459
|
+
|
|
460
|
+
@Test fun borderWidthWithColor() {
|
|
461
|
+
val p = plan("borderWidth" to 1, "borderColor" to "#ff0000")
|
|
462
|
+
assertEquals(BorderPlan(1.dp, Color(0xFFFF0000)), p.border)
|
|
463
|
+
assertNull(p.topBorder)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
@Test fun borderWithoutColorIsTransparent() {
|
|
467
|
+
assertEquals(BorderPlan(2.dp, Color.Transparent), plan("borderWidth" to 2).border)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
@Test fun zeroOrNegativeBorderWidthDrawsNothing() {
|
|
471
|
+
assertNull(plan("borderWidth" to 0, "borderColor" to "#000000").border)
|
|
472
|
+
assertNull(plan("borderWidth" to -1).border)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
@Test fun borderTopUsesOwnColorThenBorderColor() {
|
|
476
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Black), plan("borderTopWidth" to 1, "borderTopColor" to "black", "borderColor" to "white").topBorder)
|
|
477
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.White), plan("borderTopWidth" to 1, "borderColor" to "white").topBorder)
|
|
478
|
+
assertEquals(EdgeBorderPlan(3.dp, Color.Transparent), plan("borderTopWidth" to 3).topBorder)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
@Test fun borderTopIsSkippedWhenARealBorderWidthIsPresent() {
|
|
482
|
+
assertNull(plan("borderWidth" to 1, "borderTopWidth" to 4).topBorder)
|
|
483
|
+
assertNotNull(plan("borderWidth" to 1, "borderTopWidth" to 4).border)
|
|
484
|
+
assertNull(plan("borderTopWidth" to 0).topBorder)
|
|
485
|
+
assertNull(plan("borderWidth" to 0, "borderTopWidth" to 0).topBorder)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
@Test fun borderTopIsDrawnWhenBorderWidthIsZeroOrNegative() {
|
|
489
|
+
// no all-sides border is drawn for borderWidth <= 0, so the top edge is drawn on its own
|
|
490
|
+
val zero = plan("borderWidth" to 0, "borderTopWidth" to 1, "borderTopColor" to "black")
|
|
491
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Black), zero.topBorder); assertNull(zero.border)
|
|
492
|
+
assertEquals(EdgeBorderPlan(2.dp, Color.White), plan("borderWidth" to -3, "borderTopWidth" to 2, "borderColor" to "white").topBorder)
|
|
493
|
+
// absent borderWidth: unchanged
|
|
494
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Black), plan("borderTopWidth" to 1, "borderTopColor" to "black").topBorder)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
@Test fun bottomLeftRightBordersFollowTheTopBorderRules() {
|
|
498
|
+
val p = plan("borderBottomWidth" to 1, "borderBottomColor" to "black", "borderColor" to "white")
|
|
499
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Black), p.bottomBorder)
|
|
500
|
+
assertNull(p.topBorder); assertNull(p.leftBorder); assertNull(p.rightBorder); assertNull(p.border)
|
|
501
|
+
assertEquals(EdgeBorderPlan(2.dp, Color.White), plan("borderLeftWidth" to 2, "borderColor" to "white").leftBorder)
|
|
502
|
+
assertEquals(EdgeBorderPlan(3.dp, Color.Transparent), plan("borderRightWidth" to 3).rightBorder)
|
|
503
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Black), plan("borderRightWidth" to 1, "borderRightColor" to "black").rightBorder)
|
|
504
|
+
// a side colour without a width draws nothing; width 0 / negative neither
|
|
505
|
+
assertNull(plan("borderBottomColor" to "black").bottomBorder)
|
|
506
|
+
assertNull(plan("borderLeftWidth" to 0, "borderLeftColor" to "black").leftBorder)
|
|
507
|
+
assertNull(plan("borderRightWidth" to -2).rightBorder)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
@Test fun edgeBordersCombineEachWithItsOwnColour() {
|
|
511
|
+
val p = plan(
|
|
512
|
+
"borderTopWidth" to 1, "borderBottomWidth" to 2, "borderLeftWidth" to 3, "borderRightWidth" to 4,
|
|
513
|
+
"borderBottomColor" to "black", "borderColor" to "white",
|
|
514
|
+
)
|
|
515
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.White), p.topBorder)
|
|
516
|
+
assertEquals(EdgeBorderPlan(2.dp, Color.Black), p.bottomBorder)
|
|
517
|
+
assertEquals(EdgeBorderPlan(3.dp, Color.White), p.leftBorder)
|
|
518
|
+
assertEquals(EdgeBorderPlan(4.dp, Color.White), p.rightBorder)
|
|
519
|
+
assertNull(p.border)
|
|
520
|
+
assertNotNull(sty("borderTopWidth" to 1, "borderBottomWidth" to 2, "borderLeftWidth" to 3, "borderRightWidth" to 4).toModifier(false))
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
@Test fun anAllSidesBorderSuppressesEveryEdgeBorder() {
|
|
524
|
+
val p = plan("borderWidth" to 1, "borderTopWidth" to 2, "borderBottomWidth" to 2, "borderLeftWidth" to 2, "borderRightWidth" to 2)
|
|
525
|
+
assertEquals(BorderPlan(1.dp, Color.Transparent), p.border)
|
|
526
|
+
assertNull(p.topBorder); assertNull(p.bottomBorder); assertNull(p.leftBorder); assertNull(p.rightBorder)
|
|
527
|
+
// borderWidth <= 0 draws no outline, so the edges are drawn
|
|
528
|
+
val z = plan("borderWidth" to 0, "borderBottomWidth" to 1, "borderLeftWidth" to 1)
|
|
529
|
+
assertNull(z.border)
|
|
530
|
+
assertEquals(EdgeBorderPlan(1.dp, Color.Transparent), z.bottomBorder); assertEquals(EdgeBorderPlan(1.dp, Color.Transparent), z.leftBorder)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
@Test fun aTopOnlyBorderPlanIsUnchanged() {
|
|
534
|
+
// the new edges default to null: the top-only plan equals the plan built with only the old fields
|
|
535
|
+
val p = plan("borderTopWidth" to 1, "borderTopColor" to "black", "padding" to 4)
|
|
536
|
+
assertEquals(p.copy(bottomBorder = null, leftBorder = null, rightBorder = null), p)
|
|
537
|
+
assertNull(p.bottomBorder); assertNull(p.leftBorder); assertNull(p.rightBorder)
|
|
538
|
+
assertEquals(empty, plan("borderBottomColor" to "black").copy())
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// ---- background ----
|
|
542
|
+
|
|
543
|
+
@Test fun backgroundColorHexForms() {
|
|
544
|
+
assertEquals(Color(0xFFAABBCC), plan("backgroundColor" to "#abc").background)
|
|
545
|
+
assertEquals(Color(0xFF112233), plan("backgroundColor" to "#112233").background)
|
|
546
|
+
assertEquals(Color(0x80112233), plan("backgroundColor" to "#11223380").background)
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
@Test fun backgroundColorNamesTokensAndInvalid() {
|
|
550
|
+
assertEquals(Color.Transparent, plan("backgroundColor" to "transparent").background)
|
|
551
|
+
assertEquals(Colors.black_01, plan("backgroundColor" to resolveToken("Colors.black_01", defaultTheme)).background)
|
|
552
|
+
val themed = resolveToken("theme.colors.background.surface", defaultTheme)
|
|
553
|
+
assertEquals(themed, plan("backgroundColor" to themed).background)
|
|
554
|
+
assertNull(plan("backgroundColor" to "#12").background)
|
|
555
|
+
assertNull(plan("backgroundColor" to "red").background)
|
|
556
|
+
assertNull(plan("backgroundColor" to 123).background)
|
|
557
|
+
assertNull(plan("backgroundColor" to "Colors.unknown").background)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
@Test fun parseHexColorTable() {
|
|
561
|
+
assertEquals(Color(0xFF112233), parseHexColor("#123"))
|
|
562
|
+
assertEquals(Color(0xFF112233), parseHexColor("123")) // the leading # is optional
|
|
563
|
+
assertEquals(Color(0xDDAABBCC), parseHexColor("#abcd")) // #rgba: alpha last
|
|
564
|
+
assertEquals(Color(0xFFFFFFFF), parseHexColor("#FFFFFF"))
|
|
565
|
+
assertEquals(Color(0x00000000), parseHexColor("#00000000"))
|
|
566
|
+
assertEquals(Color(0xCC0A0B0C), parseHexColor("#0a0b0ccc"))
|
|
567
|
+
assertEquals(Color(0xFFAABBCC), parseHexColor("#AaBbCc"))
|
|
568
|
+
assertNull(parseHexColor("")); assertNull(parseHexColor("#")); assertNull(parseHexColor("#1"))
|
|
569
|
+
assertNull(parseHexColor("#12")); assertNull(parseHexColor("#12345")); assertNull(parseHexColor("#1234567"))
|
|
570
|
+
assertNull(parseHexColor("#123456789")); assertNull(parseHexColor("#ggg")); assertNull(parseHexColor("#12345z"))
|
|
571
|
+
assertNull(parseHexColor("#-12345")); assertNull(parseHexColor("# 12345"))
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
@Test fun parseColorKeywordsAndPassthrough() {
|
|
575
|
+
assertEquals(Color.Red, parseColor(Color.Red))
|
|
576
|
+
assertEquals(Color.White, parseColor("white")); assertEquals(Color.Black, parseColor("black"))
|
|
577
|
+
assertNull(parseColor("blue")); assertNull(parseColor(null)); assertNull(parseColor(5))
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// ---- whole-plan behaviour ----
|
|
581
|
+
|
|
582
|
+
@Test fun emptyStyleYieldsNeutralPlan() {
|
|
583
|
+
assertEquals(StylePlan(
|
|
584
|
+
0.dp, 0.dp, 0.dp, 0.dp, WidthMode.NONE, null, null, null, null, false, null, null, null, 0.dp, 0.dp, 0.dp, 0.dp,
|
|
585
|
+
), empty)
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
@Test fun combinedStyleIsDecidedPerField() {
|
|
589
|
+
val p = plan(
|
|
590
|
+
"margin" to 4, "width" to 100, "height" to 40, "borderRadius" to 8, "overflow" to "hidden",
|
|
591
|
+
"backgroundColor" to "#ffffff", "borderWidth" to 1, "borderColor" to "#000", "padding" to 12,
|
|
592
|
+
)
|
|
593
|
+
assertEquals(
|
|
594
|
+
StylePlan(
|
|
595
|
+
4.dp, 4.dp, 4.dp, 4.dp, WidthMode.FIXED, 100.dp, 40.dp, null,
|
|
596
|
+
RoundedCornerShape(8.dp, 8.dp, 8.dp, 8.dp), true, Color.White,
|
|
597
|
+
BorderPlan(1.dp, Color.Black), null, 12.dp, 12.dp, 12.dp, 12.dp,
|
|
598
|
+
),
|
|
599
|
+
p,
|
|
600
|
+
)
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
@Test fun toModifierBuildsWithoutThrowingOnHostileValues() {
|
|
604
|
+
val s = sty("width" to -1, "height" to -1, "padding" to -9, "aspectRatio" to -2, "borderWidth" to -3, "borderRadius" to "x")
|
|
605
|
+
assertNotNull(s.toModifier(stretch = true))
|
|
606
|
+
val n = sty(
|
|
607
|
+
"opacity" to Double.NaN, "zIndex" to Double.NaN, "minWidth" to 50, "maxWidth" to 10, "minHeight" to -1,
|
|
608
|
+
"maxHeight" to "x", "width" to "250%", "height" to "-3%", "paddingStart" to -2, "marginEnd" to -4,
|
|
609
|
+
)
|
|
610
|
+
assertNotNull(n.toModifier(stretch = false))
|
|
611
|
+
assertNotNull(sty("opacity" to 0.3, "zIndex" to 2, "width" to "40%", "height" to "60%", "maxWidth" to 100).toModifier(stretch = true))
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ---- percent sizes: base, min / max ----
|
|
615
|
+
|
|
616
|
+
/** One axis resolved like [specSize] does (density 1: px == dp); returns the (min, max) the content is measured with. */
|
|
617
|
+
private fun axis(a: AxisPlan, base: Int?, inMax: Int, inMin: Int = 0) = resolveAxisPx(a, base, inMin, inMax, 1f)
|
|
618
|
+
|
|
619
|
+
@Test fun percentWidthWithABaseGoesThroughTheAxisPlan() {
|
|
620
|
+
val p = plan("width" to "50%", hasBase = true)
|
|
621
|
+
assertEquals(AxisPlan(fraction = 0.5f), p.widthAxis)
|
|
622
|
+
assertNull(p.heightAxis)
|
|
623
|
+
// without a base (Column / Box parent) the plain fillMaxWidth(0.5) chain is kept
|
|
624
|
+
assertNull(plan("width" to "50%").widthAxis)
|
|
625
|
+
val h = plan("height" to "25%", hasBase = true)
|
|
626
|
+
assertEquals(AxisPlan(fraction = 0.25f), h.heightAxis); assertNull(h.widthAxis)
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
@Test fun noPercentOrHundredPercentNeverUsesTheAxisPlanEvenWithABase() {
|
|
630
|
+
listOf(
|
|
631
|
+
plan(hasBase = true), plan("width" to 120, "maxWidth" to 100, hasBase = true), plan("width" to "100%", hasBase = true),
|
|
632
|
+
plan(stretch = true, hasBase = true), plan("height" to "100%", "minHeight" to 4, hasBase = true),
|
|
633
|
+
plan("minWidth" to 10, "maxHeight" to 20, hasBase = true),
|
|
634
|
+
).forEach { assertNull(it.widthAxis); assertNull(it.heightAxis) }
|
|
635
|
+
// a base changes nothing for a style without percents: the very same plan
|
|
636
|
+
val s = arrayOf<Pair<String, Any?>>("width" to 40, "padding" to 4, "maxWidth" to 30, "height" to 8)
|
|
637
|
+
assertEquals(plan(*s), plan(*s, hasBase = true))
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
@Test fun twoHalfWidthSiblingsInARowGetEqualWidths() {
|
|
641
|
+
val half = plan("width" to "50%", hasBase = true).widthAxis!!
|
|
642
|
+
// a Row measures the 2nd child with what is left (600 - 300): with the row's width as base both are 300
|
|
643
|
+
assertEquals(300 to 300, axis(half, base = 600, inMax = 600))
|
|
644
|
+
assertEquals(300 to 300, axis(half, base = 600, inMax = 300))
|
|
645
|
+
// what plain fillMaxWidth(0.5) did there (the incoming max as base): 50% of what is left
|
|
646
|
+
assertEquals(150 to 150, axis(half, base = 300, inMax = 300))
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
@Test fun percentNeverOverflowsTheIncomingSpace() {
|
|
650
|
+
val p = plan("width" to "80%", hasBase = true).widthAxis!!
|
|
651
|
+
assertEquals(100 to 100, axis(p, base = 500, inMax = 100)) // like a fixed width: fitted into what the parent gives
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
@Test fun percentWidthIsClampedByMinMaxCssOrder() {
|
|
655
|
+
val p = plan("width" to "50%", "maxWidth" to 200)
|
|
656
|
+
assertEquals(AxisPlan(fraction = 0.5f, bounds = SizeBounds(null, 200.dp)), p.widthAxis) // bounds alone need the axis plan
|
|
657
|
+
assertEquals(200 to 200, axis(p.widthAxis!!, base = 600, inMax = 600))
|
|
658
|
+
assertEquals(150 to 150, axis(p.widthAxis!!, base = 300, inMax = 300))
|
|
659
|
+
val mn = plan("width" to "10%", "minWidth" to 80).widthAxis!!
|
|
660
|
+
assertEquals(80 to 80, axis(mn, base = 400, inMax = 400))
|
|
661
|
+
assertEquals(100 to 100, axis(mn, base = 1000, inMax = 1000))
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
@Test fun minAboveMaxMinWinsAlsoWithPercents() {
|
|
665
|
+
val a = plan("width" to "50%", "minWidth" to 300, "maxWidth" to 100).widthAxis!!
|
|
666
|
+
assertEquals(300 to 300, axis(a, base = 400, inMax = 400))
|
|
667
|
+
val b = plan("width" to 50, "minWidth" to "60%", "maxWidth" to "20%").widthAxis!!
|
|
668
|
+
assertEquals(60 to 60, axis(b, base = 100, inMax = 100))
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
@Test fun percentMinMaxAreParsedAndResolveAgainstTheBase() {
|
|
672
|
+
assertEquals(SizeBounds(null, null, maxFraction = 0.7f), plan("maxWidth" to "70%").widthBounds)
|
|
673
|
+
assertEquals(SizeBounds(4.dp, null, maxFraction = 0.5f), plan("minHeight" to 4, "maxHeight" to "50%").heightBounds)
|
|
674
|
+
// a Dp value wins over nothing; "0%" / "120%" are ignored like before
|
|
675
|
+
assertNull(plan("maxWidth" to "0%").widthBounds); assertNull(plan("maxWidth" to "120%").widthBounds)
|
|
676
|
+
// auto width + maxWidth "70%": measured with max 70% of the base (here the incoming max: no base given)
|
|
677
|
+
val auto = plan("maxWidth" to "70%").widthAxis!!
|
|
678
|
+
assertEquals(AxisPlan(bounds = SizeBounds(null, null, maxFraction = 0.7f)), auto)
|
|
679
|
+
assertEquals(0 to 280, axis(auto, base = 400, inMax = 400))
|
|
680
|
+
// fixed width + percent max: clamp(300, max 70% of 400) = 280
|
|
681
|
+
assertEquals(280 to 280, axis(plan("width" to 300, "maxWidth" to "70%").widthAxis!!, base = 400, inMax = 400))
|
|
682
|
+
// stretch (fill) + percent max
|
|
683
|
+
assertEquals(280 to 280, axis(plan("maxWidth" to "70%", stretch = true).widthAxis!!, base = 400, inMax = 400))
|
|
684
|
+
// percent min on an auto width
|
|
685
|
+
assertEquals(100 to 400, axis(plan("minWidth" to "25%").widthAxis!!, base = 400, inMax = 400))
|
|
686
|
+
// unknown base (unbounded): percents are dropped, the incoming constraints stay
|
|
687
|
+
assertEquals(0 to Int.MAX_VALUE, axis(auto, base = null, inMax = Int.MAX_VALUE))
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
@Test fun percentHeightBoundsUseTheHeightAxis() {
|
|
691
|
+
val p = plan("height" to "50%", "maxHeight" to 100)
|
|
692
|
+
assertEquals(AxisPlan(fraction = 0.5f, bounds = SizeBounds(null, 100.dp)), p.heightAxis)
|
|
693
|
+
assertEquals(100 to 100, axis(p.heightAxis!!, base = 800, inMax = 800))
|
|
694
|
+
assertNull(p.widthAxis)
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
@Test fun needsPercentBaseOnlyForPercentsThatNeedOne() {
|
|
698
|
+
assertTrue(sty("width" to "50%").needsPercentBase())
|
|
699
|
+
assertTrue(sty("height" to "30%").needsPercentBase())
|
|
700
|
+
assertTrue(sty("maxWidth" to "70%").needsPercentBase())
|
|
701
|
+
assertTrue(sty("minHeight" to "10%").needsPercentBase())
|
|
702
|
+
assertFalse(sty("width" to "100%").needsPercentBase())
|
|
703
|
+
assertFalse(sty("width" to 50, "maxWidth" to 80, "padding" to "10%").needsPercentBase())
|
|
704
|
+
assertFalse(sty().needsPercentBase())
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
@Test fun axisModifierReplacesThePlainSizeChainOnlyOnThatAxis() {
|
|
708
|
+
val b = PercentBase()
|
|
709
|
+
val m = sty("width" to "50%", "height" to 20, "minHeight" to 4).toModifier(stretch = false, base = b)
|
|
710
|
+
assertEquals(Modifier.specSize(AxisPlan(fraction = 0.5f), null, b).heightIn(4.dp, Dp.Unspecified).height(20.dp).padding(0.dp), m)
|
|
711
|
+
// the same style with no base: the old chain
|
|
712
|
+
assertEquals(Modifier.heightIn(4.dp, Dp.Unspecified).fillMaxWidth(0.5f).height(20.dp).padding(0.dp),
|
|
713
|
+
sty("width" to "50%", "height" to 20, "minHeight" to 4).toModifier(stretch = false))
|
|
714
|
+
// no percent: a base changes nothing in the chain
|
|
715
|
+
val plain = sty("width" to 15, "maxWidth" to 20, "backgroundColor" to "#fff", "padding" to 4)
|
|
716
|
+
assertEquals(plain.toModifier(stretch = true), plain.toModifier(stretch = true, base = b))
|
|
717
|
+
assertEquals(Modifier.fillMaxWidth().padding(0.dp), sty().toModifier(stretch = true, base = b))
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
@Test fun percentBaseModifierIsANoOpWithoutABase() {
|
|
721
|
+
assertSame(Modifier, Modifier.percentBase(null))
|
|
722
|
+
val b = PercentBase()
|
|
723
|
+
assertEquals(Modifier.percentBase(b), Modifier.percentBase(b))
|
|
724
|
+
assertNotSame(Modifier, Modifier.percentBase(b))
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// ---- percent sizes: intrinsics (a Row with alignItems stretch asks height(IntrinsicSize.Max)) ----
|
|
728
|
+
|
|
729
|
+
/** Content with no width of its own and an aspect ratio (width / height = [ratio]); records the widths it is asked at. */
|
|
730
|
+
private class AspectContent(val ratio: Float, val ownWidth: Int = 0) : IntrinsicMeasurable {
|
|
731
|
+
val askedWidths = mutableListOf<Int>()
|
|
732
|
+
override val parentData: Any? get() = null
|
|
733
|
+
override fun minIntrinsicWidth(height: Int) = ownWidth
|
|
734
|
+
override fun maxIntrinsicWidth(height: Int) = ownWidth
|
|
735
|
+
override fun minIntrinsicHeight(width: Int) = h(width)
|
|
736
|
+
override fun maxIntrinsicHeight(width: Int) = h(width)
|
|
737
|
+
private fun h(width: Int): Int {
|
|
738
|
+
askedWidths += width
|
|
739
|
+
return if (width == Constraints.Infinity) 0 else (width / ratio).roundToInt()
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
private val scope1 = object : IntrinsicMeasureScope {
|
|
744
|
+
override val density = 1f
|
|
745
|
+
override val fontScale = 1f
|
|
746
|
+
override val layoutDirection = LayoutDirection.Ltr
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
private fun SpecSizeModifier.maxW(m: IntrinsicMeasurable, height: Int = Constraints.Infinity) = with(this) { scope1.maxIntrinsicWidth(m, height) }
|
|
750
|
+
private fun SpecSizeModifier.minW(m: IntrinsicMeasurable, height: Int = Constraints.Infinity) = with(this) { scope1.minIntrinsicWidth(m, height) }
|
|
751
|
+
private fun SpecSizeModifier.maxH(m: IntrinsicMeasurable, width: Int) = with(this) { scope1.maxIntrinsicHeight(m, width) }
|
|
752
|
+
private fun SpecSizeModifier.minH(m: IntrinsicMeasurable, width: Int) = with(this) { scope1.minIntrinsicHeight(m, width) }
|
|
753
|
+
|
|
754
|
+
private fun baseOf(w: Int, h: Int = Constraints.Infinity) = PercentBase().also { it.width = w; it.height = h }
|
|
755
|
+
|
|
756
|
+
@Test fun intrinsicAxisReportsTheResolvedSizeOrDelegates() {
|
|
757
|
+
val inf = Constraints.Infinity
|
|
758
|
+
// fixed / resolvable percent: that size, the content is not asked
|
|
759
|
+
assertEquals(120, intrinsicAxisPx(AxisPlan(fixed = 120.dp), null, 1f) { error("not asked") })
|
|
760
|
+
assertEquals(300, intrinsicAxisPx(AxisPlan(fraction = 0.5f), 600, 1f) { error("not asked") })
|
|
761
|
+
assertEquals(240, intrinsicAxisPx(AxisPlan(fixed = 120.dp), null, 2f) { error("not asked") }) // px = dp * density
|
|
762
|
+
// clamped by min / max (min wins)
|
|
763
|
+
assertEquals(100, intrinsicAxisPx(AxisPlan(fixed = 120.dp, bounds = SizeBounds(null, null, maxFraction = 0.5f)), 200, 1f) { 0 })
|
|
764
|
+
assertEquals(120, intrinsicAxisPx(AxisPlan(fixed = 120.dp, bounds = SizeBounds(null, null, maxFraction = 0.5f)), null, 1f) { 0 })
|
|
765
|
+
assertEquals(80, intrinsicAxisPx(AxisPlan(fraction = 0.1f, bounds = SizeBounds(80.dp, 40.dp)), 400, 1f) { 0 })
|
|
766
|
+
// a percent with no base, auto, fill: the content's own intrinsic, clamped by what resolves
|
|
767
|
+
assertEquals(37, intrinsicAxisPx(AxisPlan(fraction = 0.5f), null, 1f) { 37 })
|
|
768
|
+
assertEquals(50, intrinsicAxisPx(AxisPlan(bounds = SizeBounds(null, 50.dp)), null, 1f) { 90 })
|
|
769
|
+
assertEquals(100, intrinsicAxisPx(AxisPlan(bounds = SizeBounds(null, null, minFraction = 0.25f)), 400, 1f) { 10 })
|
|
770
|
+
assertEquals(90, intrinsicAxisPx(AxisPlan(fill = true), 400, 1f) { 90 })
|
|
771
|
+
assertEquals(7, intrinsicAxisPx(null, 400, 1f) { 7 })
|
|
772
|
+
// cross size: the axis resolved against the base, else against the incoming size
|
|
773
|
+
assertEquals(300, intrinsicCrossPx(AxisPlan(fraction = 0.5f), 600, inf, 1f))
|
|
774
|
+
assertEquals(300, intrinsicCrossPx(AxisPlan(fraction = 0.5f), 600, 1000, 1f))
|
|
775
|
+
assertEquals(200, intrinsicCrossPx(AxisPlan(fraction = 0.5f), null, 400, 1f))
|
|
776
|
+
assertEquals(inf, intrinsicCrossPx(AxisPlan(fraction = 0.5f), null, inf, 1f))
|
|
777
|
+
assertEquals(120, intrinsicCrossPx(AxisPlan(fixed = 120.dp), null, inf, 1f))
|
|
778
|
+
assertEquals(50, intrinsicCrossPx(AxisPlan(bounds = SizeBounds(null, 50.dp)), null, inf, 1f))
|
|
779
|
+
assertEquals(inf, intrinsicCrossPx(null, 600, inf, 1f))
|
|
780
|
+
assertEquals(500, intrinsicCrossPx(null, 600, 500, 1f))
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
@Test fun halfWidthAspectChildrenKeepTheirHeightInAnIntrinsicRow() {
|
|
784
|
+
// two `width: "50%", aspectRatio: 16/9` children in a 640px row: each 320 wide, 180 high (not 0)
|
|
785
|
+
val mod = SpecSizeModifier(plan("width" to "50%", hasBase = true).widthAxis, null, baseOf(640))
|
|
786
|
+
val content = AspectContent(16f / 9f)
|
|
787
|
+
assertEquals(320, mod.maxW(content)); assertEquals(320, mod.minW(content))
|
|
788
|
+
assertEquals(180, mod.maxH(content, 320))
|
|
789
|
+
// whatever width the row offers, the content's height is asked at the resolved 320
|
|
790
|
+
assertEquals(180, mod.maxH(content, Constraints.Infinity)); assertEquals(180, mod.minH(content, 1000))
|
|
791
|
+
assertEquals(listOf(320, 320, 320), content.askedWidths)
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
@Test fun fixedWidthWithPercentMaxReportsTheClampedWidth() {
|
|
795
|
+
val axis = plan("width" to 120, "maxWidth" to "50%").widthAxis!!
|
|
796
|
+
val content = AspectContent(1f)
|
|
797
|
+
assertEquals(120, SpecSizeModifier(axis, null, null).maxW(content)) // no base: the percent max is dropped
|
|
798
|
+
assertEquals(100, SpecSizeModifier(axis, null, baseOf(200)).maxW(content))
|
|
799
|
+
assertEquals(100, SpecSizeModifier(axis, null, baseOf(200)).maxH(content, Constraints.Infinity))
|
|
800
|
+
assertEquals(listOf(100), content.askedWidths)
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
@Test fun percentHeightAxisReportsItsHeightAndAsksWidthAtTheHeight() {
|
|
804
|
+
val mod = SpecSizeModifier(null, plan("height" to "25%", hasBase = true).heightAxis, baseOf(Constraints.Infinity, 400))
|
|
805
|
+
val content = object : IntrinsicMeasurable {
|
|
806
|
+
val asked = mutableListOf<Int>()
|
|
807
|
+
override val parentData: Any? get() = null
|
|
808
|
+
override fun minIntrinsicWidth(height: Int) = height.also { asked += it }
|
|
809
|
+
override fun maxIntrinsicWidth(height: Int) = (height * 2).also { asked += height }
|
|
810
|
+
override fun minIntrinsicHeight(width: Int) = 0
|
|
811
|
+
override fun maxIntrinsicHeight(width: Int) = 0
|
|
812
|
+
}
|
|
813
|
+
assertEquals(100, mod.maxH(content, 500))
|
|
814
|
+
assertEquals(200, mod.maxW(content)); assertEquals(100, mod.minW(content, 1000))
|
|
815
|
+
assertEquals(listOf(100, 100), content.asked)
|
|
816
|
+
// no base and an unbounded query: the percent is unknown, the content's own intrinsics are reported
|
|
817
|
+
val free = SpecSizeModifier(null, AxisPlan(fraction = 0.25f), null)
|
|
818
|
+
assertEquals(0, free.maxH(content, 500))
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
// ---- margin arithmetic ----
|
|
822
|
+
|
|
823
|
+
@Test fun specMarginArithmetic() {
|
|
824
|
+
assertEquals(-10, SpecMargin.constraintDelta(4, 6))
|
|
825
|
+
assertEquals(14, SpecMargin.constraintDelta(-4, -10))
|
|
826
|
+
assertEquals(15, SpecMargin.outerSize(10, 2, 3))
|
|
827
|
+
assertEquals(0, SpecMargin.outerSize(10, -20, 0))
|
|
828
|
+
assertEquals(6, SpecMargin.outerSize(10, -4, 0))
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// ---- typography ----
|
|
832
|
+
|
|
833
|
+
@Test fun typographyOfKnownUnknownAndNull() {
|
|
834
|
+
assertEquals(Typography.headerMBold, typographyOf("header_m_bold"))
|
|
835
|
+
assertEquals(Typography.bodyDefaultRegular, typographyOf("does_not_exist"))
|
|
836
|
+
assertEquals(Typography.bodyDefaultRegular, typographyOf(null))
|
|
837
|
+
assertEquals(18, TYPOGRAPHY.size)
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
@Test fun specLineHeightScalesWithWidthAndClamps() {
|
|
841
|
+
assertEquals(26f, specLineHeight("header_m_bold", 375f)!!, 0.001f)
|
|
842
|
+
assertEquals(26f * 1.2f, specLineHeight("header_m_bold", 1000f)!!, 0.001f)
|
|
843
|
+
assertEquals(26f * 0.8f, specLineHeight("header_m_bold", 100f)!!, 0.001f)
|
|
844
|
+
assertEquals(20f, specLineHeight(null, 375f)!!, 0.001f) // null name = body_default_regular
|
|
845
|
+
assertEquals(20f * 450f / 375f, specLineHeight("body_default_regular", 450f)!!, 0.001f)
|
|
846
|
+
assertNull(specLineHeight("nope", 375f))
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
// AI-GENERATED END: unit tests for the pure style -> StylePlan decisions behind Sty.toModifier
|