Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.test |
| 18 | |
| 19 | import androidx.compose.Composable |
Alexandre Elias | 5c8252f | 2020-05-27 15:52:40 -0700 | [diff] [blame] | 20 | import androidx.ui.core.Modifier |
| 21 | import androidx.ui.core.semantics.semantics |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 22 | import androidx.compose.foundation.selection.ToggleableState |
| 23 | import androidx.compose.foundation.semantics.inMutuallyExclusiveGroup |
| 24 | import androidx.compose.foundation.semantics.selected |
| 25 | import androidx.compose.foundation.semantics.toggleableState |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 26 | import androidx.compose.foundation.layout.Column |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 27 | import androidx.ui.semantics.SemanticsPropertyReceiver |
| 28 | import androidx.ui.semantics.hidden |
| 29 | import androidx.ui.semantics.testTag |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 30 | import org.junit.Rule |
| 31 | import org.junit.Test |
| 32 | |
| 33 | class AssertsTest { |
| 34 | |
| 35 | @get:Rule |
| 36 | val composeTestRule = createComposeRule(disableTransitions = true) |
| 37 | |
| 38 | @Test |
| 39 | fun assertIsNotHidden_forVisibleElement_isOk() { |
| 40 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 41 | BoundaryNode { testTag = "test" } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 44 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 45 | .assertIsNotHidden() |
| 46 | } |
| 47 | |
| 48 | @Test(expected = AssertionError::class) |
| 49 | fun assertIsNotHidden_forHiddenElement_throwsError() { |
| 50 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 51 | BoundaryNode { testTag = "test"; hidden() } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 54 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 55 | .assertIsNotHidden() |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | fun assertIsHidden_forHiddenElement_isOk() { |
| 60 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 61 | BoundaryNode { testTag = "test"; hidden() } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 64 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 65 | .assertIsHidden() |
| 66 | } |
| 67 | |
| 68 | @Test(expected = AssertionError::class) |
| 69 | fun assertIsHidden_forNotHiddenElement_throwsError() { |
| 70 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 71 | BoundaryNode { testTag = "test" } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 74 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 75 | .assertIsHidden() |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | fun assertIsOn_forCheckedElement_isOk() { |
| 80 | composeTestRule.setContent { |
| 81 | BoundaryNode { testTag = "test"; toggleableState = ToggleableState.On } |
| 82 | } |
| 83 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 84 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 85 | .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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 94 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 95 | .assertIsOn() |
| 96 | } |
| 97 | |
| 98 | @Test(expected = AssertionError::class) |
| 99 | fun assertIsOn_forNotToggleableElement_throwsError() { |
| 100 | composeTestRule.setContent { |
| 101 | BoundaryNode { testTag = "test" } |
| 102 | } |
| 103 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 104 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 105 | .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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 114 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 115 | .assertIsOff() |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | fun assertIsOff_forUncheckedElement_isOk() { |
| 120 | composeTestRule.setContent { |
| 121 | BoundaryNode { testTag = "test"; toggleableState = ToggleableState.Off } |
| 122 | } |
| 123 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 124 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 125 | .assertIsOff() |
| 126 | } |
| 127 | |
| 128 | @Test(expected = AssertionError::class) |
| 129 | fun assertIsOff_forNotToggleableElement_throwsError() { |
| 130 | composeTestRule.setContent { |
| 131 | BoundaryNode { testTag = "test"; } |
| 132 | } |
| 133 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 134 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 135 | .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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 144 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 145 | .assertIsSelected() |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | fun assertIsSelected_forSelectedElement_isOk() { |
| 150 | composeTestRule.setContent { |
| 151 | BoundaryNode { testTag = "test"; selected = true } |
| 152 | } |
| 153 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 154 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 155 | .assertIsSelected() |
| 156 | } |
| 157 | |
| 158 | @Test(expected = AssertionError::class) |
| 159 | fun assertIsSelected_forNotSelectableElement_throwsError() { |
| 160 | composeTestRule.setContent { |
| 161 | BoundaryNode { testTag = "test"; } |
| 162 | } |
| 163 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 164 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 165 | .assertIsSelected() |
| 166 | } |
| 167 | |
| 168 | @Test(expected = AssertionError::class) |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 169 | fun assertIsNotSelected_forSelectedElement_throwsError() { |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 170 | composeTestRule.setContent { |
| 171 | BoundaryNode { testTag = "test"; selected = true } |
| 172 | } |
| 173 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 174 | onNodeWithTag("test") |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 175 | .assertIsNotSelected() |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Test |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 179 | fun assertIsNotSelected_forNotSelectedElement_isOk() { |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 180 | composeTestRule.setContent { |
| 181 | BoundaryNode { testTag = "test"; selected = false } |
| 182 | } |
| 183 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 184 | onNodeWithTag("test") |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 185 | .assertIsNotSelected() |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test(expected = AssertionError::class) |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 189 | fun assertIsNotSelected_forNotSelectableElement_throwsError() { |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 190 | composeTestRule.setContent { |
| 191 | BoundaryNode { testTag = "test"; } |
| 192 | } |
| 193 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 194 | onNodeWithTag("test") |
yingleiw | 56f5ea8 | 2020-07-17 12:05:11 -0700 | [diff] [blame] | 195 | .assertIsNotSelected() |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 196 | } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 197 | @Test(expected = AssertionError::class) |
| 198 | fun assertItemInExclusiveGroup_forItemNotInGroup_throwsError() { |
| 199 | composeTestRule.setContent { |
| 200 | BoundaryNode { testTag = "test"; inMutuallyExclusiveGroup = false } |
| 201 | } |
| 202 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 203 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 204 | .assertIsInMutuallyExclusiveGroup() |
| 205 | } |
| 206 | |
| 207 | @Test(expected = AssertionError::class) |
| 208 | fun assertItemInExclusiveGroup_forItemWithoutProperty_throwsError() { |
| 209 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 210 | BoundaryNode { testTag = "test" } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 213 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 214 | .assertIsInMutuallyExclusiveGroup() |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | fun assertItemInExclusiveGroup_forItemInGroup_isOk() { |
| 219 | composeTestRule.setContent { |
| 220 | BoundaryNode { testTag = "test"; inMutuallyExclusiveGroup = true } |
| 221 | } |
| 222 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 223 | onNodeWithTag("test") |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 224 | .assertIsInMutuallyExclusiveGroup() |
| 225 | } |
| 226 | |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 227 | @Composable |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 228 | fun BoundaryNode(props: (SemanticsPropertyReceiver.() -> Unit)) { |
Alexandre Elias | 5c8252f | 2020-05-27 15:52:40 -0700 | [diff] [blame] | 229 | Column(Modifier.semantics(properties = props)) {} |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 230 | } |
| 231 | } |