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 android_commands | |
19 from pylib import cmd_helper | 18 from pylib import cmd_helper |
20 from pylib import constants | 19 from pylib import constants |
21 from pylib import pexpect | 20 from pylib import pexpect |
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 |
(...skipping 54 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 and | 89 running but a device slot is taken. A little bot trouble and and |
91 we're out of room forever. | 90 we're out of room forever. |
92 """ | 91 """ |
93 emulators = android_commands.GetAttachedDevices(hardware=False) | 92 emulators = [d for d in device_utils.HealthyDevices() if d.adb.is_emulator] |
94 if not emulators: | 93 if not emulators: |
95 return | 94 return |
96 for emu_name in emulators: | 95 for e in emulators: |
97 cmd_helper.RunCmd(['adb', '-s', emu_name, 'emu', 'kill']) | 96 e.adb.Emu(['kill']) |
98 logging.info('Emulator killing is async; give a few seconds for all to die.') | 97 logging.info('Emulator killing is async; give a few seconds for all to die.') |
99 for _ in range(5): | 98 for _ in range(5): |
100 if not android_commands.GetAttachedDevices(hardware=False): | 99 if not any(d.adb.is_emulator for d in device_utils.HealthyDevices()): |
101 return | 100 return |
102 time.sleep(1) | 101 time.sleep(1) |
103 | 102 |
104 | 103 |
105 def DeleteAllTempAVDs(): | 104 def DeleteAllTempAVDs(): |
106 """Delete all temporary AVDs which are created for tests. | 105 """Delete all temporary AVDs which are created for tests. |
107 | 106 |
108 If the test exits abnormally and some temporary AVDs created when testing may | 107 If the test exits abnormally and some temporary AVDs created when testing may |
109 be left in the system. Clean these AVDs. | 108 be left in the system. Clean these AVDs. |
110 """ | 109 """ |
(...skipping 23 matching lines...) Expand all Loading... |
134 Cycling through a port start position helps make us resilient.""" | 133 Cycling through a port start position helps make us resilient.""" |
135 ports = range(cls._port_min, cls._port_max, 2) | 134 ports = range(cls._port_min, cls._port_max, 2) |
136 n = cls._port_current_index | 135 n = cls._port_current_index |
137 cls._port_current_index = (n + 1) % len(ports) | 136 cls._port_current_index = (n + 1) % len(ports) |
138 return ports[n:] + ports[:n] | 137 return ports[n:] + ports[:n] |
139 | 138 |
140 | 139 |
141 def _GetAvailablePort(): | 140 def _GetAvailablePort(): |
142 """Returns an available TCP port for the console.""" | 141 """Returns an available TCP port for the console.""" |
143 used_ports = [] | 142 used_ports = [] |
144 emulators = android_commands.GetAttachedDevices(hardware=False) | 143 emulators = [d for d in device_utils.HealthyDevices() if d.adb.is_emulator] |
145 for emulator in emulators: | 144 for emulator in emulators: |
146 used_ports.append(emulator.split('-')[1]) | 145 used_ports.append(emulator.adb.GetDeviceSerial().split('-')[1]) |
147 for port in PortPool.port_range(): | 146 for port in PortPool.port_range(): |
148 if str(port) not in used_ports: | 147 if str(port) not in used_ports: |
149 return port | 148 return port |
150 | 149 |
151 | 150 |
152 def LaunchTempEmulators(emulator_count, abi, api_level, wait_for_boot=True): | 151 def LaunchTempEmulators(emulator_count, abi, api_level, wait_for_boot=True): |
153 """Create and launch temporary emulators and wait for them to boot. | 152 """Create and launch temporary emulators and wait for them to boot. |
154 | 153 |
155 Args: | 154 Args: |
156 emulator_count: number of emulators to launch. | 155 emulator_count: number of emulators to launch. |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 logging.critical('emulator _ShutdownOnSignal') | 432 logging.critical('emulator _ShutdownOnSignal') |
434 for sig in self._SIGNALS: | 433 for sig in self._SIGNALS: |
435 signal.signal(sig, signal.SIG_DFL) | 434 signal.signal(sig, signal.SIG_DFL) |
436 self.Shutdown() | 435 self.Shutdown() |
437 raise KeyboardInterrupt # print a stack | 436 raise KeyboardInterrupt # print a stack |
438 | 437 |
439 def _InstallKillHandler(self): | 438 def _InstallKillHandler(self): |
440 """Install a handler to kill the emulator when we exit unexpectedly.""" | 439 """Install a handler to kill the emulator when we exit unexpectedly.""" |
441 for sig in self._SIGNALS: | 440 for sig in self._SIGNALS: |
442 signal.signal(sig, self._ShutdownOnSignal) | 441 signal.signal(sig, self._ShutdownOnSignal) |
OLD | NEW |