Skip to content

Commit

Permalink
Merge pull request #51 from square/py/gh_workflow
Browse files Browse the repository at this point in the history
Reenable UI tests
  • Loading branch information
pyricau authored Sep 8, 2024
2 parents e553408 + fdd28e6 commit 4d8e46c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 42 deletions.
101 changes: 71 additions & 30 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -1,68 +1,109 @@
name: Android CI

on:
push:
branches: [ main ]
# Build on all pull requests, regardless of target.
pull_request:
push:
branches:
- main

jobs:
build:
strategy:
fail-fast: false
validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: set up JDK 1.8
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '8'
- name: Build with Gradle
run: ./gradlew build --stacktrace
- uses: actions/checkout@v3
- uses: gradle/wrapper-validation-action@v1

checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
java-version: 8
distribution: 'zulu'
- uses: gradle/gradle-build-action@v2
- name: Build project
run: ./gradlew build --stacktrace


instrumentation-tests:
name: Instrumentation tests
runs-on: macos-latest
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
# Allow tests to continue on other devices if they fail on one device.
fail-fast: false
matrix:
arch: [x86_64]
arch: [ x86_64 ]
target: [ google_apis ]
channel: [ stable ]
api-level:
- 21
- 23
- 26
# Disabled 26, running into https:/ReactiveCircus/android-emulator-runner/issues/385
# - 26
- 29
- 30

# Disabled 34: needs min target SDK 23, and upgrading AGP.
# - 34
steps:
- uses: actions/checkout@v3
- name: Set up JDK 1.8
uses: actions/setup-java@v3
- name: Enable KVM group perms
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
ls /dev/kvm
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: '8'

java-version: 11
distribution: 'zulu'
- uses: gradle/gradle-build-action@v2
- name: AVD cache
uses: actions/cache@v4
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-cached-${{ matrix.api-level }}-${{ matrix.os }}-${{ matrix.target }}
- name: Create AVD and generate snapshot for caching
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
target: ${{ matrix.target }}
arch: ${{ matrix.arch }}
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -no-snapshot-load
disable-animations: false
script: echo "Generated AVD snapshot for caching."
- name: Instrumentation Tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
target: google_apis
force-avd-creation: false
target: ${{ matrix.target }}
arch: ${{ matrix.arch }}
script: ./gradlew connectedCheck --no-build-cache --no-daemon --stacktrace
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -no-metrics -camera-back none -no-snapshot-save
script: |
touch emulator.log # create log file
chmod 777 emulator.log # allow writing to log file
adb logcat >> emulator.log & # pipe all logcat messages into log file as a background process
adb shell settings put global package_verifier_user_consent -1
./gradlew connectedCheck --no-build-cache --no-daemon --stacktrace
- name: Upload results
if: ${{ always() }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.api-level }}-${{ matrix.arch }}-instrumentation-test-results
path: ./**/build/reports/androidTests/connected/**
path: |
emulator.log
./**/build/reports/androidTests/connected/**
snapshot-deployment:
if: github.repository == 'square/papa' && github.event_name == 'push'
needs: [ build ] #, instrumentation-tests ] UI tests setup is currently broken.
needs: [ checks , instrumentation-tests ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
19 changes: 7 additions & 12 deletions papa/src/androidTest/java/papa/test/PerfMonitoringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,13 @@ class PerfMonitoringTest {

private fun dismissCheckForUpdates() {
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val checkForUpdate = uiDevice.wait(Until.hasObject(By.text("Check for update")), 500)
// null or boolean
if (checkForUpdate == true) {
val deprecationDialog = uiDevice.wait(
Until.findObject(
By.pkg("android").depth(0)
), 1000
)

check(deprecationDialog != null)

val okButton = deprecationDialog.findObject(By.text("OK"))
val deprecationDialog = uiDevice.wait(
Until.findObject(
By.pkg("android").depth(0)
), 500
)
if (deprecationDialog != null) {
val okButton = deprecationDialog.findObject(By.text("OK"))!!
okButton.click()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import androidx.test.espresso.Espresso
import androidx.test.espresso.base.DefaultFailureHandler
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.AndroidJUnitRunner
import androidx.test.uiautomator.UiDevice
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import radiography.Radiography
import radiography.ViewStateRenderers.DefaultsIncludingPii
import java.io.ByteArrayOutputStream
import java.io.StringReader

class PapaTestInstrumentationRunner : AndroidJUnitRunner() {

Expand All @@ -18,6 +23,66 @@ class PapaTestInstrumentationRunner : AndroidJUnitRunner() {
try {
defaultFailureHandler.handle(error, viewMatcher)
} catch (decoratedError: Throwable) {
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val os = ByteArrayOutputStream()
uiDevice.dumpWindowHierarchy(os)
val uiAutomatorWindowHierarchy = os.toString()
val factory = XmlPullParserFactory.newInstance()
val xpp = factory.newPullParser()
xpp.setInput(StringReader(uiAutomatorWindowHierarchy))
val result = StringBuilder()

var eventType = xpp.eventType
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
when (xpp.name) {
"hierarchy" -> {
result.appendLine(
"view hierarchy with screen rotation ${xpp.getAttributeValue(null, "rotation")}"
)
}
"node" -> {
val interestingAttributes = listOf(
"text", "resource-id", "checked", "enabled", "focused", "selected", "bounds",
"visible-to-user", "package"
)
val className = xpp.getAttributeValue(null, "class").substringAfterLast(".")
val attributes = (0 until xpp.attributeCount)
.asSequence()
.mapNotNull { index ->
val name = xpp.getAttributeName(index)
val value = xpp.getAttributeValue(index)
if (value.isNullOrBlank() || name !in interestingAttributes) {
return@mapNotNull null
}
when (name) {
"checked" -> if (value == "true") "checked" else null
"focused" -> if (value == "true") "focused" else null
"selected" -> if (value == "true") "selected" else null
"enabled" -> if (value == "true") null else "disabled"
"visible-to-user" -> if (value == "true") null else "invisible"
"text" -> "text:\"$value\""
"package" -> {
// Root view nodes have depth 2 (depth starts at 1 with the "hierarchy" node)
if (xpp.depth == 2) {
"app-package:$value"
} else {
null
}
}
"resource-id" -> "id:${value.substringAfter(":id/")}"
else -> "$name:$value"
}
}.toList().joinToString(separator = ", ")
result.append("")
.append(" ".repeat(xpp.depth - 2))
.appendLine("$className { $attributes }")
}
else -> error("Unexpected tag ${xpp.name}")
}
}
eventType = xpp.next()
}
val detailMessageField = Throwable::class.java.getDeclaredField("detailMessage")
val previouslyAccessible = detailMessageField.isAccessible
try {
Expand All @@ -30,6 +95,7 @@ class PapaTestInstrumentationRunner : AndroidJUnitRunner() {
val hierarchy = Radiography.scan(viewStateRenderers = DefaultsIncludingPii)
// Notice the plural: there's one view hierarchy per window.
message += "\nView hierarchies:\n$hierarchy"
message += "\nUI Automator window hierarchy:\n$result"
detailMessageField[decoratedError] = message
} finally {
detailMessageField.isAccessible = previouslyAccessible
Expand Down

0 comments on commit 4d8e46c

Please sign in to comment.