[go: nahoru, domu]

blob: 7cb40aec964dbfb46b2d01f907db767bb1e21ecb [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.testTag
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010027import androidx.compose.foundation.Box
28import androidx.compose.foundation.selection.ToggleableState
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010029import androidx.compose.foundation.layout.wrapContentSize
Filip Pavlis95fb5142020-03-24 14:16:16 +000030import androidx.ui.test.captureToBitmap
Matvei Malkov80684d32020-06-23 13:40:38 +010031import androidx.ui.test.center
Filip Pavlis95fb5142020-03-24 14:16:16 +000032import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010033import androidx.ui.test.performClick
34import androidx.ui.test.performPartialGesture
35import androidx.ui.test.onNode
36import androidx.ui.test.onNodeWithTag
Filip Pavlis95fb5142020-03-24 14:16:16 +000037import androidx.ui.test.isToggleable
Filip Pavlis659ea722020-07-13 14:14:32 +010038import androidx.ui.test.down
Matvei Malkovc5839b32020-06-04 16:50:15 +010039import androidx.ui.test.waitForIdle
Filip Pavlis95fb5142020-03-24 14:16:16 +000040import org.junit.Rule
41import org.junit.Test
42import org.junit.runner.RunWith
43import org.junit.runners.JUnit4
44
Nader Jawadcd6cac12020-07-06 13:20:06 -070045@MediumTest
Filip Pavlis95fb5142020-03-24 14:16:16 +000046@RunWith(JUnit4::class)
47@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
48class CheckboxScreenshotTest {
49
50 @get:Rule
51 val composeTestRule = createComposeRule()
52
53 @get:Rule
54 val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL)
55
56 val wrap = Modifier.wrapContentSize(Alignment.TopStart)
57
Matvei Malkovc5839b32020-06-04 16:50:15 +010058 // TODO: this test tag as well as Boxes inside testa are temporarty, remove then b/157687898
59 // is fixed
60 private val wrapperTestTag = "checkboxWrapper"
61
Filip Pavlis95fb5142020-03-24 14:16:16 +000062 @Test
63 fun checkBoxTest_checked() {
64 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070065 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +010066 Checkbox(checked = true, onCheckedChange = { })
67 }
Filip Pavlis95fb5142020-03-24 14:16:16 +000068 }
Matvei Malkovc5839b32020-06-04 16:50:15 +010069 assertToggeableAgainstGolden("checkbox_checked")
Filip Pavlis95fb5142020-03-24 14:16:16 +000070 }
71
72 @Test
73 fun checkBoxTest_unchecked() {
74 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070075 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +010076 Checkbox(modifier = wrap, checked = false, onCheckedChange = { })
77 }
Filip Pavlis95fb5142020-03-24 14:16:16 +000078 }
Matvei Malkovc5839b32020-06-04 16:50:15 +010079 assertToggeableAgainstGolden("checkbox_unchecked")
80 }
81
82 @Test
Matvei Malkov80684d32020-06-23 13:40:38 +010083 fun checkBoxTest_pressed() {
84 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070085 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkov80684d32020-06-23 13:40:38 +010086 Checkbox(modifier = wrap, checked = false, onCheckedChange = { })
87 }
88 }
Filip Pavlis659ea722020-07-13 14:14:32 +010089 onNodeWithTag(wrapperTestTag).performPartialGesture {
90 down(center)
Matvei Malkov80684d32020-06-23 13:40:38 +010091 }
92 assertToggeableAgainstGolden("checkbox_pressed")
93 }
94
95 @Test
Matvei Malkovc5839b32020-06-04 16:50:15 +010096 fun checkBoxTest_indeterminate() {
97 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070098 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +010099 TriStateCheckbox(
100 state = ToggleableState.Indeterminate,
101 modifier = wrap,
102 onClick = {})
103 }
104 }
105 assertToggeableAgainstGolden("checkbox_indeterminate")
106 }
107
108 @Test
109 fun checkBoxTest_disabled_checked() {
110 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700111 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +0100112 Checkbox(modifier = wrap, checked = true, enabled = false, onCheckedChange = { })
113 }
114 }
115 assertToggeableAgainstGolden("checkbox_disabled_checked")
116 }
117
118 @Test
119 fun checkBoxTest_disabled_unchecked() {
120 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700121 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +0100122 Checkbox(modifier = wrap, checked = false, enabled = false, onCheckedChange = { })
123 }
124 }
125 assertToggeableAgainstGolden("checkbox_disabled_unchecked")
126 }
127
128 @Test
129 fun checkBoxTest_disabled_indeterminate() {
130 composeTestRule.setMaterialContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700131 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +0100132 TriStateCheckbox(
133 state = ToggleableState.Indeterminate,
134 enabled = false,
135 modifier = wrap,
136 onClick = {})
137 }
138 }
139 assertToggeableAgainstGolden("checkbox_disabled_indeterminate")
Filip Pavlis95fb5142020-03-24 14:16:16 +0000140 }
141
142 @Test
143 fun checkBoxTest_unchecked_animateToChecked() {
144 composeTestRule.setMaterialContent {
145 val isChecked = state { false }
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700146 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +0100147 Checkbox(
148 modifier = wrap,
149 checked = isChecked.value,
150 onCheckedChange = { isChecked.value = it }
151 )
152 }
Filip Pavlis95fb5142020-03-24 14:16:16 +0000153 }
154
155 composeTestRule.clockTestRule.pauseClock()
156
Filip Pavlis659ea722020-07-13 14:14:32 +0100157 onNode(isToggleable())
158 .performClick()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000159
Matvei Malkovc5839b32020-06-04 16:50:15 +0100160 waitForIdle()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000161
Matvei Malkovc5839b32020-06-04 16:50:15 +0100162 composeTestRule.clockTestRule.advanceClock(60)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000163
Matvei Malkovc5839b32020-06-04 16:50:15 +0100164 assertToggeableAgainstGolden("checkbox_animateToChecked")
Filip Pavlis95fb5142020-03-24 14:16:16 +0000165 }
166
167 @Test
168 fun checkBoxTest_checked_animateToUnchecked() {
169 composeTestRule.setMaterialContent {
170 val isChecked = state { true }
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700171 Box(wrap.testTag(wrapperTestTag)) {
Matvei Malkovc5839b32020-06-04 16:50:15 +0100172 Checkbox(
173 modifier = wrap,
174 checked = isChecked.value,
175 onCheckedChange = { isChecked.value = it }
176 )
177 }
Filip Pavlis95fb5142020-03-24 14:16:16 +0000178 }
179
180 composeTestRule.clockTestRule.pauseClock()
181
Filip Pavlis659ea722020-07-13 14:14:32 +0100182 onNode(isToggleable())
183 .performClick()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000184
Matvei Malkovc5839b32020-06-04 16:50:15 +0100185 waitForIdle()
Filip Pavlis95fb5142020-03-24 14:16:16 +0000186
Matvei Malkovc5839b32020-06-04 16:50:15 +0100187 composeTestRule.clockTestRule.advanceClock(60)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000188
Matvei Malkovc5839b32020-06-04 16:50:15 +0100189 assertToggeableAgainstGolden("checkbox_animateToUnchecked")
190 }
191
192 private fun assertToggeableAgainstGolden(goldenName: String) {
193 // TODO: replace with find(isToggeable()) after b/157687898 is fixed
Filip Pavlis659ea722020-07-13 14:14:32 +0100194 onNodeWithTag(wrapperTestTag)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000195 .captureToBitmap()
Matvei Malkovc5839b32020-06-04 16:50:15 +0100196 .assertAgainstGolden(screenshotRule, goldenName)
Filip Pavlis95fb5142020-03-24 14:16:16 +0000197 }
198}