This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
forked from spacecowboy/NotePad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_on_emu_started.sh
100 lines (79 loc) · 3.98 KB
/
github_on_emu_started.sh
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
#!/bin/bash
# once the android emulator starts, this script tries to:
# * close all popups
# * configure the emulator
# * start recording a video
# * run the tests
#
# sources: https://stackoverflow.com/a/66155744/6307322
# https://stackoverflow.com/a/62723329/6307322
# it will run all commands, even if one fails
echo "github action script started"
# wait a bit for the emulator to finish loading
sleep 15
# take screenshot of the host PC after the emulator starts
screencapture screenshot-desktop.jpg
# output may be useful
adb devices
# try to close all OS popups in the emulator, like "System UI stopped working"
adb shell am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS
# close that useless popup that tells you how to exit fullscreen mode
adb shell settings put secure immersive_mode_confirmations confirmed
# manually uninstall the app, if possible. Sometimes it's needed
adb shell pm uninstall --user 0 com.nononsenseapps.notepad.test
adb shell pm uninstall --user 0 com.nononsenseapps.notepad
# if the host PC is under heavy load, the emulator sometimes receives a "long click" instead
# of a normal "click" signal, thus some tests will fail. The workaround is to raise the
# treshold for a "long click" from ~400ms to 3s, so it will be harder to send long clicks
# by mistake
adb shell settings put secure long_press_timeout 3000
# Take a screenshot of the emulator. See also
# https://developer.android.com/studio/run/advanced-emulator-usage#screenshots
adb emu screenrecord screenshot ./screenshot-emu-tests-starting.png
# disable animations once again, for safety
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0
function getScreenStreamFromEmu() {
while true; do
# exec-out: run command on emulated android, get the output on the host PC
# bitrate & size: to get a smaller video
# output-format and - at the end: stream to console output, don't save
# bugreport: helpful info when a video starts
# alternative: `adb emu screenrecord start --bit-rate 100000 --size 540x960 ./vid.webm`,
# but the tool is hardcoded to die after 3 minutes. Thanks Google.
# returns: 0 if 3 minutes passed, a number > 0 if there was an error
adb exec-out screenrecord --output-format=h264 --bit-rate 1M --size 480x854 --bugreport -
ADB_RET_CODE=$?
if [ $ADB_RET_CODE -gt 0 ] ; then
# the emulator died: exit the loop & the function. The video is saved correctly
echo "[getScreenStreamFromEmu] Exiting..." >&2
break
fi
# should go to STDERR, without polluting the video stream, hopefully
echo "[getScreenStreamFromEmu] restarting screenrecord, PID = " $! ". " $ADB_RET_CODE " was the return code" >&2
done
}
# TODO android API 32 images have a built-in screen record feature that can make long videos, but you have to start it from the top drawer menu. Check if API 23 has it, and try to use it with ADB. Videos are saved on /sdcard/movies/ then you have to pull them with adb
# save the video stream to a file
{ getScreenStreamFromEmu | ffmpeg -i - -s 480x854 -loglevel error \
-nostats -hide_banner -framerate 15 -bufsize 1M emu-video.mp4 ; } &
# press "HOME" to close any "system UI not responding" popups still showing.
# Notice that these popups appear when the host PC is too slow, so the best solution
# is to run the tests under a different host OS, device skin, API version, ...
adb shell input keyevent 3
# clear logcat before tests begin
adb logcat -c
# run tests
./gradlew connectedCheck --info
GRADLE_RETURN_CODE=$?
# dump the logcat to a file. Log level: Debug
adb logcat -d *:D > logcat-dump.txt
# check if pictures, videos & logs exist
echo "----------"
ls -lh -- *.mp4 *.png *.jpg logcat-dump.txt
echo "----------"
# return with the code from gradle, so the github action step can fail if the tests failed
exit $GRADLE_RETURN_CODE
# then the github action will stop the emulator, and getScreenStreamFromEmu() will stop
# the recording, we don't have to do it manually.