OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Provides an interface to start and stop Android emulator. | 7 """Provides an interface to start and stop Android emulator. |
8 | 8 |
9 Assumes system environment ANDROID_NDK_ROOT has been set. | 9 Assumes system environment ANDROID_NDK_ROOT has been set. |
10 | 10 |
11 Emulator: The class provides the methods to launch/shutdown the emulator with | 11 Emulator: The class provides the methods to launch/shutdown the emulator with |
12 the android virtual device named 'avd_armeabi' . | 12 the android virtual device named 'avd_armeabi' . |
13 """ | 13 """ |
14 | 14 |
15 import logging | 15 import logging |
16 import os | 16 import os |
17 import signal | 17 import signal |
18 import subprocess | 18 import subprocess |
19 import sys | 19 import sys |
20 import time | 20 import time |
21 | 21 |
| 22 import time_profile |
| 23 # TODO(craigdh): Move these pylib dependencies to pylib/utils/. |
22 from pylib import android_commands | 24 from pylib import android_commands |
23 from pylib import cmd_helper | 25 from pylib import cmd_helper |
24 | 26 |
25 # adb_interface.py is under ../../third_party/android_testrunner/ | 27 # adb_interface.py is under ../../third_party/android_testrunner/ |
26 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | 28 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', |
27 '..', 'third_party', 'android_testrunner')) | 29 '..', 'third_party', 'android_testrunner')) |
28 import adb_interface | 30 import adb_interface |
29 import errors | 31 import errors |
30 import run_command | 32 import run_command |
31 | 33 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 """Returns an available TCP port for the console.""" | 94 """Returns an available TCP port for the console.""" |
93 used_ports = [] | 95 used_ports = [] |
94 emulators = android_commands.GetEmulators() | 96 emulators = android_commands.GetEmulators() |
95 for emulator in emulators: | 97 for emulator in emulators: |
96 used_ports.append(emulator.split('-')[1]) | 98 used_ports.append(emulator.split('-')[1]) |
97 for port in PortPool.port_range(): | 99 for port in PortPool.port_range(): |
98 if str(port) not in used_ports: | 100 if str(port) not in used_ports: |
99 return port | 101 return port |
100 | 102 |
101 | 103 |
| 104 def LaunchEmulators(emulator_count, wait_for_boot=True): |
| 105 """Launch multiple emulators and wait for them to boot. |
| 106 |
| 107 Args: |
| 108 emulator_count: number of emulators to launch. |
| 109 |
| 110 Returns: |
| 111 List of emulators. |
| 112 """ |
| 113 emulators = [] |
| 114 for n in xrange(emulator_count): |
| 115 t = time_profile.TimeProfile('Emulator launch %d' % n) |
| 116 avd_name = None |
| 117 if n > 0: |
| 118 # Creates a temporary AVD for the extra emulators. |
| 119 avd_name = 'run_tests_avd_%d' % n |
| 120 logging.info('Emulator launch %d with avd_name=%s', n, avd_name) |
| 121 emulator = Emulator(avd_name) |
| 122 emulator.Launch(kill_all_emulators=n == 0) |
| 123 t.Stop() |
| 124 emulators.append(emulator) |
| 125 # Wait for all emulators to boot completed. |
| 126 if wait_for_boot: |
| 127 for emulator in emulators: |
| 128 emulator.ConfirmLaunch(True) |
| 129 return emulators |
| 130 |
| 131 |
102 class Emulator(object): | 132 class Emulator(object): |
103 """Provides the methods to lanuch/shutdown the emulator. | 133 """Provides the methods to launch/shutdown the emulator. |
104 | 134 |
105 The emulator has the android virtual device named 'avd_armeabi'. | 135 The emulator has the android virtual device named 'avd_armeabi'. |
106 | 136 |
107 The emulator could use any even TCP port between 5554 and 5584 for the | 137 The emulator could use any even TCP port between 5554 and 5584 for the |
108 console communication, and this port will be part of the device name like | 138 console communication, and this port will be part of the device name like |
109 'emulator-5554'. Assume it is always True, as the device name is the id of | 139 'emulator-5554'. Assume it is always True, as the device name is the id of |
110 emulator managed in this class. | 140 emulator managed in this class. |
111 | 141 |
112 Attributes: | 142 Attributes: |
113 emulator: Path of Android's emulator tool. | 143 emulator: Path of Android's emulator tool. |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 logging.critical('emulator _ShutdownOnSignal') | 329 logging.critical('emulator _ShutdownOnSignal') |
300 for sig in self._SIGNALS: | 330 for sig in self._SIGNALS: |
301 signal.signal(sig, signal.SIG_DFL) | 331 signal.signal(sig, signal.SIG_DFL) |
302 self.Shutdown() | 332 self.Shutdown() |
303 raise KeyboardInterrupt # print a stack | 333 raise KeyboardInterrupt # print a stack |
304 | 334 |
305 def _InstallKillHandler(self): | 335 def _InstallKillHandler(self): |
306 """Install a handler to kill the emulator when we exit unexpectedly.""" | 336 """Install a handler to kill the emulator when we exit unexpectedly.""" |
307 for sig in self._SIGNALS: | 337 for sig in self._SIGNALS: |
308 signal.signal(sig, self._ShutdownOnSignal) | 338 signal.signal(sig, self._ShutdownOnSignal) |
309 | |
310 def main(argv): | |
311 Emulator(None, True).Launch(True) | |
312 | |
313 | |
314 if __name__ == '__main__': | |
315 main(sys.argv) | |
OLD | NEW |