Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +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.onChildAt |
| 22 | import androidx.ui.test.onChildren |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 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 | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 26 | import androidx.ui.test.hasTestTag |
| 27 | import androidx.ui.test.util.BoundaryNode |
| 28 | import androidx.ui.test.util.expectErrorMessageStartsWith |
| 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 AddIndexSelectorTest { |
| 37 | |
| 38 | @get:Rule |
| 39 | val composeTestRule = createComposeRule() |
| 40 | |
| 41 | @Test |
| 42 | fun getFirst() { |
| 43 | composeTestRule.setContent { |
| 44 | BoundaryNode(testTag = "Parent") { |
| 45 | BoundaryNode(testTag = "Child1") |
| 46 | BoundaryNode(testTag = "Child2") |
| 47 | } |
| 48 | } |
| 49 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 50 | onNodeWithTag("Parent") |
| 51 | .onChildren() |
| 52 | .onFirst() |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 53 | .assert(hasTestTag("Child1")) |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun getAtIndex() { |
| 58 | composeTestRule.setContent { |
| 59 | BoundaryNode(testTag = "Parent") { |
| 60 | BoundaryNode(testTag = "Child1") |
| 61 | BoundaryNode(testTag = "Child2") |
| 62 | } |
| 63 | } |
| 64 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 65 | onNodeWithTag("Parent") |
| 66 | .onChildAt(1) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 67 | .assert(hasTestTag("Child2")) |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | fun getAtIndex_wrongIndex_fail() { |
| 72 | composeTestRule.setContent { |
| 73 | BoundaryNode(testTag = "Parent") { |
| 74 | BoundaryNode(testTag = "Child1") |
| 75 | BoundaryNode(testTag = "Child2") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | expectErrorMessageStartsWith("" + |
| 80 | "Failed: assertExists.\n" + |
| 81 | "Can't retrieve node at index '2' of '(TestTag = 'Parent').children'\n" + |
| 82 | "There are '2' nodes only:") { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 83 | onNodeWithTag("Parent") |
| 84 | .onChildAt(2) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 85 | .assertExists() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun getAtIndex_noItems() { |
| 91 | composeTestRule.setContent { |
| 92 | BoundaryNode(testTag = "Parent") |
| 93 | } |
| 94 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 95 | onNodeWithTag("Parent") |
| 96 | .onChildAt(2) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 97 | .assertDoesNotExist() |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | fun getAtIndex_noItems_fail() { |
| 102 | composeTestRule.setContent { |
| 103 | BoundaryNode(testTag = "Parent") |
| 104 | } |
| 105 | |
| 106 | expectErrorMessageStartsWith("" + |
| 107 | "Failed: assertExists.\n" + |
| 108 | "Can't retrieve node at index '2' of '(TestTag = 'Parent').children'\n" + |
| 109 | "There are no existing nodes for that selector.") { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 110 | onNodeWithTag("Parent") |
| 111 | .onChildAt(2) |
Filip Pavlis | 21b8c20 | 2020-04-23 14:36:29 +0100 | [diff] [blame] | 112 | .assertExists() |
| 113 | } |
| 114 | } |
| 115 | } |