Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package androidx.ui.test |
| 18 | |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 19 | import androidx.test.espresso.matcher.ViewMatchers |
Jelle Fresen | 7556395 | 2020-05-06 19:06:18 +0100 | [diff] [blame] | 20 | import androidx.ui.core.AndroidOwner |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 21 | import androidx.ui.core.ExperimentalLayoutNodeApi |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 22 | import androidx.ui.core.LayoutNode |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 23 | import androidx.ui.core.findClosestParentNode |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 24 | import androidx.ui.core.semantics.SemanticsNode |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 25 | import androidx.ui.geometry.Offset |
| 26 | import androidx.ui.geometry.Rect |
yingleiw | 4bfcd92 | 2020-04-09 15:34:27 -0700 | [diff] [blame] | 27 | import androidx.ui.semantics.AccessibilityRangeInfo |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 28 | import androidx.ui.semantics.SemanticsProperties |
George Mount | 4640554 | 2020-03-12 23:15:24 -0700 | [diff] [blame] | 29 | import androidx.ui.unit.height |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 30 | import androidx.ui.unit.toRect |
George Mount | 4640554 | 2020-03-12 23:15:24 -0700 | [diff] [blame] | 31 | import androidx.ui.unit.width |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 34 | * Asserts that the current semantics node has hidden property set to true. |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 35 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 36 | * Note that this does not verify parents of the node. For stronger guarantees of visibility |
| 37 | * see [assertIsNotDisplayed]. If you want to assert that the node is not even in the hierarchy |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 38 | * use [SemanticsNodeInteraction.assertDoesNotExist]. |
| 39 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 40 | * Throws [AssertionError] if the node is not hidden. |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 41 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 42 | fun SemanticsNodeInteraction.assertIsHidden(): SemanticsNodeInteraction = assert(isHidden()) |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 45 | * Asserts that the current semantics node has hidden property set to false. |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 46 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 47 | * Note that this does not verify parents of the node. For stronger guarantees of visibility |
| 48 | * see [assertIsDisplayed]. If you only want to assert that the node is in the hierarchy use |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 49 | * [SemanticsNodeInteraction.assertExists] |
| 50 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 51 | * Throws [AssertionError] if the node is hidden. |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 52 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 53 | fun SemanticsNodeInteraction.assertIsNotHidden(): SemanticsNodeInteraction = assert(isNotHidden()) |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 56 | * Asserts that the current semantics node is displayed on screen. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 57 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 58 | * Throws [AssertionError] if the node is not displayed. |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 59 | */ |
| 60 | fun SemanticsNodeInteraction.assertIsDisplayed(): SemanticsNodeInteraction { |
| 61 | // TODO(b/143607231): check semantics hidden property |
| 62 | // TODO(b/143608742): check the correct AndroidCraneView is visible |
| 63 | |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 64 | if (!checkIsDisplayed()) { |
| 65 | // TODO(b/133217292) |
| 66 | throw AssertionError("Assert failed: The component is not displayed!") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 67 | } |
| 68 | return this |
| 69 | } |
| 70 | |
| 71 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 72 | * Asserts that the current semantics node is not displayed on screen. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 73 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 74 | * Throws [AssertionError] if the node is displayed. |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 75 | */ |
| 76 | fun SemanticsNodeInteraction.assertIsNotDisplayed(): SemanticsNodeInteraction { |
| 77 | // TODO(b/143607231): check semantics hidden property |
| 78 | // TODO(b/143608742): check no AndroidCraneView contains the given component |
| 79 | |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 80 | if (checkIsDisplayed()) { |
| 81 | // TODO(b/133217292) |
| 82 | throw AssertionError("Assert failed: The component is displayed!") |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 83 | } |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 84 | return this |
| 85 | } |
| 86 | |
| 87 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 88 | * Asserts that the current semantics node is enabled. |
Filip Pavlis | 5a3690b | 2020-04-24 15:00:43 +0100 | [diff] [blame] | 89 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 90 | * Throws [AssertionError] if the node is not enabled or does not define the property at all. |
Filip Pavlis | 5a3690b | 2020-04-24 15:00:43 +0100 | [diff] [blame] | 91 | */ |
| 92 | fun SemanticsNodeInteraction.assertIsEnabled(): SemanticsNodeInteraction = assert(isEnabled()) |
| 93 | |
| 94 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 95 | * Asserts that the current semantics node is not enabled. |
Filip Pavlis | 5a3690b | 2020-04-24 15:00:43 +0100 | [diff] [blame] | 96 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 97 | * Throws [AssertionError] if the node is enabled or does not defined the property at all. |
Filip Pavlis | 5a3690b | 2020-04-24 15:00:43 +0100 | [diff] [blame] | 98 | */ |
| 99 | fun SemanticsNodeInteraction.assertIsNotEnabled(): SemanticsNodeInteraction = assert(isNotEnabled()) |
| 100 | |
| 101 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 102 | * Asserts that the current semantics node is checked. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 103 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 104 | * Throws [AssertionError] if the node is not unchecked, indeterminate, or not toggleable. |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 105 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 106 | fun SemanticsNodeInteraction.assertIsOn(): SemanticsNodeInteraction = assert(isOn()) |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 107 | |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 108 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 109 | * Asserts that the current semantics node is unchecked. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 110 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 111 | * Throws [AssertionError] if the node is checked, indeterminate, or not toggleable. |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 112 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 113 | fun SemanticsNodeInteraction.assertIsOff(): SemanticsNodeInteraction = assert(isOff()) |
Cătălin Tudor | 316c2b0 | 2019-02-12 18:24:18 +0000 | [diff] [blame] | 114 | |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 115 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 116 | * Asserts that the current semantics node is selected. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 117 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 118 | * Throws [AssertionError] if the node is unselected or not selectable. |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 119 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 120 | fun SemanticsNodeInteraction.assertIsSelected(): SemanticsNodeInteraction = assert(isSelected()) |
Matvei Malkov | bf7f55a | 2019-03-18 16:19:33 +0000 | [diff] [blame] | 121 | |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 122 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 123 | * Asserts that the current semantics node is unselected. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 124 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 125 | * Throws [AssertionError] if the node is selected or not selectable. |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 126 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 127 | fun SemanticsNodeInteraction.assertIsUnselected(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 128 | assert(isUnselected()) |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 129 | |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 130 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 131 | * Asserts that the current semantics node is toggleable. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 132 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 133 | * Throws [AssertionError] if the node is not toggleable. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 134 | */ |
| 135 | fun SemanticsNodeInteraction.assertIsToggleable(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 136 | assert(isToggleable()) |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 139 | * Asserts that the current semantics node is selectable. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 140 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 141 | * Throws [AssertionError] if the node is not selectable. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 142 | */ |
| 143 | fun SemanticsNodeInteraction.assertIsSelectable(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 144 | assert(isSelectable()) |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 147 | * Asserts the semantics node is in a mutually exclusive group. This is used by radio groups to |
| 148 | * assert only one is selected at a given time. |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 149 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 150 | fun SemanticsNodeInteraction.assertIsInMutuallyExclusiveGroup(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 151 | assert(isInMutuallyExclusiveGroup()) |
Louis Pullen-Freilich | 7ecf512 | 2019-02-25 18:52:05 +0000 | [diff] [blame] | 152 | |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 153 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 154 | * Asserts the node's label equals the given String. |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 155 | * For further details please check [SemanticsProperties.AccessibilityLabel]. |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 156 | * Throws [AssertionError] if the node's value is not equal to `value`, or if the node has no value |
| 157 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 158 | fun SemanticsNodeInteraction.assertLabelEquals(value: String): SemanticsNodeInteraction = |
yingleiw | 8103f44 | 2020-05-21 11:46:39 -0700 | [diff] [blame] | 159 | assert(hasLabel(value)) |
| 160 | |
| 161 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 162 | * Asserts the node's text equals the given String. |
yingleiw | 8103f44 | 2020-05-21 11:46:39 -0700 | [diff] [blame] | 163 | * For further details please check [SemanticsProperties.Text]. |
| 164 | * Throws [AssertionError] if the node's value is not equal to `value`, or if the node has no value |
| 165 | */ |
| 166 | fun SemanticsNodeInteraction.assertTextEquals(value: String): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 167 | assert(hasText(value)) |
Ryan Mentley | 7865a63 | 2019-08-20 20:10:29 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 170 | * Asserts the node's value equals the given value. |
Filip Pavlis | 08b1993 | 2019-11-27 17:09:55 +0000 | [diff] [blame] | 171 | * |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 172 | * For further details please check [SemanticsProperties.AccessibilityValue]. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 173 | * Throws [AssertionError] if the node's value is not equal to `value`, or if the node has no value |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 174 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 175 | fun SemanticsNodeInteraction.assertValueEquals(value: String): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 176 | assert(hasValue(value)) |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 177 | |
Cătălin Tudor | 83cb550 | 2019-05-22 11:29:23 +0100 | [diff] [blame] | 178 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 179 | * Asserts the node's range info equals the given value. |
yingleiw | 4bfcd92 | 2020-04-09 15:34:27 -0700 | [diff] [blame] | 180 | * |
| 181 | * For further details please check [SemanticsProperties.AccessibilityRangeInfo]. |
| 182 | * Throws [AssertionError] if the node's value is not equal to `value`, or if the node has no value |
| 183 | */ |
| 184 | fun SemanticsNodeInteraction.assertRangeInfoEquals(value: AccessibilityRangeInfo): |
| 185 | SemanticsNodeInteraction = |
| 186 | assert(hasRangeInfo(value)) |
| 187 | |
| 188 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 189 | * Asserts that the current semantics node has a click action. |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 190 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 191 | * Throws [AssertionError] if the node is doesn't have a click action. |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 192 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 193 | fun SemanticsNodeInteraction.assertHasClickAction(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 194 | assert(hasClickAction()) |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 195 | |
| 196 | /** |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 197 | * Asserts that the current semantics node has doesn't have a click action. |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 198 | * |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 199 | * Throws [AssertionError] if the node has a click action. |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 200 | */ |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 201 | fun SemanticsNodeInteraction.assertHasNoClickAction(): SemanticsNodeInteraction = |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 202 | assert(hasNoClickAction()) |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 203 | |
| 204 | /** |
Filip Pavlis | fc3b32f | 2020-04-15 15:36:02 +0100 | [diff] [blame] | 205 | * Asserts that the provided [matcher] is satisfied for this node. |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 206 | * |
Filip Pavlis | fc3b32f | 2020-04-15 15:36:02 +0100 | [diff] [blame] | 207 | * @param matcher Matcher to verify. |
Filip Pavlis | 66f9c89 | 2020-05-14 12:00:08 +0100 | [diff] [blame] | 208 | * @param messagePrefixOnError Prefix to be put in front of an error that gets thrown in case this |
| 209 | * assert fails. This can be helpful in situations where this assert fails as part of a bigger |
| 210 | * operation that used this assert as a precondition check. |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 211 | * |
Filip Pavlis | fc3b32f | 2020-04-15 15:36:02 +0100 | [diff] [blame] | 212 | * @throws AssertionError if the matcher does not match or the node can no longer be found. |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 213 | */ |
Filip Pavlis | f46a1d81 | 2020-02-18 18:23:05 +0000 | [diff] [blame] | 214 | fun SemanticsNodeInteraction.assert( |
Filip Pavlis | 66f9c89 | 2020-05-14 12:00:08 +0100 | [diff] [blame] | 215 | matcher: SemanticsMatcher, |
| 216 | messagePrefixOnError: (() -> String)? = null |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 217 | ): SemanticsNodeInteraction { |
Filip Pavlis | 66f9c89 | 2020-05-14 12:00:08 +0100 | [diff] [blame] | 218 | var errorMessageOnFail = "Failed to assert the following: (${matcher.description})" |
| 219 | if (messagePrefixOnError != null) { |
| 220 | errorMessageOnFail = messagePrefixOnError() + "\n" + errorMessageOnFail |
| 221 | } |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 222 | val node = fetchSemanticsNode(errorMessageOnFail) |
Filip Pavlis | fc3b32f | 2020-04-15 15:36:02 +0100 | [diff] [blame] | 223 | if (!matcher.matches(node)) { |
Filip Pavlis | 66f9c89 | 2020-05-14 12:00:08 +0100 | [diff] [blame] | 224 | throw AssertionError(buildGeneralErrorMessage(errorMessageOnFail, selector, node)) |
Filip Pavlis | 6ee6aa6 | 2019-01-31 16:19:30 +0000 | [diff] [blame] | 225 | } |
Filip Pavlis | cd255fb | 2020-01-22 16:06:33 +0000 | [diff] [blame] | 226 | return this |
Ryan Mentley | 6cd5719 | 2019-05-29 21:03:18 -0700 | [diff] [blame] | 227 | } |
Cătălin Tudor | 8d91edd | 2019-08-05 15:14:09 +0100 | [diff] [blame] | 228 | |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 229 | /** |
| 230 | * Asserts that this collection of nodes is equal to the given [expectedSize]. |
| 231 | * |
| 232 | * Provides a detailed error message on failure. |
| 233 | * |
| 234 | * @throws AssertionError if the size is not equal to [expectedSize] |
| 235 | */ |
| 236 | fun SemanticsNodeInteractionCollection.assertCountEquals( |
| 237 | expectedSize: Int |
| 238 | ): SemanticsNodeInteractionCollection { |
| 239 | val errorOnFail = "Failed to assert count of nodes." |
| 240 | val matchedNodes = fetchSemanticsNodes(errorOnFail) |
| 241 | if (matchedNodes.size != expectedSize) { |
| 242 | throw AssertionError(buildErrorMessageForCountMismatch( |
| 243 | errorMessage = errorOnFail, |
| 244 | selector = selector, |
| 245 | foundNodes = matchedNodes, |
| 246 | expectedCount = expectedSize)) |
| 247 | } |
| 248 | return this |
| 249 | } |
| 250 | |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 251 | /** |
| 252 | * Asserts that this collection contains at least one element that satisfies the given [matcher]. |
| 253 | * |
| 254 | * @param matcher Matcher that has to be satisfied by at least one of the nodes in the collection. |
| 255 | * |
| 256 | * @throws AssertionError if not at least one matching node was node. |
| 257 | */ |
| 258 | fun SemanticsNodeInteractionCollection.assertAny( |
| 259 | matcher: SemanticsMatcher |
| 260 | ): SemanticsNodeInteractionCollection { |
| 261 | val errorOnFail = "Failed to assertAny(${matcher.description})" |
| 262 | val nodes = fetchSemanticsNodes(errorOnFail) |
| 263 | if (nodes.isEmpty()) { |
| 264 | throw AssertionError(buildErrorMessageForAtLeastOneNodeExpected(errorOnFail, selector)) |
| 265 | } |
| 266 | if (!matcher.matchesAny(nodes)) { |
| 267 | throw AssertionError(buildErrorMessageForAssertAnyFail(selector, nodes, matcher)) |
| 268 | } |
| 269 | return this |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Asserts that all the nodes in this collection satisfy the given [matcher]. |
| 274 | * |
| 275 | * This passes also for empty collections. |
| 276 | * |
| 277 | * @param matcher Matcher that has to be satisfied by all the nodes in the collection. |
| 278 | * |
| 279 | * @throws AssertionError if the collection contains at least one element that does not satisfy |
| 280 | * the given matcher. |
| 281 | */ |
| 282 | fun SemanticsNodeInteractionCollection.assertAll( |
| 283 | matcher: SemanticsMatcher |
| 284 | ): SemanticsNodeInteractionCollection { |
| 285 | val errorOnFail = "Failed to assertAll(${matcher.description})" |
| 286 | val nodes = fetchSemanticsNodes(errorOnFail) |
| 287 | |
| 288 | val violations = mutableListOf<SemanticsNode>() |
| 289 | nodes.forEach { |
| 290 | if (!matcher.matches(it)) { |
| 291 | violations.add(it) |
| 292 | } |
| 293 | } |
| 294 | if (violations.isNotEmpty()) { |
| 295 | throw AssertionError(buildErrorMessageForAssertAllFail(selector, violations, matcher)) |
| 296 | } |
| 297 | return this |
| 298 | } |
| 299 | |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 300 | @OptIn(ExperimentalLayoutNodeApi::class) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 301 | private fun SemanticsNodeInteraction.checkIsDisplayed(): Boolean { |
| 302 | // hierarchy check - check layout nodes are visible |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 303 | val errorMessageOnFail = "Failed to perform isDisplayed check." |
| 304 | val node = fetchSemanticsNode(errorMessageOnFail) |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 305 | |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 306 | fun isNotPlaced(node: LayoutNode): Boolean { |
| 307 | return !node.isPlaced |
Jelle Fresen | d4bca95 | 2020-05-11 15:51:02 +0100 | [diff] [blame] | 308 | } |
| 309 | |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 310 | val layoutNode = node.componentNode |
| 311 | if (isNotPlaced(layoutNode) || layoutNode.findClosestParentNode(::isNotPlaced) != null) { |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 312 | return false |
| 313 | } |
| 314 | |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 315 | (layoutNode.owner as? AndroidOwner)?.let { |
Jelle Fresen | 891799c | 2020-05-12 10:30:34 +0100 | [diff] [blame] | 316 | if (!ViewMatchers.isDisplayed().matches(it.view)) { |
| 317 | return false |
| 318 | } |
| 319 | } |
| 320 | |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 321 | // check node doesn't clip unintentionally (e.g. row too small for content) |
Filip Pavlis | 8b8a862 | 2020-02-13 16:32:04 +0000 | [diff] [blame] | 322 | val globalRect = node.globalBounds |
Jelle Fresen | 7556395 | 2020-05-06 19:06:18 +0100 | [diff] [blame] | 323 | if (!node.isInScreenBounds()) { |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 324 | return false |
| 325 | } |
| 326 | |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 327 | return (globalRect.width > 0f && globalRect.height > 0f) |
Cătălin Tudor | 37adfc7 | 2019-09-04 17:58:58 +0100 | [diff] [blame] | 328 | } |
| 329 | |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 330 | @OptIn(ExperimentalLayoutNodeApi::class) |
Jelle Fresen | f1efe67 | 2020-07-02 15:24:59 +0100 | [diff] [blame^] | 331 | private fun SemanticsNode.clippedNodeBoundsInWindow(): Rect { |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 332 | val composeView = (componentNode.owner as AndroidOwner).view |
| 333 | val rootLocationInWindow = intArrayOf(0, 0).let { |
| 334 | composeView.getLocationInWindow(it) |
| 335 | Offset(it[0].toFloat(), it[1].toFloat()) |
| 336 | } |
| 337 | return boundsInRoot.toRect().shift(rootLocationInWindow) |
| 338 | } |
| 339 | |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 340 | @OptIn(ExperimentalLayoutNodeApi::class) |
Jelle Fresen | 7556395 | 2020-05-06 19:06:18 +0100 | [diff] [blame] | 341 | private fun SemanticsNode.isInScreenBounds(): Boolean { |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 342 | val composeView = (componentNode.owner as AndroidOwner).view |
| 343 | |
| 344 | // Window relative bounds of our node |
Jelle Fresen | f1efe67 | 2020-07-02 15:24:59 +0100 | [diff] [blame^] | 345 | val nodeBoundsInWindow = clippedNodeBoundsInWindow() |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 346 | if (nodeBoundsInWindow.width == 0f || nodeBoundsInWindow.height == 0f) { |
Filip Pavlis | 5aa0764 | 2020-04-09 17:41:18 +0100 | [diff] [blame] | 347 | return false |
| 348 | } |
Filip Pavlis | 5aa0764 | 2020-04-09 17:41:18 +0100 | [diff] [blame] | 349 | |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 350 | // Window relative bounds of our compose root view that are visible on the screen |
| 351 | val globalRootRect = android.graphics.Rect() |
| 352 | if (!composeView.getGlobalVisibleRect(globalRootRect)) { |
| 353 | return false |
| 354 | } |
Jelle Fresen | 7556395 | 2020-05-06 19:06:18 +0100 | [diff] [blame] | 355 | |
Filip Pavlis | ed382ac | 2020-06-04 17:10:14 +0100 | [diff] [blame] | 356 | return nodeBoundsInWindow.top >= globalRootRect.top && |
| 357 | nodeBoundsInWindow.left >= globalRootRect.left && |
| 358 | nodeBoundsInWindow.right <= globalRootRect.right && |
| 359 | nodeBoundsInWindow.bottom <= globalRootRect.bottom |
Filip Pavlis | 5aa0764 | 2020-04-09 17:41:18 +0100 | [diff] [blame] | 360 | } |