Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [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.selectors |
| 18 | |
| 19 | import androidx.test.filters.MediumTest |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 20 | import androidx.ui.test.onAncestors |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 21 | import androidx.ui.test.assert |
| 22 | import androidx.ui.test.assertCountEquals |
| 23 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 24 | import androidx.ui.test.onNodeWithTag |
| 25 | import androidx.ui.test.onFirst |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 26 | import androidx.ui.test.hasTestTag |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 27 | import androidx.ui.test.onParent |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 28 | import androidx.ui.test.util.BoundaryNode |
| 29 | import org.junit.Rule |
| 30 | import org.junit.Test |
| 31 | import org.junit.runner.RunWith |
| 32 | import org.junit.runners.JUnit4 |
| 33 | |
| 34 | @MediumTest |
| 35 | @RunWith(JUnit4::class) |
| 36 | class AncestorsSelectorTest { |
| 37 | |
| 38 | @get:Rule |
| 39 | val composeTestRule = createComposeRule() |
| 40 | |
| 41 | @Test |
| 42 | fun threeAncestors() { |
| 43 | composeTestRule.setContent { |
| 44 | BoundaryNode(testTag = "NodeA") { |
| 45 | BoundaryNode(testTag = "NodeB") { |
| 46 | BoundaryNode(testTag = "NodeC") { |
| 47 | BoundaryNode(testTag = "NodeD") |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 53 | onNodeWithTag("NodeD") |
| 54 | .onAncestors() |
Matvei Malkov | 1da47fe | 2020-05-15 12:40:16 +0100 | [diff] [blame] | 55 | .assertCountEquals(4) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 56 | .apply { |
| 57 | get(0).assert(hasTestTag("NodeC")) |
| 58 | get(1).assert(hasTestTag("NodeB")) |
| 59 | get(2).assert(hasTestTag("NodeA")) |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | fun threeAncestors_navigateUp() { |
| 65 | composeTestRule.setContent { |
| 66 | BoundaryNode(testTag = "NodeA") { |
| 67 | BoundaryNode(testTag = "NodeB") { |
| 68 | BoundaryNode(testTag = "NodeC") { |
| 69 | BoundaryNode(testTag = "NodeD") |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 75 | onNodeWithTag("NodeD") |
| 76 | .onAncestors() |
| 77 | .onFirst() |
| 78 | .onAncestors() |
Matvei Malkov | 1da47fe | 2020-05-15 12:40:16 +0100 | [diff] [blame] | 79 | .assertCountEquals(3) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 80 | .apply { |
| 81 | get(0).assert(hasTestTag("NodeB")) |
| 82 | get(1).assert(hasTestTag("NodeA")) |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | fun noAncestors() { |
| 88 | composeTestRule.setContent { |
| 89 | BoundaryNode(testTag = "Node") |
| 90 | } |
| 91 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 92 | onNodeWithTag("Node") |
| 93 | .onParent() |
| 94 | .onAncestors() |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 95 | .assertCountEquals(0) |
| 96 | } |
| 97 | } |