Skip to content

Commit ce6913c

Browse files
committed
chore: android ui test
1 parent 0aa2828 commit ce6913c

File tree

2 files changed

+175
-17
lines changed

2 files changed

+175
-17
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
BOOT_TIMEOUT=$1
3+
retry() {
4+
local -r -i max_attempts="$1";shift 1
5+
local -i attempt_num=1
6+
local -a COMMANDS=( timeout $BOOT_TIMEOUT )
7+
8+
while (( "$#" )); do
9+
local arg="$1";
10+
if [[ "$arg" = *" "* ]]; then
11+
COMMANDS+=( $( printf "\''%s'\'" "$arg" ) )
12+
else
13+
COMMANDS+=( "$arg" )
14+
fi
15+
shift
16+
done
17+
18+
until "${COMMANDS[@]}"
19+
do
20+
if ((attempt_num==max_attempts))
21+
then
22+
echo "Last attempt $attempt_num failed, exiting."
23+
return 1
24+
else
25+
echo "Attempt $attempt_num failed! Waiting $attempt_num seconds..."
26+
sleep $((attempt_num++))
27+
fi
28+
done
29+
}
30+
31+
echo "[Waiting for device to boot] timeout $BOOT_TIMEOUT sec"
32+
33+
if [ $ANDROID_SIMULATOR_APILEVEL -gt 25 ];
34+
then
35+
retry 3 "$ANDROID_HOME/platform-tools/adb" wait-for-device shell 'echo "emulator is attached, wait for boot completion"; while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]] && [[ "$SECONDS" -lt 300 ]]; do sleep 1; done; input keyevent 82; echo "boot is complete."'
36+
else
37+
retry 3 "$ANDROID_HOME/platform-tools/adb" wait-for-device shell 'echo "emulator is attached, wait for boot completion"; while [[ -z $(getprop sys.boot_completed) ]] && [[ "$SECONDS" -lt 300 ]]; do sleep 1; done; input keyevent 82; echo "boot is complete."'
38+
fi
39+
40+
# Wait for com.android.systemui to become available,
41+
# as the CPU of the build machine may be slow
42+
# See: https://stackoverflow.com/questions/52410440/error-system-ui-isnt-responding-while-running-aosp-build-on-emulator
43+
#
44+
45+
echo "boot_completed after $SECONDS"
46+
echo "[Waiting for launcher to start]"
47+
LAUNCHER_READY=
48+
MAX_START_TIME=300
49+
START_TIME=$SECONDS
50+
while [[ -z ${LAUNCHER_READY} ]]; do
51+
52+
if [ $ANDROID_SIMULATOR_APILEVEL -ge 29 ];
53+
then
54+
UI_FOCUS=`$ANDROID_HOME/platform-tools/adb shell dumpsys window 2>/dev/null | grep -E 'mCurrentFocus|mFocusedApp' || true`
55+
else
56+
UI_FOCUS=`$ANDROID_HOME/platform-tools/adb shell dumpsys window windows 2>/dev/null | grep -i mCurrentFocus || true`
57+
fi
58+
59+
ELAPSED_TIME=$(( SECONDS - START_TIME ))
60+
if [ ${ELAPSED_TIME} -gt ${MAX_START_TIME} ];
61+
then
62+
echo "(FAIL) Emulator failed to start properly after $MAX_START_TIME"
63+
exit 1
64+
fi
65+
66+
echo "(DEBUG $SECONDS) Current focus: ${UI_FOCUS}"
67+
68+
case $UI_FOCUS in
69+
*"Launcher"*)
70+
LAUNCHER_READY=true
71+
;;
72+
"")
73+
echo "Waiting for window service..."
74+
sleep 3
75+
;;
76+
*"Not Responding"*)
77+
echo "Detected an ANR! Dismissing..."
78+
$ANDROID_HOME/platform-tools/adb shell input keyevent KEYCODE_DPAD_DOWN
79+
$ANDROID_HOME/platform-tools/adb shell input keyevent KEYCODE_DPAD_DOWN
80+
$ANDROID_HOME/platform-tools/adb shell input keyevent KEYCODE_ENTER
81+
;;
82+
*)
83+
echo "Waiting for launcher..."
84+
sleep 3
85+
86+
# For some reason the messaging app can be brought up in front
87+
# (DEBUG) Current focus: mCurrentFocus=Window{1170051 u0 com.google.android.apps.messaging/com.google.android.apps.messaging.ui.ConversationListActivity}
88+
# Try bringing back the home screen to check on the launcher.
89+
$ANDROID_HOME/platform-tools/adb shell input keyevent KEYCODE_HOME
90+
;;
91+
esac
92+
done
93+
94+
echo "Launcher is ready!"

build/scripts/android-uitest.sh

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,96 @@
22
set -euo pipefail
33
IFS=$'\n\t'
44

5-
# Install AVD files
6-
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install "system-images;android-$UNO_UITEST_ANDROID_API_LEVEL;google_apis_playstore;x86_64"
5+
cd $BUILD_SOURCESDIRECTORY/build
76

8-
# Create emulator
9-
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n xamarin_android_emulator --abi "x86_64" -k "system-images;android-$UNO_UITEST_ANDROID_API_LEVEL;google_apis_playstore;x86_64" --force
7+
export ANDROID_HOME=$BUILD_SOURCESDIRECTORY/build/android-sdk
8+
export ANDROID_SDK_ROOT=$BUILD_SOURCESDIRECTORY/build/android-sdk
9+
export LATEST_CMDLINE_TOOLS_PATH=$ANDROID_SDK_ROOT/cmdline-tools/latest
10+
export CMDLINETOOLS=commandlinetools-mac-8512546_latest.zip
11+
mkdir -p $ANDROID_HOME
1012

