[go: nahoru, domu]

blob: eed138fdcd420b8a8db367566aefabfe29d62cfc [file] [log] [blame]
Filip Pavlis86dc4df2020-07-07 20:08:33 +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
18
19import androidx.compose.Composable
20import androidx.test.filters.MediumTest
21import androidx.ui.core.Modifier
22import androidx.ui.core.semantics.semantics
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010023import androidx.compose.foundation.Box
24import androidx.compose.foundation.Text
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010025import androidx.compose.foundation.layout.Column
Filip Pavlis86dc4df2020-07-07 20:08:33 +010026import androidx.ui.material.Button
27import androidx.ui.material.MaterialTheme
Alexandre Eliasc60f33e2020-07-10 16:23:09 -070028import androidx.ui.semantics.disabled
Filip Pavlis86dc4df2020-07-07 20:08:33 +010029import androidx.ui.semantics.testTag
30import androidx.ui.test.util.BoundaryNode
31import androidx.ui.test.util.expectErrorMessageStartsWith
32import androidx.ui.test.util.obfuscateNodesInfo
33import com.google.common.truth.Truth.assertThat
34import org.junit.Rule
35import org.junit.Test
36import org.junit.runner.RunWith
37import org.junit.runners.JUnit4
38
39@MediumTest
40@RunWith(JUnit4::class)
41class PrintToStringTest {
42
43 @get:Rule
44 val composeTestRule = createComposeRule(disableTransitions = true)
45
46 @Test
47 fun printToString_nothingFound() {
48 composeTestRule.setContent {
49 ComposeSimpleCase()
50 }
51
52 expectErrorMessageStartsWith(
53 "Failed: assertExists.\n" +
54 "Reason: Expected exactly '1' node but could not find any node that satisfies:"
55 ) {
Filip Pavlis659ea722020-07-13 14:14:32 +010056 onNodeWithText("Oops").printToString()
Filip Pavlis86dc4df2020-07-07 20:08:33 +010057 }
58 }
59
60 @Test
61 fun printToString_one() {
62 composeTestRule.setContent {
63 ComposeSimpleCase()
64 }
65
Filip Pavlis659ea722020-07-13 14:14:32 +010066 val result = onNodeWithText("Hello")
Filip Pavlis86dc4df2020-07-07 20:08:33 +010067 .printToString(maxDepth = 0)
68
69 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
70 "Node #X at (X, X, X, X)px\n" +
71 "Text = 'Hello'\n" +
yingleiwa6d82982020-07-08 16:38:05 -070072 "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" +
Louis Pullen-Freilichab194752020-07-21 22:21:22 +010073 ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +010074 "Has 1 sibling")
75 }
76
77 @Test
78 fun printToString_many() {
79 composeTestRule.setContent {
80 ComposeSimpleCase()
81 }
82
Filip Pavlis659ea722020-07-13 14:14:32 +010083 val result = onRoot()
84 .onChildren()
Filip Pavlis86dc4df2020-07-07 20:08:33 +010085 .printToString()
86
87 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
88 "1) Node #X at (X, X, X, X)px\n" +
89 "Text = 'Hello'\n" +
yingleiwa6d82982020-07-08 16:38:05 -070090 "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" +
Louis Pullen-Freilichab194752020-07-21 22:21:22 +010091 ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +010092 "Has 1 sibling\n" +
93 "2) Node #X at (X, X, X, X)px\n" +
94 "Text = 'World'\n" +
yingleiwa6d82982020-07-08 16:38:05 -070095 "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" +
Louis Pullen-Freilichab194752020-07-21 22:21:22 +010096 ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +010097 "Has 1 sibling")
98 }
99
100 @Test
101 fun printHierarchy() {
102 composeTestRule.setContent {
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700103 Column(Modifier.semantics { this.disabled(); this.testTag = "column" }) {
104 Box(Modifier.semantics { this.disabled(); this.testTag = "box" }) {
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100105 Button(onClick = {}) {
106 Text("Button")
107 }
108 }
109 Text("Hello")
110 }
111 }
112
Filip Pavlis659ea722020-07-13 14:14:32 +0100113 val result = onRoot()
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100114 .printToString()
115
116 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
117 "Node #X at (X, X, X, X)px\n" +
118 " |-Node #X at (X, X, X, X)px, Tag: 'column'\n" +
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700119 " Disabled = 'kotlin.Unit'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100120 " |-Node #X at (X, X, X, X)px, Tag: 'box'\n" +
Alexandre Eliasc60f33e2020-07-10 16:23:09 -0700121 " | Disabled = 'kotlin.Unit'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100122 " | |-Node #X at (X, X, X, X)px\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100123 " | action=Function0" +
124 "<java.lang.Boolean>)'\n" +
125 " | Text = 'Button'\n" +
yingleiwa6d82982020-07-08 16:38:05 -0700126 " | GetTextLayoutResult = 'AccessibilityAction(label=null, " +
Louis Pullen-Freilichab194752020-07-21 22:21:22 +0100127 "action=Function1<java.util.List<androidx.compose.ui.text.TextLayoutResult>," +
128 " java.lang" +
yingleiwa6d82982020-07-08 16:38:05 -0700129 ".Boolean>)'\n" +
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100130 " | MergeDescendants = 'true'\n" +
131 " |-Node #X at (X, X, X, X)px\n" +
yingleiwa6d82982020-07-08 16:38:05 -0700132 " Text = 'Hello'\n" +
133 " GetTextLayoutResult = 'AccessibilityAction(label=null, " +
Louis Pullen-Freilichab194752020-07-21 22:21:22 +0100134 "action=Function1<java.util.List<androidx.compose.ui.text.TextLayoutResult>," +
135 " java.lang" +
yingleiwa6d82982020-07-08 16:38:05 -0700136 ".Boolean>)'")
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100137 }
138
139 @Test
140 fun printMultiple_withDepth() {
141 composeTestRule.setContent {
142 BoundaryNode("tag1") {
143 BoundaryNode("tag11") {
144 BoundaryNode("tag111")
145 }
146 }
147 BoundaryNode("tag2") {
148 BoundaryNode("tag22") {
149 BoundaryNode("tag222")
150 }
151 }
152 }
153
Filip Pavlis659ea722020-07-13 14:14:32 +0100154 val result = onRoot()
155 .onChildren()
Filip Pavlis86dc4df2020-07-07 20:08:33 +0100156 .printToString(maxDepth = 1)
157
158 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
159 "1) Node #X at (X, X, X, X)px, Tag: 'tag1'\n" +
160 " |-Node #X at (X, X, X, X)px, Tag: 'tag11'\n" +
161 " Has 1 child\n" +
162 "2) Node #X at (X, X, X, X)px, Tag: 'tag2'\n" +
163 " |-Node #X at (X, X, X, X)px, Tag: 'tag22'\n" +
164 " Has 1 child")
165 }
166
167 @Composable
168 fun ComposeSimpleCase() {
169 MaterialTheme {
170 Column {
171 Text("Hello")
172 Text("World")
173 }
174 }
175 }
176}