Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Provides an interface to start and stop Android emulator. | 5 """Provides an interface to start and stop Android emulator. |
| 6 | 6 |
| 7 Emulator: The class provides the methods to launch/shutdown the emulator with | 7 Emulator: The class provides the methods to launch/shutdown the emulator with |
| 8 the android virtual device named 'avd_armeabi' . | 8 the android virtual device named 'avd_armeabi' . |
| 9 """ | 9 """ |
| 10 | 10 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 if a.is_emulator] | 151 if a.is_emulator] |
| 152 for emulator in emulators: | 152 for emulator in emulators: |
| 153 used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1]) | 153 used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1]) |
| 154 for port in PortPool.port_range(): | 154 for port in PortPool.port_range(): |
| 155 if str(port) not in used_ports: | 155 if str(port) not in used_ports: |
| 156 return port | 156 return port |
| 157 | 157 |
| 158 | 158 |
| 159 def LaunchTempEmulators(emulator_count, abi, api_level, enable_kvm=False, | 159 def LaunchTempEmulators(emulator_count, abi, api_level, enable_kvm=False, |
| 160 kill_and_launch=True, sdcard_size=DEFAULT_SDCARD_SIZE, | 160 kill_and_launch=True, sdcard_size=DEFAULT_SDCARD_SIZE, |
| 161 storage_size=DEFAULT_STORAGE_SIZE, wait_for_boot=True): | 161 storage_size=DEFAULT_STORAGE_SIZE, wait_for_boot=True, |
| 162 on_bot=False): | |
|
jbudorick
2016/01/27 15:58:42
headless instead of on_bot here, too
Yoland Yan(Google)
2016/01/27 17:57:14
Done.
| |
| 162 """Create and launch temporary emulators and wait for them to boot. | 163 """Create and launch temporary emulators and wait for them to boot. |
| 163 | 164 |
| 164 Args: | 165 Args: |
| 165 emulator_count: number of emulators to launch. | 166 emulator_count: number of emulators to launch. |
| 166 abi: the emulator target platform | 167 abi: the emulator target platform |
| 167 api_level: the api level (e.g., 19 for Android v4.4 - KitKat release) | 168 api_level: the api level (e.g., 19 for Android v4.4 - KitKat release) |
| 168 wait_for_boot: whether or not to wait for emulators to boot up | 169 wait_for_boot: whether or not to wait for emulators to boot up |
| 170 on_bot: running emulator on a bot with no ui | |
| 169 | 171 |
| 170 Returns: | 172 Returns: |
| 171 List of emulators. | 173 List of emulators. |
| 172 """ | 174 """ |
| 173 emulators = [] | 175 emulators = [] |
| 174 for n in xrange(emulator_count): | 176 for n in xrange(emulator_count): |
| 175 t = time_profile.TimeProfile('Emulator launch %d' % n) | 177 t = time_profile.TimeProfile('Emulator launch %d' % n) |
| 176 # Creates a temporary AVD. | 178 # Creates a temporary AVD. |
| 177 avd_name = 'run_tests_avd_%d' % n | 179 avd_name = 'run_tests_avd_%d' % n |
| 178 logging.info('Emulator launch %d with avd_name=%s and api=%d', | 180 logging.info('Emulator launch %d with avd_name=%s and api=%d', |
| 179 n, avd_name, api_level) | 181 n, avd_name, api_level) |
| 180 emulator = Emulator(avd_name, abi, enable_kvm=enable_kvm, | 182 emulator = Emulator(avd_name, abi, enable_kvm=enable_kvm, |
| 181 sdcard_size=sdcard_size, storage_size=storage_size) | 183 sdcard_size=sdcard_size, storage_size=storage_size, |
| 184 on_bot=on_bot) | |
| 182 emulator.CreateAVD(api_level) | 185 emulator.CreateAVD(api_level) |
| 183 emulator.Launch(kill_all_emulators=(n == 0 and kill_and_launch)) | 186 emulator.Launch(kill_all_emulators=(n == 0 and kill_and_launch)) |
| 184 t.Stop() | 187 t.Stop() |
| 185 emulators.append(emulator) | 188 emulators.append(emulator) |
| 186 # Wait for all emulators to boot completed. | 189 # Wait for all emulators to boot completed. |
| 187 if wait_for_boot: | 190 if wait_for_boot: |
| 188 for emulator in emulators: | 191 for emulator in emulators: |
| 189 emulator.ConfirmLaunch(True) | 192 emulator.ConfirmLaunch(True) |
| 190 return emulators | 193 return emulators |
| 191 | 194 |
| 192 | 195 |
| 193 def LaunchEmulator(avd_name, abi, kill_and_launch=True, enable_kvm=False, | 196 def LaunchEmulator(avd_name, abi, kill_and_launch=True, enable_kvm=False, |
| 194 sdcard_size=DEFAULT_SDCARD_SIZE, | 197 sdcard_size=DEFAULT_SDCARD_SIZE, |
| 195 storage_size=DEFAULT_STORAGE_SIZE): | 198 storage_size=DEFAULT_STORAGE_SIZE, on_bot=False): |
| 196 """Launch an existing emulator with name avd_name. | 199 """Launch an existing emulator with name avd_name. |
| 197 | 200 |
| 198 Args: | 201 Args: |
| 199 avd_name: name of existing emulator | 202 avd_name: name of existing emulator |
| 200 abi: the emulator target platform | 203 abi: the emulator target platform |
| 204 on_bot: running emulator on a bot with no ui | |
| 201 | 205 |
| 202 Returns: | 206 Returns: |
| 203 emulator object. | 207 emulator object. |
| 204 """ | 208 """ |
| 205 logging.info('Specified emulator named avd_name=%s launched', avd_name) | 209 logging.info('Specified emulator named avd_name=%s launched', avd_name) |
| 206 emulator = Emulator(avd_name, abi, enable_kvm=enable_kvm, | 210 emulator = Emulator(avd_name, abi, enable_kvm=enable_kvm, |
| 207 sdcard_size=sdcard_size, storage_size=storage_size) | 211 sdcard_size=sdcard_size, storage_size=storage_size, |
| 212 on_bot=on_bot) | |
| 208 emulator.Launch(kill_all_emulators=kill_and_launch) | 213 emulator.Launch(kill_all_emulators=kill_and_launch) |
| 209 emulator.ConfirmLaunch(True) | 214 emulator.ConfirmLaunch(True) |
| 210 return emulator | 215 return emulator |
| 211 | 216 |
| 212 | 217 |
| 213 class Emulator(object): | 218 class Emulator(object): |
| 214 """Provides the methods to launch/shutdown the emulator. | 219 """Provides the methods to launch/shutdown the emulator. |
| 215 | 220 |
| 216 The emulator has the android virtual device named 'avd_armeabi'. | 221 The emulator has the android virtual device named 'avd_armeabi'. |
| 217 | 222 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 235 | 240 |
| 236 # Timeout interval of wait-for-device command before bouncing to a a | 241 # Timeout interval of wait-for-device command before bouncing to a a |
| 237 # process life check. | 242 # process life check. |
| 238 _WAITFORDEVICE_TIMEOUT = 5 | 243 _WAITFORDEVICE_TIMEOUT = 5 |
| 239 | 244 |
| 240 # Time to wait for a 'wait for boot complete' (property set on device). | 245 # Time to wait for a 'wait for boot complete' (property set on device). |
| 241 _WAITFORBOOT_TIMEOUT = 300 | 246 _WAITFORBOOT_TIMEOUT = 300 |
| 242 | 247 |
| 243 def __init__(self, avd_name, abi, enable_kvm=False, | 248 def __init__(self, avd_name, abi, enable_kvm=False, |
| 244 sdcard_size=DEFAULT_SDCARD_SIZE, | 249 sdcard_size=DEFAULT_SDCARD_SIZE, |
| 245 storage_size=DEFAULT_STORAGE_SIZE): | 250 storage_size=DEFAULT_STORAGE_SIZE, on_bot=False): |
| 246 """Init an Emulator. | 251 """Init an Emulator. |
| 247 | 252 |
| 248 Args: | 253 Args: |
| 249 avd_name: name of the AVD to create | 254 avd_name: name of the AVD to create |
| 250 abi: target platform for emulator being created, defaults to x86 | 255 abi: target platform for emulator being created, defaults to x86 |
| 251 """ | 256 """ |
| 252 android_sdk_root = constants.ANDROID_SDK_ROOT | 257 android_sdk_root = constants.ANDROID_SDK_ROOT |
| 253 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') | 258 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
| 254 self.android = os.path.join(android_sdk_root, 'tools', 'android') | 259 self.android = os.path.join(android_sdk_root, 'tools', 'android') |
| 255 self.popen = None | 260 self.popen = None |
| 256 self.device_serial = None | 261 self.device_serial = None |
| 257 self.abi = abi | 262 self.abi = abi |
| 258 self.avd_name = avd_name | 263 self.avd_name = avd_name |
| 259 self.sdcard_size = sdcard_size | 264 self.sdcard_size = sdcard_size |
| 260 self.storage_size = storage_size | 265 self.storage_size = storage_size |
| 261 self.enable_kvm = enable_kvm | 266 self.enable_kvm = enable_kvm |
| 267 self.on_bot = on_bot | |
| 262 | 268 |
| 263 @staticmethod | 269 @staticmethod |
| 264 def _DeviceName(): | 270 def _DeviceName(): |
| 265 """Return our device name.""" | 271 """Return our device name.""" |
| 266 port = _GetAvailablePort() | 272 port = _GetAvailablePort() |
| 267 return ('emulator-%d' % port, port) | 273 return ('emulator-%d' % port, port) |
| 268 | 274 |
| 269 def CreateAVD(self, api_level): | 275 def CreateAVD(self, api_level): |
| 270 """Creates an AVD with the given name. | 276 """Creates an AVD with the given name. |
| 271 | 277 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 """ | 374 """ |
| 369 if kill_all_emulators: | 375 if kill_all_emulators: |
| 370 KillAllEmulators() # just to be sure | 376 KillAllEmulators() # just to be sure |
| 371 self._AggressiveImageCleanup() | 377 self._AggressiveImageCleanup() |
| 372 (self.device_serial, port) = self._DeviceName() | 378 (self.device_serial, port) = self._DeviceName() |
| 373 self.ResizeAndWipeAvd(storage_size=self.storage_size) | 379 self.ResizeAndWipeAvd(storage_size=self.storage_size) |
| 374 emulator_command = [ | 380 emulator_command = [ |
| 375 self.emulator, | 381 self.emulator, |
| 376 # Speed up emulator launch by 40%. Really. | 382 # Speed up emulator launch by 40%. Really. |
| 377 '-no-boot-anim', | 383 '-no-boot-anim', |
| 384 ] | |
| 385 if self.on_bot: | |
| 386 emulator_command.extend([ | |
| 387 '-no-skin', | |
| 388 '-no-audio', | |
| 389 '-no-window' | |
| 390 ]) | |
| 391 emulator_command.extend([ | |
| 378 # Use a familiar name and port. | 392 # Use a familiar name and port. |
| 379 '-avd', self.avd_name, | 393 '-avd', self.avd_name, |
| 380 '-port', str(port), | 394 '-port', str(port), |
| 381 # Enable GPU by default. | 395 # Enable GPU by default. |
| 382 '-gpu', 'on', | 396 '-gpu', 'on', |
| 397 # all the argument after qemu are sub arguments for qemu | |
| 383 '-qemu', '-m', '1024', | 398 '-qemu', '-m', '1024', |
| 384 ] | 399 ]) |
| 385 if self.abi == 'x86' and self.enable_kvm: | 400 if self.abi == 'x86' and self.enable_kvm: |
| 386 emulator_command.extend([ | 401 emulator_command.extend([ |
| 387 # For x86 emulator --enable-kvm will fail early, avoiding accidental | 402 # For x86 emulator --enable-kvm will fail early, avoiding accidental |
| 388 # runs in a slow mode (i.e. without hardware virtualization support). | 403 # runs in a slow mode (i.e. without hardware virtualization support). |
| 389 '--enable-kvm', | 404 '--enable-kvm', |
| 390 ]) | 405 ]) |
| 391 | 406 |
| 392 logging.info('Emulator launch command: %s', ' '.join(emulator_command)) | 407 logging.info('Emulator launch command: %s', ' '.join(emulator_command)) |
| 393 self.popen = subprocess.Popen(args=emulator_command, | 408 self.popen = subprocess.Popen(args=emulator_command, |
| 394 stderr=subprocess.STDOUT) | 409 stderr=subprocess.STDOUT) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 logging.critical('emulator _ShutdownOnSignal') | 476 logging.critical('emulator _ShutdownOnSignal') |
| 462 for sig in self._SIGNALS: | 477 for sig in self._SIGNALS: |
| 463 signal.signal(sig, signal.SIG_DFL) | 478 signal.signal(sig, signal.SIG_DFL) |
| 464 self.Shutdown() | 479 self.Shutdown() |
| 465 raise KeyboardInterrupt # print a stack | 480 raise KeyboardInterrupt # print a stack |
| 466 | 481 |
| 467 def _InstallKillHandler(self): | 482 def _InstallKillHandler(self): |
| 468 """Install a handler to kill the emulator when we exit unexpectedly.""" | 483 """Install a handler to kill the emulator when we exit unexpectedly.""" |
| 469 for sig in self._SIGNALS: | 484 for sig in self._SIGNALS: |
| 470 signal.signal(sig, self._ShutdownOnSignal) | 485 signal.signal(sig, self._ShutdownOnSignal) |
| OLD | NEW |