1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.wear.compose.material.test
17 
18 import android.os.Build
19 import androidx.compose.runtime.Composable
20 import androidx.compose.runtime.CompositionLocalProvider
21 import androidx.compose.testutils.assertAgainstGolden
22 import androidx.compose.ui.Modifier
23 import androidx.compose.ui.platform.LocalLayoutDirection
24 import androidx.compose.ui.platform.testTag
25 import androidx.compose.ui.res.painterResource
26 import androidx.compose.ui.test.captureToImage
27 import androidx.compose.ui.test.junit4.createComposeRule
28 import androidx.compose.ui.test.onNodeWithTag
29 import androidx.compose.ui.unit.LayoutDirection
30 import androidx.test.ext.junit.runners.AndroidJUnit4
31 import androidx.test.filters.MediumTest
32 import androidx.test.filters.SdkSuppress
33 import androidx.test.screenshot.AndroidXScreenshotTestRule
34 import androidx.wear.compose.material.Chip
35 import androidx.wear.compose.material.ChipColors
36 import androidx.wear.compose.material.ChipDefaults
37 import androidx.wear.compose.material.CompactChip
38 import androidx.wear.compose.material.OutlinedChip
39 import androidx.wear.compose.material.SCREENSHOT_GOLDEN_PATH
40 import androidx.wear.compose.material.TEST_TAG
41 import androidx.wear.compose.material.TestIcon
42 import androidx.wear.compose.material.Text
43 import androidx.wear.compose.material.setContentWithTheme
44 import org.junit.Rule
45 import org.junit.Test
46 import org.junit.rules.TestName
47 import org.junit.runner.RunWith
48 
49 @MediumTest
50 @RunWith(AndroidJUnit4::class)
51 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
52 class ChipScreenshotTest {
53 
54     @get:Rule val rule = createComposeRule()
55 
56     @get:Rule val screenshotRule = AndroidXScreenshotTestRule(SCREENSHOT_GOLDEN_PATH)
57 
58     @get:Rule val testName = TestName()
59 
<lambda>null60     @Test fun chip_ltr() = verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleChip() }
61 
<lambda>null62     @Test fun chip_rtl() = verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleChip() }
63 
64     @Test
chip_secondary_ltrnull65     fun chip_secondary_ltr() =
66         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) {
67             sampleChip(colors = ChipDefaults.secondaryChipColors())
68         }
69 
70     @Test
chip_secondary_rtlnull71     fun chip_secondary_rtl() =
72         verifyScreenshot(layoutDirection = LayoutDirection.Rtl) {
73             sampleChip(colors = ChipDefaults.secondaryChipColors())
74         }
75 
76     @Test
chip_multiline_textnull77     fun chip_multiline_text() =
78         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) {
79             sampleChip(
80                 label = "Long label to show truncation which does not fit into 1 line",
81                 secondaryLabel =
82                     "Long secondary label that will not fit on one single lines and " +
83                         "flows onto another line"
84             )
85         }
86 
87     @Test
chip_outlined_ltrnull88     fun chip_outlined_ltr() =
89         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleOutlinedChip() }
90 
91     @Test
chip_outlined_rtlnull92     fun chip_outlined_rtl() =
93         verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleOutlinedChip() }
94 
95     @Test
chip_disablednull96     fun chip_disabled() =
97         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleChip(enabled = false) }
98 
99     @Test
chip_gradient_ltrnull100     fun chip_gradient_ltr() =
101         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) {
102             sampleChip(colors = ChipDefaults.gradientBackgroundChipColors())
103         }
104 
105     @Test
chip_gradient_rtlnull106     fun chip_gradient_rtl() =
107         verifyScreenshot(layoutDirection = LayoutDirection.Rtl) {
108             sampleChip(colors = ChipDefaults.gradientBackgroundChipColors())
109         }
110 
111     @Test
<lambda>null112     fun chip_image_background() = verifyScreenshot {
113         sampleChip(
114             colors =
115                 ChipDefaults.imageBackgroundChipColors(
116                     backgroundImagePainter = painterResource(id = R.drawable.backgroundimage1)
117                 )
118         )
119     }
120 
121     @Test
compact_chip_ltrnull122     fun compact_chip_ltr() =
123         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleCompactChip() }
124 
125     @Test
compact_chip_rtlnull126     fun compact_chip_rtl() =
127         verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleCompactChip() }
128 
129     @Test
compact_chip_disablednull130     fun compact_chip_disabled() =
131         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) {
132             sampleCompactChip(enabled = false)
133         }
134 
135     @Composable
sampleChipnull136     private fun sampleChip(
137         enabled: Boolean = true,
138         colors: ChipColors = ChipDefaults.primaryChipColors(),
139         label: String = "Standard chip",
140         secondaryLabel: String = "Secondary text",
141     ) {
142         Chip(
143             enabled = enabled,
144             colors = colors,
145             onClick = {},
146             label = { Text(label) },
147             secondaryLabel = { Text(secondaryLabel) },
148             icon = { TestIcon() },
149             modifier = Modifier.testTag(TEST_TAG),
150         )
151     }
152 
153     @Composable
sampleOutlinedChipnull154     private fun sampleOutlinedChip(
155         enabled: Boolean = true,
156         colors: ChipColors = ChipDefaults.outlinedChipColors()
157     ) {
158         OutlinedChip(
159             enabled = enabled,
160             colors = colors,
161             onClick = {},
162             label = { Text("Standard chip") },
163             secondaryLabel = { Text("Secondary text") },
164             icon = { TestIcon() },
165             modifier = Modifier.testTag(TEST_TAG),
166         )
167     }
168 
169     @Composable
sampleCompactChipnull170     private fun sampleCompactChip(enabled: Boolean = true) {
171         CompactChip(
172             enabled = enabled,
173             onClick = {},
174             label = { Text("Compact chip") },
175             icon = { TestIcon() },
176             modifier = Modifier.testTag(TEST_TAG),
177         )
178     }
179 
verifyScreenshotnull180     private fun verifyScreenshot(
181         layoutDirection: LayoutDirection = LayoutDirection.Ltr,
182         content: @Composable () -> Unit
183     ) {
184         rule.setContentWithTheme {
185             CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) { content() }
186         }
187 
188         rule
189             .onNodeWithTag(TEST_TAG)
190             .captureToImage()
191             .assertAgainstGolden(screenshotRule, testName.methodName)
192     }
193 }
194