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 1156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1180 self.device.WriteFile('/test/file/written.to.device', | 1182 self.device.WriteFile('/test/file/written.to.device', |
1181 'new test file contents', as_root=True) | 1183 'new test file contents', as_root=True) |
1182 | 1184 |
1183 def testWriteFile_asRoot_rejected(self): | 1185 def testWriteFile_asRoot_rejected(self): |
1184 self.device.old_interface._privileged_command_runner = None | 1186 self.device.old_interface._privileged_command_runner = None |
1185 self.device.old_interface._protected_file_access_method_initialized = True | 1187 self.device.old_interface._protected_file_access_method_initialized = True |
1186 with self.assertRaises(device_errors.CommandFailedError): | 1188 with self.assertRaises(device_errors.CommandFailedError): |
1187 self.device.WriteFile('/test/file/no.permissions.to.write', | 1189 self.device.WriteFile('/test/file/no.permissions.to.write', |
1188 'new test file contents', as_root=True) | 1190 'new test file contents', as_root=True) |
1189 | 1191 |
1192 def testLs_nothing(self): | |
1193 with self.assertOldImplCallsSequence([ | |
1194 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | |
1195 '/this/file/does.not.exist: No such file or directory\r\n'), | |
1196 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | |
1197 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | |
1198 | |
1199 def testLs_file(self): | |
1200 with self.assertOldImplCallsSequence([ | |
1201 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.file'", | |
1202 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), | |
1203 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | |
1204 self.assertEqual( | |
1205 {'test.file': (4096, datetime.datetime(1970, 1, 1))}, | |
1206 self.device.Ls('/this/is/a/test.file')) | |
1207 | |
1208 def testLs_directory(self): | |
1209 with self.assertOldImplCallsSequence([ | |
1210 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | |
1211 '\r\n' | |
1212 '/this/is/a/test.directory:\r\n' | |
1213 '-rw-rw---- testuser testgroup 4096 1970-01-01 18:19 test.file\r\n'), | |
1214 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | |
1215 self.assertEqual( | |
1216 {'test.file': (4096, datetime.datetime(1970, 1, 1, 18, 19))}, | |
1217 self.device.Ls('/this/is/a/test.directory')) | |
1218 | |
1219 def testLs_directories(self): | |
1220 with self.assertOldImplCallsSequence([ | |
1221 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | |
1222 '\r\n' | |
1223 '/this/is/a/test.directory:\r\n' | |
1224 'drwxr-xr-x testuser testgroup 1970-01-01 00:00 test.subdirectory\r\n' | |
1225 '\r\n' | |
1226 '/this/is/a/test.directory/test.subdirectory:\r\n' | |
1227 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), | |
1228 ("adb -s 0123456789abcdef shell 'date +%z'", '-0700')]): | |
1229 self.assertEqual( | |
1230 {'test.subdirectory/test.file': | |
1231 (4096, datetime.datetime(1970, 1, 1, 7, 0, 0))}, | |
1232 self.device.Ls('/this/is/a/test.directory')) | |
1233 | |
1234 def testSetJavaAsserts_enable(self): | |
1235 mock_file = mock.MagicMock(spec=file) | |
1236 mock_file.name = '/tmp/file/property.file' | |
1237 mock_file.__enter__.return_value = mock_file | |
1238 mock_file.read.return_value = '' | |
1239 with mock.patch('tempfile.NamedTemporaryFile', | |
1240 return_value=mock_file), ( | |
1241 mock.patch('__builtin__.open', return_value=mock_file)): | |
1242 with self.assertOldImplCallsSequence( | |
1243 [('adb -s 0123456789abcdef shell ls %s' % | |
1244 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1245 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
1246 ('adb -s 0123456789abcdef pull %s /tmp/file/property.file' % | |
1247 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1248 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1249 ('adb -s 0123456789abcdef push /tmp/file/property.file %s' % | |
1250 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1251 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1252 ('adb -s 0123456789abcdef shell ' | |
1253 'getprop dalvik.vm.enableassertions', | |
1254 '\r\n'), | |
1255 ('adb -s 0123456789abcdef shell ' | |
1256 'setprop dalvik.vm.enableassertions "all"', | |
1257 '')]): | |
1258 self.device.SetJavaAsserts(True) | |
1259 | |
1260 def testSetJavaAsserts_disable(self): | |
1261 mock_file = mock.MagicMock(spec=file) | |
1262 mock_file.name = '/tmp/file/property.file' | |
1263 mock_file.__enter__.return_value = mock_file | |
1264 mock_file.read.return_value = 'dalvik.vm.enableassertions=all\n' | |
1265 with mock.patch('tempfile.NamedTemporaryFile', | |
1266 return_value=mock_file), ( | |
1267 mock.patch('__builtin__.open', return_value=mock_file)): | |
1268 with self.assertOldImplCallsSequence( | |
1269 [('adb -s 0123456789abcdef shell ls %s' % | |
1270 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1271 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
1272 ('adb -s 0123456789abcdef pull %s /tmp/file/property.file' % | |
1273 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1274 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1275 ('adb -s 0123456789abcdef push /tmp/file/property.file %s' % | |
1276 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1277 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1278 ('adb -s 0123456789abcdef shell ' | |
1279 'getprop dalvik.vm.enableassertions', | |
1280 'all\r\n'), | |
1281 ('adb -s 0123456789abcdef shell ' | |
1282 'setprop dalvik.vm.enableassertions ""', | |
1283 '')]): | |
1284 self.device.SetJavaAsserts(False) | |
1285 | |
1286 def testSetJavaAsserts_alreadyEnabled(self): | |
1287 mock_file = mock.MagicMock(spec=file) | |
1288 mock_file.name = '/tmp/file/property.file' | |
1289 mock_file.__enter__.return_value = mock_file | |
1290 mock_file.read.return_value = 'dalvik.vm.enableassertions=all\n' | |
1291 with mock.patch('tempfile.NamedTemporaryFile', | |
1292 return_value=mock_file), ( | |
1293 mock.patch('__builtin__.open', return_value=mock_file)): | |
1294 with self.assertOldImplCallsSequence( | |
1295 [('adb -s 0123456789abcdef shell ls %s' % | |
1296 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1297 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | |
1298 ('adb -s 0123456789abcdef pull %s /tmp/file/property.file' % | |
1299 constants.DEVICE_LOCAL_PROPERTIES_PATH, | |
1300 '100 B/s (100 bytes in 1.000s)\r\n'), | |
1301 ('adb -s 0123456789abcdef shell ' | |
1302 'getprop dalvik.vm.enableassertions', | |
1303 'all\r\n')]): | |
1304 self.assertFalse(self.device.SetJavaAsserts(True)) | |
1305 | |
1306 def testGetProp_exists(self): | |
tonyg
2014/07/07 15:22:56
The system_properties interface caches certain ro
jbudorick
2014/07/07 16:12:45
Done.
| |
1307 with self.assertOldImplCalls( | |
1308 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', | |
1309 'test_property_value\r\n'): | |
1310 self.assertEqual('test_property_value', | |
1311 self.device.GetProp('this.is.a.test.property')) | |
1312 | |
1313 def testGetProp_doesNotExist(self): | |
1314 with self.assertOldImplCalls( | |
1315 'adb -s 0123456789abcdef shell ' | |
1316 'getprop this.property.does.not.exist', ''): | |
1317 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | |
1318 | |
1319 def testSetProp(self): | |
1320 with self.assertOldImplCalls( | |
1321 'adb -s 0123456789abcdef shell ' | |
1322 'setprop this.is.a.test.property "test_property_value"', | |
1323 ''): | |
1324 self.device.SetProp('this.is.a.test.property', 'test_property_value') | |
1325 | |
1190 | 1326 |
1191 if __name__ == '__main__': | 1327 if __name__ == '__main__': |
1328 logging.getLogger().setLevel(logging.DEBUG) | |
1192 unittest.main(verbosity=2) | 1329 unittest.main(verbosity=2) |
1193 | 1330 |
OLD | NEW |