OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 """ | 6 """ |
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
8 """ | 8 """ |
9 | 9 |
10 # pylint: disable=C0321 | 10 # pylint: disable=C0321 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 | 134 |
135 class DeviceUtilsTest(mock_calls.TestCase): | 135 class DeviceUtilsTest(mock_calls.TestCase): |
136 | 136 |
137 def setUp(self): | 137 def setUp(self): |
138 self.adb = _AdbWrapperMock('0123456789abcdef') | 138 self.adb = _AdbWrapperMock('0123456789abcdef') |
139 self.device = device_utils.DeviceUtils( | 139 self.device = device_utils.DeviceUtils( |
140 self.adb, default_timeout=10, default_retries=0) | 140 self.adb, default_timeout=10, default_retries=0) |
141 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) | 141 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
142 | 142 |
| 143 def AdbCommandError(self, args=None, output=None, status=None, msg=None): |
| 144 if args is None: |
| 145 args = ['[unspecified]'] |
| 146 return mock.Mock(side_effect=device_errors.AdbCommandFailedError( |
| 147 args, output, status, msg, str(self.device))) |
| 148 |
| 149 def CommandError(self, msg=None): |
| 150 if msg is None: |
| 151 msg = 'Command failed' |
| 152 return mock.Mock(side_effect=device_errors.CommandFailedError( |
| 153 msg, str(self.device))) |
| 154 |
143 def ShellError(self, output=None, status=1): | 155 def ShellError(self, output=None, status=1): |
144 def action(cmd, *args, **kwargs): | 156 def action(cmd, *args, **kwargs): |
145 raise device_errors.AdbShellCommandFailedError( | 157 raise device_errors.AdbShellCommandFailedError( |
146 cmd, output, status, str(self.device)) | 158 cmd, output, status, str(self.device)) |
147 if output is None: | 159 if output is None: |
148 output = 'Permission denied\n' | 160 output = 'Permission denied\n' |
149 return action | 161 return action |
150 | 162 |
151 def TimeoutError(self, msg=None): | 163 def TimeoutError(self, msg=None): |
152 if msg is None: | 164 if msg is None: |
153 msg = 'Operation timed out' | 165 msg = 'Operation timed out' |
154 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | 166 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
155 msg, str(self.device))) | 167 msg, str(self.device))) |
156 | 168 |
157 def CommandError(self, msg=None): | |
158 if msg is None: | |
159 msg = 'Command failed' | |
160 return mock.Mock(side_effect=device_errors.CommandFailedError( | |
161 msg, str(self.device))) | |
162 | |
163 | 169 |
164 class DeviceUtilsEqTest(DeviceUtilsTest): | 170 class DeviceUtilsEqTest(DeviceUtilsTest): |
165 | 171 |
166 def testEq_equal_deviceUtils(self): | 172 def testEq_equal_deviceUtils(self): |
167 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) | 173 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) |
168 self.assertTrue(self.device == other) | 174 self.assertTrue(self.device == other) |
169 self.assertTrue(other == self.device) | 175 self.assertTrue(other == self.device) |
170 | 176 |
171 def testEq_equal_adbWrapper(self): | 177 def testEq_equal_adbWrapper(self): |
172 other = adb_wrapper.AdbWrapper('0123456789abcdef') | 178 other = adb_wrapper.AdbWrapper('0123456789abcdef') |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 def testHasRoot_false(self): | 258 def testHasRoot_false(self): |
253 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()): | 259 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()): |
254 self.assertFalse(self.device.HasRoot()) | 260 self.assertFalse(self.device.HasRoot()) |
255 | 261 |
256 | 262 |
257 class DeviceUtilsEnableRootTest(DeviceUtilsTest): | 263 class DeviceUtilsEnableRootTest(DeviceUtilsTest): |
258 | 264 |
259 def testEnableRoot_succeeds(self): | 265 def testEnableRoot_succeeds(self): |
260 with self.assertCalls( | 266 with self.assertCalls( |
261 (self.call.device.IsUserBuild(), False), | 267 (self.call.device.IsUserBuild(), False), |
262 self.call.adb.Root(), | 268 self.call.adb.Root(), |
263 self.call.adb.WaitForDevice()): | 269 self.call.device.WaitUntilFullyBooted()): |
264 self.device.EnableRoot() | 270 self.device.EnableRoot() |
265 | 271 |
266 def testEnableRoot_userBuild(self): | 272 def testEnableRoot_userBuild(self): |
267 with self.assertCalls( | 273 with self.assertCalls( |
268 (self.call.device.IsUserBuild(), True)): | 274 (self.call.device.IsUserBuild(), True)): |
269 with self.assertRaises(device_errors.CommandFailedError): | 275 with self.assertRaises(device_errors.CommandFailedError): |
270 self.device.EnableRoot() | 276 self.device.EnableRoot() |
271 | 277 |
272 def testEnableRoot_rootFails(self): | 278 def testEnableRoot_rootFails(self): |
273 with self.assertCalls( | 279 with self.assertCalls( |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 # pm_ready | 361 # pm_ready |
356 (self.call.device.GetApplicationPath('android'), | 362 (self.call.device.GetApplicationPath('android'), |
357 'package:/some/fake/path'), | 363 'package:/some/fake/path'), |
358 # boot_completed | 364 # boot_completed |
359 (self.call.device.GetProp('sys.boot_completed'), '1'), | 365 (self.call.device.GetProp('sys.boot_completed'), '1'), |
360 # wifi_enabled | 366 # wifi_enabled |
361 (self.call.adb.Shell('dumpsys wifi'), | 367 (self.call.adb.Shell('dumpsys wifi'), |
362 'stuff\nWi-Fi is enabled\nmore stuff\n')): | 368 'stuff\nWi-Fi is enabled\nmore stuff\n')): |
363 self.device.WaitUntilFullyBooted(wifi=True) | 369 self.device.WaitUntilFullyBooted(wifi=True) |
364 | 370 |
| 371 def testWaitUntilFullyBooted_deviceNotInitiallyAvailable(self): |
| 372 with self.assertCalls( |
| 373 self.call.adb.WaitForDevice(), |
| 374 # sd_card_ready |
| 375 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), |
| 376 # sd_card_ready |
| 377 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), |
| 378 # sd_card_ready |
| 379 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), |
| 380 # sd_card_ready |
| 381 (self.call.device.GetExternalStoragePath(), self.AdbCommandError()), |
| 382 # sd_card_ready |
| 383 (self.call.device.GetExternalStoragePath(), '/fake/storage/path'), |
| 384 (self.call.adb.Shell('test -d /fake/storage/path'), ''), |
| 385 # pm_ready |
| 386 (self.call.device.GetApplicationPath('android'), |
| 387 'package:/some/fake/path'), |
| 388 # boot_completed |
| 389 (self.call.device.GetProp('sys.boot_completed'), '1')): |
| 390 self.device.WaitUntilFullyBooted(wifi=False) |
| 391 |
365 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 392 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): |
366 with self.assertCalls( | 393 with self.assertCalls( |
367 self.call.adb.WaitForDevice(), | 394 self.call.adb.WaitForDevice(), |
368 # sd_card_ready | 395 # sd_card_ready |
369 (self.call.device.GetExternalStoragePath(), self.CommandError())): | 396 (self.call.device.GetExternalStoragePath(), self.CommandError())): |
370 with self.assertRaises(device_errors.CommandFailedError): | 397 with self.assertRaises(device_errors.CommandFailedError): |
371 self.device.WaitUntilFullyBooted(wifi=False) | 398 self.device.WaitUntilFullyBooted(wifi=False) |
372 | 399 |
373 def testWaitUntilFullyBooted_sdCardReadyFails_notExists(self): | 400 def testWaitUntilFullyBooted_sdCardReadyFails_notExists(self): |
374 with self.assertCalls( | 401 with self.assertCalls( |
(...skipping 1298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1673 devices = device_utils.DeviceUtils.HealthyDevices() | 1700 devices = device_utils.DeviceUtils.HealthyDevices() |
1674 self.assertEquals(1, len(devices)) | 1701 self.assertEquals(1, len(devices)) |
1675 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) | 1702 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) |
1676 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) | 1703 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) |
1677 | 1704 |
1678 | 1705 |
1679 if __name__ == '__main__': | 1706 if __name__ == '__main__': |
1680 logging.getLogger().setLevel(logging.DEBUG) | 1707 logging.getLogger().setLevel(logging.DEBUG) |
1681 unittest.main(verbosity=2) | 1708 unittest.main(verbosity=2) |
1682 | 1709 |
OLD | NEW |