[go: nahoru, domu]

blob: 023c898b93bedf1fe072fdf8fcbf9ea749fc9734 [file] [log] [blame]
Filip Pavlis21b8c202020-04-23 14:36:29 +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.onChildAt
22import androidx.ui.test.onChildren
Filip Pavlis21b8c202020-04-23 14:36:29 +010023import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010024import androidx.ui.test.onNodeWithTag
25import androidx.ui.test.onFirst
Filip Pavlis21b8c202020-04-23 14:36:29 +010026import androidx.ui.test.hasTestTag
27import androidx.ui.test.util.BoundaryNode
28import androidx.ui.test.util.expectErrorMessageStartsWith
29import org.junit.Rule
30import org.junit.Test
31import org.junit.runner.RunWith
32import org.junit.runners.JUnit4
33
34@MediumTest
35@RunWith(JUnit4::class)
36class 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 Pavlis659ea722020-07-13 14:14:32 +010050 onNodeWithTag("Parent")
51 .onChildren()
52 .onFirst()
Filip Pavlis21b8c202020-04-23 14:36:29 +010053 .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 Pavlis659ea722020-07-13 14:14:32 +010065 onNodeWithTag("Parent")
66 .onChildAt(1)
Filip Pavlis21b8c202020-04-23 14:36:29 +010067 .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 Pavlis659ea722020-07-13 14:14:32 +010083 onNodeWithTag("Parent")
84 .onChildAt(2)
Filip Pavlis21b8c202020-04-23 14:36:29 +010085 .assertExists()
86 }
87 }
88
89 @Test
90 fun getAtIndex_noItems() {
91 composeTestRule.setContent {
92 BoundaryNode(testTag = "Parent")
93 }
94
Filip Pavlis659ea722020-07-13 14:14:32 +010095 onNodeWithTag("Parent")
96 .onChildAt(2)
Filip Pavlis21b8c202020-04-23 14:36:29 +010097 .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 Pavlis659ea722020-07-13 14:14:32 +0100110 onNodeWithTag("Parent")
111 .onChildAt(2)
Filip Pavlis21b8c202020-04-23 14:36:29 +0100112 .assertExists()
113 }
114 }
115}