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 |
| 20 | import androidx.ui.test.assert |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 21 | import androidx.ui.test.onChild |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 22 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 23 | import androidx.ui.test.onNodeWithTag |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 24 | import androidx.ui.test.hasTestTag |
| 25 | import androidx.ui.test.util.BoundaryNode |
| 26 | import androidx.ui.test.util.expectErrorMessageStartsWith |
| 27 | import org.junit.Rule |
| 28 | import org.junit.Test |
| 29 | import org.junit.runner.RunWith |
| 30 | import org.junit.runners.JUnit4 |
| 31 | |
| 32 | @MediumTest |
| 33 | @RunWith(JUnit4::class) |
| 34 | class ChildSelectorTest { |
| 35 | |
| 36 | @get:Rule |
| 37 | val composeTestRule = createComposeRule() |
| 38 | |
| 39 | @Test |
| 40 | fun oneChild() { |
| 41 | composeTestRule.setContent { |
| 42 | BoundaryNode(testTag = "Parent") { |
| 43 | BoundaryNode(testTag = "Child") |
| 44 | } |
| 45 | } |
| 46 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 47 | onNodeWithTag("Parent") |
| 48 | .onChild() |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 49 | .assert(hasTestTag("Child")) |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun noChild() { |
| 54 | composeTestRule.setContent { |
| 55 | BoundaryNode(testTag = "Parent") |
| 56 | } |
| 57 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 58 | onNodeWithTag("Parent") |
| 59 | .onChild() |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 60 | .assertDoesNotExist() |
| 61 | } |
| 62 | |
| 63 | @Test(expected = AssertionError::class) |
| 64 | fun noChild_fail() { |
| 65 | composeTestRule.setContent { |
| 66 | BoundaryNode(testTag = "Parent") |
| 67 | } |
| 68 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 69 | onNodeWithTag("Parent") |
| 70 | .onChild() |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 71 | .assertExists() |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | fun twoChildren_fail() { |
| 76 | composeTestRule.setContent { |
| 77 | BoundaryNode(testTag = "Parent") { |
| 78 | BoundaryNode(testTag = "Child1") |
| 79 | BoundaryNode(testTag = "Child2") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | expectErrorMessageStartsWith("" + |
| 84 | "Failed: assertExists.\n" + |
| 85 | "Reason: Expected exactly '1' node but found '2' nodes that satisfy: " + |
| 86 | "((TestTag = 'Parent').child)\n" + |
| 87 | "Nodes found:" |
| 88 | ) { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 89 | onNodeWithTag("Parent") |
| 90 | .onChild() |
Filip Pavlis | 4570f20 | 2020-04-17 18:40:22 +0100 | [diff] [blame] | 91 | .assertExists() |
| 92 | } |
| 93 | } |
| 94 | } |