Merge: doc: fixed some typos and other misc. corrections
[nit.git] / misc / jenkins / check_android.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Script to test Android apps using `adb monkey`
17
18 # Show the emulator window?
19 show=false
20
21 # Name of an already running device, as shown by `adb devices`
22 dev=none
23
24 # Parse command line options
25 stop=false
26 while [ $stop = false ]; do
27 case $1 in
28 --show) show=true; shift;;
29 --dev) dev=$2; shift; shift;;
30 *) stop=true
31 esac
32 done
33
34 if [ $# = 0 ]; then
35 echo "Test an apk"
36 echo "Usage: check_android.sh [--show] [--dev android-device-name] file.apk [other_file.apk [...]]"
37 exit 1
38 fi
39
40 set -x
41
42 # Create an AVD if none is specified
43 kill_emu=false
44 if [ $dev = none ]; then
45 kill_emu=true
46
47 # Name of the AVD
48 # TODO have alternatives AVD with different specs
49 avd=check_android_avd
50
51 # Port for the emulator
52 port=5600
53
54 dev=emulator-$port
55
56 # Time to wait for the emulator to be ready
57 to_sleep=20
58
59 # Does the AVD exists?
60 android list avd | grep $avd
61 if [ $? -eq 1 ]; then
62 # Create it
63 echo no | android create avd -n $avd --snapshot -t android-19 --abi x86 || true
64 to_sleep=120
65 fi
66
67 # Show the emulator window on request only
68 emu_opts=
69 if [ $show = false ]; then
70 emu_opts="-no-window"
71 fi
72
73 emulator -avd $avd -port $port -logcat app.nit $emu_opts &
74 # To use hardware optimization add: -qemu -m 512 -enable-kvm
75
76 sleep $to_sleep
77 fi
78
79 for apk in $@; do
80
81 # Target package name
82 pkg=`aapt dump badging $apk | grep package | sed -e "s/.*name='\([a-z._]*\)'.*/\1/"`
83
84 # Force reinstall
85 adb -s $dev uninstall $pkg
86 adb -s $dev install -r "$apk"
87
88 # Unlock screen
89 adb -s $dev shell input keyevent 82
90 adb -s $dev shell input keyevent 4
91
92 # Run monkey
93 tools_dir=`dirname $0`
94 $tools_dir/unitrun.sh "android-`basename $apk .apk`" \
95 adb -s $dev shell monkey -p $pkg \
96 --monitor-native-crashes --throttle 2 --pct-touch 50 --pct-motion 50 5000
97 done
98
99 if [ $kill_emu = true ]; then
100 # Kill emulator
101 adb -s $dev emu kill
102 fi