Skip to content

Commit 0c073ec

Browse files
committed
Espresso test with screenshot compare
1 parent a3721d3 commit 0c073ec

File tree

6 files changed

+192
-3
lines changed

6 files changed

+192
-3
lines changed

.github/workflows/pull-request.yml

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,45 @@ on:
66
- main
77
- develop
88
pull_request:
9+
env:
10+
resourceRunID: ${{ github.run_id }}-${{ github.run_number }}
911

1012
jobs:
1113
androidBuild:
12-
runs-on: ubuntu-latest
14+
runs-on: ${{ matrix.os }}
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
os: [ ubuntu-latest ]
19+
api: [ 34 ]
20+
abi: [ x86_64 ]
21+
emulatorApi: [ 14 ]
22+
tag: [ 'google_apis' ]
23+
# include:
24+
# - os: macOS-latest
25+
# abi: x86_64
26+
# api: 28
27+
# emulatorApi: [ 9 ]
28+
# tag: 'default'
1329
steps:
14-
- uses: actions/[email protected]
30+
- name: kvm support
31+
run: |
32+
egrep -c '(vmx|svm)' /proc/cpuinfo
33+
id
34+
sudo adduser $USER kvm
35+
sudo chown -R $USER /dev/kvm
36+
id
37+
- name: prepare
38+
run: |
39+
sudo apt-get update && sudo apt-get install -y exiftool imagemagick xdg-utils libimage-exiftool-perl zsh jq xorg
40+
# brew install exiftool imagemagick
41+
- uses: actions/checkout@v4
42+
with:
43+
submodules: true
1544
- name: set up JDK
1645
uses: actions/setup-java@v4
1746
with:
18-
distribution: "adopt"
47+
distribution: 'adopt'
1948
java-version: 17
2049

