-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Android sample project for using Roborazzi with JUnit 5
This requires the introduction of the third-party Robolectric extension and the Gradle plugin for JUnit 5 with Android.
- Loading branch information
1 parent
8553637
commit 828ee19
Showing
14 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
14 changes: 14 additions & 0 deletions
14
...t5/src/androidTest/kotlin/com/github/takahirom/roborazzi/sample/JUnit5InstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
22 changes: 22 additions & 0 deletions
22
sample-android-junit5/src/main/kotlin/com/github/takahirom/roborazzi/sample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
sample-android-junit5/src/main/res/layout/activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
76 changes: 76 additions & 0 deletions
76
...-android-junit5/src/test/kotlin/com/github/takahirom/roborazzi/sample/JUnit5ManualTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters