Skip to content

Commit

Permalink
Introduce Android sample project for using Roborazzi with JUnit 5
Browse files Browse the repository at this point in the history
This requires the introduction of the third-party Robolectric extension
and the Gradle plugin for JUnit 5 with Android.
  • Loading branch information
mannodermaus committed May 14, 2024
1 parent 8553637 commit 828ee19
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ plugins {
id 'com.android.library' version libs.versions.agp apply false
id 'org.jetbrains.kotlin.multiplatform' version libs.versions.kotlin apply false
id 'org.jetbrains.compose' version libs.versions.composeMultiplatform apply false
id 'de.mannodermaus.android-junit5' version libs.versions.junit5Android apply false
id 'tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin' version libs.versions.junit5Robolectric apply false

id 'org.jetbrains.kotlin.android' version libs.versions.kotlin apply false
id "com.vanniktech.maven.publish" version libs.versions.mavenPublish apply false
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ kotlin = "1.9.21"
mavenPublish = "0.25.3"
composeCompiler = "1.5.6"
composeMultiplatform = "1.6.2"
junit5Android = "1.10.0.0"
junit5Robolectric = "0.5.2"
robolectric = "4.12.1"
robolectric-android-all = "Q-robolectric-5415296"

Expand All @@ -29,6 +31,7 @@ androidx-lifecycle = "2.6.1"
androidx-navigation = "2.7.7"
androidx-test-espresso-core = "3.5.1"
androidx-test-ext-junit = "1.1.5"
androidx-test-runner = "1.5.2"
kim = "0.17.7"

dropbox-differ = "0.0.2"
Expand Down Expand Up @@ -71,6 +74,7 @@ androidx-navigation-ui-ktx = { module = "androidx.navigation:navigation-ui-ktx",
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso-core" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-ext-junit" }
androidx-test-ext-junit-ktx = { module = "androidx.test.ext:junit-ktx", version.ref = "androidx-test-ext-junit" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
kim = { module = "com.ashampoo:kim", version.ref = "kim" }

android-tools-build-gradle = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down
1 change: 1 addition & 0 deletions sample-android-junit5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
69 changes: 69 additions & 0 deletions sample-android-junit5/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
plugins {
id("com.android.library")
kotlin("android")
id("io.github.takahirom.roborazzi")
id("de.mannodermaus.android-junit5")
id("tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin")
}

val jvmVersion = JavaVersion.VERSION_17

android {
namespace = "com.github.takahirom.roborazzi.sample"
compileSdk = 34

defaultConfig {
minSdk = 21
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = jvmVersion
targetCompatibility = jvmVersion
}

kotlinOptions {
jvmTarget = jvmVersion.toString()
}

buildFeatures {
viewBinding = true
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(jvmVersion.toString()))
}
}

junitPlatform {
configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true")
}

dependencies {
testImplementation(project(":roborazzi"))
testImplementation(project(":roborazzi-junit5"))

implementation(kotlin("stdlib"))
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.google.android.material)

testImplementation(libs.robolectric)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.params)
testImplementation(libs.androidx.test.espresso.core)
testRuntimeOnly(libs.junit.jupiter.engine)

androidTestImplementation(libs.junit.jupiter.api)
androidTestImplementation(libs.androidx.test.runner)
}
21 changes: 21 additions & 0 deletions sample-android-junit5/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.takahirom.roborazzi.sample

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class JUnit5InstrumentedTest {
@Test
fun useAppContext() {
// The basic example instrumentation test, but with JUnit 5
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.github.takahirom.roborazzi.sample.test", appContext.packageName)
}
}
14 changes: 14 additions & 0 deletions sample-android-junit5/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application android:theme="@style/Theme.Roborazzi">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.takahirom.roborazzi.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.takahirom.roborazzi.sample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.updateButton.setOnClickListener {
val input = binding.inputEditText.text ?: ""

binding.descriptionText.text = getString(R.string.text_description_2, input)
}
}
}
45 changes: 45 additions & 0 deletions sample-android-junit5/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:id="@+id/captionText"
style="@style/TextAppearance.Material3.HeadlineMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:padding="16dp"
android:text="@string/text_caption"
android:textColor="?onColorPrimary"
android:textStyle="bold" />

<TextView
android:id="@+id/descriptionText"
style="@style/TextAppearance.Material3.BodyMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/text_description_1" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="@string/input_placeholder">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/updateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/button_update" />
</LinearLayout>
4 changes: 4 additions & 0 deletions sample-android-junit5/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="onColorPrimary" format="color" />
</resources>
8 changes: 8 additions & 0 deletions sample-android-junit5/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="text_caption">Caption</string>
<string name="text_description_1">Original description</string>
<string name="text_description_2">Updated description: %s</string>
<string name="button_update">Update</string>
<string name="input_placeholder">Enter something</string>
</resources>
7 changes: 7 additions & 0 deletions sample-android-junit5/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Roborazzi" parent="Theme.Material3.Light.NoActionBar">
<item name="colorPrimary">#55ACEE</item>
<item name="onColorPrimary">#FFFFFF</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.takahirom.roborazzi.sample

import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers
import com.github.takahirom.roborazzi.captureRoboImage
import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.RepetitionInfo
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import tech.apter.junit.jupiter.robolectric.RobolectricExtension

@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(
sdk = [30],
qualifiers = RobolectricDeviceQualifiers.NexusOne,
)
@ExtendWith(RobolectricExtension::class)
class JUnit5ManualTest {
@Test
@Config(qualifiers = "+land")
fun captureRoboImageSample() {
ActivityScenario.launch(MainActivity::class.java)

onView(isRoot()).captureRoboImage()
onView(withId(R.id.inputEditText)).perform(typeText("hello"))
onView(withId(R.id.updateButton)).perform(click())
onView(isRoot()).captureRoboImage()
}

@ParameterizedTest
@ValueSource(booleans = [false, true])
fun parameterizedTest(value: Boolean) {
ActivityScenario.launch(MainActivity::class.java)

onView(withId(R.id.inputEditText)).perform(typeText("parameter=$value"))
onView(withId(R.id.updateButton)).perform(click())
onView(isRoot()).captureRoboImage()
}

@RepeatedTest(2)
fun repeatedTest(repetitionInfo: RepetitionInfo) {
ActivityScenario.launch(MainActivity::class.java)

onView(withId(R.id.inputEditText)).perform(typeText("repeated=${repetitionInfo.currentRepetition}"))
onView(withId(R.id.updateButton)).perform(click())
onView(isRoot()).captureRoboImage()
}

@TestFactory
fun testFactory() = listOf(
dynamicTest("First") {
ActivityScenario.launch(MainActivity::class.java)

onView(withId(R.id.inputEditText)).perform(typeText("typed but not pressed"))
onView(isRoot()).captureRoboImage()
},
dynamicTest("Second") {
ActivityScenario.launch(MainActivity::class.java)

onView(withId(R.id.inputEditText)).perform(typeText("typed and pressed"))
onView(withId(R.id.updateButton)).perform(click())
onView(isRoot()).captureRoboImage()
},
)
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include ':roborazzi-compose'
include ':roborazzi-painter'

include ':sample-android'
include ':sample-android-junit5'
include ':sample-android-without-compose'
include ':sample-compose-desktop-multiplatform'
include ':sample-compose-desktop-jvm'
Expand Down

0 comments on commit 828ee19

Please sign in to comment.