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 |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 23 | import androidx.compose.testutils.assertAgainstGolden |
| 24 | import androidx.compose.ui.Modifier |
| 25 | import androidx.compose.ui.graphics.compositeOver |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 26 | import androidx.compose.ui.platform.testTag |
| 27 | import androidx.compose.ui.test.captureToImage |
| 28 | import androidx.compose.ui.test.junit4.createComposeRule |
| 29 | import androidx.compose.ui.test.onNodeWithTag |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 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.test.screenshot.matchers.MSSIMMatcher |
| 35 | import org.junit.Rule |
| 36 | import org.junit.Test |
| 37 | import org.junit.rules.TestName |
| 38 | import org.junit.runner.RunWith |
| 39 | |
| 40 | @MediumTest |
| 41 | @RunWith(AndroidJUnit4::class) |
| 42 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 43 | class SelectionControlsScreenshotTest { |
| 44 | @get:Rule |
| 45 | val rule = createComposeRule() |
| 46 | |
| 47 | @get:Rule |
| 48 | val screenshotRule = AndroidXScreenshotTestRule(SCREENSHOT_GOLDEN_PATH) |
| 49 | |
| 50 | @get:Rule |
| 51 | val testName = TestName() |
| 52 | |
| 53 | @Test |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 54 | fun radiobutton_checked_enabled() = |
| 55 | verifyScreenshot { |
stevebower | ee3d3fc | 2023-12-01 16:01:14 +0000 | [diff] [blame] | 56 | with(SelectionControlScope(isEnabled = true, isSelected = true)) { |
| 57 | Radio(modifier = testBackgroundModifier()) |
| 58 | } |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Test |
| 62 | fun radiobutton_unchecked_enabled() = |
| 63 | verifyScreenshot { |
stevebower | ee3d3fc | 2023-12-01 16:01:14 +0000 | [diff] [blame] | 64 | with(SelectionControlScope(isEnabled = true, isSelected = false)) { |
| 65 | Radio(modifier = testBackgroundModifier()) |
| 66 | } |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
| 70 | fun radiobutton_checked_disabled() = |
| 71 | verifyScreenshot { |
stevebower | ee3d3fc | 2023-12-01 16:01:14 +0000 | [diff] [blame] | 72 | with(SelectionControlScope(isEnabled = false, isSelected = true)) { |
| 73 | Radio(modifier = testBackgroundModifier()) |
| 74 | } |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun radiobutton_unchecked_disabled() = |
| 79 | verifyScreenshot { |
stevebower | ee3d3fc | 2023-12-01 16:01:14 +0000 | [diff] [blame] | 80 | with(SelectionControlScope(isEnabled = false, isSelected = false)) { |
| 81 | Radio(modifier = testBackgroundModifier()) |
| 82 | } |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private fun verifyScreenshot( |
| 86 | threshold: Double = 0.98, |
| 87 | content: @Composable BoxScope.() -> Unit |
| 88 | ) { |
| 89 | rule.setContentWithTheme(composable = content) |
| 90 | rule.onNodeWithTag(TEST_TAG) |
| 91 | .captureToImage() |
| 92 | .assertAgainstGolden(screenshotRule, testName.methodName, MSSIMMatcher(threshold)) |
| 93 | } |
| 94 | |
| 95 | @Composable |
| 96 | private fun testBackgroundModifier(): Modifier = |
| 97 | Modifier |
| 98 | .testTag(TEST_TAG) |
| 99 | .background( |
| 100 | MaterialTheme.colorScheme.primary |
| 101 | .copy(alpha = 0.5f) |
Artemiy Sobolev | 4522a9c | 2024-04-11 17:10:22 +0100 | [diff] [blame] | 102 | .compositeOver(MaterialTheme.colorScheme.surfaceContainer) |
hardik | b33d0db | 2023-07-10 10:45:05 +0530 | [diff] [blame] | 103 | ) |
| 104 | } |