Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +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 |
| 18 | |
| 19 | import androidx.compose.Composable |
| 20 | import androidx.test.filters.MediumTest |
| 21 | import androidx.ui.core.Modifier |
| 22 | import androidx.ui.core.semantics.semantics |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 23 | import androidx.compose.foundation.Box |
| 24 | import androidx.compose.foundation.Text |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 25 | import androidx.compose.foundation.layout.Column |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 26 | import androidx.ui.material.Button |
| 27 | import androidx.ui.material.MaterialTheme |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 28 | import androidx.ui.semantics.disabled |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 29 | import androidx.ui.semantics.testTag |
| 30 | import androidx.ui.test.util.BoundaryNode |
| 31 | import androidx.ui.test.util.expectErrorMessageStartsWith |
| 32 | import androidx.ui.test.util.obfuscateNodesInfo |
| 33 | import com.google.common.truth.Truth.assertThat |
| 34 | import org.junit.Rule |
| 35 | import org.junit.Test |
| 36 | import org.junit.runner.RunWith |
| 37 | import org.junit.runners.JUnit4 |
| 38 | |
| 39 | @MediumTest |
| 40 | @RunWith(JUnit4::class) |
| 41 | class 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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 56 | onNodeWithText("Oops").printToString() |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | fun printToString_one() { |
| 62 | composeTestRule.setContent { |
| 63 | ComposeSimpleCase() |
| 64 | } |
| 65 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 66 | val result = onNodeWithText("Hello") |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 67 | .printToString(maxDepth = 0) |
| 68 | |
| 69 | assertThat(obfuscateNodesInfo(result)).isEqualTo("" + |
| 70 | "Node #X at (X, X, X, X)px\n" + |
| 71 | "Text = 'Hello'\n" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 72 | "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" + |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 73 | ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 74 | "Has 1 sibling") |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun printToString_many() { |
| 79 | composeTestRule.setContent { |
| 80 | ComposeSimpleCase() |
| 81 | } |
| 82 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 83 | val result = onRoot() |
| 84 | .onChildren() |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 85 | .printToString() |
| 86 | |
| 87 | assertThat(obfuscateNodesInfo(result)).isEqualTo("" + |
| 88 | "1) Node #X at (X, X, X, X)px\n" + |
| 89 | "Text = 'Hello'\n" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 90 | "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" + |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 91 | ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 92 | "Has 1 sibling\n" + |
| 93 | "2) Node #X at (X, X, X, X)px\n" + |
| 94 | "Text = 'World'\n" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 95 | "GetTextLayoutResult = 'AccessibilityAction(label=null, action=Function1<java" + |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 96 | ".util.List<androidx.compose.ui.text.TextLayoutResult>, java.lang.Boolean>)'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 97 | "Has 1 sibling") |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | fun printHierarchy() { |
| 102 | composeTestRule.setContent { |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 103 | Column(Modifier.semantics { this.disabled(); this.testTag = "column" }) { |
| 104 | Box(Modifier.semantics { this.disabled(); this.testTag = "box" }) { |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 105 | Button(onClick = {}) { |
| 106 | Text("Button") |
| 107 | } |
| 108 | } |
| 109 | Text("Hello") |
| 110 | } |
| 111 | } |
| 112 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 113 | val result = onRoot() |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 114 | .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 Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 119 | " Disabled = 'kotlin.Unit'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 120 | " |-Node #X at (X, X, X, X)px, Tag: 'box'\n" + |
Alexandre Elias | c60f33e | 2020-07-10 16:23:09 -0700 | [diff] [blame] | 121 | " | Disabled = 'kotlin.Unit'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 122 | " | |-Node #X at (X, X, X, X)px\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 123 | " | action=Function0" + |
| 124 | "<java.lang.Boolean>)'\n" + |
| 125 | " | Text = 'Button'\n" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 126 | " | GetTextLayoutResult = 'AccessibilityAction(label=null, " + |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 127 | "action=Function1<java.util.List<androidx.compose.ui.text.TextLayoutResult>," + |
| 128 | " java.lang" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 129 | ".Boolean>)'\n" + |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 130 | " | MergeDescendants = 'true'\n" + |
| 131 | " |-Node #X at (X, X, X, X)px\n" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 132 | " Text = 'Hello'\n" + |
| 133 | " GetTextLayoutResult = 'AccessibilityAction(label=null, " + |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 134 | "action=Function1<java.util.List<androidx.compose.ui.text.TextLayoutResult>," + |
| 135 | " java.lang" + |
yingleiw | a6d8298 | 2020-07-08 16:38:05 -0700 | [diff] [blame] | 136 | ".Boolean>)'") |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 137 | } |
| 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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 154 | val result = onRoot() |
| 155 | .onChildren() |
Filip Pavlis | 86dc4df | 2020-07-07 20:08:33 +0100 | [diff] [blame] | 156 | .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 | } |