Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Espresso screenshot tests #625

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ dependencies {
implementation "androidx.viewpager2:viewpager2:1.1.0"
implementation 'com.github.bumptech.glide:glide:4.16.0'

androidTestImplementation "androidx.test.ext:junit-ktx:1.1.5"
androidTestUtil "androidx.test.services:test-services:1.4.2"
androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
androidTestImplementation "androidx.test:rules:1.5.0" // GrantPermissionRule
androidTestImplementation 'com.github.AppDevNext:Moka:1.6'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'com.github.AppDevNext:Moka:1.7'
androidTestImplementation "androidx.test.ext:junit-ktx:1.2.1"
androidTestImplementation "com.github.AppDevNext.Logcat:LogcatCoreLib:3.3.1"
androidTestUtil "androidx.test.services:test-services:1.5.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.6.1"
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
}
89 changes: 39 additions & 50 deletions app/src/androidTest/java/info/touchimage/demo/MainSmokeTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package info.touchimage.demo

import android.graphics.Bitmap
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.captureToBitmap
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.moka.lib.assertions.WaitingAssertion
Expand Down Expand Up @@ -37,111 +38,99 @@ class MainSmokeTest {

@Test
fun smokeTestSimplyStart() {
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testSingleTouch() {
Espresso.onView(withId(R.id.single_touchimageview_button)).perform(ViewActions.click())
onView(withId(R.id.single_touchimageview_button)).perform(ViewActions.click())
Intents.intended(hasComponent(SingleTouchImageViewActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testViewPager() {
Espresso.onView(withId(R.id.viewpager_example_button)).perform(ViewActions.click())
onView(withId(R.id.viewpager_example_button)).perform(ViewActions.click())
Intents.intended(hasComponent(ViewPagerExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testView2Pager() {
Espresso.onView(withId(R.id.viewpager2_example_button)).perform(ViewActions.click())
onView(withId(R.id.viewpager2_example_button)).perform(ViewActions.click())
Intents.intended(hasComponent(ViewPager2ExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testMirroring() {
Espresso.onView(withId(R.id.mirror_touchimageview_button)).perform(ViewActions.click())
onView(withId(R.id.mirror_touchimageview_button)).perform(ViewActions.click())
Intents.intended(hasComponent(MirroringExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testSwitchImage() {
Espresso.onView(withId(R.id.switch_image_button)).perform(ViewActions.click())
onView(withId(R.id.switch_image_button)).perform(ViewActions.click())
Intents.intended(hasComponent(SwitchImageExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testSwitchScale() {
Espresso.onView(withId(R.id.switch_scaletype_button)).perform(ViewActions.click())
onView(withId(R.id.switch_scaletype_button)).perform(ViewActions.click())
Intents.intended(hasComponent(SwitchScaleTypeExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testChangeSize() {
Espresso.onView(withId(R.id.resize_button)).perform(ViewActions.click())
onView(withId(R.id.resize_button)).perform(ViewActions.click())
Intents.intended(hasComponent(ChangeSizeExampleActivity::class.java.name))
Thread.sleep(500)
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testRecycler() {
Espresso.onView(withId(R.id.recycler_button)).perform(ViewActions.click())
onView(withId(R.id.recycler_button)).perform(ViewActions.click())
Intents.intended(hasComponent(RecyclerExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testAnimateZoom() {
Espresso.onView(withId(R.id.animate_button)).perform(ViewActions.click())
onView(withId(R.id.animate_button)).perform(ViewActions.click())
Intents.intended(hasComponent(AnimateZoomActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun testGlide() {
Espresso.onView(withId(R.id.glide_button)).perform(ViewActions.click())
onView(withId(R.id.glide_button)).perform(ViewActions.click())
Intents.intended(hasComponent(GlideExampleActivity::class.java.name))

WaitingAssertion.checkAssertion(R.id.textLoaded, isDisplayed(), 1500)
Espresso.onView(withId(R.id.textLoaded)).check( matches(withText(containsString(" ms"))))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(withId(R.id.textLoaded)).check( matches(withText(containsString(" ms"))))
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}

@Test
fun makeScreenshotOfShapedImage() {
Espresso.onView(withId(R.id.shaped_image_button)).perform(ViewActions.click())
onView(withId(R.id.shaped_image_button)).perform(ViewActions.click())
Intents.intended(hasComponent(ShapedExampleActivity::class.java.name))
Espresso.onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
onView(isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") })
}
}
17 changes: 8 additions & 9 deletions app/src/androidTest/java/info/touchimage/demo/TouchTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package info.touchimage.demo

import android.graphics.Bitmap
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.captureToBitmap
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 info.touchimage.demo.utils.MultiTouchDownEvent
Expand All @@ -27,28 +28,26 @@ class TouchTest {
@Test
fun testSingleTouch() {
onView(withId(R.id.imageSingle)).perform(TouchAction(4f, 8f))
onView(withId(R.id.imageSingle)).captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-touch1")
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-touch1") })
onView(withId(R.id.imageSingle)).perform(TouchAction(40f, 80f))
Thread.sleep(300)
onView(withId(R.id.imageSingle)).captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-touch2")
onView(ViewMatchers.isRoot())
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-touch2") })
}

@Test
@Ignore("It is flaky")
fun testMultiTouch() {
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-before")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-before") })
val touchList: Array<Pair<Float, Float>> = listOf(
Pair(4f, 8f),
Pair(40f, 80f),
Pair(30f, 70f)
).toTypedArray()
onView(withId(R.id.imageSingle)).perform(MultiTouchDownEvent(touchList))
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-after")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-after") })
}
}
15 changes: 6 additions & 9 deletions app/src/androidTest/java/info/touchimage/demo/ZoomTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package info.touchimage.demo

import android.graphics.Bitmap
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.captureToBitmap
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
Expand All @@ -26,23 +27,19 @@ class ZoomTest {
fun zoom() {
Thread.sleep(WAIT)
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1-init")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1-init") })
onView(withId(R.id.current_zoom)).perform(ViewActions.click())
Thread.sleep(WAIT)
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2-reset")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2-reset") })
onView(withId(R.id.current_zoom)).perform(ViewActions.click())
Thread.sleep(WAIT)
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3-zoom")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3-zoom") })
onView(withId(R.id.current_zoom)).perform(ViewActions.click())
Thread.sleep(WAIT)
onView(ViewMatchers.isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-4-end")
.perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-4-end") })
}

companion object {
Expand Down
65 changes: 64 additions & 1 deletion screenShotCompare.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
#!/bin/zsh

diffFiles=./screenshotDiffs
mkdir $diffFiles
#cp app/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator\(AVD\)\ -\ 9/* screenshotsToCompare
#cp MPChartExample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator\(AVD\)\ -\ 9/* screenshotsToCompare
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

OS="`uname`"
case $OS in
'Linux')
;;
'FreeBSD')
;;
'WindowsNT')
;;
'Darwin')
brew install jq
;;
'SunOS')
;;
'AIX') ;;
*) ;;
esac

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) + "|" + (.body | test("Screenshot differs:'$emulatorApi'.*") | tostring)' | grep "|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
pwd
body=""
COUNTER=0
ls -la

echo "=> 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="${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) <br/><br/>"
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 <br/><br/> $body \" }"
fi

popd 1>/dev/null

# set error when diffs are there
[ "$(ls -A $diffFiles)" ] && exit 1 || exit 0
18 changes: 18 additions & 0 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

## This file is intended to be sourced by other scripts

function err() {
echo >&2 "$@"
}

function curl_gh() {
if [[ -n "$CLASSIC_TOKEN" ]]; then
curl \
--silent \
--header "Authorization: token $CLASSIC_TOKEN" \
"$@"
else
err "WARNING: No CLASSIC_TOKEN found. Skipping API call"
fi
}
Loading