-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-emulator-modes.sh
More file actions
executable file
·193 lines (159 loc) · 5.51 KB
/
test-emulator-modes.sh
File metadata and controls
executable file
·193 lines (159 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
# Test emulator pure mode vs normal mode behavior
# Tests the logic for reusing vs creating fresh emulators
set -euo pipefail
# Setup
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$SCRIPT_DIR/../test-framework.sh"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
cd "$REPO_ROOT/examples/android"
if [ ! -d ".devbox/virtenv/android" ]; then
echo "ERROR: Android virtenv not found. Run 'devbox shell' first."
exit 1
fi
. .devbox/virtenv/android/scripts/init/setup.sh
. .devbox/virtenv/android/scripts/domain/emulator.sh
echo "========================================"
echo "Emulator Mode Behavior Tests"
echo "========================================"
echo ""
echo "This test demonstrates the difference between:"
echo " 1. Normal mode: Reuses existing emulator if AVD matches"
echo " 2. Pure mode: Always creates fresh emulator with clean state"
echo ""
# Check current emulator state
echo "Current State:"
running_count=$(adb devices 2>/dev/null | grep -c "emulator-" | tr -d '\n' || echo "0")
echo " Running emulators: $running_count"
if [ "$running_count" -gt 0 ]; then
echo " Emulator details:"
android_list_running_emulators | while IFS=: read -r serial avd; do
echo " - $serial: $avd"
done
fi
echo ""
# Test 1: Normal mode behavior
log_test "Normal Mode (Reuse existing)"
echo ""
echo "Scenario: Running 'android.sh emulator start max' (no --pure flag)"
echo ""
echo "Expected behavior:"
echo " - If emulator with matching AVD exists: Reuse it"
echo " - If no matching emulator: Start new one"
echo " - Emulator persists after script exits"
echo ""
# Check if max AVD exists
if [ "$running_count" -gt 0 ]; then
assert_success "true" "Emulator already running - normal mode would reuse it"
echo ""
echo "Test reuse detection:"
first_serial=$(adb devices | awk 'NR>1 && $1 ~ /^emulator-/ && $2=="device" {print $1; exit}')
avd_name=$(adb -s "$first_serial" shell getprop ro.boot.qemu.avd_name 2>/dev/null | tr -d '\r')
echo " Existing: $first_serial ($avd_name)"
found=$(android_find_running_emulator "$avd_name" || echo "")
assert_equal "$first_serial" "$found" "android_find_running_emulator correctly finds emulator"
else
echo "No emulators running - normal mode would start new one"
echo " (Run 'devbox run start:emu' to test reuse behavior)"
fi
echo ""
# Test 2: Pure mode behavior
log_test "Pure Mode (Fresh instance)"
echo ""
echo "Scenario: Running 'android.sh emulator start --pure max'"
echo ""
echo "Expected behavior:"
echo " - Always starts fresh emulator with --wipe-data flag"
echo " - Ignores any existing emulators"
echo " - Creates clean state for deterministic testing"
echo " - Should be stopped after test completes (in e2e tests)"
echo ""
echo "Pure mode flag:"
echo " export ANDROID_EMULATOR_PURE=1"
echo " This triggers --wipe-data flag in emulator command"
echo ""
# Test 3: AVD matching logic
log_test "AVD Matching Logic"
echo ""
echo "How emulators are matched to AVDs:"
echo ""
echo "1. Query running emulators:"
echo " adb devices | grep emulator-"
echo ""
echo "2. For each emulator, get AVD name:"
echo " adb shell getprop ro.boot.qemu.avd_name"
echo ""
echo "3. Compare with target AVD:"
echo " if [ \"\$running_avd\" = \"\$target_avd\" ]; then"
echo " # Reuse this emulator"
echo " fi"
echo ""
if [ "$running_count" -gt 0 ]; then
echo "Current AVD mapping:"
android_list_running_emulators | while IFS=: read -r serial avd; do
state=$(adb devices | grep "$serial" | awk '{print $2}')
echo " $serial -> $avd ($state)"
done
else
echo " (No emulators running to demonstrate)"
fi
echo ""
# Test 4: Serial file tracking
log_test "Serial File Tracking"
echo ""
echo "Serial (emulator-5554) is the standard adb identifier because:"
echo " - Unique per emulator instance"
echo " - Required for all adb commands: adb -s emulator-5554 shell ..."
echo " - Stable for the lifetime of the emulator process"
echo " - Not tied to PID (which can be unreliable)"
echo ""
echo "Serial file: /tmp/android-emulator-serial.txt"
if [ -f /tmp/android-emulator-serial.txt ]; then
serial=$(cat /tmp/android-emulator-serial.txt)
echo " File exists: $serial"
if android_is_emulator_running "$serial"; then
echo " Emulator is running and responsive"
else
echo " Emulator not running (stale serial file)"
fi
else
echo " File does not exist (no emulator started this session)"
fi
echo ""
# Test 5: Cleanup behavior
log_test "Cleanup Behavior"
echo ""
echo "Normal mode cleanup:"
echo " - App stopped: adb shell am force-stop com.example.app"
echo " - Emulator kept running for dev convenience"
echo ""
echo "Pure mode cleanup (TEST_PURE=1):"
echo " - App stopped: adb shell am force-stop com.example.app"
echo " - Emulator killed: android.sh emulator stop"
echo " - Serial file cleaned up"
echo " - Next run starts completely fresh"
echo ""
# Summary
echo "========================================"
echo "SUMMARY"
echo "========================================"
echo ""
echo "Key Differences:"
echo ""
echo "Normal Mode:"
echo " + Fast (reuses existing emulator)"
echo " + Good for development/iteration"
echo " + Emulator persists between runs"
echo " - May have state from previous runs"
echo ""
echo "Pure Mode:"
echo " + Deterministic (clean state every time)"
echo " + Good for CI/CD pipelines"
echo " + Isolated test runs"
echo " - Slower (boots fresh emulator)"
echo ""
echo "Usage:"
echo " devbox run test:e2e # Normal mode"
echo " TEST_PURE=1 devbox run test:e2e # Pure mode"
echo ""
echo "All behavior tests passed!"