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 |
11 the android virtual device named 'buildbot' . | 11 the android virtual device named 'buildbot' . |
12 """ | 12 """ |
13 | 13 |
14 import logging | 14 import logging |
15 import os | 15 import os |
16 import subprocess | 16 import subprocess |
17 import sys | 17 import sys |
18 | 18 |
19 import android_commands | 19 import android_commands |
20 | 20 |
| 21 # adb_interface.py is under ../../third_party/android/testrunner/ |
| 22 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', |
| 23 '..', 'third_party', 'android', 'testrunner')) |
| 24 import adb_interface |
| 25 |
21 | 26 |
22 def _GetAvailablePort(): | 27 def _GetAvailablePort(): |
23 """Returns an available TCP port for the console.""" | 28 """Returns an available TCP port for the console.""" |
24 used_ports = [] | 29 used_ports = [] |
25 emulators = android_commands.GetEmulators() | 30 emulators = android_commands.GetEmulators() |
26 for emulator in emulators: | 31 for emulator in emulators: |
27 used_ports.append(emulator.split('-')[1]) | 32 used_ports.append(emulator.split('-')[1]) |
28 # The port must be an even number between 5554 and 5584. | 33 # The port must be an even number between 5554 and 5584. |
29 for port in range(5554, 5585, 2): | 34 for port in range(5554, 5585, 2): |
30 if str(port) not in used_ports: | 35 if str(port) not in used_ports: |
(...skipping 20 matching lines...) Expand all Loading... |
51 try: | 56 try: |
52 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] | 57 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] |
53 except KeyError: | 58 except KeyError: |
54 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' | 59 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' |
55 'emulator.') | 60 'emulator.') |
56 raise | 61 raise |
57 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') | 62 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
58 self.popen = None | 63 self.popen = None |
59 self.device = None | 64 self.device = None |
60 | 65 |
| 66 def Reset(self): |
| 67 """Kill a running emulator. |
| 68 |
| 69 May be needed if the test scripts stopped abnormally and an |
| 70 emulator is left around.""" |
| 71 adb = adb_interface.AdbInterface() |
| 72 logging.info('Killing any existing emulator.') |
| 73 adb.SendCommand('emu kill') |
| 74 |
61 def Launch(self): | 75 def Launch(self): |
62 """Launches the emulator and waits for package manager to startup. | 76 """Launches the emulator and waits for package manager to startup. |
63 | 77 |
64 If fails, an exception will be raised. | 78 If fails, an exception will be raised. |
65 """ | 79 """ |
66 port = _GetAvailablePort() | 80 port = _GetAvailablePort() |
67 self.device = "emulator-%d" % port | 81 self.device = "emulator-%d" % port |
68 self.popen = subprocess.Popen(args=[self.emulator, '-avd', 'buildbot', | 82 self.popen = subprocess.Popen(args=[ |
69 '-port', str(port)]) | 83 self.emulator, |
| 84 # Speed up emulator launch by 40%. Really. |
| 85 '-no-boot-anim', |
| 86 # The default /data size is 64M. |
| 87 # That's not enough for 4 unit test bundles and their data. |
| 88 '-partition-size', '256', |
| 89 # Use a familiar name and port. |
| 90 '-avd', 'buildbot', |
| 91 '-port', str(port)]) |
70 # This will not return until device's package manager starts up or an | 92 # This will not return until device's package manager starts up or an |
71 # exception is raised. | 93 # exception is raised. |
72 android_commands.AndroidCommands(self.device, True) | 94 android_commands.AndroidCommands(self.device, True) |
73 | 95 |
74 def Shutdown(self): | 96 def Shutdown(self): |
75 """Shuts down the process started by launch.""" | 97 """Shuts down the process started by launch.""" |
76 self.popen.terminate() | 98 self.popen.terminate() |
77 | 99 |
78 | 100 |
79 def main(argv): | 101 def main(argv): |
80 Emulator().launch() | 102 Emulator().launch() |
81 | 103 |
82 | 104 |
83 if __name__ == '__main__': | 105 if __name__ == '__main__': |
84 main(sys.argv) | 106 main(sys.argv) |
OLD | NEW |