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=protected-access | 10 # pylint: disable=protected-access |
11 # pylint: disable=unused-argument | 11 # pylint: disable=unused-argument |
12 | 12 |
13 import logging | 13 import logging |
14 import os | |
15 import sys | 14 import sys |
16 import unittest | 15 import unittest |
17 | 16 |
| 17 from devil import devil_env |
18 from devil.android import device_errors | 18 from devil.android import device_errors |
19 from devil.android import device_signal | 19 from devil.android import device_signal |
20 from devil.android import device_utils | 20 from devil.android import device_utils |
21 from devil.android.sdk import adb_wrapper | 21 from devil.android.sdk import adb_wrapper |
22 from devil.android.sdk import intent | 22 from devil.android.sdk import intent |
23 from devil.android.sdk import version_codes | 23 from devil.android.sdk import version_codes |
24 from devil.utils import cmd_helper | 24 from devil.utils import cmd_helper |
25 from devil.utils import mock_calls | 25 from devil.utils import mock_calls |
26 from pylib import constants | |
27 | 26 |
28 sys.path.append(os.path.join( | 27 devil_env.InitializeForTests() |
29 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 28 sys.path.append(devil_env.instance.pymock_path) |
30 import mock # pylint: disable=F0401 | 29 import mock # pylint: disable=F0401 |
31 | 30 |
32 | 31 |
33 class DeviceUtilsInitTest(unittest.TestCase): | 32 class DeviceUtilsInitTest(unittest.TestCase): |
34 | 33 |
35 def testInitWithStr(self): | 34 def testInitWithStr(self): |
36 serial_as_str = str('0123456789abcdef') | 35 serial_as_str = str('0123456789abcdef') |
37 d = device_utils.DeviceUtils('0123456789abcdef') | 36 d = device_utils.DeviceUtils('0123456789abcdef') |
38 self.assertEqual(serial_as_str, d.adb.GetDeviceSerial()) | 37 self.assertEqual(serial_as_str, d.adb.GetDeviceSerial()) |
39 | 38 |
(...skipping 10 matching lines...) Expand all Loading... |
50 | 49 |
51 def testInitWithMissing_fails(self): | 50 def testInitWithMissing_fails(self): |
52 with self.assertRaises(ValueError): | 51 with self.assertRaises(ValueError): |
53 device_utils.DeviceUtils(None) | 52 device_utils.DeviceUtils(None) |
54 with self.assertRaises(ValueError): | 53 with self.assertRaises(ValueError): |
55 device_utils.DeviceUtils('') | 54 device_utils.DeviceUtils('') |
56 | 55 |
57 | 56 |
58 class DeviceUtilsGetAVDsTest(mock_calls.TestCase): | 57 class DeviceUtilsGetAVDsTest(mock_calls.TestCase): |
59 | 58 |
| 59 @mock.patch('devil.devil_env._Environment.android_sdk_path', |
| 60 mock.PropertyMock(return_value='/my/android/sdk')) |
60 def testGetAVDs(self): | 61 def testGetAVDs(self): |
61 with self.assertCall( | 62 with self.assertCall( |
62 mock.call.devil.utils.cmd_helper.GetCmdOutput( | 63 mock.call.devil.utils.cmd_helper.GetCmdOutput( |
63 [mock.ANY, 'list', 'avd']), | 64 [mock.ANY, 'list', 'avd']), |
64 'Available Android Virtual Devices:\n' | 65 'Available Android Virtual Devices:\n' |
65 ' Name: my_android5.0\n' | 66 ' Name: my_android5.0\n' |
66 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' | 67 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' |
67 ' Target: Android 5.0 (API level 21)\n' | 68 ' Target: Android 5.0 (API level 21)\n' |
68 ' Tag/ABI: default/x86\n' | 69 ' Tag/ABI: default/x86\n' |
69 ' Skin: WVGA800\n'): | 70 ' Skin: WVGA800\n'): |
70 self.assertEquals(['my_android5.0'], | 71 self.assertEquals(['my_android5.0'], device_utils.GetAVDs()) |
71 device_utils.GetAVDs()) | |
72 | 72 |
73 | 73 |
74 class DeviceUtilsRestartServerTest(mock_calls.TestCase): | 74 class DeviceUtilsRestartServerTest(mock_calls.TestCase): |
75 | 75 |
76 @mock.patch('time.sleep', mock.Mock()) | 76 @mock.patch('time.sleep', mock.Mock()) |
77 def testRestartServer_succeeds(self): | 77 def testRestartServer_succeeds(self): |
78 with self.assertCalls( | 78 with self.assertCalls( |
79 mock.call.devil.android.sdk.adb_wrapper.AdbWrapper.KillServer(), | 79 mock.call.devil.android.sdk.adb_wrapper.AdbWrapper.KillServer(), |
80 (mock.call.devil.utils.cmd_helper.GetCmdStatusAndOutput( | 80 (mock.call.devil.utils.cmd_helper.GetCmdStatusAndOutput( |
81 ['pgrep', 'adb']), | 81 ['pgrep', 'adb']), |
(...skipping 2011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2093 def testGrantPermissions_BlackList(self): | 2093 def testGrantPermissions_BlackList(self): |
2094 with self.patch_call( | 2094 with self.patch_call( |
2095 self.call.device.build_version_sdk, return_value=23): | 2095 self.call.device.build_version_sdk, return_value=23): |
2096 self.device.GrantPermissions( | 2096 self.device.GrantPermissions( |
2097 'package', ['android.permission.ACCESS_MOCK_LOCATION']) | 2097 'package', ['android.permission.ACCESS_MOCK_LOCATION']) |
2098 | 2098 |
2099 | 2099 |
2100 if __name__ == '__main__': | 2100 if __name__ == '__main__': |
2101 logging.getLogger().setLevel(logging.DEBUG) | 2101 logging.getLogger().setLevel(logging.DEBUG) |
2102 unittest.main(verbosity=2) | 2102 unittest.main(verbosity=2) |
OLD | NEW |