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 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.test.captureToImage 26 import androidx.compose.ui.test.junit4.createComposeRule 27 import androidx.compose.ui.test.onNodeWithTag 28 import androidx.compose.ui.text.style.TextOverflow 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 org.junit.Rule 35 import org.junit.Test 36 import org.junit.rules.TestName 37 import org.junit.runner.RunWith 38 39 @MediumTest 40 @RunWith(AndroidJUnit4::class) 41 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) 42 class ToggleChipScreenshotTest { 43 44 @get:Rule val rule = createComposeRule() 45 46 @get:Rule val screenshotRule = AndroidXScreenshotTestRule(SCREENSHOT_GOLDEN_PATH) 47 48 @get:Rule val testName = TestName() 49 <lambda>null50 @Test fun toggle_chip_checkbox() = verifyScreenshot { sampleToggleChip(checked = true) } 51 52 @Test <lambda>null53 fun toggle_chip_checkbox_unchecked() = verifyScreenshot { sampleToggleChip(checked = false) } 54 55 @Test toggle_chip_checkbox_rtlnull56 fun toggle_chip_checkbox_rtl() = 57 verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleToggleChip() } 58 59 @Test <lambda>null60 fun toggle_chip_radio() = verifyScreenshot { 61 val checked = true 62 sampleToggleChip( 63 toggleControl = { 64 Icon(imageVector = ToggleChipDefaults.radioIcon(checked), contentDescription = "") 65 }, 66 checked = checked 67 ) 68 } 69 70 @Test <lambda>null71 fun toggle_chip_radio_unchecked() = verifyScreenshot { 72 val checked = false 73 sampleToggleChip( 74 toggleControl = { 75 Icon(imageVector = ToggleChipDefaults.radioIcon(checked), contentDescription = "") 76 }, 77 checked = checked, 78 ) 79 } 80 81 @Test <lambda>null82 fun toggle_chip_switch_checked() = verifyScreenshot { 83 val checked = true 84 sampleToggleChip( 85 toggleControl = { 86 Icon(imageVector = ToggleChipDefaults.switchIcon(checked), contentDescription = "") 87 }, 88 checked = checked, 89 ) 90 } 91 92 @Test <lambda>null93 fun toggle_chip_switch_unchecked() = verifyScreenshot { 94 val checked = false 95 sampleToggleChip( 96 colors = 97 ToggleChipDefaults.toggleChipColors( 98 uncheckedToggleControlColor = ToggleChipDefaults.SwitchUncheckedIconColor 99 ), 100 toggleControl = { 101 // For Switch toggle controls the Wear Material UX guidance is to set the 102 // unselected toggle control color to ToggleChipDefaults.switchUncheckedIconColor() 103 // rather than the default. 104 Icon(imageVector = ToggleChipDefaults.switchIcon(checked), contentDescription = "") 105 }, 106 checked = checked, 107 ) 108 } 109 110 @Test <lambda>null111 fun toggle_chip_disabled() = verifyScreenshot { 112 sampleToggleChip( 113 enabled = false, 114 ) 115 } 116 split_toggle_chipnull117 @Test fun split_toggle_chip() = verifyScreenshot { sampleSplitToggleChip() } 118 119 @Test split_toggle_chip_rtlnull120 fun split_toggle_chip_rtl() = 121 verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleSplitToggleChip() } 122 123 @Test <lambda>null124 fun split_toggle_chip_disabled() = verifyScreenshot { sampleSplitToggleChip(enabled = false) } 125 126 @Composable sampleToggleChipnull127 private fun sampleToggleChip( 128 colors: ToggleChipColors = ToggleChipDefaults.toggleChipColors(), 129 enabled: Boolean = true, 130 checked: Boolean = true, 131 toggleControl: @Composable () -> Unit = { 132 Icon( 133 imageVector = ToggleChipDefaults.checkboxIcon(checked = checked), 134 contentDescription = "" 135 ) 136 }, 137 ) { 138 ToggleChip( <lambda>null139 appIcon = { TestIcon() }, <lambda>null140 label = { 141 Text("Standard toggle chip", maxLines = 1, overflow = TextOverflow.Ellipsis) 142 }, <lambda>null143 secondaryLabel = { 144 Text("Secondary label", maxLines = 1, overflow = TextOverflow.Ellipsis) 145 }, 146 checked = checked, 147 enabled = enabled, 148 colors = colors, 149 toggleControl = toggleControl, <lambda>null150 onCheckedChange = {}, 151 modifier = Modifier.testTag(TEST_TAG), 152 ) 153 } 154 155 @Composable sampleSplitToggleChipnull156 private fun sampleSplitToggleChip( 157 enabled: Boolean = true, 158 ) { 159 SplitToggleChip( 160 label = { Text("Split chip", maxLines = 1, overflow = TextOverflow.Ellipsis) }, 161 secondaryLabel = { Text("Secondary", maxLines = 1, overflow = TextOverflow.Ellipsis) }, 162 checked = true, 163 enabled = enabled, 164 toggleControl = { 165 Icon( 166 imageVector = ToggleChipDefaults.checkboxIcon(checked = true), 167 contentDescription = "" 168 ) 169 }, 170 onCheckedChange = {}, 171 onClick = {}, 172 modifier = Modifier.testTag(TEST_TAG), 173 ) 174 } 175 verifyScreenshotnull176 private fun verifyScreenshot( 177 layoutDirection: LayoutDirection = LayoutDirection.Ltr, 178 content: @Composable () -> Unit 179 ) { 180 rule.setContentWithTheme { 181 CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) { content() } 182 } 183 184 rule 185 .onNodeWithTag(TEST_TAG) 186 .captureToImage() 187 .assertAgainstGolden(screenshotRule, testName.methodName) 188 } 189 } 190