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 sys.path.append(devil_env.config.pymock_path) |
29 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | |
30 import mock # pylint: disable=F0401 | 28 import mock # pylint: disable=F0401 |
31 | 29 |
32 | 30 |
33 class _MockApkHelper(object): | 31 class _MockApkHelper(object): |
34 def __init__(self, path, package_name, perms=None): | 32 def __init__(self, path, package_name, perms=None): |
35 self.path = path | 33 self.path = path |
36 self.package_name = package_name | 34 self.package_name = package_name |
37 self.perms = perms | 35 self.perms = perms |
38 | 36 |
39 def GetPackageName(self): | 37 def GetPackageName(self): |
(...skipping 24 matching lines...) Expand all Loading... |
64 def testInitWithMissing_fails(self): | 62 def testInitWithMissing_fails(self): |
65 with self.assertRaises(ValueError): | 63 with self.assertRaises(ValueError): |
66 device_utils.DeviceUtils(None) | 64 device_utils.DeviceUtils(None) |
67 with self.assertRaises(ValueError): | 65 with self.assertRaises(ValueError): |
68 device_utils.DeviceUtils('') | 66 device_utils.DeviceUtils('') |
69 | 67 |
70 | 68 |
71 class DeviceUtilsGetAVDsTest(mock_calls.TestCase): | 69 class DeviceUtilsGetAVDsTest(mock_calls.TestCase): |
72 | 70 |
73 def testGetAVDs(self): | 71 def testGetAVDs(self): |
74 with self.assertCall( | 72 mocked_attrs = { |
75 mock.call.devil.utils.cmd_helper.GetCmdOutput( | 73 'android_sdk_path': '/my/sdk/path' |
76 [mock.ANY, 'list', 'avd']), | 74 } |
77 'Available Android Virtual Devices:\n' | 75 with mock.patch('devil.devil_env._Environment.__getattr__', |
78 ' Name: my_android5.0\n' | 76 mock.Mock(side_effect=lambda a: mocked_attrs[a])): |
79 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' | 77 with self.assertCall( |
80 ' Target: Android 5.0 (API level 21)\n' | 78 mock.call.devil.utils.cmd_helper.GetCmdOutput( |
81 ' Tag/ABI: default/x86\n' | 79 [mock.ANY, 'list', 'avd']), |
82 ' Skin: WVGA800\n'): | 80 'Available Android Virtual Devices:\n' |
83 self.assertEquals(['my_android5.0'], | 81 ' Name: my_android5.0\n' |
84 device_utils.GetAVDs()) | 82 ' Path: /some/path/to/.android/avd/my_android5.0.avd\n' |
| 83 ' Target: Android 5.0 (API level 21)\n' |
| 84 ' Tag/ABI: default/x86\n' |
| 85 ' Skin: WVGA800\n'): |
| 86 self.assertEquals(['my_android5.0'], device_utils.GetAVDs()) |
85 | 87 |
86 | 88 |
87 class DeviceUtilsRestartServerTest(mock_calls.TestCase): | 89 class DeviceUtilsRestartServerTest(mock_calls.TestCase): |
88 | 90 |
89 @mock.patch('time.sleep', mock.Mock()) | 91 @mock.patch('time.sleep', mock.Mock()) |
90 def testRestartServer_succeeds(self): | 92 def testRestartServer_succeeds(self): |
91 with self.assertCalls( | 93 with self.assertCalls( |
92 mock.call.devil.android.sdk.adb_wrapper.AdbWrapper.KillServer(), | 94 mock.call.devil.android.sdk.adb_wrapper.AdbWrapper.KillServer(), |
93 (mock.call.devil.utils.cmd_helper.GetCmdStatusAndOutput( | 95 (mock.call.devil.utils.cmd_helper.GetCmdStatusAndOutput( |
94 ['pgrep', 'adb']), | 96 ['pgrep', 'adb']), |
(...skipping 2035 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2130 def testGrantPermissions_BlackList(self): | 2132 def testGrantPermissions_BlackList(self): |
2131 with self.patch_call( | 2133 with self.patch_call( |
2132 self.call.device.build_version_sdk, return_value=23): | 2134 self.call.device.build_version_sdk, return_value=23): |
2133 self.device.GrantPermissions( | 2135 self.device.GrantPermissions( |
2134 'package', ['android.permission.ACCESS_MOCK_LOCATION']) | 2136 'package', ['android.permission.ACCESS_MOCK_LOCATION']) |
2135 | 2137 |
2136 | 2138 |
2137 if __name__ == '__main__': | 2139 if __name__ == '__main__': |
2138 logging.getLogger().setLevel(logging.DEBUG) | 2140 logging.getLogger().setLevel(logging.DEBUG) |
2139 unittest.main(verbosity=2) | 2141 unittest.main(verbosity=2) |
OLD | NEW |