Skip to content

Commit

Permalink
Merge pull request #57 from mori-atsushi/padding-style
Browse files Browse the repository at this point in the history
Improve PaddingSetter
  • Loading branch information
mori-atsushi authored Nov 24, 2023
2 parents 9473a9c + 41c0935 commit 23a1e0a
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,28 @@ class ComponentPaddingTest {
.assertHeightIsEqualTo(50.dp)
}

@Test
fun testPadding_vertical() {
val style = DummyComponentStyle {
padding.vertical += 10.dp
}

composeTestRule.setContent {
DummyComponent(
modifier = Modifier.testTag("dummyComponent"),
style = style,
)
}

composeTestRule.onNodeWithTag("dummyComponent")
.assertWidthIsEqualTo(10.dp)
.assertHeightIsEqualTo(30.dp)
}

@Test
fun testPadding_horizontal() {
val style = DummyComponentStyle {
padding += padding(horizontal = 10.dp)
padding.horizontal += 10.dp
}

composeTestRule.setContent {
Expand All @@ -97,7 +115,7 @@ class ComponentPaddingTest {
@Test
fun testPadding_top() {
val style = DummyComponentStyle {
padding += padding(top = 10.dp)
padding.top += 10.dp
}

composeTestRule.setContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package com.moriatsushi.compose.stylesheet.component.padding

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.moriatsushi.compose.stylesheet.token.Token
import kotlin.test.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ComponentPaddingTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testAsPaddingValues() {
val componentPadding = ComponentPadding(
start = Token(10.dp),
top = Token(20.dp),
end = Token(30.dp),
bottom = Token(40.dp),
)
composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 10.dp,
top = 20.dp,
right = 30.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
assertEquals(
PaddingValues.Absolute(
left = 30.dp,
top = 20.dp,
right = 10.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}
}
}

@Test
fun testAsPaddingValues_absolute() {
val componentPadding = ComponentPadding(
left = Token(10.dp),
top = Token(20.dp),
right = Token(30.dp),
bottom = Token(40.dp),
)
composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 10.dp,
top = 20.dp,
right = 30.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
assertEquals(
PaddingValues.Absolute(
left = 10.dp,
top = 20.dp,
right = 30.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}
}
}

@Test
fun testAsPaddingValues_paddingValues() {
val paddingValues = PaddingValues(
start = 10.dp,
top = 20.dp,
end = 30.dp,
bottom = 40.dp,
)
val componentPadding = ComponentPadding(paddingValues)
composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 10.dp,
top = 20.dp,
right = 30.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
assertEquals(
PaddingValues.Absolute(
left = 30.dp,
top = 20.dp,
right = 10.dp,
bottom = 40.dp,
),
componentPadding.asPaddingValues(),
)
}
}
}

@Test
fun testAsPaddingValues_copied() {
val paddingValues = PaddingValues(
start = 10.dp,
top = 20.dp,
end = 30.dp,
bottom = 40.dp,
)
val componentPadding = ComponentPadding(paddingValues)
.copy(left = Token(50.dp))
.copy(bottom = Token(60.dp))
.copy(start = Token(70.dp))

composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 70.dp,
top = 20.dp,
right = 30.dp,
bottom = 60.dp,
),
componentPadding.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
assertEquals(
PaddingValues.Absolute(
left = 50.dp,
top = 20.dp,
right = 70.dp,
bottom = 60.dp,
),
componentPadding.asPaddingValues(),
)
}
}
}

@Test
fun testAsPaddingValues_merged() {
val paddingValues = PaddingValues(
start = 10.dp,
top = 20.dp,
end = 30.dp,
bottom = 40.dp,
)
val padding1 = ComponentPadding(
PaddingValues(
start = 10.dp,
top = 20.dp,
end = 30.dp,
bottom = 40.dp,
),
)
val padding2 = ComponentPadding(
start = Token(50.dp),
top = Token(60.dp),
)
val merged1 = padding1.merge(padding2)
val merged2 = padding2.merge(padding1)

composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 50.dp,
top = 60.dp,
right = 30.dp,
bottom = 40.dp,
),
merged1.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 10.dp,
top = 20.dp,
right = 30.dp,
bottom = 40.dp,
),
merged2.asPaddingValues(),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.moriatsushi.compose.stylesheet.token.value
@StyleSheetComponentApi
data class ComponentCommonStyle internal constructor(
internal val size: ComponentSize = ComponentSize.Default,
val padding: ComponentPadding? = null,
val padding: ComponentPadding = ComponentPadding.Default,
internal val background: Token<Color>? = null,
internal val shape: Token<Shape?>? = null,
internal val border: BorderStyle? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
* Example:
* ```
* padding += 10.dp
* padding += sizeToken // Token<Dp>
* padding += padding(horizontal = 10.dp, vertical = 20.dp)
* padding += padding(horizontal = horizontalToken, vertical = verticalToken)
* padding += dpToken
* padding += padding(start = 10.dp, top = 20.dp, end = 30.dp, bottom = 40.dp)
* padding += absolutePadding(left = 10.dp, top = 20.dp, right = 30.dp, bottom = 40.dp)
* padding += PaddingValues(10.dp)
* padding.top += 10.dp
* padding.top += dpToken
* padding.vertical += 10.dp
* padding.horizontal += 10.dp
* ```
*/
val padding: PaddingSetter = PaddingSetter()
Expand Down Expand Up @@ -248,7 +250,7 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
top: Token<Dp> = Token(0.dp),
right: Token<Dp> = Token(0.dp),
bottom: Token<Dp> = Token(0.dp),
): ComponentPadding = ComponentPadding.absolute(
): ComponentPadding = ComponentPadding(
left = left,
top = top,
right = right,
Expand All @@ -260,7 +262,7 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
top: Dp = 0.dp,
right: Dp = 0.dp,
bottom: Dp = 0.dp,
): ComponentPadding = ComponentPadding.absolute(
): ComponentPadding = ComponentPadding(
left = Token(left),
top = Token(top),
right = Token(right),
Expand Down
Loading

0 comments on commit 23a1e0a

Please sign in to comment.