From 1dda0bf5852a20ec95aac47b0d18863079b6e58b Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Sun, 13 Oct 2024 19:59:54 +0200 Subject: [PATCH] Espresso test wit screenshot compare --- .github/workflows/pull-request.yml | 85 ++++++++++++++++++- .gitmodules | 3 + android/git-diff-image | 1 + android/mainSample/build.gradle | 8 ++ .../example/virosample/ExtendedMainTest.kt | 39 +++++++++ android/screenShotCompare.sh | 53 ++++++++++++ 6 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 android/git-diff-image create mode 100644 android/mainSample/src/androidTest/java/com/example/virosample/ExtendedMainTest.kt create mode 100755 android/screenShotCompare.sh diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index ffd835937..5b6c6d3a8 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -6,12 +6,40 @@ on: - main - develop pull_request: +env: + resourceRunID: ${{ github.run_id }}-${{ github.run_number }} jobs: androidBuild: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest ] + api: [ 34 ] + abi: [ x86_64 ] + emulatorApi: [ 14 ] + tag: [ 'google_apis' ] + # include: + # - os: macOS-latest + # abi: x86_64 + # api: 28 + # emulatorApi: [ 9 ] + # tag: 'default' steps: - - uses: actions/checkout@v4.2.0 + - name: kvm support + run: | + egrep -c '(vmx|svm)' /proc/cpuinfo + id + sudo adduser $USER kvm + sudo chown -R $USER /dev/kvm + id + - name: prepare + run: | + sudo apt-get update && sudo apt-get install -y exiftool imagemagick xdg-utils libimage-exiftool-perl zsh jq xorg + - uses: actions/checkout@v4 + with: + submodules: true - name: set up JDK uses: actions/setup-java@v4 with: @@ -37,6 +65,59 @@ jobs: ./android/viroreact/build/outputs/aar/*.aar ./android/viroar/build/outputs/aar/*.aar + - name: Install Android SDK + uses: hannesa2/action-android/install-sdk@0.1.16.7 + - name: Android Emulator test + uses: hannesa2/action-android/emulator-run-cmd@0.1.16.7 + with: + cmd: ./android/gradlew cAT --continue + api: ${{ matrix.api }} + tag: ${{ matrix.tag }} + abi: ${{ matrix.abi }} + cmdOptions: -noaudio -no-boot-anim -no-window -metrics-collection + bootTimeout: 720 + - uses: actions/upload-artifact@v4 + if: ${{ always() }} + with: + name: Viro-Espresso-${{ matrix.api }}-${{ matrix.abi }}-report-${{ matrix.emulatorApi }} + path: | + ./android/**/build/reports/androidTests/connected + ./android/**/build/outputs/androidTest-results/connected + - name: Archive screenshots ${{ matrix.emulatorApi }} + uses: actions/upload-artifact@v4 + if: ${{ always() }} + with: + name: Viro-Screenshots-${{ matrix.api }}-${{ matrix.abi }}-${{ matrix.emulatorApi }} + path: | + android/mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected + android/mainSample/build/outputs/androidTest-results/connected + - name: Compare screenshots + if: ${{ always() }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + emulatorApi: ${{ matrix.emulatorApi }} + run: | + ls -ls android/mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected + cp android/mainSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator-5554\ -\ android/${{ matrix.emulatorApi }}/* screenshotsToCompare${{ matrix.emulatorApi }} + export DISPLAY=:99 + sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & + echo ${{ env.resourceRunID }} + ./android/screenShotCompare.sh ${{ env.resourceRunID }} + - name: Archive screenshots diffs ${{ matrix.emulatorApi }} + if: ${{ always() }} + uses: actions/upload-artifact@v4 + with: + name: Viro-Screenshots-diffs-${{ matrix.emulatorApi }} + path: | + android/screenshotDiffs + android/screenshotsToCompare${{ matrix.emulatorApi }}/view-*.png + - name: Show git status ${{ matrix.emulatorApi }} + if: ${{ always() }} + run: | + git add android/screenshotsToCompare${{ matrix.emulatorApi }} + git status + [ "$(git status -s -uno)" ] && exit 1 || exit 0 + AndroidCheck: name: Check runs-on: ubuntu-latest diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..e5f89e6c2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "android/git-diff-image"] + path = android/git-diff-image + url = git@github.com:ewanmellor/git-diff-image.git diff --git a/android/git-diff-image b/android/git-diff-image new file mode 160000 index 000000000..f12098b2b --- /dev/null +++ b/android/git-diff-image @@ -0,0 +1 @@ +Subproject commit f12098b2b9b9f56f205f8e9ca8435796a0fdc1fc diff --git a/android/mainSample/build.gradle b/android/mainSample/build.gradle index 6f2194265..ac903c108 100644 --- a/android/mainSample/build.gradle +++ b/android/mainSample/build.gradle @@ -17,6 +17,9 @@ android { versionName getTag() buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\"" buildConfigField "String", 'VERSION', "\"" + getTag() + "\"" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunnerArguments useTestStorageService: 'true' } buildTypes { @@ -64,6 +67,11 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:2.0.0" implementation "androidx.core:core-ktx:1.8.0" + + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'com.github.AppDevNext:Moka:1.6' + androidTestImplementation "androidx.test.ext:junit-ktx:1.1.5" + androidTestUtil "androidx.test.services:test-services:1.4.2" } static def getTag() { diff --git a/android/mainSample/src/androidTest/java/com/example/virosample/ExtendedMainTest.kt b/android/mainSample/src/androidTest/java/com/example/virosample/ExtendedMainTest.kt new file mode 100644 index 000000000..af19bea61 --- /dev/null +++ b/android/mainSample/src/androidTest/java/com/example/virosample/ExtendedMainTest.kt @@ -0,0 +1,39 @@ +package com.example.virosample + +import android.view.Gravity +import androidx.test.core.graphics.writeToTestStorage +import androidx.test.espresso.Espresso +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.contrib.DrawerActions +import androidx.test.espresso.contrib.DrawerMatchers.isClosed +import androidx.test.espresso.matcher.ViewMatchers +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.espresso.screenshot.captureToBitmap +import androidx.test.ext.junit.rules.activityScenarioRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TestName +import org.junit.runner.RunWith + + +@RunWith(AndroidJUnit4::class) +class ExtendedMainTest { + + @get:Rule + var nameRule = TestName() + + @get:Rule + val activityScenarioRule = activityScenarioRule() + + @Test + fun mainSmokeTest() { + Espresso.onView(withId(R.id.drawer_layout)) + .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed. + .perform(DrawerActions.open()) + Espresso.onView(ViewMatchers.isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-End") + } + +} diff --git a/android/screenShotCompare.sh b/android/screenShotCompare.sh new file mode 100755 index 000000000..063213da5 --- /dev/null +++ b/android/screenShotCompare.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +diffFiles=./screenshotDiffs +mkdir $diffFiles +set -x +./git-diff-image/install.sh +GIT_DIFF_IMAGE_OUTPUT_DIR=$diffFiles git diff-image + +source scripts/lib.sh + +PR=$(echo "$GITHUB_REF_NAME" | sed "s/\// /" | awk '{print $1}') +echo pr=$PR +brew install jq + +echo "delete all old comments, starting with Screenshot differs:$emulatorApi" +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"|") +echo "comments=$comments" +echo "$oldComments" | while read comment; do + echo "delete comment=$comment" + curl_gh -X DELETE https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/comments/"$comment" +done + +pushd $diffFiles +body="" +COUNTER=0 +ls -la + +# ignore an error, when no files where found https://unix.stackexchange.com/a/723909/201876 +setopt no_nomatch +for f in *.png; do + if [[ ${f} == "*.png" ]] + then + echo "nothing found" + else + (( COUNTER++ )) + + newName="$1-${f}" + mv "${f}" "$newName" + echo "==> Uploaded screenshot $newName" + curl -i -F "file=@$newName" https://www.mxtracks.info/github + echo "==> Add screenshot comment $PR" + body="$body ${f}![screenshot](https://www.mxtracks.info/github/uploads/$newName)

" + fi +done + +if [ ! "$body" == "" ]; then + curl_gh -X POST https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/$PR/comments -d "{ \"body\" : \"Screenshot differs:$emulatorApi $COUNTER

$body \" }" +fi + +popd 1>/dev/null + +# set error when diffs are there +[ "$(ls -A $diffFiles)" ] && exit 1 || exit 0