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 |
11 import logging | 11 import logging |
12 import os | 12 import os |
13 import signal | 13 import signal |
14 import subprocess | 14 import subprocess |
15 import time | 15 import time |
16 | 16 |
17 # TODO(craigdh): Move these pylib dependencies to pylib/utils/. | 17 # TODO(craigdh): Move these pylib dependencies to pylib/utils/. |
18 from pylib import cmd_helper | 18 from pylib import cmd_helper |
19 from pylib import constants | 19 from pylib import constants |
20 from pylib import pexpect | 20 from pylib import pexpect |
21 from pylib.device import adb_wrapper | |
22 from pylib.device import device_errors | 21 from pylib.device import device_errors |
23 from pylib.device import device_utils | 22 from pylib.device import device_utils |
24 from pylib.utils import time_profile | 23 from pylib.utils import time_profile |
25 | 24 |
26 import errors | 25 import errors |
27 import run_command | 26 import run_command |
28 | 27 |
29 # SD card size | 28 # SD card size |
30 SDCARD_SIZE = '512M' | 29 SDCARD_SIZE = '512M' |
31 | 30 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 """Emulator failed to launch.""" | 82 """Emulator failed to launch.""" |
84 pass | 83 pass |
85 | 84 |
86 def _KillAllEmulators(): | 85 def _KillAllEmulators(): |
87 """Kill all running emulators that look like ones we started. | 86 """Kill all running emulators that look like ones we started. |
88 | 87 |
89 There are odd 'sticky' cases where there can be no emulator process | 88 There are odd 'sticky' cases where there can be no emulator process |
90 running but a device slot is taken. A little bot trouble and we're out of | 89 running but a device slot is taken. A little bot trouble and we're out of |
91 room forever. | 90 room forever. |
92 """ | 91 """ |
93 emulators = [device_utils.DeviceUtils(a) | 92 emulators = [d for d in device_utils.DeviceUtils.HealthyDevices() |
94 for a in adb_wrapper.AdbWrapper.Devices() | 93 if d.adb.is_emulator] |
95 if a.is_emulator] | |
96 if not emulators: | 94 if not emulators: |
97 return | 95 return |
98 for e in emulators: | 96 for e in emulators: |
99 e.adb.Emu(['kill']) | 97 e.adb.Emu(['kill']) |
100 logging.info('Emulator killing is async; give a few seconds for all to die.') | 98 logging.info('Emulator killing is async; give a few seconds for all to die.') |
101 for _ in range(5): | 99 for _ in range(5): |
102 if not any(a.is_emulator for a in adb_wrapper.AdbWrapper.Devices()): | 100 if not any(d.adb.is_emulator for d |
| 101 in device_utils.DeviceUtils.HealthyDevices()): |
103 return | 102 return |
104 time.sleep(1) | 103 time.sleep(1) |
105 | 104 |
106 | 105 |
107 def DeleteAllTempAVDs(): | 106 def DeleteAllTempAVDs(): |
108 """Delete all temporary AVDs which are created for tests. | 107 """Delete all temporary AVDs which are created for tests. |
109 | 108 |
110 If the test exits abnormally and some temporary AVDs created when testing may | 109 If the test exits abnormally and some temporary AVDs created when testing may |
111 be left in the system. Clean these AVDs. | 110 be left in the system. Clean these AVDs. |
112 """ | 111 """ |
(...skipping 23 matching lines...) Expand all Loading... |
136 Cycling through a port start position helps make us resilient.""" | 135 Cycling through a port start position helps make us resilient.""" |
137 ports = range(cls._port_min, cls._port_max, 2) | 136 ports = range(cls._port_min, cls._port_max, 2) |
138 n = cls._port_current_index | 137 n = cls._port_current_index |
139 cls._port_current_index = (n + 1) % len(ports) | 138 cls._port_current_index = (n + 1) % len(ports) |
140 return ports[n:] + ports[:n] | 139 return ports[n:] + ports[:n] |
141 | 140 |
142 | 141 |
143 def _GetAvailablePort(): | 142 def _GetAvailablePort(): |
144 """Returns an available TCP port for the console.""" | 143 """Returns an available TCP port for the console.""" |
145 used_ports = [] | 144 used_ports = [] |
146 emulators = [device_utils.DeviceUtils(a) | 145 emulators = [d for d in device_utils.DeviceUtils.HealthyDevices() |
147 for a in adb_wrapper.AdbWrapper.Devices() | 146 if d.adb.is_emulator] |
148 if a.is_emulator] | |
149 for emulator in emulators: | 147 for emulator in emulators: |
150 used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1]) | 148 used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1]) |
151 for port in PortPool.port_range(): | 149 for port in PortPool.port_range(): |
152 if str(port) not in used_ports: | 150 if str(port) not in used_ports: |
153 return port | 151 return port |
154 | 152 |
155 | 153 |
156 def LaunchTempEmulators(emulator_count, abi, api_level, wait_for_boot=True): | 154 def LaunchTempEmulators(emulator_count, abi, api_level, wait_for_boot=True): |
157 """Create and launch temporary emulators and wait for them to boot. | 155 """Create and launch temporary emulators and wait for them to boot. |
158 | 156 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 logging.critical('emulator _ShutdownOnSignal') | 435 logging.critical('emulator _ShutdownOnSignal') |
438 for sig in self._SIGNALS: | 436 for sig in self._SIGNALS: |
439 signal.signal(sig, signal.SIG_DFL) | 437 signal.signal(sig, signal.SIG_DFL) |
440 self.Shutdown() | 438 self.Shutdown() |
441 raise KeyboardInterrupt # print a stack | 439 raise KeyboardInterrupt # print a stack |
442 | 440 |
443 def _InstallKillHandler(self): | 441 def _InstallKillHandler(self): |
444 """Install a handler to kill the emulator when we exit unexpectedly.""" | 442 """Install a handler to kill the emulator when we exit unexpectedly.""" |
445 for sig in self._SIGNALS: | 443 for sig in self._SIGNALS: |
446 signal.signal(sig, self._ShutdownOnSignal) | 444 signal.signal(sig, self._ShutdownOnSignal) |
OLD | NEW |