From 4cf7a2158aa7996f2958be78a1ba7747bf8df030 Mon Sep 17 00:00:00 2001 From: Jean Silva Date: Wed, 27 Mar 2024 12:04:58 -0300 Subject: [PATCH] =?UTF-8?q?add:=20semantics=20matchers=20and=20interaction?= =?UTF-8?q?s=20for=20=CE=B1=E1=BD=90=CF=84=CF=8C=CF=82=20buttons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../button/SemanticsMatcherExtensionsTests.kt | 44 +++++++++++++++++++ ...NodeInteractionsProviderExtensionsTests.kt | 44 +++++++++++++++++++ .../button/SemanticsMatcher.extensions.kt | 33 ++++++++++++++ ...ticsNodeInteractionsProvider.extensions.kt | 31 +++++++++++++ .../autos/kit/action/button/PrimaryButton.kt | 6 ++- .../kit/action/button/SecondaryButton.kt | 6 ++- 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcherExtensionsTests.kt create mode 100644 platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProviderExtensionsTests.kt create mode 100644 platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcher.extensions.kt create mode 100644 platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProvider.extensions.kt diff --git a/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcherExtensionsTests.kt b/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcherExtensionsTests.kt new file mode 100644 index 000000000..549a1311d --- /dev/null +++ b/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcherExtensionsTests.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2024 Orca + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see https://www.gnu.org/licenses. + */ + +package com.jeanbarrossilva.orca.platform.autos.test.kit.action.button + +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.PrimaryButton +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.SecondaryButton +import com.jeanbarrossilva.orca.platform.autos.theme.AutosTheme +import org.junit.Rule +import org.junit.Test + +internal class SemanticsMatcherExtensionsTests { + @get:Rule val composeRule = createComposeRule() + + @Test + fun matchesPrimaryButton() { + composeRule + .apply { setContent { AutosTheme { PrimaryButton(onClick = {}) {} } } } + .onNode(isPrimaryButton()) + .assertIsDisplayed() + } + + @Test + fun matchesSecondaryButton() { + composeRule + .apply { setContent { AutosTheme { SecondaryButton(onClick = {}) {} } } } + .onNode(isSecondaryButton()) + .assertIsDisplayed() + } +} diff --git a/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProviderExtensionsTests.kt b/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProviderExtensionsTests.kt new file mode 100644 index 000000000..ff31a6be3 --- /dev/null +++ b/platform/autos-test/src/androidTest/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProviderExtensionsTests.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2024 Orca + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see https://www.gnu.org/licenses. + */ + +package com.jeanbarrossilva.orca.platform.autos.test.kit.action.button + +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.PrimaryButton +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.SecondaryButton +import com.jeanbarrossilva.orca.platform.autos.theme.AutosTheme +import org.junit.Rule +import org.junit.Test + +internal class SemanticsNodeInteractionsProviderExtensionsTests { + @get:Rule val composeRule = createComposeRule() + + @Test + fun findsPrimaryButton() { + composeRule + .apply { setContent { AutosTheme { PrimaryButton(onClick = {}) {} } } } + .onPrimaryButton() + .assertIsDisplayed() + } + + @Test + fun findsSecondaryButton() { + composeRule + .apply { setContent { AutosTheme { SecondaryButton(onClick = {}) {} } } } + .onSecondaryButton() + .assertIsDisplayed() + } +} diff --git a/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcher.extensions.kt b/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcher.extensions.kt new file mode 100644 index 000000000..71fcf9f35 --- /dev/null +++ b/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsMatcher.extensions.kt @@ -0,0 +1,33 @@ +/* + * Copyright © 2024 Orca + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see https://www.gnu.org/licenses. + */ + +package com.jeanbarrossilva.orca.platform.autos.test.kit.action.button + +import androidx.compose.ui.test.SemanticsMatcher +import androidx.compose.ui.test.hasTestTag +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.PRIMARY_BUTTON_TAG +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.PrimaryButton +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.SECONDARY_BUTTON_TAG +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.SecondaryButton + +/** [SemanticsMatcher] that matches a [PrimaryButton]. */ +fun isPrimaryButton(): SemanticsMatcher { + return hasTestTag(PRIMARY_BUTTON_TAG) +} + +/** [SemanticsMatcher] that matches a [SecondaryButton]. */ +fun isSecondaryButton(): SemanticsMatcher { + return hasTestTag(SECONDARY_BUTTON_TAG) +} diff --git a/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProvider.extensions.kt b/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProvider.extensions.kt new file mode 100644 index 000000000..79a3d8eaa --- /dev/null +++ b/platform/autos-test/src/main/java/com/jeanbarrossilva/orca/platform/autos/test/kit/action/button/SemanticsNodeInteractionsProvider.extensions.kt @@ -0,0 +1,31 @@ +/* + * Copyright © 2024 Orca + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If + * not, see https://www.gnu.org/licenses. + */ + +package com.jeanbarrossilva.orca.platform.autos.test.kit.action.button + +import androidx.compose.ui.test.SemanticsNodeInteraction +import androidx.compose.ui.test.SemanticsNodeInteractionsProvider +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.PrimaryButton +import com.jeanbarrossilva.orca.platform.autos.kit.action.button.SecondaryButton + +/** [SemanticsNodeInteraction] of a [PrimaryButton]. */ +fun SemanticsNodeInteractionsProvider.onPrimaryButton(): SemanticsNodeInteraction { + return onNode(isPrimaryButton()) +} + +/** [SemanticsNodeInteraction] of a [SecondaryButton]. */ +fun SemanticsNodeInteractionsProvider.onSecondaryButton(): SemanticsNodeInteraction { + return onNode(isSecondaryButton()) +} diff --git a/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/PrimaryButton.kt b/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/PrimaryButton.kt index ffee667fb..75dc83400 100644 --- a/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/PrimaryButton.kt +++ b/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/PrimaryButton.kt @@ -20,12 +20,16 @@ import androidx.compose.material3.ElevatedButton import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import com.jeanbarrossilva.orca.platform.autos.colors.asColor import com.jeanbarrossilva.orca.platform.autos.kit.action.button.skeleton.Button import com.jeanbarrossilva.orca.platform.autos.kit.action.button.skeleton.ButtonDefaults as _ButtonDefaults import com.jeanbarrossilva.orca.platform.autos.theme.AutosTheme import com.jeanbarrossilva.orca.platform.autos.theme.MultiThemePreview +/** Tag that identifies a [PrimaryButton] for testing purposes. */ +const val PRIMARY_BUTTON_TAG = "primary-button" + /** * [Button] that represents a primary action, performed or requested to be performed through * [onClick]; usually is placed on the bottom of the screen, filling its width. @@ -46,7 +50,7 @@ fun PrimaryButton( Button { ElevatedButton( onClick = { load(onClick) }, - modifier, + modifier.testTag(PRIMARY_BUTTON_TAG), isEnabled, _ButtonDefaults.shape, colors = diff --git a/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/SecondaryButton.kt b/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/SecondaryButton.kt index 53b3b4a80..f3655babb 100644 --- a/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/SecondaryButton.kt +++ b/platform/autos/src/main/java/com/jeanbarrossilva/orca/platform/autos/kit/action/button/SecondaryButton.kt @@ -21,6 +21,7 @@ import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import com.jeanbarrossilva.orca.platform.autos.colors.asColor import com.jeanbarrossilva.orca.platform.autos.kit.action.button.skeleton.Button as _Button import com.jeanbarrossilva.orca.platform.autos.kit.action.button.skeleton.ButtonDefaults as _ButtonDefaults @@ -31,6 +32,9 @@ import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking +/** Tag that identifies the [SecondaryButton] for testing purposes. */ +const val SECONDARY_BUTTON_TAG = "secondary-button" + /** * [Button][_Button] that executes a secondary action when clicked, and is usually preceded by a * [PrimaryButton] within a [ButtonBar]. @@ -49,7 +53,7 @@ fun SecondaryButton( _Button { Button( onClick = { load(onClick) }, - modifier, + modifier.testTag(SECONDARY_BUTTON_TAG), shape = _ButtonDefaults.shape, colors = ButtonDefaults.buttonColors(