Skip to content

Commit

Permalink
Automated screenshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
FliegendeWurst committed Jan 16, 2025
1 parent cda15d3 commit 50ac029
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ There is also some preliminary support for scripting, but it doesn't work exactl

Please report bugs and missing features. I will soon setup a way to contribute translations. Any coding help is also welcome!

### Testing

- Gradle action `pixel9api35DebugAndroidTest`
- Scripts `copy-test-images.sh`, `compare-test-images.sh`

## License

Copyright © 2023 Arne Keller
Expand Down
15 changes: 15 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
versionName "0.90.12-alpha2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments useTestStorageService: "true"

buildConfigField "long", "TIMESTAMP", "1734200570000L"
}
Expand Down Expand Up @@ -48,6 +49,17 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
testOptions {
managedDevices {
localDevices {
create("pixel9api35") {
device = "Pixel 9"
apiLevel = 35
systemImageSource = "aosp-atd"
}
}
}
}
namespace 'eu.fliegendewurst.triliumdroid'
}

Expand All @@ -68,4 +80,7 @@ dependencies {
implementation "org.wordpress:aztec:v2.1.4"

testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test.ext:junit-ktx:1.2.1"
androidTestImplementation "androidx.test.espresso:espresso-core:3.6.1"
androidTestUtil "androidx.test.services:test-services:1.5.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package eu.fliegendewurst.triliumdroid

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.captureToBitmap
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.pressBack
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.matcher.ViewMatchers
import eu.fliegendewurst.triliumdroid.activity.main.MainActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException

import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.FixMethodOrder
import org.junit.rules.TestName
import org.junit.runners.MethodSorters

@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class InitialSyncTest {

@get:Rule
var nameRule = TestName()

@get:Rule
val activityScenarioRule = activityScenarioRule<MainActivity>()

@Test
@Throws(IOException::class)
fun test_010_initialSync() {
var index = 1
onView(ViewMatchers.withId(R.id.button_setup_sync))
.perform(click())
onView(ViewMatchers.withId(R.id.server))
.perform(typeText("http://10.0.2.2:12391"))
onView(ViewMatchers.withId(R.id.password))
.perform(typeText("1234"))
Espresso.closeSoftKeyboard()
Espresso.pressBack()
Thread.sleep(10000) // wait for Sync to finish
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}_${index++}") })
onView(ViewMatchers.withText("Trilium Demo"))
.perform(click())
Thread.sleep(1000) // wait for WebView to load
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}_${index++}") })
}

@Test
@Throws(IOException::class)
fun test_015_renameDialog() {
var index = 1
// click to open rename dialog
onView(ViewMatchers.withId(R.id.toolbar_title))
.perform(click())
// wait for WebView to load
Thread.sleep(1000)
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}_${index++}") })
onView(ViewMatchers.withId(R.id.note_title))
.perform(typeText(" Title Edited!"))
onView(ViewMatchers.withId(R.id.button_rename_note))
.perform(click())
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}_${index++}") })
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/kotlin/eu/fliegendewurst/triliumdroid/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,12 @@ object Cache {
}
val keys = entity.keys().asSequence().toList()

if (entityName == "notes") {
notes[entityName]?.title = "INVALID"
notes[entityName]?.content = null
notes.remove(entityName)
}

val cv = ContentValues(entity.length())
keys.map { fieldName ->
val x = entity.get(fieldName)
Expand Down
14 changes: 14 additions & 0 deletions compare-test-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# Use this script to compare current results against previously "blessed" results.

for x in app/src/androidTest/res/screenshots/*; do
f=$(basename "$x")
[ ! -e "app/build/outputs/managed_device_android_test_additional_output/debug/pixel9api35/${f}scaled.png" ] \
&& magick "app/build/outputs/managed_device_android_test_additional_output/debug/pixel9api35/$f" \
-scale 25% "app/build/outputs/managed_device_android_test_additional_output/debug/pixel9api35/${f}scaled.png"
echo -n "comparing $f.. "
magick compare -metric mae "$x" \
"app/build/outputs/managed_device_android_test_additional_output/debug/pixel9api35/${f}scaled.png" "/tmp/diff_$f"
echo
done
10 changes: 10 additions & 0 deletions copy-test-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

# Use this script to "bless" the current screenshot results.

rm app/src/androidTest/res/screenshots/* 2>/dev/null
cp app/build/outputs/managed_device_android_test_additional_output/debug/pixel9api35/* app/src/androidTest/res/screenshots/
for x in app/src/androidTest/res/screenshots/*; do
magick "$x" -scale 25% "${x}scaled.png"
mv "${x}scaled.png" "$x"
done

0 comments on commit 50ac029

Please sign in to comment.