Skip to content

Commit 15abeac

Browse files
authored
Add test rule to compare screenshots (#1)
1 parent fc7a94c commit 15abeac

File tree

17 files changed

+327
-28
lines changed

17 files changed

+327
-28
lines changed

app/src/androidTest/java/com/telefonica/loggerazzi/ExampleInstrumentedTest.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.telefonica.loggerazzi
22

3+
import androidx.test.core.app.ActivityScenario
34
import androidx.test.ext.junit.runners.AndroidJUnit4
4-
5+
import org.junit.Rule
56
import org.junit.Test
67
import org.junit.runner.RunWith
78

8-
import org.junit.Rule
9-
109
/**
1110
* Instrumented test, which will execute on an Android device.
1211
*
@@ -17,11 +16,12 @@ class ExampleInstrumentedTest {
1716

1817
private val recorder = FakeTestRecorder()
1918

20-
@JvmField
21-
@Rule
19+
@get:Rule
2220
val loggerazziRule = LoggerazziRule(
2321
recorder = recorder
2422
)
23+
@get:Rule
24+
val screenshotsRule = ScreenshotsRule()
2525

2626
@Test
2727
fun testSingleLog() {
@@ -41,16 +41,24 @@ class ExampleInstrumentedTest {
4141
}
4242

4343
@Test
44-
@IgnoreLoggerazzi
44+
@IgnoreLogs
4545
fun testIgnoreLoggerazzi() {
4646
recorder.record("My log")
4747
}
4848

4949
@Test
50-
@IgnoreLoggerazzi
50+
@IgnoreLogs
5151
fun testIgnoreLoggerazziWithoutGoldenFile() {
5252
recorder.record("My log")
5353
}
54+
55+
@Test
56+
@IgnoreLogs
57+
fun testLaunchActivity() {
58+
ActivityScenario.launch(MainActivity::class.java).onActivity {
59+
screenshotsRule.compareScreenshot(it, name = "launch_activity")
60+
}
61+
}
5462
}
5563

5664
class FakeTestRecorder: LogsRecorder<String> {
16.9 KB
Loading

app/src/androidTestDebug/assets/loggerazzi-golden-files/com.telefonica.loggerazzi.ExampleInstrumentedTest_testLaunchActivity.txt

Whitespace-only changes.

build-tools/detekt/detekt.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ complexity:
55
active: false
66
LongMethod:
77
ignoreAnnotated: 'Composable'
8+
NestedBlockDepth:
9+
threshold: 5
810

911
style:
1012
NewLineAtEndOfFile:

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ detekt = "1.23.6"
1414
espresso-core = "3.6.1"
1515
junit = "4.13.2"
1616
publish = "2.0.0"
17+
ui-test-junit4-android = "1.8.2"
1718

1819
[libraries]
1920
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
@@ -24,6 +25,9 @@ espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref =
2425
espresso-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-junit" }
2526
junit = { module = "junit:junit", version.ref = "junit" }
2627
material = { module = "com.google.android.material:material", version.ref = "material" }
28+
androidx-test-runner = { module = "androidx.test:runner", version = "1.6.2" }
29+
androidx-ui-test-junit4-android = { group = "androidx.compose.ui", name = "ui-test-junit4-android", version.ref = "ui-test-junit4-android" }
30+
differ = "com.dropbox.differ:differ:0.3.0"
2731

2832
[plugins]
2933
android-application = { id = "com.android.application", version.ref = "agp" }

include-build/gradle-plugin/src/main/java/com/telefonica/loggerazzi/LoggerazziPlugin.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class LoggerazziPlugin @Inject constructor(
147147
file.delete()
148148
}
149149
}
150-
lastFile?.renameTo(File(this, "$key.txt"))
150+
151+
lastFile?.renameTo(File(this, key))
151152
}
152153
}
153154
}

loggerazzi/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ android {
3636
}
3737
}
3838

39+
kotlin {
40+
explicitApi()
41+
}
42+
3943
dependencies {
44+
api(libs.differ)
45+
4046
implementation(libs.core.ktx)
4147
implementation(libs.junit)
4248
implementation(libs.androidx.test.monitor)
49+
implementation(libs.androidx.test.runner)
50+
implementation(libs.androidx.ui.test.junit4.android)
4351
}
4452

4553
apply("${rootProject.projectDir}/mavencentral.gradle")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.telefonica.loggerazzi
2+
3+
import android.graphics.Bitmap
4+
import com.dropbox.differ.Color
5+
import com.dropbox.differ.Image
6+
import androidx.core.graphics.get
7+
8+
internal class BitmapImage(private val src: Bitmap) : Image {
9+
override val width: Int get() = src.width
10+
override val height: Int get() = src.height
11+
override fun getPixel(x: Int, y: Int): Color {
12+
try {
13+
return Color(src[x, y])
14+
} catch (e: IllegalArgumentException) {
15+
throw IllegalArgumentException("Can't request pixel {x = $x, y = $y} from image {width = $width, height = $height}", e)
16+
}
17+
}
18+
}

loggerazzi/src/main/java/com/telefonica/loggerazzi/IgnoreLoggerazzi.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.telefonica.loggerazzi
2+
3+
4+
@Retention(AnnotationRetention.RUNTIME)
5+
@Target(AnnotationTarget.FUNCTION)
6+
public annotation class IgnoreLogs
7+
8+
@Retention(AnnotationRetention.RUNTIME)
9+
@Target(AnnotationTarget.FUNCTION)
10+
public annotation class IgnoreScreenshots

0 commit comments

Comments
 (0)