1#!/usr/bin/python3 2# 3# Copyright (C) 2015 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the 'License'); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an 'AS IS' BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import os 19import sys 20import tempfile 21import threading 22import time 23import traceback 24 25from android_device import * 26from avd import * 27from queue import Queue, Empty 28 29 30# This dict should contain one entry for every density listed in DisplayMetrics. 31# See CDD 7.1.1.3 for more information on densities at which CTS can run. If you 32# are only generating reference images for a single density, you can comment out 33# the other densities and then run the script. 34CTS_THEME_dict = { 35 120: "ldpi", 36 140: "140dpi", 37 160: "mdpi", 38 180: "180dpi", 39 200: "200dpi", 40 213: "tvdpi", 41 220: "220dpi", 42 240: "hdpi", 43 260: "260dpi", 44 280: "280dpi", 45 300: "300dpi", 46 320: "xhdpi", 47 340: "340dpi", 48 360: "360dpi", 49 400: "400dpi", 50 420: "420dpi", 51 440: "440dpi", 52 450: "450dpi", 53 480: "xxhdpi", 54 560: "560dpi", 55 600: "600dpi", 56 640: "xxxhdpi", 57} 58 59OUT_FILE = "/sdcard/cts-theme-assets.zip" 60 61 62class ParallelExecutor(threading.Thread): 63 def __init__(self, tasks, setup, q): 64 threading.Thread.__init__(self) 65 self._q = q 66 self._tasks = tasks 67 self._setup = setup 68 self._result = 0 69 70 def run(self): 71 try: 72 while True: 73 config = self._q.get(block=True, timeout=2) 74 for t in self._tasks: 75 try: 76 if t(self._setup, config): 77 self._result += 1 78 except KeyboardInterrupt: 79 raise 80 except: 81 print("Failed to execute thread:", sys.exc_info()[0]) 82 traceback.print_exc() 83 self._q.task_done() 84 except KeyboardInterrupt: 85 raise 86 except Empty: 87 pass 88 89 def get_result(self): 90 return self._result 91 92 93# pass a function with number of instances to be executed in parallel 94# each thread continues until config q is empty. 95def execute_parallel(tasks, setup, q, num_threads): 96 result = 0 97 threads = [] 98 for i in range(num_threads): 99 t = ParallelExecutor(tasks, setup, q) 100 t.start() 101 threads.append(t) 102 for t in threads: 103 t.join() 104 result += t.get_result() 105 return result 106 107 108def print_adb_result(device, out, err): 109 print("device: " + device) 110 if out is not None: 111 print("out:\n" + out) 112 if err is not None: 113 print("err:\n" + err) 114 115 116def do_capture(setup, device_serial): 117 (themeApkPath, out_path) = setup 118 119 device = AndroidDevice(device_serial) 120 121 version = device.get_version_codename() 122 if version == "REL": 123 version = str(device.get_version_sdk()) 124 125 density = device.get_density() 126 127 if CTS_THEME_dict[density]: 128 density_bucket = CTS_THEME_dict[density] 129 else: 130 density_bucket = str(density) + "dpi" 131 132 out_file = os.path.join(out_path, os.path.join(version, "%s.zip" % density_bucket)) 133 134 device.uninstall_package('android.theme.app') 135 136 (out, err, success) = device.install_apk(themeApkPath) 137 if not success: 138 print("Failed to install APK on " + device_serial) 139 print_adb_result(device_serial, out, err) 140 return False 141 142 print("Generating images on " + device_serial + "...") 143 try: 144 (out, err) = device.run_instrumentation_test( 145 "android.theme.app/androidx.test.runner.AndroidJUnitRunner") 146 except KeyboardInterrupt: 147 raise 148 except: 149 (out, err) = device.run_instrumentation_test( 150 "android.theme.app/android.test.InstrumentationTestRunner") 151 152 # Detect test failure and abort. 153 if "FAILURES!!!" in out.split(): 154 print_adb_result(device_serial, out, err) 155 return False 156 157 # Make sure that the run is complete by checking the process itself 158 print("Waiting for " + device_serial + "...") 159 wait_time = 0 160 while device.is_process_alive("android.theme.app"): 161 time.sleep(1) 162 wait_time = wait_time + 1 163 if wait_time > 180: 164 print("Timed out") 165 break 166 167 time.sleep(10) 168 169 print("Pulling images from " + device_serial + " to " + out_file) 170 device.run_adb_command("pull " + OUT_FILE + " " + out_file) 171 device.run_adb_command("shell rm -rf " + OUT_FILE) 172 return True 173 174 175def get_emulator_path(): 176 if 'ANDROID_SDK_ROOT' not in os.environ: 177 print('Environment variable ANDROID_SDK_ROOT must point to your Android SDK root.') 178 sys.exit(1) 179 180 sdk_path = os.environ['ANDROID_SDK_ROOT'] 181 if not os.path.isdir(sdk_path): 182 print("Failed to find Android SDK at ANDROID_SDK_ROOT: %s" % sdk_path) 183 sys.exit(1) 184 185 emu_path = os.path.join(os.path.join(sdk_path, 'tools'), 'emulator') 186 if not os.path.isfile(emu_path): 187 print("Failed to find emulator within ANDROID_SDK_ROOT: %s" % sdk_path) 188 sys.exit(1) 189 190 return emu_path 191 192 193def start_emulator(name, density): 194 if name == "local": 195 emu_path = "" 196 else: 197 emu_path = get_emulator_path() 198 199 # Start emulator for 560dpi, normal screen size. 200 test_avd = AVD(name, emu_path) 201 test_avd.configure_screen(density, 360, 640) 202 test_avd.start() 203 try: 204 test_avd_device = test_avd.get_device() 205 test_avd_device.wait_for_device() 206 test_avd_device.wait_for_boot_complete() 207 return test_avd 208 except: 209 test_avd.stop() 210 return None 211 212 213def main(argv): 214 if 'ANDROID_BUILD_TOP' not in os.environ or 'ANDROID_HOST_OUT' not in os.environ: 215 print('Missing environment variables. Did you run build/envsetup.sh and lunch?') 216 sys.exit(1) 217 218 theme_apk = os.path.join(os.environ['ANDROID_HOST_OUT'], 219 'cts/android-cts/testcases/CtsThemeDeviceApp.apk') 220 if not os.path.isfile(theme_apk): 221 print('Couldn\'t find test APK. Did you run make cts?') 222 sys.exit(1) 223 224 out_path = os.path.join(os.environ['ANDROID_BUILD_TOP'], 225 'cts/hostsidetests/theme/assets') 226 os.system("mkdir -p %s" % out_path) 227 228 if len(argv) == 2: 229 for density in CTS_THEME_dict.keys(): 230 retries = 0 231 result = False 232 while result != True: 233 retries += 1 234 emulator = start_emulator(argv[1], density) 235 result = do_capture(setup=(theme_apk, out_path), device_serial=emulator.get_serial()) 236 emulator.stop() 237 if result: 238 print("Generated reference images for %ddpi" % density) 239 else: 240 print("Failed to generate reference images for %ddpi" % density) 241 print("Try number %d" % retries) 242 else: 243 tasks = [do_capture] 244 setup = (theme_apk, out_path) 245 246 devices = enumerate_android_devices() 247 248 if len(devices) > 0: 249 device_queue = Queue() 250 for device in devices: 251 device_queue.put(device) 252 253 result = execute_parallel(tasks, setup, device_queue, len(devices)) 254 255 if result > 0: 256 print('Generated reference images for %(count)d devices' % {"count": result}) 257 else: 258 print('Failed to generate reference images') 259 else: 260 print('No devices found') 261 262 263if __name__ == '__main__': 264 main(sys.argv) 265