[go: nahoru, domu]

blob: a99798961e21ad8a748370ddf5fec3c154f2fe8e [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
23import androidx.ui.foundation.Box
24import androidx.ui.foundation.Text
25import androidx.ui.layout.Column
26import androidx.ui.material.Button
27import androidx.ui.material.MaterialTheme
28import androidx.ui.semantics.enabled
29import 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 ) {
56 findByText("Oops").printToString()
57 }
58 }
59
60 @Test
61 fun printToString_one() {
62 composeTestRule.setContent {
63 ComposeSimpleCase()
64 }
65
66 val result = findByText("Hello")
67 .printToString(maxDepth = 0)
68
69 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
70 "Node #X at (X, X, X, X)px\n" +
71 "Text = 'Hello'\n" +
72 "Has 1 sibling")
73 }
74
75 @Test
76 fun printToString_many() {
77 composeTestRule.setContent {
78 ComposeSimpleCase()
79 }
80
81 val result = findRoot()
82 .children()
83 .printToString()
84
85 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
86 "1) Node #X at (X, X, X, X)px\n" +
87 "Text = 'Hello'\n" +
88 "Has 1 sibling\n" +
89 "2) Node #X at (X, X, X, X)px\n" +
90 "Text = 'World'\n" +
91 "Has 1 sibling")
92 }
93
94 @Test
95 fun printHierarchy() {
96 composeTestRule.setContent {
97 Column(Modifier.semantics { this.enabled = true; this.testTag = "column" }) {
98 Box(Modifier.semantics { this.enabled = true; this.testTag = "box" }) {
99 Button(onClick = {}) {
100 Text("Button")
101 }
102 }
103 Text("Hello")
104 }
105 }
106
107 val result = findRoot()
108 .printToString()
109
110 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
111 "Node #X at (X, X, X, X)px\n" +
112 " |-Node #X at (X, X, X, X)px, Tag: 'column'\n" +
113 " Enabled = 'true'\n" +
114 " |-Node #X at (X, X, X, X)px, Tag: 'box'\n" +
115 " | Enabled = 'true'\n" +
116 " | |-Node #X at (X, X, X, X)px\n" +
117 " | Enabled = 'true'\n" +
118 " | action=Function0" +
119 "<java.lang.Boolean>)'\n" +
120 " | Text = 'Button'\n" +
121 " | MergeDescendants = 'true'\n" +
122 " |-Node #X at (X, X, X, X)px\n" +
123 " Text = 'Hello'")
124 }
125
126 @Test
127 fun printMultiple_withDepth() {
128 composeTestRule.setContent {
129 BoundaryNode("tag1") {
130 BoundaryNode("tag11") {
131 BoundaryNode("tag111")
132 }
133 }
134 BoundaryNode("tag2") {
135 BoundaryNode("tag22") {
136 BoundaryNode("tag222")
137 }
138 }
139 }
140
141 val result = findRoot()
142 .children()
143 .printToString(maxDepth = 1)
144
145 assertThat(obfuscateNodesInfo(result)).isEqualTo("" +
146 "1) Node #X at (X, X, X, X)px, Tag: 'tag1'\n" +
147 " |-Node #X at (X, X, X, X)px, Tag: 'tag11'\n" +
148 " Has 1 child\n" +
149 "2) Node #X at (X, X, X, X)px, Tag: 'tag2'\n" +
150 " |-Node #X at (X, X, X, X)px, Tag: 'tag22'\n" +
151 " Has 1 child")
152 }
153
154 @Composable
155 fun ComposeSimpleCase() {
156 MaterialTheme {
157 Column {
158 Text("Hello")
159 Text("World")
160 }
161 }
162 }
163}