Skip to content

Commit 09e7d10

Browse files
Merge pull request #16233 from PhilLab/ph/screenshot_tests_tooling
Screenshot tests tooling
2 parents c4d080e + e1cff4e commit 09e7d10

5 files changed

Lines changed: 60 additions & 16 deletions

File tree

.github/workflows/screenShotTest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
echo "org.gradle.configureondemand=true" >> $HOME/.gradle/gradle.properties
7676
echo "kapt.incremental.apt=true" >> $HOME/.gradle/gradle.properties
7777
78-
- name: Build gplay
78+
- name: Build generic flavor
7979
run: ./gradlew assembleGenericDebug
8080

8181
- name: Delete old comments

CONTRIBUTING.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ Source code of app:
231231

232232

233233
#### UI tests
234-
We use [shot](https://github.com/Karumi/Shot) for taking screenshots and compare them
235-
- check screenshots: ```./gradlew gplayDebugExecuteScreenshotTests ```
234+
We use [shot](https://github.com/Karumi/Shot) for taking screenshots and compare them.
235+
To exclude the shot dependency from normal builds, the dependency needs to be activated via an environment variable `SHOT_TEST`.
236+
For convenience, this and other prerequisites are encapsulated in utility scripts, so it is advised to use them
237+
- check screenshots: ```scripts/androidScreenshotTest ```
238+
- check the script for a detailed documentation of the parameters
236239
- update/generate new screenshots: ```scripts/updateScreenshots.sh ```
237240
- in this script are samples how to only execute a given class/test
238241
- this will fire up docker & emulator to ensure that screenshots look the same

scripts/androidScreenshotTest

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
#!/bin/bash
22
#
3-
# SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
3+
# SPDX-FileCopyrightText: 2020-2026 Nextcloud GmbH and Nextcloud contributors
4+
# SPDX-FileCopyrightText: 2026 Philipp Hasper <vcs@hasper.info>
45
# SPDX-FileCopyrightText: 2020-2024 Tobias Kaminsky <tobias@kaminsky.me>
56
# SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
67
#
8+
# Run instrumentation screenshot tests (record or compare) for a given test class/method.
9+
#
10+
# Usage:
11+
# ./scripts/androidScreenshotTest <record:true|false> <class-pattern> [method] [darkMode: dark|light|all] [color]
12+
#
13+
# Arguments:
14+
# record "true" to record/update reference screenshots (-Precord), "false" to compare.
15+
# class name the first matching .java or .kt file under app/src/androidTest/java is passed to the instrumentation runner
16+
# method name optional test method name to run (appends #method to class). If not given, the entire class's tests run
17+
# darkMode optional: dark | light | all (if "all", the script runs scripts/runAllScreenshotCombinations).
18+
# color optional value passed as COLOR to the instrumentation runner, for testing different color themes
19+
#
20+
# Behavior notes:
21+
# - Temporarily sets is_beta to true in app/src/main/res/values/setup.xml and restores it afterwards.
22+
# - Searches for or launches an emulator AVD named "uiComparison" and sets ANDROID_SERIAL for the run.
23+
# - Requires adb and emulator on PATH and the repo's ./gradlew wrapper.
24+
# - Caution: If interrupted the setup.xml change may remain; revert with:
25+
# git checkout -- app/src/main/res/values/setup.xml
26+
#
27+
# Examples:
28+
# ./scripts/androidScreenshotTest false MyScreenshotTest
29+
# ./scripts/androidScreenshotTest true MyScreenshotTest testCaptureAll all
30+
# ./scripts/androidScreenshotTest false MyScreenshotTest "" dark red
31+
# END_DOCUMENTATION
732
set -e
833

934
if [ $# -lt 2 ]; then
10-
echo "1: record: true/false
11-
2: class name
12-
3: method name
13-
4: darkMode: dark/light / \"all\" to run all screenshot combinations
14-
5: color"
15-
35+
# Print the documentation from the file header, up until END_DOCUMENTATION, excluding specific lines
36+
sed -n '2,/^#[[:space:]]*END_DOCUMENTATION/ {
37+
/^#[[:space:]]*SPDX-FileCopyrightText/ d
38+
/^#[[:space:]]*SPDX-License-Identifier/ d
39+
/^#[[:space:]]*END_DOCUMENTATION/ d
40+
s/^#//
41+
p
42+
}' "$0" | sed '/^$/d'
1643
exit
1744
fi
1845

@@ -65,6 +92,20 @@ while read line ; do
6592
done < <(adb devices | cut -f1)
6693

6794
if [ "$emulatorIsRunning" == false ] ; then
95+
if [ -z "$(command -v emulator || true)" ]; then
96+
echo "emulator not found in PATH; typically located in Android/sdk/emulator" >&2
97+
exit 1
98+
fi
99+
if [ -z "$(command -v avdmanager || true)" ]; then
100+
echo "avdmanager not found in PATH; typically located in Android/sdk/cmdline-tools/latest/bin" >&2
101+
exit 1
102+
fi
103+
# If emulator of expected name doesn't exist, create it
104+
# TODO - this was copied from updateScreenshots.sh - move it to a common helper script
105+
if [[ $(emulator -list-avds | grep uiComparison -c) -eq 0 ]]; then
106+
(sleep 5; echo "no") | avdmanager create avd -n uiComparison -c 100M -k "system-images;android-28;google_apis;x86" --abi "google_apis/x86"
107+
fi
108+
68109
"$(command -v emulator)" -writable-system -avd uiComparison -no-snapshot -gpu swiftshader_indirect -no-audio -skin 500x833 &
69110
sleep 20
70111
fi

scripts/runAllScreenshotCombinations

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ do
2828
echo -n "Run $color on $darkMode mode"
2929

3030
if [[ $1 = "noCI" ]]; then
31-
./gradlew --console plain gplayDebugExecuteScreenshotTests \
31+
./gradlew --console plain genericDebugExecuteScreenshotTests \
3232
$record \
3333
-Pscreenshot=true \
3434
-Pandroid.testInstrumentationRunnerArguments.annotation=com.owncloud.android.utils.ScreenshotTest \
@@ -41,7 +41,7 @@ do
4141
echo
4242
fi
4343
else
44-
./gradlew --console plain gplayDebugExecuteScreenshotTests \
44+
./gradlew --console plain genericDebugExecuteScreenshotTests \
4545
$record \
4646
-Pandroid.testInstrumentationRunnerArguments.annotation=com.owncloud.android.utils.ScreenshotTest \
4747
-Pandroid.testInstrumentationRunnerArguments.COLOR="$color" \

scripts/updateScreenshots.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ adb shell "echo $IP server >> /system/etc/hosts"
7777
sed -i s'#<bool name="is_beta">false</bool>#<bool name="is_beta">true</bool>#'g app/src/main/res/values/setup.xml
7878

7979
## update/create all screenshots
80-
#./gradlew gplayDebugExecuteScreenshotTests -Precord \
80+
#./gradlew genericDebugExecuteScreenshotTests -Precord \
8181
#-Pandroid.testInstrumentationRunnerArguments.annotation=com.owncloud.android.utils.ScreenshotTest
8282

8383
## update screenshots in a class
84-
#./gradlew gplayDebugExecuteScreenshotTests \
84+
#./gradlew genericDebugExecuteScreenshotTests \
8585
#-Precord \
8686
#-Pandroid.testInstrumentationRunnerArguments.class=\
8787
#com.owncloud.android.ui.dialog.SyncFileNotEnoughSpaceDialogFragmentTest
8888

8989
## update single screenshot within a class
90-
#./gradlew gplayDebugExecuteScreenshotTests \
90+
#./gradlew genericDebugExecuteScreenshotTests \
9191
#-Precord \
9292
#-Pandroid.testInstrumentationRunnerArguments.class=\
9393
#com.nextcloud.client.FileDisplayActivityIT#showShares
@@ -97,7 +97,7 @@ retryCount=0
9797
until [ $resultCode -eq 0 ] || [ $retryCount -gt 2 ]
9898
do
9999
# test all screenshots
100-
./gradlew gplayDebugExecuteScreenshotTests \
100+
./gradlew genericDebugExecuteScreenshotTests \
101101
-Pandroid.testInstrumentationRunnerArguments.annotation=com.owncloud.android.utils.ScreenshotTest
102102

103103
resultCode=$?

0 commit comments

Comments
 (0)