From b33289437c18c4fa548309fa4c8d54b6ba966256 Mon Sep 17 00:00:00 2001 From: Ryan Harter Date: Tue, 21 May 2024 15:11:16 -0500 Subject: [PATCH] Creates EmulatorConfigRule to configure the emulator for full screen screenshots. --- .github/scripts/emu_setup.sh | 13 +----- dropshots/build.gradle.kts | 1 + .../com/dropbox/dropshots/DropshotsTest.kt | 3 ++ .../dropbox/dropshots/EmulatorConfigRule.kt | 42 +++++++++++++++++++ gradle/libs.versions.toml | 1 + 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 dropshots/src/androidTest/kotlin/com/dropbox/dropshots/EmulatorConfigRule.kt diff --git a/.github/scripts/emu_setup.sh b/.github/scripts/emu_setup.sh index a416d22..ead90f1 100755 --- a/.github/scripts/emu_setup.sh +++ b/.github/scripts/emu_setup.sh @@ -1,16 +1,7 @@ #!/bin/bash +adb wait-for-devices + echo "Cleaning up old emulator data" adb uninstall com.dropbox.dropshots.test || true adb shell rm -rf /storage/emulated/0/Download/screenshots || true - -adb wait-for-devices -adb shell settings put global sysui_demo_allowed 1 -adb shell am broadcast -a com.android.systemui.demo -e command enter -adb shell am broadcast -a com.android.systemui.demo -e command clock -e hhmm 1234 -adb shell am broadcast -a com.android.systemui.demo -e command battery -e plugged false -adb shell am broadcast -a com.android.systemui.demo -e command battery -e level 100 -adb shell am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4 -adb shell am broadcast -a com.android.systemui.demo -e command network -e mobile show -e datatype none -e level 4 -adb shell am broadcast -a com.android.systemui.demo -e command notifications -e visible false -adb shell cmd overlay enable com.android.internal.systemui.navbar.gestural diff --git a/dropshots/build.gradle.kts b/dropshots/build.gradle.kts index ec3c523..8b8d220 100644 --- a/dropshots/build.gradle.kts +++ b/dropshots/build.gradle.kts @@ -55,6 +55,7 @@ dependencies { androidTestImplementation(libs.androidx.test.ext.junit) androidTestImplementation(libs.androidx.test.rules) androidTestImplementation(libs.androidx.test.runner) + androidTestImplementation(libs.androidx.test.uiautomator) } mavenPublishing { diff --git a/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/DropshotsTest.kt b/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/DropshotsTest.kt index f25772f..bab0ac4 100644 --- a/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/DropshotsTest.kt +++ b/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/DropshotsTest.kt @@ -18,6 +18,9 @@ class DropshotsTest { private val fakeValidator = FakeResultValidator() private val isRecordingScreenshots = isRecordingScreenshots() + @get:Rule + val emulatorConfigRule = EmulatorConfigRule() + @get:Rule val activityScenarioRule = ActivityScenarioRule(TestActivity::class.java) diff --git a/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/EmulatorConfigRule.kt b/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/EmulatorConfigRule.kt new file mode 100644 index 0000000..fb21b45 --- /dev/null +++ b/dropshots/src/androidTest/kotlin/com/dropbox/dropshots/EmulatorConfigRule.kt @@ -0,0 +1,42 @@ +package com.dropbox.dropshots + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement + +class EmulatorConfigRule : TestRule { + override fun apply(base: Statement, description: Description): Statement = + EmulatorConfigStatement(base) + + private class EmulatorConfigStatement( + private val base: Statement, + ) : Statement() { + override fun evaluate() { + enableDemoMode() + base.evaluate() + disableDemoMode() + } + + private fun enableDemoMode() { + UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + .apply { + executeShellCommand("cmd overlay enable com.android.internal.systemui.navbar.gestural") + executeShellCommand("settings put global sysui_demo_allowed 1") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command enter") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command clock -e hhmm 1234") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command battery -e plugged false") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command battery -e level 100") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command network -e mobile show -e datatype none -e level 4") + executeShellCommand("am broadcast -a com.android.systemui.demo -e command notifications -e visible false") + } + } + + private fun disableDemoMode() { + UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + .executeShellCommand("am broadcast -a com.android.systemui.demo -e command exit") + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index dc7ab9b..c1420ba 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,6 +15,7 @@ androidx-test-core = { module = "androidx.test:core-ktx", version = "1.5.0" } androidx-test-ext-junit = { module = "androidx.test.ext:junit-ktx", version = "1.1.5" } androidx-test-rules = { module = "androidx.test:rules", version = "1.5.0" } androidx-test-runner = { module = "androidx.test:runner", version = "1.5.2" } +androidx-test-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version = "2.3.0" } differ = "com.dropbox.differ:differ:0.0.1-alpha1" junit = "junit:junit:4.12" truth = "com.google.truth:truth:1.1.3"