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 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 74 |
75 Attributes: | 75 Attributes: |
76 emulator: Path of Android's emulator tool. | 76 emulator: Path of Android's emulator tool. |
77 popen: Popen object of the running emulator process. | 77 popen: Popen object of the running emulator process. |
78 device: Device name of this emulator. | 78 device: Device name of this emulator. |
79 """ | 79 """ |
80 | 80 |
81 # Signals we listen for to kill the emulator on | 81 # Signals we listen for to kill the emulator on |
82 _SIGNALS = (signal.SIGINT, signal.SIGHUP) | 82 _SIGNALS = (signal.SIGINT, signal.SIGHUP) |
83 | 83 |
84 # Time to wait for an emulator launch, in seconds. | 84 # Time to wait for an emulator launch, in seconds. This includes |
85 # the time to launch the emulator and a wait-for-device command. | |
85 _EMULATOR_LAUNCH_TIMEOUT = 120 | 86 _EMULATOR_LAUNCH_TIMEOUT = 120 |
86 | 87 |
87 # Timeout interval of wait-for-device command before bouncing to a a | 88 # Timeout interval of wait-for-device command before bouncing to a a |
88 # process life check. | 89 # process life check. |
89 _EMULATOR_WFD_TIMEOUT = 5 | 90 _EMULATOR_WFD_TIMEOUT = 5 |
90 | 91 |
92 # Time to wait for a "wait for boot complete" (property set on device). | |
93 _EMULATOR_WFBC_TIMEOUT = 120 | |
bulach
2011/12/07 06:27:09
nit: this class is called "Emulator", so you could
| |
94 | |
91 def __init__(self): | 95 def __init__(self): |
92 try: | 96 try: |
93 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] | 97 android_sdk_root = os.environ['ANDROID_SDK_ROOT'] |
94 except KeyError: | 98 except KeyError: |
95 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' | 99 logging.critical('The ANDROID_SDK_ROOT must be set to run the test on ' |
96 'emulator.') | 100 'emulator.') |
97 raise | 101 raise |
98 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') | 102 self.emulator = os.path.join(android_sdk_root, 'tools', 'emulator') |
99 self.popen = None | 103 self.popen = None |
100 self.device = None | 104 self.device = None |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 adb_cmd = "adb -s %s %s" % (self.device, 'kill-server') | 153 adb_cmd = "adb -s %s %s" % (self.device, 'kill-server') |
150 run_command.RunCommand(adb_cmd) | 154 run_command.RunCommand(adb_cmd) |
151 self.popen.poll() | 155 self.popen.poll() |
152 if self.popen.returncode != None: | 156 if self.popen.returncode != None: |
153 raise EmulatorLaunchException('EMULATOR DIED') | 157 raise EmulatorLaunchException('EMULATOR DIED') |
154 if seconds_waited >= self._EMULATOR_LAUNCH_TIMEOUT: | 158 if seconds_waited >= self._EMULATOR_LAUNCH_TIMEOUT: |
155 raise EmulatorLaunchException('TIMEOUT with wait-for-device') | 159 raise EmulatorLaunchException('TIMEOUT with wait-for-device') |
156 logging.info('Seconds waited on wait-for-device: %d', seconds_waited) | 160 logging.info('Seconds waited on wait-for-device: %d', seconds_waited) |
157 # Now that we checked for obvious problems, wait for a boot complete. | 161 # Now that we checked for obvious problems, wait for a boot complete. |
158 # Waiting for the package manager has been problematic. | 162 # Waiting for the package manager has been problematic. |
159 a.Adb().WaitForBootComplete() | 163 a.Adb().SetTargetSerial(self.device) |
164 a.Adb().WaitForBootComplete(self._EMULATOR_WFBC_TIMEOUT) | |
160 | 165 |
161 def Shutdown(self): | 166 def Shutdown(self): |
162 """Shuts down the process started by launch.""" | 167 """Shuts down the process started by launch.""" |
163 if self.popen: | 168 if self.popen: |
164 self.popen.poll() | 169 self.popen.poll() |
165 if self.popen.returncode == None: | 170 if self.popen.returncode == None: |
166 self.popen.kill() | 171 self.popen.kill() |
167 self.popen = None | 172 self.popen = None |
168 | 173 |
169 def _ShutdownOnSignal(self, signum, frame): | 174 def _ShutdownOnSignal(self, signum, frame): |
170 logging.critical('emulator _ShutdownOnSignal') | 175 logging.critical('emulator _ShutdownOnSignal') |
171 for sig in self._SIGNALS: | 176 for sig in self._SIGNALS: |
172 signal.signal(sig, signal.SIG_DFL) | 177 signal.signal(sig, signal.SIG_DFL) |
173 self.Shutdown() | 178 self.Shutdown() |
174 raise KeyboardInterrupt # print a stack | 179 raise KeyboardInterrupt # print a stack |
175 | 180 |
176 def _InstallKillHandler(self): | 181 def _InstallKillHandler(self): |
177 """Install a handler to kill the emulator when we exit unexpectedly.""" | 182 """Install a handler to kill the emulator when we exit unexpectedly.""" |
178 for sig in self._SIGNALS: | 183 for sig in self._SIGNALS: |
179 signal.signal(sig, self._ShutdownOnSignal) | 184 signal.signal(sig, self._ShutdownOnSignal) |
180 | 185 |
181 def main(argv): | 186 def main(argv): |
182 Emulator().launch() | 187 Emulator().launch() |
183 | 188 |
184 | 189 |
185 if __name__ == '__main__': | 190 if __name__ == '__main__': |
186 main(sys.argv) | 191 main(sys.argv) |
OLD | NEW |