From fd645782a7ef71a232d079601bd3c289f8b2d973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Mon, 13 Nov 2023 17:27:22 +0100 Subject: [PATCH] Screenshot tests for XML buttons --- library/build.gradle | 4 + library/src/test/AndroidManifest.xml | 5 +- .../com/telefonica/mistica/DummyActivity.kt | 6 +- .../telefonica/mistica/button/ButtonTest.kt | 127 ++++++++++++++++++ .../mistica/compose/button/ButtonKtTest.kt | 27 +--- .../telefonica/mistica/testutils/TestUtils.kt | 19 +++ library/src/test/res/values/styles.xml | 108 +++++++++++++++ 7 files changed, 271 insertions(+), 25 deletions(-) create mode 100644 library/src/test/java/com/telefonica/mistica/button/ButtonTest.kt create mode 100644 library/src/test/res/values/styles.xml diff --git a/library/build.gradle b/library/build.gradle index ccf7740d3..2943fa06e 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -63,6 +63,10 @@ android { } } +tasks.withType(Test) { + android.sourceSets.main.res.srcDirs += 'src/test/res' +} + task sourceJar(type: Jar) { from android.sourceSets.main.java.srcDirs classifier "sources" diff --git a/library/src/test/AndroidManifest.xml b/library/src/test/AndroidManifest.xml index c2944977c..6e823f516 100644 --- a/library/src/test/AndroidManifest.xml +++ b/library/src/test/AndroidManifest.xml @@ -7,9 +7,8 @@ + android:label="Activity for testing Mistica xml elements" + android:exported="true"> diff --git a/library/src/test/java/com/telefonica/mistica/DummyActivity.kt b/library/src/test/java/com/telefonica/mistica/DummyActivity.kt index f84cb814d..e80070319 100644 --- a/library/src/test/java/com/telefonica/mistica/DummyActivity.kt +++ b/library/src/test/java/com/telefonica/mistica/DummyActivity.kt @@ -2,12 +2,16 @@ package com.telefonica.mistica import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import com.telefonica.mistica.R class DummyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { + setTheme(intent.getIntExtra(EXTRA_THEME, DEFAULT_CLASSIC_THEME)) super.onCreate(savedInstanceState) setContentView(R.layout.test_dummy_activity) } + companion object { + const val EXTRA_THEME = "extra_theme" + val DEFAULT_CLASSIC_THEME = R.style.MisticaTheme_Movistar + } } diff --git a/library/src/test/java/com/telefonica/mistica/button/ButtonTest.kt b/library/src/test/java/com/telefonica/mistica/button/ButtonTest.kt new file mode 100644 index 000000000..5ec744edf --- /dev/null +++ b/library/src/test/java/com/telefonica/mistica/button/ButtonTest.kt @@ -0,0 +1,127 @@ +package com.telefonica.mistica.button + +import android.content.Intent +import android.widget.FrameLayout +import androidx.test.core.app.ApplicationProvider +import androidx.test.espresso.Espresso +import androidx.test.espresso.matcher.ViewMatchers +import androidx.test.ext.junit.rules.activityScenarioRule +import com.telefonica.mistica.DummyActivity +import com.telefonica.mistica.DummyActivity.Companion.EXTRA_THEME +import com.telefonica.mistica.R +import com.telefonica.mistica.compose.button.ButtonStyle +import com.telefonica.mistica.compose.theme.brand.Brand +import com.telefonica.mistica.compose.theme.brand.MovistarBrand +import com.telefonica.mistica.compose.theme.brand.VivoBrand +import com.telefonica.mistica.testutils.ScreenshotsTest +import com.telefonica.mistica.testutils.TestUtils.getAllBrands +import com.telefonica.mistica.testutils.TestUtils.getAllButtonStyles +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.ParameterizedRobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(ParameterizedRobolectricTestRunner::class) +internal class ButtonTest( + private val brand: Brand, + private val style: ButtonStyle, + private val icon: Boolean, +): ScreenshotsTest() { + + private val intent = Intent(ApplicationProvider.getApplicationContext(), DummyActivity::class.java).apply { + this.putExtra(EXTRA_THEME, brand.getBaseThemeForBrand()) + } + + @get:Rule + val rule = activityScenarioRule(intent) + + @Test + fun `check Xml Button`() { + checkXmlButton(brand, style, icon, false) + } + + @Config(qualifiers = "+night") + @Test + fun `check Xml Button dark`() { + checkXmlButton(brand, style, icon, true) + } + private fun checkXmlButton(brand: Brand = MovistarBrand, style: ButtonStyle, icon: Boolean, darkTheme: Boolean) { + rule.scenario.onActivity { activity -> + val wrapper: FrameLayout = activity.findViewById(R.id.dummy_activity_wrapper) + val button = Button(activity, null, style.getButtonStyleRef()) + button.text = "textValue" + var iconSuffix: String? = null + if(icon) { + button.setIconResource(android.R.drawable.ic_lock_power_off) + iconSuffix = "icon" + } + wrapper.removeAllViews() + wrapper.addView(button) + + compareScreenshot( + Espresso.onView(ViewMatchers.withId(R.id.dummy_activity_wrapper)), + component = "XMLButton", + style = style.toString(), + brand = brand, + darkTheme = darkTheme, + extra = iconSuffix + ) + } + } + + fun getStyleResFromButtonStyle(style: ButtonStyle) = when(style) { + ButtonStyle.PRIMARY -> R.style.AppTheme_Button_DefaultButton + ButtonStyle.PRIMARY_SMALL -> R.style.AppTheme_Button_DefaultButton_Small + ButtonStyle.SECONDARY -> R.style.AppTheme_Button_SecondaryButton + ButtonStyle.SECONDARY_SMALL -> R.style.AppTheme_Button_SecondaryButton_Small + ButtonStyle.DANGER -> R.style.AppTheme_Button_DangerButton + ButtonStyle.DANGER_SMALL -> R.style.AppTheme_Button_DangerButton_Small + ButtonStyle.LINK -> R.style.AppTheme_Button_LinkButton + ButtonStyle.PRIMARY_INVERSE -> R.style.AppTheme_Button_PrimaryButtonInverse + ButtonStyle.PRIMARY_SMALL_INVERSE -> R.style.AppTheme_Button_PrimaryButtonInverse_Small + ButtonStyle.SECONDARY_INVERSE -> R.style.AppTheme_Button_SecondaryButtonInverse + ButtonStyle.SECONDARY_SMALL_INVERSE -> R.style.AppTheme_Button_SecondaryButtonInverse_Small + ButtonStyle.LINK_INVERSE -> R.style.AppTheme_Button_LinkButtonInverse + } + + companion object { + @JvmStatic + @ParameterizedRobolectricTestRunner.Parameters(name = "ButtonXML {1} {0} icon={2}") + fun brands(): List> { + val allBrands = getAllBrands() + val buttonStyles = getAllButtonStyles() + val icons = listOf(false, true) + return allBrands.flatMap { brand -> + buttonStyles.flatMap { buttonStyle -> + icons.map { hasIcon -> + arrayOf(brand, buttonStyle, hasIcon) + } + } + } + } + } +} + +fun Brand.getBaseThemeForBrand() = when(this) { + MovistarBrand -> R.style.MisticaTheme_Movistar_test + VivoBrand -> R.style.MisticaTheme_Vivo_test + VivoBrand -> R.style.MisticaTheme_O2_test + VivoBrand -> R.style.MisticaTheme_Blau_test + else -> R.style.MisticaTheme_Telefonica_test + +} +fun ButtonStyle.getButtonStyleRef() = when(this) { + ButtonStyle.PRIMARY -> R.attr.defaultButtonTest_test + ButtonStyle.PRIMARY_SMALL -> R.attr.defaultButton_Small_test + ButtonStyle.SECONDARY -> R.attr.secondaryButton_test + ButtonStyle.SECONDARY_SMALL -> R.attr.secondaryButton_Small_test + ButtonStyle.DANGER -> R.attr.dangerButton_test + ButtonStyle.DANGER_SMALL -> R.attr.dangerButton_Small_test + ButtonStyle.LINK -> R.attr.linkButton_test + ButtonStyle.PRIMARY_INVERSE -> R.attr.primaryButtonInverse_test + ButtonStyle.PRIMARY_SMALL_INVERSE -> R.attr.primaryButtonInverse_Small_test + ButtonStyle.SECONDARY_INVERSE -> R.attr.secondaryButtonInverse_test + ButtonStyle.SECONDARY_SMALL_INVERSE -> R.attr.secondaryButtonInverse_Small_test + ButtonStyle.LINK_INVERSE -> R.attr.linkButtonInverse_test +} \ No newline at end of file diff --git a/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt b/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt index a227c36c7..552532ee6 100644 --- a/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt +++ b/library/src/test/java/com/telefonica/mistica/compose/button/ButtonKtTest.kt @@ -7,13 +7,10 @@ import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onRoot import androidx.compose.ui.unit.dp import com.telefonica.mistica.compose.theme.MisticaTheme -import com.telefonica.mistica.compose.theme.brand.BlauBrand import com.telefonica.mistica.compose.theme.brand.Brand import com.telefonica.mistica.compose.theme.brand.MovistarBrand -import com.telefonica.mistica.compose.theme.brand.O2Brand -import com.telefonica.mistica.compose.theme.brand.TelefonicaBrand -import com.telefonica.mistica.compose.theme.brand.VivoBrand import com.telefonica.mistica.testutils.ScreenshotsTest +import com.telefonica.mistica.testutils.TestUtils import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -61,31 +58,19 @@ internal class ButtonKtTest(private val brand: Brand, private val style: ButtonS @JvmStatic @ParameterizedRobolectricTestRunner.Parameters(name = "Button {1} {0} icon={2} darkTheme={3}") fun brands(): List> { - val brands2 = listOf( - MovistarBrand, - VivoBrand, - O2Brand, - BlauBrand, - TelefonicaBrand, - ) - val themes = listOf(false, true) - val buttonStyles = listOf( - ButtonStyle.PRIMARY, ButtonStyle.PRIMARY_SMALL, ButtonStyle.SECONDARY, ButtonStyle.SECONDARY_SMALL, ButtonStyle.DANGER, - ButtonStyle.DANGER_SMALL, ButtonStyle.LINK, ButtonStyle.PRIMARY_INVERSE, ButtonStyle.PRIMARY_SMALL_INVERSE, ButtonStyle.SECONDARY_INVERSE, - ButtonStyle.SECONDARY_SMALL_INVERSE, ButtonStyle.LINK_INVERSE - ) + val allBrands = TestUtils.getAllBrands() + val buttonStyles = TestUtils.getAllButtonStyles() val icons = listOf(false, true) - - return brands2.flatMap { brand -> + val darkTheme = listOf(false, true) + return allBrands.flatMap { brand -> buttonStyles.flatMap { buttonStyle -> icons.flatMap { hasIcon -> - themes.map { darkTheme -> + darkTheme.map { darkTheme -> arrayOf(brand, buttonStyle, hasIcon, darkTheme) } } } } } - } } diff --git a/library/src/test/java/com/telefonica/mistica/testutils/TestUtils.kt b/library/src/test/java/com/telefonica/mistica/testutils/TestUtils.kt index b0a24a910..2eb7c1814 100644 --- a/library/src/test/java/com/telefonica/mistica/testutils/TestUtils.kt +++ b/library/src/test/java/com/telefonica/mistica/testutils/TestUtils.kt @@ -1,5 +1,11 @@ package com.telefonica.mistica.testutils +import com.telefonica.mistica.compose.button.ButtonStyle +import com.telefonica.mistica.compose.theme.brand.BlauBrand +import com.telefonica.mistica.compose.theme.brand.MovistarBrand +import com.telefonica.mistica.compose.theme.brand.O2Brand +import com.telefonica.mistica.compose.theme.brand.TelefonicaBrand +import com.telefonica.mistica.compose.theme.brand.VivoBrand import org.junit.Test object TestUtils { @@ -21,4 +27,17 @@ object TestUtils { } throw IllegalStateException("Could not find running test method name") } + fun getAllBrands() = listOf( + MovistarBrand, + VivoBrand, + O2Brand, + BlauBrand, + TelefonicaBrand, + ) + + fun getAllButtonStyles() = listOf( + ButtonStyle.PRIMARY, ButtonStyle.PRIMARY_SMALL, ButtonStyle.SECONDARY, ButtonStyle.SECONDARY_SMALL, ButtonStyle.DANGER, + ButtonStyle.DANGER_SMALL, ButtonStyle.LINK, ButtonStyle.PRIMARY_INVERSE, ButtonStyle.PRIMARY_SMALL_INVERSE, ButtonStyle.SECONDARY_INVERSE, + ButtonStyle.SECONDARY_SMALL_INVERSE, ButtonStyle.LINK_INVERSE + ) } \ No newline at end of file diff --git a/library/src/test/res/values/styles.xml b/library/src/test/res/values/styles.xml new file mode 100644 index 000000000..d589ac522 --- /dev/null +++ b/library/src/test/res/values/styles.xml @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file