hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2023 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 | |
| 17 | package androidx.wear.compose.material3 |
| 18 | |
| 19 | import android.os.Build |
| 20 | import androidx.compose.foundation.background |
| 21 | import androidx.compose.foundation.layout.BoxScope |
| 22 | import androidx.compose.runtime.Composable |
| 23 | import androidx.compose.runtime.CompositionLocalProvider |
| 24 | import androidx.compose.testutils.assertAgainstGolden |
| 25 | import androidx.compose.ui.Modifier |
| 26 | import androidx.compose.ui.graphics.compositeOver |
| 27 | import androidx.compose.ui.platform.LocalLayoutDirection |
| 28 | import androidx.compose.ui.platform.testTag |
| 29 | import androidx.compose.ui.test.captureToImage |
| 30 | import androidx.compose.ui.test.junit4.createComposeRule |
| 31 | import androidx.compose.ui.test.onNodeWithTag |
| 32 | import androidx.compose.ui.unit.LayoutDirection |
| 33 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 34 | import androidx.test.filters.MediumTest |
| 35 | import androidx.test.filters.SdkSuppress |
| 36 | import androidx.test.screenshot.AndroidXScreenshotTestRule |
| 37 | import androidx.test.screenshot.matchers.MSSIMMatcher |
| 38 | import org.junit.Rule |
| 39 | import org.junit.Test |
| 40 | import org.junit.rules.TestName |
| 41 | import org.junit.runner.RunWith |
| 42 | |
| 43 | @MediumTest |
| 44 | @RunWith(AndroidJUnit4::class) |
| 45 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 46 | class SelectionControlsScreenshotTest { |
| 47 | @get:Rule |
| 48 | val rule = createComposeRule() |
| 49 | |
| 50 | @get:Rule |
| 51 | val screenshotRule = AndroidXScreenshotTestRule(SCREENSHOT_GOLDEN_PATH) |
| 52 | |
| 53 | @get:Rule |
| 54 | val testName = TestName() |
| 55 | |
| 56 | @Test |
| 57 | fun checkbox_checked_enabled() = |
| 58 | verifyScreenshot { |
| 59 | Checkbox(checked = true, enabled = true, modifier = testBackgroundModifier()) |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | fun checkbox_unchecked_enabled() = |
| 64 | verifyScreenshot { |
| 65 | Checkbox(checked = false, enabled = true, modifier = testBackgroundModifier()) |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | fun checkbox_checked_disabled() = |
| 70 | verifyScreenshot { |
| 71 | Checkbox(checked = true, enabled = false, modifier = testBackgroundModifier()) |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | fun checkbox_unchecked_disabled() = |
| 76 | verifyScreenshot { |
| 77 | Checkbox(checked = false, enabled = false, modifier = testBackgroundModifier()) |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun switch_checked_enabled() = |
| 82 | verifyScreenshot { |
| 83 | Switch(checked = true, enabled = true, modifier = testBackgroundModifier()) |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | fun switch_unchecked_enabled() = |
| 88 | verifyScreenshot { |
| 89 | Switch(checked = false, enabled = true, modifier = testBackgroundModifier()) |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | fun switch_checked_disabled() = |
| 94 | verifyScreenshot { |
| 95 | Switch(checked = true, enabled = false, modifier = testBackgroundModifier()) |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | fun switch_unchecked_disabled() = |
| 100 | verifyScreenshot { |
| 101 | Switch(checked = false, enabled = false, modifier = testBackgroundModifier()) |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun switch_rtl_checked_enabled() = |
| 106 | verifyScreenshot { |
| 107 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 108 | Switch(checked = true, enabled = true, modifier = testBackgroundModifier()) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | fun switch_rtl_unchecked_enabled() = |
| 114 | verifyScreenshot { |
| 115 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 116 | Switch(checked = false, enabled = true, modifier = testBackgroundModifier()) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun switch_rtl_checked_disabled() = |
| 122 | verifyScreenshot { |
| 123 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 124 | Switch(checked = true, enabled = false, modifier = testBackgroundModifier()) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | fun switch_rtl_unchecked_disabled() = |
| 130 | verifyScreenshot { |
| 131 | CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { |
| 132 | Switch(checked = false, enabled = false, modifier = testBackgroundModifier()) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | fun radiobutton_checked_enabled() = |
| 138 | verifyScreenshot { |
| 139 | RadioButton(selected = true, enabled = true, modifier = testBackgroundModifier()) |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | fun radiobutton_unchecked_enabled() = |
| 144 | verifyScreenshot { |
| 145 | RadioButton(selected = false, enabled = true, modifier = testBackgroundModifier()) |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | fun radiobutton_checked_disabled() = |
| 150 | verifyScreenshot { |
| 151 | RadioButton(selected = true, enabled = false, modifier = testBackgroundModifier()) |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | fun radiobutton_unchecked_disabled() = |
| 156 | verifyScreenshot { |
| 157 | RadioButton(selected = false, enabled = false, modifier = testBackgroundModifier()) |
| 158 | } |
| 159 | |
| 160 | private fun verifyScreenshot( |
| 161 | threshold: Double = 0.98, |
| 162 | content: @Composable BoxScope.() -> Unit |
| 163 | ) { |
| 164 | rule.setContentWithTheme(composable = content) |
| 165 | rule.onNodeWithTag(TEST_TAG) |
| 166 | .captureToImage() |
| 167 | .assertAgainstGolden(screenshotRule, testName.methodName, MSSIMMatcher(threshold)) |
| 168 | } |
| 169 | |
| 170 | @Composable |
| 171 | private fun testBackgroundModifier(): Modifier = |
| 172 | Modifier |
| 173 | .testTag(TEST_TAG) |
| 174 | .background( |
| 175 | MaterialTheme.colorScheme.primary |
| 176 | .copy(alpha = 0.5f) |
| 177 | .compositeOver(MaterialTheme.colorScheme.surface) |
| 178 | ) |
| 179 | } |