Skip to content

Commit

Permalink
Screenshot tests for XML buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinbTEF committed Nov 13, 2023
1 parent d4dbef6 commit fd64578
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 25 deletions.
4 changes: 4 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions library/src/test/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
<application>
<activity
android:name=".DummyActivity"
android:label="Text Input Activity"
android:exported="true"
android:theme="@style/MisticaTheme.Movistar">
android:label="Activity for testing Mistica xml elements"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
127 changes: 127 additions & 0 deletions library/src/test/java/com/telefonica/mistica/button/ButtonTest.kt
Original file line number Diff line number Diff line change
@@ -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<DummyActivity>(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<Array<Any>> {
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Array<Any>> {
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)
}
}
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
)
}
108 changes: 108 additions & 0 deletions library/src/test/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="defaultButtonTest_test" format="reference"/>
<attr name="defaultButton_Small_test" format="reference"/>
<attr name="secondaryButton_test" format="reference"/>
<attr name="secondaryButton_Small_test" format="reference"/>
<attr name="dangerButton_test" format="reference"/>
<attr name="dangerButton_Small_test" format="reference"/>
<attr name="linkButton_test" format="reference"/>
<attr name="primaryButtonInverse_test" format="reference"/>
<attr name="primaryButtonInverse_Small_test" format="reference"/>
<attr name="secondaryButtonInverse_test" format="reference"/>
<attr name="secondaryButtonInverse_Small_test" format="reference"/>
<attr name="linkButtonInverse_test" format="reference"/>

<style name="MisticaTheme.Blau.test" parent="MisticaTheme.Blau">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>

<style name="MisticaTheme.Movistar.test" parent="MisticaTheme.Movistar">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>

<style name="MisticaTheme.O2.test" parent="MisticaTheme.O2">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>

<style name="MisticaTheme.Telefonica.test" parent="MisticaTheme.Telefonica">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>

<style name="MisticaTheme.Vivo.test" parent="MisticaTheme.Vivo">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>

<style name="MisticaTheme.VivoNew.test" parent="MisticaTheme.VivoNew">
<item name="defaultButtonTest_test">@style/AppTheme.Button.DefaultButton</item>
<item name="defaultButton_Small_test">@style/AppTheme.Button.DefaultButton.Small</item>
<item name="secondaryButton_test">@style/AppTheme.Button.SecondaryButton</item>
<item name="secondaryButton_Small_test">@style/AppTheme.Button.SecondaryButton.Small</item>
<item name="dangerButton_test">@style/AppTheme.Button.DangerButton</item>
<item name="dangerButton_Small_test">@style/AppTheme.Button.DangerButton.Small</item>
<item name="linkButton_test">@style/AppTheme.Button.LinkButton</item>
<item name="primaryButtonInverse_test">@style/AppTheme.Button.PrimaryButtonInverse</item>
<item name="primaryButtonInverse_Small_test">@style/AppTheme.Button.PrimaryButtonInverse.Small</item>
<item name="secondaryButtonInverse_test">@style/AppTheme.Button.SecondaryButtonInverse</item>
<item name="secondaryButtonInverse_Small_test">@style/AppTheme.Button.SecondaryButtonInverse.Small</item>
<item name="linkButtonInverse_test">@style/AppTheme.Button.LinkButtonInverse</item>
</style>


</resources>

0 comments on commit fd64578

Please sign in to comment.