Skip to content

Commit 8460fdc

Browse files
committed
[#70] Optimizes screenshot tests for faster execution
Speeds up screenshot tests by enabling AVD caching, KVM acceleration, and optimizing emulator boot parameters. Reduces wait times in tests and adjusts target system images for faster AVD creation. Increases concurrency for non-API 27 tests to improve overall efficiency.
1 parent 5c90222 commit 8460fdc

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

.github/workflows/dropshots-screenshot-test.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ on:
1212
env:
1313
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2 -Dkotlin.incremental=false
1414
JAVA_OPTS: -Xmx4g
15+
# KVM acceleration for faster emulator boot
16+
ANDROID_EMULATOR_USE_SYSTEM_LIBS: 1
17+
ANDROID_AVD_HOME: ~/.android/avd
1518

1619
jobs:
1720
# Detect changes to prevent unnecessary execution
@@ -57,6 +60,18 @@ jobs:
5760
restore-keys: |
5861
${{ runner.os }}-gradle-
5962
63+
# AVD snapshot caching for faster emulator boot
64+
- name: AVD cache
65+
uses: actions/cache@v4
66+
id: avd-cache
67+
with:
68+
path: |
69+
~/.android/avd/*
70+
~/.android/adb*
71+
key: avd-api27-${{ runner.os }}-${{ hashFiles('**/*.gradle*') }}
72+
restore-keys: |
73+
avd-api27-${{ runner.os }}-
74+
6075
- name: Setup Android SDK
6176
uses: android-actions/setup-android@v2
6277

@@ -68,14 +83,14 @@ jobs:
6883
uses: reactivecircus/android-emulator-runner@v2
6984
with:
7085
api-level: 27
71-
target: google_apis
86+
target: default
7287
arch: x86_64
7388
ram-size: 2048M
7489
heap-size: 512M
7590
force-avd-creation: false
76-
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -partition-size 2048 -accel off -no-snapshot -memory 2048
91+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -partition-size 2048 -accel on -no-snapshot-save -memory 2048 -cores 2 -netfast
7792
disable-animations: true
78-
emulator-boot-timeout: 1200
93+
emulator-boot-timeout: 600
7994
script: |
8095
# Set fixed screen resolution (for screenshot consistency)
8196
adb shell wm size 1080x1920
@@ -137,6 +152,7 @@ jobs:
137152
runs-on: ubuntu-latest
138153
strategy:
139154
fail-fast: false
155+
max-parallel: 2
140156
matrix:
141157
api-level: [30, 33] # API 27 excluded as it only runs on x86_64 environment
142158

@@ -161,6 +177,18 @@ jobs:
161177
restore-keys: |
162178
${{ runner.os }}-gradle-
163179
180+
# AVD snapshot caching for faster emulator boot
181+
- name: AVD cache
182+
uses: actions/cache@v4
183+
id: avd-cache
184+
with:
185+
path: |
186+
~/.android/avd/*
187+
~/.android/adb*
188+
key: avd-api-${{ matrix.api-level }}-${{ runner.os }}-${{ hashFiles('**/*.gradle*') }}
189+
restore-keys: |
190+
avd-api-${{ matrix.api-level }}-${{ runner.os }}-
191+
164192
- name: Setup Android SDK
165193
uses: android-actions/setup-android@v2
166194

@@ -172,14 +200,14 @@ jobs:
172200
uses: reactivecircus/android-emulator-runner@v2
173201
with:
174202
api-level: ${{ matrix.api-level }}
175-
target: google_apis
203+
target: ${{ matrix.api-level == 30 && 'google_apis' || 'default' }}
176204
arch: x86_64
177205
ram-size: 2048M
178206
heap-size: 512M
179207
force-avd-creation: false
180-
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -partition-size 2048 -accel off -no-snapshot -memory 2048
208+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -partition-size 2048 -accel on -no-snapshot-save -memory 2048 -cores 2 -netfast
181209
disable-animations: true
182-
emulator-boot-timeout: 1200
210+
emulator-boot-timeout: 600
183211
script: |
184212
# Set fixed screen resolution (for screenshot consistency)
185213
adb shell wm size 1080x1920

app/src/androidTest/kotlin/com/skydoves/cloudydemo/MainTest.kt

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class MainTest {
4242
@get:Rule
4343
val dropshots = Dropshots()
4444

45-
@Before
46-
fun setup() {
45+
@Before fun setup() {
4746
// Grant permissions for all API levels
4847
val context = InstrumentationRegistry.getInstrumentation().targetContext
4948
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
@@ -111,15 +110,15 @@ class MainTest {
111110
try {
112111
ActivityScenario.launch<MainActivity>(intent).use { scenario ->
113112
scenario.onActivity { activity ->
114-
// Wait for activity to be fully loaded and Compose to render
115-
// Use multiple shorter sleeps instead of one long sleep
116-
repeat(6) { i ->
117-
Thread.sleep(500)
118-
println("Waiting for Compose to render... (${i + 1}/6)")
113+
// Optimized wait for activity to be fully loaded and Compose to render
114+
// Reduced wait time for faster test execution
115+
repeat(3) { i ->
116+
Thread.sleep(300)
117+
println("Waiting for Compose to render... (${i + 1}/3)")
119118
}
120119

121-
// Additional wait for any background operations
122-
Thread.sleep(1000)
120+
// Reduced wait for background operations
121+
Thread.sleep(500)
123122

124123
// Capture screenshot with error handling
125124
try {
@@ -130,8 +129,8 @@ class MainTest {
130129
println("Successfully captured screenshot: cloudy_main_screen_$apiSuffix")
131130
} catch (e: Exception) {
132131
println("Error capturing screenshot: ${e.message}")
133-
// Try one more time after additional wait
134-
Thread.sleep(2000)
132+
// Try one more time after reduced wait
133+
Thread.sleep(1000)
135134
try {
136135
dropshots.assertSnapshot(
137136
view = activity.findViewById(android.R.id.content),
@@ -159,10 +158,10 @@ class MainTest {
159158
try {
160159
ActivityScenario.launch<MainActivity>(intent).use { scenario ->
161160
scenario.onActivity { activity ->
162-
// Wait for initial state (no blur) with progressive waiting
163-
repeat(4) { i ->
164-
Thread.sleep(500)
165-
println("Waiting for initial state... (${i + 1}/4)")
161+
// Optimized wait for initial state (no blur)
162+
repeat(2) { i ->
163+
Thread.sleep(300)
164+
println("Waiting for initial state... (${i + 1}/2)")
166165
}
167166

168167
try {
@@ -176,10 +175,10 @@ class MainTest {
176175
throw e
177176
}
178177

179-
// Wait for animation to complete (full blur) with progressive waiting
180-
repeat(6) { i ->
181-
Thread.sleep(500)
182-
println("Waiting for blur animation... (${i + 1}/6)")
178+
// Optimized wait for blur animation
179+
repeat(3) { i ->
180+
Thread.sleep(400)
181+
println("Waiting for blur animation... (${i + 1}/3)")
183182
}
184183

185184
try {
@@ -208,10 +207,10 @@ class MainTest {
208207
try {
209208
ActivityScenario.launch<MainActivity>(intent).use { scenario ->
210209
scenario.onActivity { activity ->
211-
// Initial state with progressive waiting
212-
repeat(4) { i ->
213-
Thread.sleep(500)
214-
println("Waiting for animation start... (${i + 1}/4)")
210+
// Optimized wait for animation start
211+
repeat(2) { i ->
212+
Thread.sleep(300)
213+
println("Waiting for animation start... (${i + 1}/2)")
215214
}
216215

217216
try {
@@ -225,10 +224,10 @@ class MainTest {
225224
throw e
226225
}
227226

228-
// Final state after animation with progressive waiting
229-
repeat(6) { i ->
230-
Thread.sleep(500)
231-
println("Waiting for animation end... (${i + 1}/6)")
227+
// Optimized wait for animation end
228+
repeat(3) { i ->
229+
Thread.sleep(400)
230+
println("Waiting for animation end... (${i + 1}/3)")
232231
}
233232

234233
try {

scripts/run-screenshot-tests-api27.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ create_avd() {
228228
# Use x86_64 architecture for API 27
229229
local arch="x86_64"
230230

231-
# Use default target for faster boot times
231+
# Use default target for API 27 (google_apis may not be available)
232232
local system_image="system-images;android-${API_LEVEL};default;${arch}"
233233

234234
# Download SDK image (if needed)

scripts/run-screenshot-tests.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,10 @@ create_avd() {
230230
local system_image=""
231231
if [[ $api_level -eq 27 ]]; then
232232
system_image="system-images;android-${api_level};default;${arch}"
233+
elif [[ $api_level -eq 30 ]]; then
234+
system_image="system-images;android-${api_level};google_apis;${arch}"
233235
else
234-
# Use default target for faster boot times on API 30 and 33
236+
# Use default target for API 33 and others
235237
system_image="system-images;android-${api_level};default;${arch}"
236238
fi
237239

0 commit comments

Comments
 (0)