OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Provides an interface to start and stop Android emulator. |
| 7 |
| 8 Assumes system environment ANDROID_NDK_ROOT has been set. |
| 9 |
| 10 Emulator: The class provides the methods to launch/shutdown the emulator with |
| 11 the android virtual device named 'buildbot' . |
| 12 """ |
| 13 |
| 14 import logging |
| 15 import os |
| 16 import subprocess |
| 17 import sys |
| 18 |
| 19 import android_commands |
| 20 |
| 21 |
| 22 def _GetAvailablePort(): |
| 23 """Returns an available TCP port for the console.""" |
| 24 used_ports = [] |
| 25 emulators = android_commands.GetEmulators() |
| 26 for emulator in emulators: |
| 27 used_ports.append(emulator.split('-')[1]) |
| 28 # The port must be an even number between 5554 and 5584. |
| 29 for port in range(5554, 5585, 2): |
| 30 if str(port) not in used_ports: |
| 31 return port |
| 32 |
| 33 |
| 34 class Emulator(object): |
| 35 """Provides the methods to lanuch/shutdown the emulator. |
| 36 |
| 37 The emulator has the android virtual device named 'buildbot'. |
| 38 |
| 39 The emulator could use any even TCP port between 5554 and 5584 for the |
| 40 console communication, and this port will be part of the device name like |
| 41 'emulator-5554'. Assume it is always True, as the device name is the id of |
| 42 emulator managed in this class. |
| 43 |
| 44 Attributes: |
| 45 emulator: Path of Android's emulator tool. |
| 46 popen: Popen object of the running emulator process. |
| 47 device: Device name of this emulator. |
| 48 """ |
| 49 |
| 50 def __init__(self): |
| 51 try: |
| 52 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] |
| 53 except KeyError: |
| 54 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' |
| 55 'emulator.') |
| 56 raise |
| 57 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
| 58 self.popen = None |
| 59 self.device = None |
| 60 |
| 61 def Launch(self): |
| 62 """Launches the emulator and waits for package manager to startup. |
| 63 |
| 64 If fails, an exception will be raised. |
| 65 """ |
| 66 port = _GetAvailablePort() |
| 67 self.device = "emulator-%d" % port |
| 68 self.popen = subprocess.Popen(args=[self.emulator, '-avd', 'buildbot', |
| 69 '-port', str(port)]) |
| 70 # This will not return until device's package manager starts up or an |
| 71 # exception is raised. |
| 72 android_commands.AndroidCommands(self.device, True) |
| 73 |
| 74 def Shutdown(self): |
| 75 """Shuts down the process started by launch.""" |
| 76 self.popen.terminate() |
| 77 |
| 78 |
| 79 def main(argv): |
| 80 Emulator().launch() |
| 81 |
| 82 |
| 83 if __name__ == '__main__': |
| 84 main(sys.argv) |
OLD | NEW |