Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 761903003: Update from https://crrev.com/306655 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/device_settings.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 25 matching lines...) Expand all
36 sys.path.append(os.path.join( 36 sys.path.append(os.path.join(
37 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) 37 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
38 import mock # pylint: disable=F0401 38 import mock # pylint: disable=F0401
39 39
40 40
41 class DeviceUtilsTest(unittest.TestCase): 41 class DeviceUtilsTest(unittest.TestCase):
42 42
43 def testInitWithStr(self): 43 def testInitWithStr(self):
44 serial_as_str = str('0123456789abcdef') 44 serial_as_str = str('0123456789abcdef')
45 d = device_utils.DeviceUtils('0123456789abcdef') 45 d = device_utils.DeviceUtils('0123456789abcdef')
46 self.assertEqual(serial_as_str, d.old_interface.GetDevice()) 46 self.assertEqual(serial_as_str, d.adb.GetDeviceSerial())
47 47
48 def testInitWithUnicode(self): 48 def testInitWithUnicode(self):
49 serial_as_unicode = unicode('fedcba9876543210') 49 serial_as_unicode = unicode('fedcba9876543210')
50 d = device_utils.DeviceUtils(serial_as_unicode) 50 d = device_utils.DeviceUtils(serial_as_unicode)
51 self.assertEqual(serial_as_unicode, d.old_interface.GetDevice()) 51 self.assertEqual(serial_as_unicode, d.adb.GetDeviceSerial())
52 52
53 def testInitWithAdbWrapper(self): 53 def testInitWithAdbWrapper(self):
54 serial = '123456789abcdef0' 54 serial = '123456789abcdef0'
55 a = adb_wrapper.AdbWrapper(serial) 55 a = adb_wrapper.AdbWrapper(serial)
56 d = device_utils.DeviceUtils(a) 56 d = device_utils.DeviceUtils(a)
57 self.assertEqual(serial, d.old_interface.GetDevice()) 57 self.assertEqual(serial, d.adb.GetDeviceSerial())
58 58
59 def testInitWithAndroidCommands(self): 59 def testInitWithAndroidCommands(self):
60 serial = '0fedcba987654321' 60 serial = '0fedcba987654321'
61 a = android_commands.AndroidCommands(device=serial) 61 a = android_commands.AndroidCommands(device=serial)
62 d = device_utils.DeviceUtils(a) 62 d = device_utils.DeviceUtils(a)
63 self.assertEqual(serial, d.old_interface.GetDevice()) 63 self.assertEqual(serial, d.adb.GetDeviceSerial())
64 64
65 def testInitWithNone(self): 65 def testInitWithMissing_fails(self):
66 d = device_utils.DeviceUtils(None) 66 with self.assertRaises(ValueError):
67 self.assertIsNone(d.old_interface.GetDevice()) 67 device_utils.DeviceUtils(None)
68 with self.assertRaises(ValueError):
69 device_utils.DeviceUtils('')
68 70
69 71
70 class MockTempFile(object): 72 class MockTempFile(object):
71 73
72 def __init__(self, name='/tmp/some/file'): 74 def __init__(self, name='/tmp/some/file'):
73 self.file = mock.MagicMock(spec=file) 75 self.file = mock.MagicMock(spec=file)
74 self.file.name = name 76 self.file.name = name
75 77
76 def __enter__(self): 78 def __enter__(self):
77 return self.file 79 return self.file
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 241
240 class DeviceUtilsNewImplTest(mock_calls.TestCase): 242 class DeviceUtilsNewImplTest(mock_calls.TestCase):
241 243
242 def setUp(self): 244 def setUp(self):
243 test_serial = '0123456789abcdef' 245 test_serial = '0123456789abcdef'
244 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) 246 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
245 self.adb.__str__ = mock.Mock(return_value=test_serial) 247 self.adb.__str__ = mock.Mock(return_value=test_serial)
246 self.adb.GetDeviceSerial.return_value = test_serial 248 self.adb.GetDeviceSerial.return_value = test_serial
247 self.device = device_utils.DeviceUtils( 249 self.device = device_utils.DeviceUtils(
248 self.adb, default_timeout=10, default_retries=0) 250 self.adb, default_timeout=10, default_retries=0)
249 self.watchMethodCalls(self.call.adb) 251 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial'])
250 252
251 def ShellError(self, output=None, exit_code=1): 253 def ShellError(self, output=None, exit_code=1):
252 def action(cmd, *args, **kwargs): 254 def action(cmd, *args, **kwargs):
253 raise device_errors.AdbShellCommandFailedError( 255 raise device_errors.AdbCommandFailedError(
254 cmd, exit_code, output, str(self.device)) 256 cmd, output, exit_code, str(self.device))
255 if output is None: 257 if output is None:
256 output = 'Permission denied\n' 258 output = 'Permission denied\n'
257 return action 259 return action
258 260
259 def TimeoutError(self, msg=None): 261 def TimeoutError(self, msg=None):
260 if msg is None: 262 if msg is None:
261 msg = 'Operation timed out' 263 msg = 'Operation timed out'
262 return mock.Mock(side_effect=device_errors.CommandTimeoutError( 264 return mock.Mock(side_effect=device_errors.CommandTimeoutError(
263 msg, str(self.device))) 265 msg, str(self.device)))
264 266
(...skipping 23 matching lines...) Expand all
288 290
289 def testHasRoot_true(self): 291 def testHasRoot_true(self):
290 with self.assertCall(self.call.adb.Shell('ls /root'), 'foo\n'): 292 with self.assertCall(self.call.adb.Shell('ls /root'), 'foo\n'):
291 self.assertTrue(self.device.HasRoot()) 293 self.assertTrue(self.device.HasRoot())
292 294
293 def testHasRoot_false(self): 295 def testHasRoot_false(self):
294 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()): 296 with self.assertCall(self.call.adb.Shell('ls /root'), self.ShellError()):
295 self.assertFalse(self.device.HasRoot()) 297 self.assertFalse(self.device.HasRoot())
296 298
297 299
298 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): 300 class DeviceUtilsEnableRootTest(DeviceUtilsNewImplTest):
299 301
300 def testEnableRoot_succeeds(self): 302 def testEnableRoot_succeeds(self):
301 with self.assertCallsSequence([ 303 with self.assertCalls(
302 ('adb -s 0123456789abcdef shell getprop ro.build.type', 304 (self.call.device.IsUserBuild(), False),
303 'userdebug\r\n'), 305 self.call.adb.Root(),
304 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), 306 self.call.adb.WaitForDevice()):
305 ('adb -s 0123456789abcdef wait-for-device', ''),
306 ('adb -s 0123456789abcdef wait-for-device', '')]):
307 self.device.EnableRoot() 307 self.device.EnableRoot()
308 308
309 def testEnableRoot_userBuild(self): 309 def testEnableRoot_userBuild(self):
310 with self.assertCallsSequence([ 310 with self.assertCalls(
311 ('adb -s 0123456789abcdef shell getprop ro.build.type', 'user\r\n')]): 311 (self.call.device.IsUserBuild(), True)):
312 with self.assertRaises(device_errors.CommandFailedError): 312 with self.assertRaises(device_errors.CommandFailedError):
313 self.device.EnableRoot() 313 self.device.EnableRoot()
314 314
315 def testEnableRoot_rootFails(self): 315 def testEnableRoot_rootFails(self):
316 with self.assertCallsSequence([ 316 with self.assertCalls(
317 ('adb -s 0123456789abcdef shell getprop ro.build.type', 317 (self.call.device.IsUserBuild(), False),
318 'userdebug\r\n'), 318 (self.call.adb.Root(), self.CommandError())):
319 ('adb -s 0123456789abcdef root', 'no\r\n'),
320 ('adb -s 0123456789abcdef wait-for-device', '')]):
321 with self.assertRaises(device_errors.CommandFailedError): 319 with self.assertRaises(device_errors.CommandFailedError):
322 self.device.EnableRoot() 320 self.device.EnableRoot()
323 321
324 322
325 class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest): 323 class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest):
326 324
327 def testIsUserBuild_yes(self): 325 def testIsUserBuild_yes(self):
328 with self.assertCall( 326 with self.assertCall(
329 self.call.device.GetProp('ro.build.type', cache=True), 'user'): 327 self.call.device.GetProp('ro.build.type', cache=True), 'user'):
330 self.assertTrue(self.device.IsUserBuild()) 328 self.assertTrue(self.device.IsUserBuild())
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 cmd = 'echo $ANDROID_DATA' 666 cmd = 'echo $ANDROID_DATA'
669 output = '/data\n' 667 output = '/data\n'
670 with self.assertCall(self.call.adb.Shell(cmd), output): 668 with self.assertCall(self.call.adb.Shell(cmd), output):
671 self.assertEquals([output.rstrip()], 669 self.assertEquals([output.rstrip()],
672 self.device.RunShellCommand(cmd, check_return=True)) 670 self.device.RunShellCommand(cmd, check_return=True))
673 671
674 def testRunShellCommand_checkReturn_failure(self): 672 def testRunShellCommand_checkReturn_failure(self):
675 cmd = 'ls /root' 673 cmd = 'ls /root'
676 output = 'opendir failed, Permission denied\n' 674 output = 'opendir failed, Permission denied\n'
677 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): 675 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)):
678 with self.assertRaises(device_errors.AdbShellCommandFailedError): 676 with self.assertRaises(device_errors.AdbCommandFailedError):
679 self.device.RunShellCommand(cmd, check_return=True) 677 self.device.RunShellCommand(cmd, check_return=True)
680 678
681 def testRunShellCommand_checkReturn_disabled(self): 679 def testRunShellCommand_checkReturn_disabled(self):
682 cmd = 'ls /root' 680 cmd = 'ls /root'
683 output = 'opendir failed, Permission denied\n' 681 output = 'opendir failed, Permission denied\n'
684 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)): 682 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError(output)):
685 self.assertEquals([output.rstrip()], 683 self.assertEquals([output.rstrip()],
686 self.device.RunShellCommand(cmd, check_return=False)) 684 self.device.RunShellCommand(cmd, check_return=False))
687 685
688 686
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 def testPushChangedFilesZipped_single(self): 1075 def testPushChangedFilesZipped_single(self):
1078 self._testPushChangedFilesZipped_spec( 1076 self._testPushChangedFilesZipped_spec(
1079 [('/test/host/path/file1', '/test/device/path/file1')]) 1077 [('/test/host/path/file1', '/test/device/path/file1')])
1080 1078
1081 def testPushChangedFilesZipped_multiple(self): 1079 def testPushChangedFilesZipped_multiple(self):
1082 self._testPushChangedFilesZipped_spec( 1080 self._testPushChangedFilesZipped_spec(
1083 [('/test/host/path/file1', '/test/device/path/file1'), 1081 [('/test/host/path/file1', '/test/device/path/file1'),
1084 ('/test/host/path/file2', '/test/device/path/file2')]) 1082 ('/test/host/path/file2', '/test/device/path/file2')])
1085 1083
1086 1084
1087 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): 1085 class DeviceUtilsFileExistsTest(DeviceUtilsNewImplTest):
1088 1086
1089 def testFileExists_usingTest_fileExists(self): 1087 def testFileExists_usingTest_fileExists(self):
1090 with self.assertCalls( 1088 with self.assertCall(
1091 "adb -s 0123456789abcdef shell " 1089 self.call.device.RunShellCommand(
1092 "'test -e \"/data/app/test.file.exists\"; echo $?'", 1090 ['test', '-e', '/path/file.exists'], check_return=True), ''):
1093 '0\r\n'): 1091 self.assertTrue(self.device.FileExists('/path/file.exists'))
1094 self.assertTrue(self.device.FileExists('/data/app/test.file.exists'))
1095 1092
1096 def testFileExists_usingTest_fileDoesntExist(self): 1093 def testFileExists_usingTest_fileDoesntExist(self):
1097 with self.assertCalls( 1094 with self.assertCall(
1098 "adb -s 0123456789abcdef shell " 1095 self.call.device.RunShellCommand(
1099 "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'", 1096 ['test', '-e', '/does/not/exist'], check_return=True),
1100 '1\r\n'): 1097 self.ShellError('', 1)):
1101 self.assertFalse(self.device.FileExists( 1098 self.assertFalse(self.device.FileExists('/does/not/exist'))
1102 '/data/app/test.file.does.not.exist'))
1103
1104 def testFileExists_usingLs_fileExists(self):
1105 with self.assertCallsSequence([
1106 ("adb -s 0123456789abcdef shell "
1107 "'test -e \"/data/app/test.file.exists\"; echo $?'",
1108 'test: not found\r\n'),
1109 ("adb -s 0123456789abcdef shell "
1110 "'ls \"/data/app/test.file.exists\" >/dev/null 2>&1; echo $?'",
1111 '0\r\n')]):
1112 self.assertTrue(self.device.FileExists('/data/app/test.file.exists'))
1113
1114 def testFileExists_usingLs_fileDoesntExist(self):
1115 with self.assertCallsSequence([
1116 ("adb -s 0123456789abcdef shell "
1117 "'test -e \"/data/app/test.file.does.not.exist\"; echo $?'",
1118 'test: not found\r\n'),
1119 ("adb -s 0123456789abcdef shell "
1120 "'ls \"/data/app/test.file.does.not.exist\" "
1121 ">/dev/null 2>&1; echo $?'",
1122 '1\r\n')]):
1123 self.assertFalse(self.device.FileExists(
1124 '/data/app/test.file.does.not.exist'))
1125 1099
1126 1100
1127 class DeviceUtilsPullFileTest(DeviceUtilsOldImplTest): 1101 class DeviceUtilsPullFileTest(DeviceUtilsOldImplTest):
1128 1102
1129 def testPullFile_existsOnDevice(self): 1103 def testPullFile_existsOnDevice(self):
1130 with mock.patch('os.path.exists', return_value=True): 1104 with mock.patch('os.path.exists', return_value=True):
1131 with self.assertCallsSequence([ 1105 with self.assertCallsSequence([
1132 ('adb -s 0123456789abcdef shell ' 1106 ('adb -s 0123456789abcdef shell '
1133 'ls /data/app/test.file.exists', 1107 'ls /data/app/test.file.exists',
1134 '/data/app/test.file.exists'), 1108 '/data/app/test.file.exists'),
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
1545 }, 1519 },
1546 self.device.GetMemoryUsageForPid(1234)) 1520 self.device.GetMemoryUsageForPid(1234))
1547 1521
1548 def testGetMemoryUsageForPid_invalidPid(self): 1522 def testGetMemoryUsageForPid_invalidPid(self):
1549 with self.assertCalls( 1523 with self.assertCalls(
1550 "adb -s 0123456789abcdef shell 'showmap 4321'", 1524 "adb -s 0123456789abcdef shell 'showmap 4321'",
1551 'cannot open /proc/4321/smaps: No such file or directory\r\n'): 1525 'cannot open /proc/4321/smaps: No such file or directory\r\n'):
1552 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321)) 1526 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321))
1553 1527
1554 1528
1555 class DeviceUtilsStrTest(DeviceUtilsOldImplTest): 1529 class DeviceUtilsStrTest(DeviceUtilsNewImplTest):
1556 1530
1557 def testStr_noAdbCalls(self): 1531 def testStr_returnsSerial(self):
1558 with self.assertNoAdbCalls(): 1532 with self.assertCalls(
1533 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
1559 self.assertEqual('0123456789abcdef', str(self.device)) 1534 self.assertEqual('0123456789abcdef', str(self.device))
1560 1535
1561 def testStr_noSerial(self):
1562 self.device = device_utils.DeviceUtils(None)
1563 with self.assertCalls('adb get-serialno', '0123456789abcdef'):
1564 self.assertEqual('0123456789abcdef', str(self.device))
1565
1566 def testStr_noSerial_noDevices(self):
1567 self.device = device_utils.DeviceUtils(None)
1568 with self.assertCalls('adb get-serialno', 'unknown'), (
1569 self.assertRaises(device_errors.NoDevicesError)):
1570 str(self.device)
1571
1572 1536
1573 if __name__ == '__main__': 1537 if __name__ == '__main__':
1574 logging.getLogger().setLevel(logging.DEBUG) 1538 logging.getLogger().setLevel(logging.DEBUG)
1575 unittest.main(verbosity=2) 1539 unittest.main(verbosity=2)
1576 1540
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/device_settings.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698