OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
7 """ | 7 """ |
8 | 8 |
9 # pylint: disable=C0321 | 9 # pylint: disable=C0321 |
10 # pylint: disable=W0212 | 10 # pylint: disable=W0212 |
11 # pylint: disable=W0613 | 11 # pylint: disable=W0613 |
12 | 12 |
13 import collections | 13 import collections |
| 14 import datetime |
| 15 import logging |
14 import os | 16 import os |
15 import re | 17 import re |
16 import signal | 18 import signal |
17 import sys | 19 import sys |
18 import unittest | 20 import unittest |
19 | 21 |
20 from pylib import android_commands | 22 from pylib import android_commands |
21 from pylib import constants | 23 from pylib import constants |
22 from pylib.device import adb_wrapper | 24 from pylib.device import adb_wrapper |
23 from pylib.device import device_errors | 25 from pylib.device import device_errors |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 serial = '0fedcba987654321' | 57 serial = '0fedcba987654321' |
56 a = android_commands.AndroidCommands(device=serial) | 58 a = android_commands.AndroidCommands(device=serial) |
57 d = device_utils.DeviceUtils(a) | 59 d = device_utils.DeviceUtils(a) |
58 self.assertEqual(serial, d.old_interface.GetDevice()) | 60 self.assertEqual(serial, d.old_interface.GetDevice()) |
59 | 61 |
60 def testInitWithNone(self): | 62 def testInitWithNone(self): |
61 d = device_utils.DeviceUtils(None) | 63 d = device_utils.DeviceUtils(None) |
62 self.assertIsNone(d.old_interface.GetDevice()) | 64 self.assertIsNone(d.old_interface.GetDevice()) |
63 | 65 |
64 | 66 |
| 67 # TODO(jbudorick) Split this into separate classes by DeviceUtils function. |
65 class DeviceUtilsOldImplTest(unittest.TestCase): | 68 class DeviceUtilsOldImplTest(unittest.TestCase): |
66 | 69 |
67 class AndroidCommandsCalls(object): | 70 class AndroidCommandsCalls(object): |
68 | 71 |
69 def __init__(self, test_case, cmd_ret, comp): | 72 def __init__(self, test_case, cmd_ret, comp): |
70 self._cmds = cmd_ret | 73 self._cmds = cmd_ret |
71 self._comp = comp | 74 self._comp = comp |
72 self._test_case = test_case | 75 self._test_case = test_case |
73 self._total_received = 0 | 76 self._total_received = 0 |
74 | 77 |
(...skipping 1105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 self.device.WriteFile('/test/file/written.to.device', | 1183 self.device.WriteFile('/test/file/written.to.device', |
1181 'new test file contents', as_root=True) | 1184 'new test file contents', as_root=True) |
1182 | 1185 |
1183 def testWriteFile_asRoot_rejected(self): | 1186 def testWriteFile_asRoot_rejected(self): |
1184 self.device.old_interface._privileged_command_runner = None | 1187 self.device.old_interface._privileged_command_runner = None |
1185 self.device.old_interface._protected_file_access_method_initialized = True | 1188 self.device.old_interface._protected_file_access_method_initialized = True |
1186 with self.assertRaises(device_errors.CommandFailedError): | 1189 with self.assertRaises(device_errors.CommandFailedError): |
1187 self.device.WriteFile('/test/file/no.permissions.to.write', | 1190 self.device.WriteFile('/test/file/no.permissions.to.write', |
1188 'new test file contents', as_root=True) | 1191 'new test file contents', as_root=True) |
1189 | 1192 |
| 1193 def testLs_nothing(self): |
| 1194 with self.assertOldImplCallsSequence([ |
| 1195 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
| 1196 '/this/file/does.not.exist: No such file or directory\r\n'), |
| 1197 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
| 1198 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
| 1199 |
| 1200 def testLs_file(self): |
| 1201 with self.assertOldImplCallsSequence([ |
| 1202 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.file'", |
| 1203 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), |
| 1204 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
| 1205 self.assertEqual( |
| 1206 {'test.file': (4096, datetime.datetime(1970, 1, 1))}, |
| 1207 self.device.Ls('/this/is/a/test.file')) |
| 1208 |
| 1209 def testLs_directory(self): |
| 1210 with self.assertOldImplCallsSequence([ |
| 1211 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", |
| 1212 '\r\n' |
| 1213 '/this/is/a/test.directory:\r\n' |
| 1214 '-rw-rw---- testuser testgroup 4096 1970-01-01 18:19 test.file\r\n'), |
| 1215 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
| 1216 self.assertEqual( |
| 1217 {'test.file': (4096, datetime.datetime(1970, 1, 1, 18, 19))}, |
| 1218 self.device.Ls('/this/is/a/test.directory')) |
| 1219 |
| 1220 def testLs_directories(self): |
| 1221 with self.assertOldImplCallsSequence([ |
| 1222 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", |
| 1223 '\r\n' |
| 1224 '/this/is/a/test.directory:\r\n' |
| 1225 'drwxr-xr-x testuser testgroup 1970-01-01 00:00 test.subdirectory\r\n' |
| 1226 '\r\n' |
| 1227 '/this/is/a/test.directory/test.subdirectory:\r\n' |
| 1228 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), |
| 1229 ("adb -s 0123456789abcdef shell 'date +%z'", '-0700')]): |
| 1230 self.assertEqual( |
| 1231 {'test.subdirectory/test.file': |
| 1232 (4096, datetime.datetime(1970, 1, 1, 7, 0, 0))}, |
| 1233 self.device.Ls('/this/is/a/test.directory')) |
| 1234 |
| 1235 @staticmethod |
| 1236 def mockNamedTemporary(name='/tmp/file/property.file', |
| 1237 read_contents=''): |
| 1238 mock_file = mock.MagicMock(spec=file) |
| 1239 mock_file.name = name |
| 1240 mock_file.__enter__.return_value = mock_file |
| 1241 mock_file.read.return_value = read_contents |
| 1242 return mock_file |
| 1243 |
| 1244 def testSetJavaAsserts_enable(self): |
| 1245 mock_file = self.mockNamedTemporary() |
| 1246 with mock.patch('tempfile.NamedTemporaryFile', |
| 1247 return_value=mock_file), ( |
| 1248 mock.patch('__builtin__.open', return_value=mock_file)): |
| 1249 with self.assertOldImplCallsSequence( |
| 1250 [('adb -s 0123456789abcdef shell ls %s' % |
| 1251 constants.DEVICE_LOCAL_PROPERTIES_PATH, |
| 1252 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1253 ('adb -s 0123456789abcdef pull %s %s' % |
| 1254 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
| 1255 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1256 ('adb -s 0123456789abcdef push %s %s' % |
| 1257 (mock_file.name, constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1258 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1259 ('adb -s 0123456789abcdef shell ' |
| 1260 'getprop dalvik.vm.enableassertions', |
| 1261 '\r\n'), |
| 1262 ('adb -s 0123456789abcdef shell ' |
| 1263 'setprop dalvik.vm.enableassertions "all"', |
| 1264 '')]): |
| 1265 self.device.SetJavaAsserts(True) |
| 1266 |
| 1267 def testSetJavaAsserts_disable(self): |
| 1268 mock_file = self.mockNamedTemporary( |
| 1269 read_contents='dalvik.vm.enableassertions=all\n') |
| 1270 with mock.patch('tempfile.NamedTemporaryFile', |
| 1271 return_value=mock_file), ( |
| 1272 mock.patch('__builtin__.open', return_value=mock_file)): |
| 1273 with self.assertOldImplCallsSequence( |
| 1274 [('adb -s 0123456789abcdef shell ls %s' % |
| 1275 constants.DEVICE_LOCAL_PROPERTIES_PATH, |
| 1276 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1277 ('adb -s 0123456789abcdef pull %s %s' % |
| 1278 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
| 1279 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1280 ('adb -s 0123456789abcdef push %s %s' % |
| 1281 (mock_file.name, constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1282 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1283 ('adb -s 0123456789abcdef shell ' |
| 1284 'getprop dalvik.vm.enableassertions', |
| 1285 'all\r\n'), |
| 1286 ('adb -s 0123456789abcdef shell ' |
| 1287 'setprop dalvik.vm.enableassertions ""', |
| 1288 '')]): |
| 1289 self.device.SetJavaAsserts(False) |
| 1290 |
| 1291 def testSetJavaAsserts_alreadyEnabled(self): |
| 1292 mock_file = self.mockNamedTemporary( |
| 1293 read_contents='dalvik.vm.enableassertions=all\n') |
| 1294 with mock.patch('tempfile.NamedTemporaryFile', |
| 1295 return_value=mock_file), ( |
| 1296 mock.patch('__builtin__.open', return_value=mock_file)): |
| 1297 with self.assertOldImplCallsSequence( |
| 1298 [('adb -s 0123456789abcdef shell ls %s' % |
| 1299 constants.DEVICE_LOCAL_PROPERTIES_PATH, |
| 1300 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1301 ('adb -s 0123456789abcdef pull %s %s' % |
| 1302 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
| 1303 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1304 ('adb -s 0123456789abcdef shell ' |
| 1305 'getprop dalvik.vm.enableassertions', |
| 1306 'all\r\n')]): |
| 1307 self.assertFalse(self.device.SetJavaAsserts(True)) |
| 1308 |
| 1309 def testGetProp_exists(self): |
| 1310 with self.assertOldImplCalls( |
| 1311 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', |
| 1312 'test_property_value\r\n'): |
| 1313 self.assertEqual('test_property_value', |
| 1314 self.device.GetProp('this.is.a.test.property')) |
| 1315 |
| 1316 def testGetProp_doesNotExist(self): |
| 1317 with self.assertOldImplCalls( |
| 1318 'adb -s 0123456789abcdef shell ' |
| 1319 'getprop this.property.does.not.exist', ''): |
| 1320 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) |
| 1321 |
| 1322 def testGetProp_cachedRoProp(self): |
| 1323 with self.assertOldImplCalls( |
| 1324 'adb -s 0123456789abcdef shell ' |
| 1325 'getprop ro.build.type', 'userdebug'): |
| 1326 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) |
| 1327 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) |
| 1328 |
| 1329 def testSetProp(self): |
| 1330 with self.assertOldImplCalls( |
| 1331 'adb -s 0123456789abcdef shell ' |
| 1332 'setprop this.is.a.test.property "test_property_value"', |
| 1333 ''): |
| 1334 self.device.SetProp('this.is.a.test.property', 'test_property_value') |
| 1335 |
1190 | 1336 |
1191 if __name__ == '__main__': | 1337 if __name__ == '__main__': |
| 1338 logging.getLogger().setLevel(logging.DEBUG) |
1192 unittest.main(verbosity=2) | 1339 unittest.main(verbosity=2) |
1193 | 1340 |
OLD | NEW |