1#!/bin/bash
2
3adb="adb"
4if [[ $# -gt 0 ]]; then
5	adb="adb $*" # for setting -e, -d or -s <serial>
6fi
7
8function atexit()
9{
10	local retval=$?
11
12	if [[ $retval -eq 0 ]]; then
13		rm $log
14	else
15		echo "There were errors, please check log at $log"
16	fi
17}
18
19log=$(mktemp)
20trap "atexit" EXIT
21
22function compile_module()
23{
24	local android_mk="$1"
25
26	echo "Compiling .${android_mk:${#PWD}}"
27	ONE_SHOT_MAKEFILE="$android_mk" make -C "../../../../../" files | tee -a $log
28	if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
29		exit 1
30	fi
31}
32
33function wait_for_boot_completed()
34{
35	echo "Rebooting device"
36	$adb wait-for-device logcat -c
37	$adb wait-for-device logcat | grep -m 1 -e 'PowerManagerService.*bootCompleted' >/dev/null
38}
39
40function mkdir_if_needed()
41{
42	local path="$1"
43
44	if [[ "${path:0:1}" != "/" ]]; then
45		echo "mkdir_if_needed: error: path '$path' does not begin with /" | tee -a $log
46		exit 1
47	fi
48
49	local basename=$(basename "$path")
50	local dirname=$(dirname "$path")
51	local t=$($adb shell ls -l $dirname | tr -d '\r' | grep -e "${basename}$" | grep -oe '^.')
52
53	case "$t" in
54		d) # File exists, and is a directory ...
55			# do nothing
56			;;
57		l) # ... (or symbolic link possibly to a directory).
58			# do nothing
59			;;
60		"") # File does not exist.
61			mkdir_if_needed "$dirname"
62			$adb shell mkdir "$path"
63			;;
64		*) # File exists, but is not a directory.
65			echo "mkdir_if_needed: file '$path' exists, but is not a directory" | tee -a $log
66			exit 1
67			;;
68	esac
69}
70
71function disable_overlay()
72{
73	echo "Disabling overlay"
74	$adb shell rm /vendor/overlay/framework/framework-res.apk
75	$adb shell rm /data/resource-cache/vendor@overlay@framework@framework-res.apk@idmap
76}
77
78function enable_overlay()
79{
80	echo "Enabling overlay"
81	mkdir_if_needed "/system/vendor"
82	mkdir_if_needed "/vendor/overlay/framework"
83	$adb shell ln -s /data/app/com.android.overlaytest.overlay.apk /vendor/overlay/framework/framework-res.apk
84}
85
86function instrument()
87{
88	local class="$1"
89
90	echo "Instrumenting $class"
91	$adb shell am instrument -w -e class $class com.android.overlaytest/android.test.InstrumentationTestRunner | tee -a $log
92}
93
94function remount()
95{
96	echo "Remounting file system writable"
97	$adb remount | tee -a $log
98}
99
100function sync()
101{
102	echo "Syncing to device"
103	$adb sync data | tee -a $log
104}
105
106# some commands require write access, remount once and for all
107remount
108
109# build and sync
110compile_module "$PWD/OverlayTest/Android.mk"
111compile_module "$PWD/OverlayTestOverlay/Android.mk"
112sync
113
114# instrument test (without overlay)
115$adb shell stop
116disable_overlay
117$adb shell start
118wait_for_boot_completed
119instrument "com.android.overlaytest.WithoutOverlayTest"
120
121# instrument test (with overlay)
122$adb shell stop
123enable_overlay
124$adb shell start
125wait_for_boot_completed
126instrument "com.android.overlaytest.WithOverlayTest"
127
128# cleanup
129exit $(grep -c -e '^FAILURES' $log)
130