2150
- name: assemble ViroCore
@@ -36,6 +65,62 @@ jobs:
3665
./android/virocore/build/outputs/aar/*.aar
3766
./android/viroreact/build/outputs/aar/*.aar
3867
./android/viroar/build/outputs/aar/*.aar
68+
- name: Install Android SDK
69+
uses: hannesa2/action-android/[email protected]
70+
- name: Android Emulator test
71+
uses: hannesa2/action-android/[email protected]
72+
with:
73+
cmd: ./gradlew cAT --continue
74+
api: ${{ matrix.api }}
75+
tag: ${{ matrix.tag }}
76+
abi: ${{ matrix.abi }}
77+
cmdOptions: -noaudio -no-boot-anim -no-window -metrics-collection
78+
bootTimeout: 720
79+
- uses: actions/upload-artifact@v4
80+
if: ${{ always() }}
81+
with:
82+
name: Viro-Espresso-${{ matrix.api }}-${{ matrix.abi }}-report-${{ matrix.emulatorApi }}
83+
path: |
84+
./**/build/reports/androidTests/connected
85+
./**/build/outputs/androidTest-results/connected
86+
- name: Archive screenshots ${{ matrix.emulatorApi }}
87+
uses: actions/upload-artifact@v4
88+
if: ${{ always() }}
89+
with:
90+
name: Viro-Screenshots-${{ matrix.api }}-${{ matrix.abi }}-${{ matrix.emulatorApi }}
91+
path: |
92+
basicSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected
93+
basicSample/build/outputs/androidTest-results/connected
94+
mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected
95+
mainSample/build/outputs/androidTest-results/connected
96+
serviceLibrary/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected
97+
serviceLibrary/build/outputs/androidTest-results/connected
98+
- name: Compare screenshots
99+
if: ${{ always() }}
100+
env:
101+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102+
emulatorApi: ${{ matrix.emulatorApi }}
103+
run: |
104+
ls -ls mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected
105+
cp mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator-5554\ -\ ${{ matrix.emulatorApi }}/* screenshotsToCompare${{ matrix.emulatorApi }}
106+
export DISPLAY=:99
107+
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
108+
echo ${{ env.resourceRunID }}
109+
./screenShotCompare.sh ${{ env.resourceRunID }}
110+
- name: Archive screenshots diffs ${{ matrix.emulatorApi }}
111+
if: ${{ always() }}
112+
uses: actions/upload-artifact@v4
113+
with:
114+
name: Viro-Screenshots-diffs-${{ matrix.emulatorApi }}
115+
path: |
116+
screenshotDiffs
117+
screenshotsToCompare${{ matrix.emulatorApi }}/view-*.png
118+
- name: Show git status ${{ matrix.emulatorApi }}
119+
if: ${{ always() }}
120+
run: |
121+
git add screenshotsToCompare${{ matrix.emulatorApi }}
122+
git status
123+
[ "$(git status -s -uno)" ] && exit 1 || exit 0
39124
40125
AndroidCheck:
41126
name: Check

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "android/git-diff-image"]
2+
path = android/git-diff-image
3+
url = [email protected]:ewanmellor/git-diff-image.git

android/git-diff-image

Submodule git-diff-image added at f12098b

android/mainSample/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ android {
1717
versionName getTag()
1818
buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\""
1919
buildConfigField "String", 'VERSION', "\"" + getTag() + "\""
20+
21+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
22+
testInstrumentationRunnerArguments useTestStorageService: 'true'
2023
}
2124

2225
buildTypes {
@@ -64,6 +67,11 @@ dependencies {
6467

6568
implementation "org.jetbrains.kotlin:kotlin-stdlib:2.0.0"
6669
implementation "androidx.core:core-ktx:1.8.0"
70+
71+
testImplementation 'junit:junit:4.13.2'
72+
androidTestImplementation 'com.github.AppDevNext:Moka:1.6'
73+
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.5"
74+
androidTestUtil "androidx.test.services:test-services:1.4.2"
6775
}
6876

6977
static def getTag() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.virosample
2+
3+
import android.view.Gravity
4+
import androidx.test.core.graphics.writeToTestStorage
5+
import androidx.test.espresso.Espresso
6+
import androidx.test.espresso.assertion.ViewAssertions.matches
7+
import androidx.test.espresso.contrib.DrawerActions
8+
import androidx.test.espresso.contrib.DrawerMatchers.isClosed
9+
import androidx.test.espresso.matcher.ViewMatchers
10+
import androidx.test.espresso.matcher.ViewMatchers.withId
11+
import androidx.test.espresso.screenshot.captureToBitmap
12+
import androidx.test.ext.junit.rules.activityScenarioRule
13+
import androidx.test.ext.junit.runners.AndroidJUnit4
14+
import org.junit.Rule
15+
import org.junit.Test
16+
import org.junit.rules.TestName
17+
import org.junit.runner.RunWith
18+
19+
20+
@RunWith(AndroidJUnit4::class)
21+
class ExtendedMainTest {
22+
23+
@get:Rule
24+
var nameRule = TestName()
25+
26+
@get:Rule
27+
val activityScenarioRule = activityScenarioRule<MLMainActivity>()
28+
29+
@Test
30+
fun mainSmokeTest() {
31+
Espresso.onView(withId(R.id.drawer_layout))
32+
.check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
33+
.perform(DrawerActions.open())
34+
Espresso.onView(ViewMatchers.isRoot())
35+
.captureToBitmap()
36+
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-End")
37+
}
38+
39+
}

android/screenShotCompare.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
diffFiles=./screenshotDiffs
4+
mkdir $diffFiles
5+
set -x
6+
./git-diff-image/install.sh
7+
GIT_DIFF_IMAGE_OUTPUT_DIR=$diffFiles git diff-image
8+
9+
source scripts/lib.sh
10+
11+
PR=$(echo "$GITHUB_REF_NAME" | sed "s/\// /" | awk '{print $1}')
12+
echo pr=$PR
13+
brew install jq
14+
15+
echo "delete all old comments, starting with Screenshot differs:$emulatorApi"
16+
oldComments=$(curl_gh -X GET https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/"$PR"/comments | jq '.[] | (.id |tostring) + "|" + (.user.login | test("github-actions") | tostring) + "|" + (.body | test("Screenshot differs:'$emulatorApi'.*") | tostring)' | grep "true|true" | tr -d "\"" | cut -f1 -d"|")
17+
echo "comments=$comments"
18+
echo "$oldComments" | while read comment; do
19+
echo "delete comment=$comment"
20+
curl_gh -X DELETE https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/comments/"$comment"
21+
done
22+
23+
pushd $diffFiles
24+
body=""
25+
COUNTER=0
26+
ls -la
27+
28+
# ignore an error, when no files where found https://unix.stackexchange.com/a/723909/201876
29+
setopt no_nomatch
30+
for f in *.png; do
31+
if [[ ${f} == "*.png" ]]
32+
then
33+
echo "nothing found"
34+
else
35+
(( COUNTER++ ))
36+
37+
newName="$1-${f}"
38+
mv "${f}" "$newName"
39+
echo "==> Uploaded screenshot $newName"
40+
curl -i -F "file=@$newName" https://www.mxtracks.info/github
41+
echo "==> Add screenshot comment $PR"
42+
body="$body ${f}![screenshot](https://www.mxtracks.info/github/uploads/$newName) <br/><br/>"
43+
fi
44+
done
45+
46+
if [ ! "$body" == "" ]; then
47+
curl_gh -X POST https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/$PR/comments -d "{ \"body\" : \"Screenshot differs:$emulatorApi $COUNTER <br/><br/> $body \" }"
48+
fi
49+
50+
popd 1>/dev/null
51+
52+
# set error when diffs are there
53+
[ "$(ls -A $diffFiles)" ] && exit 1 || exit 0

0 commit comments

Comments
 (0)