|
| 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 | +} |
0 commit comments