11-
echo $ANDROID_HOME/emulator/emulator -list-avds
13+
if [ -d $LATEST_CMDLINE_TOOLS_PATH ];
14+
then
15+
rm -rf $LATEST_CMDLINE_TOOLS_PATH
16+
fi
1217

13-
echo "Starting emulator"
18+
wget https://dl.google.com/android/repository/$CMDLINETOOLS
19+
unzip -o $CMDLINETOOLS -d $ANDROID_HOME/cmdline-tools
20+
rm $CMDLINETOOLS
21+
mv $ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools $LATEST_CMDLINE_TOOLS_PATH
1422

15-
# Kickstart the ADB server
16-
$ANDROID_HOME/platform-tools/adb devices
1723

18-
# Start emulator in background
19-
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot -no-window -qemu > /dev/null 2>&1 &
24+
AVD_NAME=xamarin_android_emulator
25+
AVD_CONFIG_FILE=~/.android/avd/$AVD_NAME.avd/config.ini
26+
EMU_UPDATE_FILE=~/.android/emu-update-last-check.ini
27+
SDK_MGR_TOOLS_FLAG=.sdk_toolkit_installed
2028

21-
# build the sample, while the emulator is starting
22-
msbuild /r /p:Configuration=Release $UNO_UITEST_PROJECT
23-
msbuild /r /p:Configuration=Release /p:IsUiAutomationMappingEnabled=True $UNO_UITEST_ANDROID_PROJECT
29+
install_android_sdk() {
30+
SIMULATOR_APILEVEL=$1
2431

25-
# Wait for the emulator to finish booting
26-
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'
32+
if [[ ! -f $SDK_MGR_TOOLS_FLAG ]];
33+
then
34+
touch $SDK_MGR_TOOLS_FLAG
2735

28-
$ANDROID_HOME/platform-tools/adb devices
36+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install 'tools'| tr '\r' '\n' | uniq
37+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install 'platform-tools' | tr '\r' '\n' | uniq
38+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install 'build-tools;35.0.0' | tr '\r' '\n' | uniq
39+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install 'extras;android;m2repository' | tr '\r' '\n' | uniq
40+
fi
41+
42+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install "platforms;android-$SIMULATOR_APILEVEL" | tr '\r' '\n' | uniq
43+
echo "y" | $LATEST_CMDLINE_TOOLS_PATH/bin/sdkmanager --sdk_root=${ANDROID_HOME} --install "system-images;android-$SIMULATOR_APILEVEL;google_apis_playstore;x86_64" | tr '\r' '\n' | uniq
44+
}
2945

30-
echo "Emulator started"
46+
if [[ ! -f $EMU_UPDATE_FILE ]];
47+
then
48+
touch $EMU_UPDATE_FILE
49+
fi
50+
51+
if [[ ! -f $AVD_CONFIG_FILE ]];
52+
then
53+
# Install AVD files
54+
install_android_sdk $ANDROID_SIMULATOR_APILEVEL
55+
56+
if [[ -f $ANDROID_HOME/platform-tools/platform-tools/adb ]]
57+
then
58+
# It appears that the platform-tools 29.0.6 are extracting into an incorrect path
59+
mv $ANDROID_HOME/platform-tools/platform-tools/* $ANDROID_HOME/platform-tools
60+
fi
61+
62+
# Create emulator
63+
echo "no" | $LATEST_CMDLINE_TOOLS_PATH/bin/avdmanager create avd -n "$AVD_NAME" --abi "x86_64" -k "system-images;android-$ANDROID_SIMULATOR_APILEVEL;google_apis_playstore;x86_64" --sdcard 128M --force
64+
65+
# based on https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#hardware
66+
# >> Agents that run macOS images are provisioned on Mac pros with a 3 core CPU, 14 GB of RAM, and 14 GB of SSD disk space.
67+
echo "hw.cpu.ncore=3" >> $AVD_CONFIG_FILE
68+
69+
# Bump the heap size as the tests are stressing the application
70+
echo "vm.heapSize=256M" >> $AVD_CONFIG_FILE
71+
72+
$ANDROID_HOME/emulator/emulator -list-avds
73+
74+
echo "Checking for hardware acceleration"
75+
$ANDROID_HOME/emulator/emulator -accel-check
76+
77+
echo "Starting emulator"
78+
79+
# kickstart ADB
80+
$ANDROID_HOME/platform-tools/adb devices
81+
82+
# Start emulator in background
83+
nohup $ANDROID_HOME/emulator/emulator -avd "$AVD_NAME" -skin 1280x800 -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim > $UNO_UITEST_SCREENSHOT_PATH/android-emulator-log.txt 2>&1 &
84+
85+
# Wait for the emulator to finish booting
86+
source $BUILD_SOURCESDIRECTORY/build/scripts/android-uitest-wait-systemui.sh 500
87+
88+
else
89+
# Restart the emulator to avoid running first-time tasks
90+
$ANDROID_HOME/platform-tools/adb reboot
91+
92+
# Wait for the emulator to finish booting
93+
source $BUILD_SOURCESDIRECTORY/build/scripts/android-uitest-wait-systemui.sh 500
94+
fi
3195

3296
wget $UNO_UITEST_NUGET_URL
3397
mono nuget.exe install NUnit.ConsoleRunner -Version $UNO_UITEST_NUNIT_VERSION

0 commit comments

Comments
 (0)