| 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 _EMULATOR_LAUNCH_TIMEOUT = 120 | 85 # the time to launch the emulator and a wait-for-device command. |
| 86 _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 _WAITFORDEVICE_TIMEOUT = 5 |
| 91 |
| 92 # Time to wait for a "wait for boot complete" (property set on device). |
| 93 _WAITFORBOOT_TIMEOUT = 120 |
| 90 | 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 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 | 134 |
| 131 Loop on a wait-for-device with a very small timeout. On each | 135 Loop on a wait-for-device with a very small timeout. On each |
| 132 timeout, check the emulator process is still alive. | 136 timeout, check the emulator process is still alive. |
| 133 After confirming a wait-for-device can be successful, make sure | 137 After confirming a wait-for-device can be successful, make sure |
| 134 it returns the right answer. | 138 it returns the right answer. |
| 135 """ | 139 """ |
| 136 a = android_commands.AndroidCommands(self.device, False) | 140 a = android_commands.AndroidCommands(self.device, False) |
| 137 seconds_waited = 0 | 141 seconds_waited = 0 |
| 138 number_of_waits = 2 # Make sure we can wfd twice | 142 number_of_waits = 2 # Make sure we can wfd twice |
| 139 adb_cmd = "adb -s %s %s" % (self.device, 'wait-for-device') | 143 adb_cmd = "adb -s %s %s" % (self.device, 'wait-for-device') |
| 140 while seconds_waited < self._EMULATOR_LAUNCH_TIMEOUT: | 144 while seconds_waited < self._LAUNCH_TIMEOUT: |
| 141 try: | 145 try: |
| 142 run_command.RunCommand(adb_cmd, timeout_time=self._EMULATOR_WFD_TIMEOUT, | 146 run_command.RunCommand(adb_cmd, |
| 147 timeout_time=self._WAITFORDEVICE_TIMEOUT, |
| 143 retry_count=1) | 148 retry_count=1) |
| 144 number_of_waits -= 1 | 149 number_of_waits -= 1 |
| 145 if not number_of_waits: | 150 if not number_of_waits: |
| 146 break | 151 break |
| 147 except errors.WaitForResponseTimedOutError as e: | 152 except errors.WaitForResponseTimedOutError as e: |
| 148 seconds_waited += self._EMULATOR_WFD_TIMEOUT | 153 seconds_waited += self._WAITFORDEVICE_TIMEOUT |
| 149 adb_cmd = "adb -s %s %s" % (self.device, 'kill-server') | 154 adb_cmd = "adb -s %s %s" % (self.device, 'kill-server') |
| 150 run_command.RunCommand(adb_cmd) | 155 run_command.RunCommand(adb_cmd) |
| 151 self.popen.poll() | 156 self.popen.poll() |
| 152 if self.popen.returncode != None: | 157 if self.popen.returncode != None: |
| 153 raise EmulatorLaunchException('EMULATOR DIED') | 158 raise EmulatorLaunchException('EMULATOR DIED') |
| 154 if seconds_waited >= self._EMULATOR_LAUNCH_TIMEOUT: | 159 if seconds_waited >= self._LAUNCH_TIMEOUT: |
| 155 raise EmulatorLaunchException('TIMEOUT with wait-for-device') | 160 raise EmulatorLaunchException('TIMEOUT with wait-for-device') |
| 156 logging.info('Seconds waited on wait-for-device: %d', seconds_waited) | 161 logging.info('Seconds waited on wait-for-device: %d', seconds_waited) |
| 157 # Now that we checked for obvious problems, wait for a boot complete. | 162 # Now that we checked for obvious problems, wait for a boot complete. |
| 158 # Waiting for the package manager has been problematic. | 163 # Waiting for the package manager has been problematic. |
| 159 a.Adb().WaitForBootComplete() | 164 a.Adb().SetTargetSerial(self.device) |
| 165 a.Adb().WaitForBootComplete(self._WAITFORBOOT_TIMEOUT) |
| 160 | 166 |
| 161 def Shutdown(self): | 167 def Shutdown(self): |
| 162 """Shuts down the process started by launch.""" | 168 """Shuts down the process started by launch.""" |
| 163 if self.popen: | 169 if self.popen: |
| 164 self.popen.poll() | 170 self.popen.poll() |
| 165 if self.popen.returncode == None: | 171 if self.popen.returncode == None: |
| 166 self.popen.kill() | 172 self.popen.kill() |
| 167 self.popen = None | 173 self.popen = None |
| 168 | 174 |
| 169 def _ShutdownOnSignal(self, signum, frame): | 175 def _ShutdownOnSignal(self, signum, frame): |
| 170 logging.critical('emulator _ShutdownOnSignal') | 176 logging.critical('emulator _ShutdownOnSignal') |
| 171 for sig in self._SIGNALS: | 177 for sig in self._SIGNALS: |
| 172 signal.signal(sig, signal.SIG_DFL) | 178 signal.signal(sig, signal.SIG_DFL) |
| 173 self.Shutdown() | 179 self.Shutdown() |
| 174 raise KeyboardInterrupt # print a stack | 180 raise KeyboardInterrupt # print a stack |
| 175 | 181 |
| 176 def _InstallKillHandler(self): | 182 def _InstallKillHandler(self): |
| 177 """Install a handler to kill the emulator when we exit unexpectedly.""" | 183 """Install a handler to kill the emulator when we exit unexpectedly.""" |
| 178 for sig in self._SIGNALS: | 184 for sig in self._SIGNALS: |
| 179 signal.signal(sig, self._ShutdownOnSignal) | 185 signal.signal(sig, self._ShutdownOnSignal) |
| 180 | 186 |
| 181 def main(argv): | 187 def main(argv): |
| 182 Emulator().launch() | 188 Emulator().launch() |
| 183 | 189 |
| 184 | 190 |
| 185 if __name__ == '__main__': | 191 if __name__ == '__main__': |
| 186 main(sys.argv) | 192 main(sys.argv) |
| OLD | NEW |