[go: nahoru, domu]

blob: 6ccecc1cd6eaf6b8418868934103c25a14e5240a [file] [log] [blame]
Filip Pavlis8b8a8622020-02-13 16:32:04 +00001/*
2 * Copyright 2020 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.ui.test
18
19import androidx.compose.Composable
Alexandre Elias5c8252f2020-05-27 15:52:40 -070020import androidx.ui.core.Modifier
21import androidx.ui.core.semantics.semantics
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010022import androidx.compose.foundation.selection.ToggleableState
23import androidx.compose.foundation.semantics.inMutuallyExclusiveGroup
24import androidx.compose.foundation.semantics.selected
25import androidx.compose.foundation.semantics.toggleableState
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010026import androidx.compose.foundation.layout.Column
Filip Pavlis8b8a8622020-02-13 16:32:04 +000027import androidx.ui.semantics.SemanticsPropertyReceiver
28import androidx.ui.semantics.hidden
29import androidx.ui.semantics.testTag
Filip Pavlis8b8a8622020-02-13 16:32:04 +000030import org.junit.Rule
31import org.junit.Test
32
33class AssertsTest {
34
35 @get:Rule
36 val composeTestRule = createComposeRule(disableTransitions = true)
37
38 @Test
39 fun assertIsNotHidden_forVisibleElement_isOk() {
40 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070041 BoundaryNode { testTag = "test" }
Filip Pavlis8b8a8622020-02-13 16:32:04 +000042 }
43
Filip Pavlis659ea722020-07-13 14:14:32 +010044 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000045 .assertIsNotHidden()
46 }
47
48 @Test(expected = AssertionError::class)
49 fun assertIsNotHidden_forHiddenElement_throwsError() {
50 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070051 BoundaryNode { testTag = "test"; hidden() }
Filip Pavlis8b8a8622020-02-13 16:32:04 +000052 }
53
Filip Pavlis659ea722020-07-13 14:14:32 +010054 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000055 .assertIsNotHidden()
56 }
57
58 @Test
59 fun assertIsHidden_forHiddenElement_isOk() {
60 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070061 BoundaryNode { testTag = "test"; hidden() }
Filip Pavlis8b8a8622020-02-13 16:32:04 +000062 }
63
Filip Pavlis659ea722020-07-13 14:14:32 +010064 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000065 .assertIsHidden()
66 }
67
68 @Test(expected = AssertionError::class)
69 fun assertIsHidden_forNotHiddenElement_throwsError() {
70 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070071 BoundaryNode { testTag = "test" }
Filip Pavlis8b8a8622020-02-13 16:32:04 +000072 }
73
Filip Pavlis659ea722020-07-13 14:14:32 +010074 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000075 .assertIsHidden()
76 }
77
78 @Test
79 fun assertIsOn_forCheckedElement_isOk() {
80 composeTestRule.setContent {
81 BoundaryNode { testTag = "test"; toggleableState = ToggleableState.On }
82 }
83
Filip Pavlis659ea722020-07-13 14:14:32 +010084 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000085 .assertIsOn()
86 }
87
88 @Test(expected = AssertionError::class)
89 fun assertIsOn_forUncheckedElement_throwsError() {
90 composeTestRule.setContent {
91 BoundaryNode { testTag = "test"; toggleableState = ToggleableState.Off }
92 }
93
Filip Pavlis659ea722020-07-13 14:14:32 +010094 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +000095 .assertIsOn()
96 }
97
98 @Test(expected = AssertionError::class)
99 fun assertIsOn_forNotToggleableElement_throwsError() {
100 composeTestRule.setContent {
101 BoundaryNode { testTag = "test" }
102 }
103
Filip Pavlis659ea722020-07-13 14:14:32 +0100104 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000105 .assertIsOn()
106 }
107
108 @Test(expected = AssertionError::class)
109 fun assertIsOff_forCheckedElement_throwsError() {
110 composeTestRule.setContent {
111 BoundaryNode { testTag = "test"; toggleableState = ToggleableState.On }
112 }
113
Filip Pavlis659ea722020-07-13 14:14:32 +0100114 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000115 .assertIsOff()
116 }
117
118 @Test
119 fun assertIsOff_forUncheckedElement_isOk() {
120 composeTestRule.setContent {
121 BoundaryNode { testTag = "test"; toggleableState = ToggleableState.Off }
122 }
123
Filip Pavlis659ea722020-07-13 14:14:32 +0100124 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000125 .assertIsOff()
126 }
127
128 @Test(expected = AssertionError::class)
129 fun assertIsOff_forNotToggleableElement_throwsError() {
130 composeTestRule.setContent {
131 BoundaryNode { testTag = "test"; }
132 }
133
Filip Pavlis659ea722020-07-13 14:14:32 +0100134 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000135 .assertIsOff()
136 }
137
138 @Test(expected = AssertionError::class)
139 fun assertIsSelected_forNotSelectedElement_throwsError() {
140 composeTestRule.setContent {
141 BoundaryNode { testTag = "test"; selected = false }
142 }
143
Filip Pavlis659ea722020-07-13 14:14:32 +0100144 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000145 .assertIsSelected()
146 }
147
148 @Test
149 fun assertIsSelected_forSelectedElement_isOk() {
150 composeTestRule.setContent {
151 BoundaryNode { testTag = "test"; selected = true }
152 }
153
Filip Pavlis659ea722020-07-13 14:14:32 +0100154 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000155 .assertIsSelected()
156 }
157
158 @Test(expected = AssertionError::class)
159 fun assertIsSelected_forNotSelectableElement_throwsError() {
160 composeTestRule.setContent {
161 BoundaryNode { testTag = "test"; }
162 }
163
Filip Pavlis659ea722020-07-13 14:14:32 +0100164 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000165 .assertIsSelected()
166 }
167
168 @Test(expected = AssertionError::class)
yingleiw56f5ea82020-07-17 12:05:11 -0700169 fun assertIsNotSelected_forSelectedElement_throwsError() {
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000170 composeTestRule.setContent {
171 BoundaryNode { testTag = "test"; selected = true }
172 }
173
Filip Pavlis659ea722020-07-13 14:14:32 +0100174 onNodeWithTag("test")
yingleiw56f5ea82020-07-17 12:05:11 -0700175 .assertIsNotSelected()
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000176 }
177
178 @Test
yingleiw56f5ea82020-07-17 12:05:11 -0700179 fun assertIsNotSelected_forNotSelectedElement_isOk() {
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000180 composeTestRule.setContent {
181 BoundaryNode { testTag = "test"; selected = false }
182 }
183
Filip Pavlis659ea722020-07-13 14:14:32 +0100184 onNodeWithTag("test")
yingleiw56f5ea82020-07-17 12:05:11 -0700185 .assertIsNotSelected()
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000186 }
187
188 @Test(expected = AssertionError::class)
yingleiw56f5ea82020-07-17 12:05:11 -0700189 fun assertIsNotSelected_forNotSelectableElement_throwsError() {
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000190 composeTestRule.setContent {
191 BoundaryNode { testTag = "test"; }
192 }
193
Filip Pavlis659ea722020-07-13 14:14:32 +0100194 onNodeWithTag("test")
yingleiw56f5ea82020-07-17 12:05:11 -0700195 .assertIsNotSelected()
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000196 }
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000197 @Test(expected = AssertionError::class)
198 fun assertItemInExclusiveGroup_forItemNotInGroup_throwsError() {
199 composeTestRule.setContent {
200 BoundaryNode { testTag = "test"; inMutuallyExclusiveGroup = false }
201 }
202
Filip Pavlis659ea722020-07-13 14:14:32 +0100203 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000204 .assertIsInMutuallyExclusiveGroup()
205 }
206
207 @Test(expected = AssertionError::class)
208 fun assertItemInExclusiveGroup_forItemWithoutProperty_throwsError() {
209 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700210 BoundaryNode { testTag = "test" }
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000211 }
212
Filip Pavlis659ea722020-07-13 14:14:32 +0100213 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000214 .assertIsInMutuallyExclusiveGroup()
215 }
216
217 @Test
218 fun assertItemInExclusiveGroup_forItemInGroup_isOk() {
219 composeTestRule.setContent {
220 BoundaryNode { testTag = "test"; inMutuallyExclusiveGroup = true }
221 }
222
Filip Pavlis659ea722020-07-13 14:14:32 +0100223 onNodeWithTag("test")
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000224 .assertIsInMutuallyExclusiveGroup()
225 }
226
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000227 @Composable
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700228 fun BoundaryNode(props: (SemanticsPropertyReceiver.() -> Unit)) {
Alexandre Elias5c8252f2020-05-27 15:52:40 -0700229 Column(Modifier.semantics(properties = props)) {}
Filip Pavlis8b8a8622020-02-13 16:32:04 +0000230 }
231}