Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +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.onChildren |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +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 | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 24 | import androidx.ui.test.hasTestTag |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 25 | import androidx.ui.test.onLast |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 26 | import androidx.ui.test.util.BoundaryNode |
| 27 | import androidx.ui.test.util.expectErrorMessageStartsWith |
| 28 | import org.junit.Rule |
| 29 | import org.junit.Test |
| 30 | import org.junit.runner.RunWith |
| 31 | import org.junit.runners.JUnit4 |
| 32 | |
| 33 | @MediumTest |
| 34 | @RunWith(JUnit4::class) |
| 35 | class LastNodeSelectorTest { |
| 36 | |
| 37 | @get:Rule |
| 38 | val composeTestRule = createComposeRule() |
| 39 | |
| 40 | @Test |
| 41 | fun twoNodes_getLast() { |
| 42 | composeTestRule.setContent { |
| 43 | BoundaryNode(testTag = "Parent") { |
| 44 | BoundaryNode(testTag = "Child1") |
| 45 | BoundaryNode(testTag = "Child2") |
| 46 | } |
| 47 | } |
| 48 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 49 | onNodeWithTag("Parent") |
| 50 | .onChildren() |
| 51 | .onLast() |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 52 | .assert(hasTestTag("Child2")) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun zeroNodes_getLast() { |
| 57 | composeTestRule.setContent { |
| 58 | BoundaryNode(testTag = "Parent") |
| 59 | } |
| 60 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 61 | onNodeWithTag("Parent") |
| 62 | .onChildren() |
| 63 | .onLast() |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 64 | .assertDoesNotExist() |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | fun zeroNodes_getLast_fail() { |
| 69 | composeTestRule.setContent { |
| 70 | BoundaryNode(testTag = "Parent") |
| 71 | } |
| 72 | |
| 73 | expectErrorMessageStartsWith("" + |
| 74 | "Failed: assertExists.\n" + |
| 75 | "Reason: Expected exactly '1' node but could not find any node that satisfies: " + |
| 76 | "(((TestTag = 'Parent').children).last)") { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 77 | onNodeWithTag("Parent") |
| 78 | .onChildren() |
| 79 | .onLast() |
Filip Pavlis | de9c6fe | 2020-05-04 14:19:02 +0100 | [diff] [blame] | 80 | .assertExists() |
| 81 | } |
| 82 | } |
| 83 | } |