[go: nahoru, domu]

blob: bb5a14a59e70cdc9a9dd23d95cdda8eb7ab2a3ca [file] [log] [blame]
hardikb33d0db2023-07-10 10:45:05 +05301/*
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
17package androidx.wear.compose.material3
18
19import android.os.Build
20import androidx.compose.foundation.background
21import androidx.compose.foundation.layout.BoxScope
22import androidx.compose.runtime.Composable
hardikb33d0db2023-07-10 10:45:05 +053023import androidx.compose.testutils.assertAgainstGolden
24import androidx.compose.ui.Modifier
25import androidx.compose.ui.graphics.compositeOver
hardikb33d0db2023-07-10 10:45:05 +053026import androidx.compose.ui.platform.testTag
27import androidx.compose.ui.test.captureToImage
28import androidx.compose.ui.test.junit4.createComposeRule
29import androidx.compose.ui.test.onNodeWithTag
hardikb33d0db2023-07-10 10:45:05 +053030import androidx.test.ext.junit.runners.AndroidJUnit4
31import androidx.test.filters.MediumTest
32import androidx.test.filters.SdkSuppress
33import androidx.test.screenshot.AndroidXScreenshotTestRule
34import androidx.test.screenshot.matchers.MSSIMMatcher
35import org.junit.Rule
36import org.junit.Test
37import org.junit.rules.TestName
38import org.junit.runner.RunWith
39
40@MediumTest
41@RunWith(AndroidJUnit4::class)
42@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
43class 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
hardikb33d0db2023-07-10 10:45:05 +053054 fun radiobutton_checked_enabled() =
55 verifyScreenshot {
steveboweree3d3fc2023-12-01 16:01:14 +000056 with(SelectionControlScope(isEnabled = true, isSelected = true)) {
57 Radio(modifier = testBackgroundModifier())
58 }
hardikb33d0db2023-07-10 10:45:05 +053059 }
60
61 @Test
62 fun radiobutton_unchecked_enabled() =
63 verifyScreenshot {
steveboweree3d3fc2023-12-01 16:01:14 +000064 with(SelectionControlScope(isEnabled = true, isSelected = false)) {
65 Radio(modifier = testBackgroundModifier())
66 }
hardikb33d0db2023-07-10 10:45:05 +053067 }
68
69 @Test
70 fun radiobutton_checked_disabled() =
71 verifyScreenshot {
steveboweree3d3fc2023-12-01 16:01:14 +000072 with(SelectionControlScope(isEnabled = false, isSelected = true)) {
73 Radio(modifier = testBackgroundModifier())
74 }
hardikb33d0db2023-07-10 10:45:05 +053075 }
76
77 @Test
78 fun radiobutton_unchecked_disabled() =
79 verifyScreenshot {
steveboweree3d3fc2023-12-01 16:01:14 +000080 with(SelectionControlScope(isEnabled = false, isSelected = false)) {
81 Radio(modifier = testBackgroundModifier())
82 }
hardikb33d0db2023-07-10 10:45:05 +053083 }
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 Sobolev4522a9c2024-04-11 17:10:22 +0100102 .compositeOver(MaterialTheme.colorScheme.surfaceContainer)
hardikb33d0db2023-07-10 10:45:05 +0530103 )
104}