Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2029f75

Browse files
authoredFeb 23, 2023
1 parent ce2b379 commit 2029f75

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
 
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2023 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+
* https://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 com.example.compose.snippets.semantics
18+
19+
import androidx.compose.material3.Text
20+
import androidx.compose.runtime.Composable
21+
import androidx.compose.ui.semantics.Role
22+
import androidx.compose.ui.semantics.SemanticsProperties
23+
import androidx.compose.ui.test.SemanticsMatcher
24+
import androidx.compose.ui.test.assertIsOff
25+
import androidx.compose.ui.test.junit4.createComposeRule
26+
import androidx.compose.ui.test.onNodeWithText
27+
import androidx.compose.ui.test.onRoot
28+
import androidx.compose.ui.test.performClick
29+
import androidx.compose.ui.test.printToLog
30+
import org.junit.Rule
31+
import org.junit.Test
32+
33+
@Suppress("TestFunctionName")
34+
// [START android_compose_semantics_logging]
35+
class MyComposeTest {
36+
37+
@get:Rule
38+
val composeTestRule = createComposeRule()
39+
40+
@Test
41+
fun MyTest() {
42+
// Start the app
43+
composeTestRule.setContent {
44+
MyTheme {
45+
Text("Hello world!")
46+
}
47+
}
48+
// Log the full semantics tree
49+
composeTestRule.onRoot().printToLog("MY TAG")
50+
}
51+
}
52+
// [END android_compose_semantics_logging]
53+
54+
class Test2 {
55+
@get:Rule
56+
val composeTestRule = createComposeRule()
57+
58+
@Test
59+
fun testSwitch() {
60+
// [START android_compose_semantics_test_switch]
61+
val mySwitch = SemanticsMatcher.expectValue(
62+
SemanticsProperties.Role, Role.Switch
63+
)
64+
composeTestRule.onNode(mySwitch)
65+
.performClick()
66+
.assertIsOff()
67+
// [END android_compose_semantics_test_switch]
68+
}
69+
}
70+
71+
class Test3 {
72+
@get:Rule
73+
val composeTestRule = createComposeRule()
74+
75+
@Test
76+
fun testLog() {
77+
// [START android_compose_semantics_print_log]
78+
composeTestRule.onRoot(useUnmergedTree = true).printToLog("MY TAG")
79+
// [END android_compose_semantics_print_log]
80+
// [START android_compose_semantics_match_text]
81+
composeTestRule.onNodeWithText("Like").performClick()
82+
// [END android_compose_semantics_match_text]
83+
}
84+
}
85+
86+
@Suppress("TestFunctionName")
87+
@Composable
88+
private fun MyTheme(
89+
content: @Composable () -> Unit
90+
) {
91+
content()
92+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
@file:Suppress("unused")
18+
19+
package com.example.compose.snippets.semantics
20+
21+
import androidx.compose.foundation.layout.Spacer
22+
import androidx.compose.foundation.layout.size
23+
import androidx.compose.material.icons.Icons
24+
import androidx.compose.material.icons.filled.Favorite
25+
import androidx.compose.material3.Button
26+
import androidx.compose.material3.ButtonDefaults
27+
import androidx.compose.material3.Icon
28+
import androidx.compose.material3.Text
29+
import androidx.compose.runtime.Composable
30+
import androidx.compose.ui.Modifier
31+
import androidx.compose.ui.tooling.preview.Preview
32+
33+
@Preview
34+
@Composable
35+
private fun Merging() {
36+
// [START android_compose_semantics_merging]
37+
Button(onClick = { /*TODO*/ }) {
38+
Icon(
39+
imageVector = Icons.Filled.Favorite,
40+
contentDescription = null
41+
)
42+
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
43+
Text("Like")
44+
}
45+
// [END android_compose_semantics_merging]
46+
}

0 commit comments

Comments
 (0)
Failed to load comments.