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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 'Available Android Virtual Devices:\n' | 77 'Available Android Virtual Devices:\n' |
78 ' Name: my_android5.0\n' | 78 ' Name: my_android5.0\n' |
79 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' | 79 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' |
80 ' Target: Android 5.0 (API level 21)\n' | 80 ' Target: Android 5.0 (API level 21)\n' |
81 ' Tag/ABI: default/x86\n' | 81 ' Tag/ABI: default/x86\n' |
82 ' Skin: WVGA800\n'): | 82 ' Skin: WVGA800\n'): |
83 self.assertEquals(['my_android5.0'], | 83 self.assertEquals(['my_android5.0'], |
84 device_utils.GetAVDs()) | 84 device_utils.GetAVDs()) |
85 | 85 |
86 | 86 |
| 87 class DeviceUtilsRestartServerTest(mock_calls.TestCase): |
| 88 |
| 89 @mock.patch('time.sleep', mock.Mock()) |
| 90 def testRestartServer_succeeds(self): |
| 91 with self.assertCalls( |
| 92 mock.call.pylib.device.adb_wrapper.AdbWrapper.KillServer(), |
| 93 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), |
| 94 (1, '')), |
| 95 mock.call.pylib.device.adb_wrapper.AdbWrapper.StartServer(), |
| 96 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), |
| 97 (1, '')), |
| 98 (mock.call.pylib.cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']), |
| 99 (0, '123\n'))): |
| 100 device_utils.RestartServer() |
| 101 |
| 102 |
87 class MockTempFile(object): | 103 class MockTempFile(object): |
88 | 104 |
89 def __init__(self, name='/tmp/some/file'): | 105 def __init__(self, name='/tmp/some/file'): |
90 self.file = mock.MagicMock(spec=file) | 106 self.file = mock.MagicMock(spec=file) |
91 self.file.name = name | 107 self.file.name = name |
92 | 108 |
93 def __enter__(self): | 109 def __enter__(self): |
94 return self.file | 110 return self.file |
95 | 111 |
96 def __exit__(self, exc_type, exc_val, exc_tb): | 112 def __exit__(self, exc_type, exc_val, exc_tb): |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 200 |
185 | 201 |
186 class DeviceUtilsNewImplTest(mock_calls.TestCase): | 202 class DeviceUtilsNewImplTest(mock_calls.TestCase): |
187 | 203 |
188 def setUp(self): | 204 def setUp(self): |
189 self.adb = _AdbWrapperMock('0123456789abcdef') | 205 self.adb = _AdbWrapperMock('0123456789abcdef') |
190 self.device = device_utils.DeviceUtils( | 206 self.device = device_utils.DeviceUtils( |
191 self.adb, default_timeout=10, default_retries=0) | 207 self.adb, default_timeout=10, default_retries=0) |
192 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) | 208 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial']) |
193 | 209 |
194 def ShellError(self, output=None, exit_code=1): | 210 def ShellError(self, output=None, status=1): |
195 def action(cmd, *args, **kwargs): | 211 def action(cmd, *args, **kwargs): |
196 raise device_errors.AdbCommandFailedError( | 212 raise device_errors.AdbShellCommandFailedError( |
197 cmd, output, exit_code, str(self.device)) | 213 cmd, output, status, str(self.device)) |
198 if output is None: | 214 if output is None: |
199 output = 'Permission denied\n' | 215 output = 'Permission denied\n' |
200 return action | 216 return action |
201 | 217 |
202 def TimeoutError(self, msg=None): | 218 def TimeoutError(self, msg=None): |
203 if msg is None: | 219 if msg is None: |
204 msg = 'Operation timed out' | 220 msg = 'Operation timed out' |
205 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | 221 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
206 msg, str(self.device))) | 222 msg, str(self.device))) |
207 | 223 |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 def testBroadcastIntent_withExtra_noValue(self): | 901 def testBroadcastIntent_withExtra_noValue(self): |
886 test_intent = intent.Intent(action='test.package.with.an.INTENT', | 902 test_intent = intent.Intent(action='test.package.with.an.INTENT', |
887 extras={'foo': None}) | 903 extras={'foo': None}) |
888 with self.assertCall( | 904 with self.assertCall( |
889 self.call.adb.Shell( | 905 self.call.adb.Shell( |
890 'am broadcast -a test.package.with.an.INTENT --esn foo'), | 906 'am broadcast -a test.package.with.an.INTENT --esn foo'), |
891 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 907 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
892 self.device.BroadcastIntent(test_intent) | 908 self.device.BroadcastIntent(test_intent) |
893 | 909 |
894 | 910 |
895 class DeviceUtilsGoHomeTest(DeviceUtilsOldImplTest): | 911 class DeviceUtilsGoHomeTest(DeviceUtilsNewImplTest): |
896 | 912 |
897 def testGoHome(self): | 913 def testGoHome(self): |
898 with self.assertCalls( | 914 with self.assertCall( |
899 "adb -s 0123456789abcdef shell 'am start " | 915 self.call.adb.Shell('am start -W -a android.intent.action.MAIN ' |
900 "-W " | 916 '-c android.intent.category.HOME'), |
901 "-a android.intent.action.MAIN " | |
902 "-c android.intent.category.HOME'", | |
903 'Starting: Intent { act=android.intent.action.MAIN }\r\n'): | 917 'Starting: Intent { act=android.intent.action.MAIN }\r\n'): |
904 self.device.GoHome() | 918 self.device.GoHome() |
905 | 919 |
906 | 920 |
907 class DeviceUtilsForceStopTest(DeviceUtilsNewImplTest): | 921 class DeviceUtilsForceStopTest(DeviceUtilsNewImplTest): |
908 | 922 |
909 def testForceStop(self): | 923 def testForceStop(self): |
910 with self.assertCall( | 924 with self.assertCall( |
911 self.call.adb.Shell('am force-stop this.is.a.test.package'), | 925 self.call.adb.Shell('am force-stop this.is.a.test.package'), |
912 ''): | 926 ''): |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1451 self.assertTrue( | 1465 self.assertTrue( |
1452 isinstance(device, device_utils.DeviceUtils) | 1466 isinstance(device, device_utils.DeviceUtils) |
1453 and serial == str(device), | 1467 and serial == str(device), |
1454 'Expected a DeviceUtils object with serial %s' % serial) | 1468 'Expected a DeviceUtils object with serial %s' % serial) |
1455 | 1469 |
1456 | 1470 |
1457 if __name__ == '__main__': | 1471 if __name__ == '__main__': |
1458 logging.getLogger().setLevel(logging.DEBUG) | 1472 logging.getLogger().setLevel(logging.DEBUG) |
1459 unittest.main(verbosity=2) | 1473 unittest.main(verbosity=2) |
1460 | 1474 |
OLD | NEW |