[go: nahoru, domu]

blob: 92edb12fef0b16b8937b08dc7e80e660fe893fe1 [file] [log] [blame]
Filip Pavlis4570f202020-04-17 18:40:22 +01001/*
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
17package androidx.ui.test.selectors
18
19import androidx.test.filters.MediumTest
20import androidx.ui.test.assert
Filip Pavlis659ea722020-07-13 14:14:32 +010021import androidx.ui.test.onChild
Filip Pavlis4570f202020-04-17 18:40:22 +010022import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010023import androidx.ui.test.onNodeWithTag
Filip Pavlis4570f202020-04-17 18:40:22 +010024import androidx.ui.test.hasTestTag
25import androidx.ui.test.util.BoundaryNode
26import androidx.ui.test.util.expectErrorMessageStartsWith
27import org.junit.Rule
28import org.junit.Test
29import org.junit.runner.RunWith
30import org.junit.runners.JUnit4
31
32@MediumTest
33@RunWith(JUnit4::class)
34class 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 Pavlis659ea722020-07-13 14:14:32 +010047 onNodeWithTag("Parent")
48 .onChild()
Filip Pavlis4570f202020-04-17 18:40:22 +010049 .assert(hasTestTag("Child"))
50 }
51
52 @Test
53 fun noChild() {
54 composeTestRule.setContent {
55 BoundaryNode(testTag = "Parent")
56 }
57
Filip Pavlis659ea722020-07-13 14:14:32 +010058 onNodeWithTag("Parent")
59 .onChild()
Filip Pavlis4570f202020-04-17 18:40:22 +010060 .assertDoesNotExist()
61 }
62
63 @Test(expected = AssertionError::class)
64 fun noChild_fail() {
65 composeTestRule.setContent {
66 BoundaryNode(testTag = "Parent")
67 }
68
Filip Pavlis659ea722020-07-13 14:14:32 +010069 onNodeWithTag("Parent")
70 .onChild()
Filip Pavlis4570f202020-04-17 18:40:22 +010071 .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 Pavlis659ea722020-07-13 14:14:32 +010089 onNodeWithTag("Parent")
90 .onChild()
Filip Pavlis4570f202020-04-17 18:40:22 +010091 .assertExists()
92 }
93 }
94}