[go: nahoru, domu]

blob: f10d79597c6833bf89af644944433c0f2f219804 [file] [log] [blame]
Filip Pavlis95fb5142020-03-24 14:16:16 +00001/*
2 * Copyright 2019 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 */
16package androidx.ui.material
17
18import android.os.Build
19import androidx.compose.state
Nader Jawadcd6cac12020-07-06 13:20:06 -070020import androidx.test.filters.MediumTest
Filip Pavlis95fb5142020-03-24 14:16:16 +000021import androidx.test.filters.SdkSuppress
22import androidx.test.screenshot.AndroidXScreenshotTestRule
23import androidx.test.screenshot.assertAgainstGolden
24import androidx.ui.core.Alignment
25import androidx.ui.core.Modifier
Matvei Malkovc5839b32020-06-04 16:50:15 +010026import androidx.ui.core.semantics.semantics
27import androidx.ui.core.testTag
28import androidx.ui.foundation.Box
29import androidx.ui.foundation.selection.ToggleableState
Filip Pavlis95fb5142020-03-24 14:16:16 +000030import androidx.ui.layout.wrapContentSize
31import androidx.ui.test.captureToBitmap
Matvei Malkov80684d32020-06-23 13:40:38 +010032import androidx.ui.test.center
Filip Pavlis95fb5142020-03-24 14:16:16 +000033import androidx.ui.test.createComposeRule
34import androidx.ui.test.doClick
Matvei Malkov80684d32020-06-23 13:40:38 +010035import androidx.ui.test.doPartialGesture
Filip Pavlis95fb5142020-03-24 14:16:16 +000036import androidx.ui.test.find
Matvei Malkovc5839b32020-06-04 16:50:15 +010037import androidx.ui.test.findByTag
Filip Pavlis95fb5142020-03-24 14:16:16 +000038import androidx.ui.test.isToggleable
Matvei Malkov80684d32020-06-23 13:40:38 +010039import androidx.ui.test.sendDown
Matvei Malkovc5839b32020-06-04 16:50:15 +010040import androidx.ui.test.waitForIdle
Filip Pavlis95fb5142020-03-24 14:16:16 +000041import org.junit.Rule
42import org.junit.Test
43import org.junit.runner.RunWith
44import org.junit.runners.JUnit4
45
Nader Jawadcd6cac12020-07-06 13:20:06 -070046@MediumTest
Filip Pavlis95fb5142020-03-24 14:16:16 +000047@RunWith(JUnit4::class)
48@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
49class CheckboxScreenshotTest {
50
51 @get:Rule
52 val composeTestRule = createComposeRule()
53
54 @get:Rule
55 val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL)
56
57 val wrap = Modifier.wrapContentSize(Alignment.TopStart)
58
Matvei Malkovc5839b32020-06-04 16:50:15 +010059 // TODO: this test tag as well as Boxes inside testa are temporarty, remove then b/157687898
60 // is fixed
61 private val wrapperTestTag = "checkboxWrapper"
62
Filip Pavlis95fb5142020-03-24 14:16:16 +000063 @Test
64 fun checkBoxTest_checked() {
65 composeTestRule.setMaterialContent {
Matvei Malkovc5839b32020-06-04 16:50:15 +010066 Box(wrap.semantics().testTag(wrapperTestTag)) {
67 Checkbox(checked = true, onCheckedChange = { })
68 }
Filip Pavlis95fb5142020-03-24 14:16:16 +000069 }
Matvei Malkovc5839b32020-06-04 16:50:15 +010070 assertToggeableAgainstGolden("checkbox_checked")
Filip Pavlis95fb5142020-03-24 14:16:16 +000071 }
72
73 @Test
74 fun checkBoxTest_unchecked() {
75 composeTestRule.setMaterialContent {
Matvei Malkovc5839b32020-06-04 16:50:15 +010076 Box(wrap.semantics().testTag(wrapperTestTag)) {
77 Checkbox(modifier = wrap, checked = false, onCheckedChange = { })
78 }
Filip Pavlis95fb5142020-03-24 14:16:16 +000079 }
Matvei Malkovc5839b32020-06-04 16:50:15 +010080 assertToggeableAgainstGolden("checkbox_unchecked")
81 }
82
83 @Test
Matvei Malkov80684d32020-06-23 13:40:38 +010084 fun checkBoxTest_pressed() {
85 composeTestRule.setMaterialContent {
86 Box(wrap.semantics().testTag(wrapperTestTag)) {
87 Checkbox(modifier = wrap, checked = false, onCheckedChange = { })
88 }
89 }
90 findByTag(wrapperTestTag).doPartialGesture {
91 sendDown(center)
92 }
93 assertToggeableAgainstGolden("checkbox_pressed")
94 }
95
96 @Test
Matvei Malkovc5839b32020-06-04 16:50:15 +010097 fun checkBoxTest_indeterminate() {
98 composeTestRule.setMaterialContent {
99 Box(wrap.semantics().testTag(wrapperTestTag)) {
100 TriStateCheckbox(
101 state = ToggleableState.Indeterminate,
102 modifier = wrap,
103 onClick = {})
104 }
105 }
106 assertToggeableAgainstGolden("checkbox_indeterminate")
107 }
108
109 @Test
110 fun checkBoxTest_disabled_checked() {
111 composeTestRule.setMaterialContent {
112 Box(wrap.semantics().testTag(wrapperTestTag)) {
113 Checkbox(modifier = wrap, checked = true, enabled = false, onCheckedChange = { })
114 }
115 }
116 assertToggeableAgainstGolden("checkbox_disabled_checked")
117 }
118
119 @Test
120 fun checkBoxTest_disabled_unchecked() {
121 composeTestRule.setMaterialContent {
122 Box(wrap.semantics().testTag(wrapperTestTag)) {
123 Checkbox(modifier = wrap, checked = false, enabled = false, onCheckedChange = { })
124 }
125 }
126 assertToggeableAgainstGolden("checkbox_disabled_unchecked")
127 }
128
129 @Test
130 fun checkBoxTest_disabled_indeterminate() {
131 composeTestRule.setMaterialContent {
132 Box(wrap.semantics().testTag(wrapperTestTag)) {
133 TriStateCheckbox(
134 state = ToggleableState.Indeterminate,
135 enabled = false,
136 modifier = wrap,
137 onClick = {})
138 }
139 }
140 assertToggeableAgainstGolden("checkbox_disabled_indeterminate")
Filip Pavlis95fb5142020-03-24 14:16:16 +0000141 }
142
143 @Test
144 fun checkBoxTest_unchecked_animateToChecked() {
145 composeTestRule.setMaterialContent {
146 val isChecked = state { false }
Matvei Malkovc5839b32020-06-04 16:50:15 +0100147 Box(wrap.semantics().testTag(wrapperTestTag)) {
148 Checkbox(
149 modifier = wrap,
150 checked = isChecked.value,
151 onCheckedChange = { isChecked.value = it }
152 )
153 }
Filip Pavlis95fb5142020-03-24 14:16:16 +0000154 }
155
156 composeTestRule.clockTestRule.pauseClock()
157
158 find(isToggleable())
159 .doClick()
160
Matvei Malkovc5839b32020-06-04 16:50:15 +0100161 waitForIdle()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000162
Matvei Malkovc5839b32020-06-04 16:50:15 +0100163 composeTestRule.clockTestRule.advanceClock(60)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000164
Matvei Malkovc5839b32020-06-04 16:50:15 +0100165 assertToggeableAgainstGolden("checkbox_animateToChecked")
Filip Pavlis95fb5142020-03-24 14:16:16 +0000166 }
167
168 @Test
169 fun checkBoxTest_checked_animateToUnchecked() {
170 composeTestRule.setMaterialContent {
171 val isChecked = state { true }
Matvei Malkovc5839b32020-06-04 16:50:15 +0100172 Box(wrap.semantics().testTag(wrapperTestTag)) {
173 Checkbox(
174 modifier = wrap,
175 checked = isChecked.value,
176 onCheckedChange = { isChecked.value = it }
177 )
178 }
Filip Pavlis95fb5142020-03-24 14:16:16 +0000179 }
180
181 composeTestRule.clockTestRule.pauseClock()
182
183 find(isToggleable())
184 .doClick()
185
Matvei Malkovc5839b32020-06-04 16:50:15 +0100186 waitForIdle()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000187
Matvei Malkovc5839b32020-06-04 16:50:15 +0100188 composeTestRule.clockTestRule.advanceClock(60)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000189
Matvei Malkovc5839b32020-06-04 16:50:15 +0100190 assertToggeableAgainstGolden("checkbox_animateToUnchecked")
191 }
192
193 private fun assertToggeableAgainstGolden(goldenName: String) {
194 // TODO: replace with find(isToggeable()) after b/157687898 is fixed
195 findByTag(wrapperTestTag)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000196 .captureToBitmap()
Matvei Malkovc5839b32020-06-04 16:50:15 +0100197 .assertAgainstGolden(screenshotRule, goldenName)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000198 }
199}