| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Provides an interface to start and stop Android emulator. | 6 """Provides an interface to start and stop Android emulator. |
| 7 | 7 |
| 8 Assumes system environment ANDROID_NDK_ROOT has been set. | 8 Assumes system environment ANDROID_NDK_ROOT has been set. |
| 9 | 9 |
| 10 Emulator: The class provides the methods to launch/shutdown the emulator with | 10 Emulator: The class provides the methods to launch/shutdown the emulator with |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 # the time to launch the emulator and a wait-for-device command. | 85 # the time to launch the emulator and a wait-for-device command. |
| 86 _LAUNCH_TIMEOUT = 120 | 86 _LAUNCH_TIMEOUT = 120 |
| 87 | 87 |
| 88 # Timeout interval of wait-for-device command before bouncing to a a | 88 # Timeout interval of wait-for-device command before bouncing to a a |
| 89 # process life check. | 89 # process life check. |
| 90 _WAITFORDEVICE_TIMEOUT = 5 | 90 _WAITFORDEVICE_TIMEOUT = 5 |
| 91 | 91 |
| 92 # Time to wait for a "wait for boot complete" (property set on device). | 92 # Time to wait for a "wait for boot complete" (property set on device). |
| 93 _WAITFORBOOT_TIMEOUT = 300 | 93 _WAITFORBOOT_TIMEOUT = 300 |
| 94 | 94 |
| 95 def __init__(self): | 95 def __init__(self, fast_and_loose=False): |
| 96 """Init an Emulator. |
| 97 |
| 98 Args: |
| 99 fast_and_loose: Loosen up the rules for reliable running for speed. |
| 100 Intended for quick testing or re-testing. |
| 101 |
| 102 """ |
| 96 try: | 103 try: |
| 97 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] | 104 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] |
| 98 except KeyError: | 105 except KeyError: |
| 99 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' | 106 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' |
| 100 'emulator.') | 107 'emulator.') |
| 101 raise | 108 raise |
| 102 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') | 109 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
| 103 self.popen = None | 110 self.popen = None |
| 104 self.device = None | 111 self.device = None |
| 112 self.fast_and_loose = fast_and_loose |
| 105 | 113 |
| 106 def _DeviceName(self): | 114 def _DeviceName(self): |
| 107 """Return our device name.""" | 115 """Return our device name.""" |
| 108 port = _GetAvailablePort() | 116 port = _GetAvailablePort() |
| 109 return ('emulator-%d' % port, port) | 117 return ('emulator-%d' % port, port) |
| 110 | 118 |
| 111 def Launch(self): | 119 def Launch(self): |
| 112 """Launches the emulator and waits for package manager to startup. | 120 """Launches the emulator and waits for package manager to startup. |
| 113 | 121 |
| 114 If fails, an exception will be raised. | 122 If fails, an exception will be raised. |
| 115 """ | 123 """ |
| 116 _KillAllEmulators() # just to be sure | 124 _KillAllEmulators() # just to be sure |
| 117 self._AggressiveImageCleanup() | 125 if not self.fast_and_loose: |
| 126 self._AggressiveImageCleanup() |
| 118 (self.device, port) = self._DeviceName() | 127 (self.device, port) = self._DeviceName() |
| 119 emulator_command = [ | 128 emulator_command = [ |
| 120 self.emulator, | 129 self.emulator, |
| 121 # Speed up emulator launch by 40%. Really. | 130 # Speed up emulator launch by 40%. Really. |
| 122 '-no-boot-anim', | 131 '-no-boot-anim', |
| 123 # The default /data size is 64M. | 132 # The default /data size is 64M. |
| 124 # That's not enough for 4 unit test bundles and their data. | 133 # That's not enough for 4 unit test bundles and their data. |
| 125 '-partition-size', '256', | 134 '-partition-size', '256', |
| 126 # ALWAYS wipe the data. We've seen cases where an emulator | |
| 127 # gets 'stuck' if we don't do this (every thousand runs or | |
| 128 # so). | |
| 129 '-wipe-data', | |
| 130 # Use a familiar name and port. | 135 # Use a familiar name and port. |
| 131 '-avd', 'buildbot', | 136 '-avd', 'buildbot', |
| 132 '-port', str(port)] | 137 '-port', str(port)] |
| 138 if not self.fast_and_loose: |
| 139 emulator_command.extend([ |
| 140 # Wipe the data. We've seen cases where an emulator |
| 141 # gets 'stuck' if we don't do this (every thousand runs or |
| 142 # so). |
| 143 '-wipe-data', |
| 144 ]) |
| 133 logging.info('Emulator launch command: %s', ' '.join(emulator_command)) | 145 logging.info('Emulator launch command: %s', ' '.join(emulator_command)) |
| 134 self.popen = subprocess.Popen(args=emulator_command, | 146 self.popen = subprocess.Popen(args=emulator_command, |
| 135 stderr=subprocess.STDOUT) | 147 stderr=subprocess.STDOUT) |
| 136 self._InstallKillHandler() | 148 self._InstallKillHandler() |
| 137 self._ConfirmLaunch() | 149 self._ConfirmLaunch() |
| 138 | 150 |
| 139 def _AggressiveImageCleanup(self): | 151 def _AggressiveImageCleanup(self): |
| 140 """Aggressive cleanup of emulator images. | 152 """Aggressive cleanup of emulator images. |
| 141 | 153 |
| 142 Experimentally it looks like our current emulator use on the bot | 154 Experimentally it looks like our current emulator use on the bot |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 """Install a handler to kill the emulator when we exit unexpectedly.""" | 224 """Install a handler to kill the emulator when we exit unexpectedly.""" |
| 213 for sig in self._SIGNALS: | 225 for sig in self._SIGNALS: |
| 214 signal.signal(sig, self._ShutdownOnSignal) | 226 signal.signal(sig, self._ShutdownOnSignal) |
| 215 | 227 |
| 216 def main(argv): | 228 def main(argv): |
| 217 Emulator().launch() | 229 Emulator().launch() |
| 218 | 230 |
| 219 | 231 |
| 220 if __name__ == '__main__': | 232 if __name__ == '__main__': |
| 221 main(sys.argv) | 233 main(sys.argv) |
| OLD | NEW |