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 |
| 23 | import androidx.ui.foundation.Box |
| 24 | import androidx.ui.foundation.Text |
| 25 | import androidx.ui.layout.Column |
| 26 | import androidx.ui.material.Button |
| 27 | import androidx.ui.material.MaterialTheme |
| 28 | import androidx.ui.semantics.enabled |
| 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 | ) { |
| 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 | } |