| 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=C0321 | 10 # pylint: disable=C0321 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 return mock.Mock(side_effect=device_errors.CommandTimeoutError( | 154 return mock.Mock(side_effect=device_errors.CommandTimeoutError( |
| 155 msg, str(self.device))) | 155 msg, str(self.device))) |
| 156 | 156 |
| 157 def CommandError(self, msg=None): | 157 def CommandError(self, msg=None): |
| 158 if msg is None: | 158 if msg is None: |
| 159 msg = 'Command failed' | 159 msg = 'Command failed' |
| 160 return mock.Mock(side_effect=device_errors.CommandFailedError( | 160 return mock.Mock(side_effect=device_errors.CommandFailedError( |
| 161 msg, str(self.device))) | 161 msg, str(self.device))) |
| 162 | 162 |
| 163 | 163 |
| 164 class DeviceUtilsEqTest(DeviceUtilsTest): |
| 165 |
| 166 def testEq_equal_deviceUtils(self): |
| 167 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) |
| 168 self.assertTrue(self.device == other) |
| 169 self.assertTrue(other == self.device) |
| 170 |
| 171 def testEq_equal_adbWrapper(self): |
| 172 other = adb_wrapper.AdbWrapper('0123456789abcdef') |
| 173 self.assertTrue(self.device == other) |
| 174 self.assertTrue(other == self.device) |
| 175 |
| 176 def testEq_equal_string(self): |
| 177 other = '0123456789abcdef' |
| 178 self.assertTrue(self.device == other) |
| 179 self.assertTrue(other == self.device) |
| 180 |
| 181 def testEq_devicesNotEqual(self): |
| 182 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdee')) |
| 183 self.assertFalse(self.device == other) |
| 184 self.assertFalse(other == self.device) |
| 185 |
| 186 def testEq_identity(self): |
| 187 self.assertTrue(self.device == self.device) |
| 188 |
| 189 def testEq_serialInList(self): |
| 190 devices = [self.device] |
| 191 self.assertTrue('0123456789abcdef' in devices) |
| 192 |
| 193 |
| 194 class DeviceUtilsLtTest(DeviceUtilsTest): |
| 195 |
| 196 def testLt_lessThan(self): |
| 197 other = device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff')) |
| 198 self.assertTrue(self.device < other) |
| 199 self.assertTrue(other > self.device) |
| 200 |
| 201 def testLt_greaterThan_lhs(self): |
| 202 other = device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000')) |
| 203 self.assertFalse(self.device < other) |
| 204 self.assertFalse(other > self.device) |
| 205 |
| 206 def testLt_equal(self): |
| 207 other = device_utils.DeviceUtils(_AdbWrapperMock('0123456789abcdef')) |
| 208 self.assertFalse(self.device < other) |
| 209 self.assertFalse(other > self.device) |
| 210 |
| 211 def testLt_sorted(self): |
| 212 devices = [ |
| 213 device_utils.DeviceUtils(_AdbWrapperMock('ffffffffffffffff')), |
| 214 device_utils.DeviceUtils(_AdbWrapperMock('0000000000000000')), |
| 215 ] |
| 216 sorted_devices = sorted(devices) |
| 217 self.assertEquals('0000000000000000', |
| 218 sorted_devices[0].adb.GetDeviceSerial()) |
| 219 self.assertEquals('ffffffffffffffff', |
| 220 sorted_devices[1].adb.GetDeviceSerial()) |
| 221 |
| 222 |
| 223 class DeviceUtilsStrTest(DeviceUtilsTest): |
| 224 |
| 225 def testStr_returnsSerial(self): |
| 226 with self.assertCalls( |
| 227 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): |
| 228 self.assertEqual('0123456789abcdef', str(self.device)) |
| 229 |
| 230 |
| 164 class DeviceUtilsIsOnlineTest(DeviceUtilsTest): | 231 class DeviceUtilsIsOnlineTest(DeviceUtilsTest): |
| 165 | 232 |
| 166 def testIsOnline_true(self): | 233 def testIsOnline_true(self): |
| 167 with self.assertCall(self.call.adb.GetState(), 'device'): | 234 with self.assertCall(self.call.adb.GetState(), 'device'): |
| 168 self.assertTrue(self.device.IsOnline()) | 235 self.assertTrue(self.device.IsOnline()) |
| 169 | 236 |
| 170 def testIsOnline_false(self): | 237 def testIsOnline_false(self): |
| 171 with self.assertCall(self.call.adb.GetState(), 'offline'): | 238 with self.assertCall(self.call.adb.GetState(), 'offline'): |
| 172 self.assertFalse(self.device.IsOnline()) | 239 self.assertFalse(self.device.IsOnline()) |
| 173 | 240 |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 self.device.RunShellCommand(cmd, check_return=False)) | 654 self.device.RunShellCommand(cmd, check_return=False)) |
| 588 | 655 |
| 589 def testRunShellCommand_largeOutput_enabled(self): | 656 def testRunShellCommand_largeOutput_enabled(self): |
| 590 cmd = 'echo $VALUE' | 657 cmd = 'echo $VALUE' |
| 591 temp_file = MockTempFile('/sdcard/temp-123') | 658 temp_file = MockTempFile('/sdcard/temp-123') |
| 592 cmd_redirect = '%s > %s' % (cmd, temp_file.name) | 659 cmd_redirect = '%s > %s' % (cmd, temp_file.name) |
| 593 with self.assertCalls( | 660 with self.assertCalls( |
| 594 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | 661 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), |
| 595 temp_file), | 662 temp_file), |
| 596 (self.call.adb.Shell(cmd_redirect)), | 663 (self.call.adb.Shell(cmd_redirect)), |
| 597 (self.call.device.ReadFile(temp_file.name), 'something')): | 664 (self.call.device.ReadFile(temp_file.name, force_pull=True), |
| 665 'something')): |
| 598 self.assertEquals( | 666 self.assertEquals( |
| 599 ['something'], | 667 ['something'], |
| 600 self.device.RunShellCommand( | 668 self.device.RunShellCommand( |
| 601 cmd, large_output=True, check_return=True)) | 669 cmd, large_output=True, check_return=True)) |
| 602 | 670 |
| 603 def testRunShellCommand_largeOutput_disabledNoTrigger(self): | 671 def testRunShellCommand_largeOutput_disabledNoTrigger(self): |
| 604 cmd = 'something' | 672 cmd = 'something' |
| 605 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError('')): | 673 with self.assertCall(self.call.adb.Shell(cmd), self.ShellError('')): |
| 606 with self.assertRaises(device_errors.AdbCommandFailedError): | 674 with self.assertRaises(device_errors.AdbCommandFailedError): |
| 607 self.device.RunShellCommand(cmd, check_return=True) | 675 self.device.RunShellCommand(cmd, check_return=True) |
| 608 | 676 |
| 609 def testRunShellCommand_largeOutput_disabledTrigger(self): | 677 def testRunShellCommand_largeOutput_disabledTrigger(self): |
| 610 cmd = 'echo $VALUE' | 678 cmd = 'echo $VALUE' |
| 611 temp_file = MockTempFile('/sdcard/temp-123') | 679 temp_file = MockTempFile('/sdcard/temp-123') |
| 612 cmd_redirect = '%s > %s' % (cmd, temp_file.name) | 680 cmd_redirect = '%s > %s' % (cmd, temp_file.name) |
| 613 with self.assertCalls( | 681 with self.assertCalls( |
| 614 (self.call.adb.Shell(cmd), self.ShellError('', None)), | 682 (self.call.adb.Shell(cmd), self.ShellError('', None)), |
| 615 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), | 683 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(self.adb), |
| 616 temp_file), | 684 temp_file), |
| 617 (self.call.adb.Shell(cmd_redirect)), | 685 (self.call.adb.Shell(cmd_redirect)), |
| 618 (self.call.device.ReadFile(mock.ANY), 'something')): | 686 (self.call.device.ReadFile(mock.ANY, force_pull=True), |
| 687 'something')): |
| 619 self.assertEquals(['something'], | 688 self.assertEquals(['something'], |
| 620 self.device.RunShellCommand(cmd, check_return=True)) | 689 self.device.RunShellCommand(cmd, check_return=True)) |
| 621 | 690 |
| 622 | 691 |
| 623 class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest): | 692 class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest): |
| 624 | 693 |
| 625 def testRunPipedShellCommand_success(self): | 694 def testRunPipedShellCommand_success(self): |
| 626 with self.assertCall( | 695 with self.assertCall( |
| 627 self.call.device.RunShellCommand( | 696 self.call.device.RunShellCommand( |
| 628 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | 697 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
| (...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1216 ['cp', '/this/big/file/can.be.read.with.su', | 1285 ['cp', '/this/big/file/can.be.read.with.su', |
| 1217 '/sdcard/tmp/on.device'], | 1286 '/sdcard/tmp/on.device'], |
| 1218 as_root=True, check_return=True), | 1287 as_root=True, check_return=True), |
| 1219 (self.call.device._ReadFileWithPull('/sdcard/tmp/on.device'), | 1288 (self.call.device._ReadFileWithPull('/sdcard/tmp/on.device'), |
| 1220 contents)): | 1289 contents)): |
| 1221 self.assertEqual( | 1290 self.assertEqual( |
| 1222 contents, | 1291 contents, |
| 1223 self.device.ReadFile('/this/big/file/can.be.read.with.su', | 1292 self.device.ReadFile('/this/big/file/can.be.read.with.su', |
| 1224 as_root=True)) | 1293 as_root=True)) |
| 1225 | 1294 |
| 1295 def testReadFile_forcePull(self): |
| 1296 contents = 'a' * 123456 |
| 1297 with self.assertCall( |
| 1298 self.call.device._ReadFileWithPull('/read/this/big/test/file'), |
| 1299 contents): |
| 1300 self.assertEqual( |
| 1301 contents, |
| 1302 self.device.ReadFile('/read/this/big/test/file', force_pull=True)) |
| 1303 |
| 1226 | 1304 |
| 1227 class DeviceUtilsWriteFileTest(DeviceUtilsTest): | 1305 class DeviceUtilsWriteFileTest(DeviceUtilsTest): |
| 1228 | 1306 |
| 1229 def testWriteFileWithPush_success(self): | 1307 def testWriteFileWithPush_success(self): |
| 1230 tmp_host = MockTempFile('/tmp/file/on.host') | 1308 tmp_host = MockTempFile('/tmp/file/on.host') |
| 1231 contents = 'some interesting contents' | 1309 contents = 'some interesting contents' |
| 1232 with self.assertCalls( | 1310 with self.assertCalls( |
| 1233 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), | 1311 (mock.call.tempfile.NamedTemporaryFile(), tmp_host), |
| 1234 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): | 1312 self.call.adb.Push('/tmp/file/on.host', '/path/to/device/file')): |
| 1235 self.device._WriteFileWithPush('/path/to/device/file', contents) | 1313 self.device._WriteFileWithPush('/path/to/device/file', contents) |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1532 'Rss': 101, | 1610 'Rss': 101, |
| 1533 'Pss': 102, | 1611 'Pss': 102, |
| 1534 'Shared_Clean': 103, | 1612 'Shared_Clean': 103, |
| 1535 'Shared_Dirty': 104, | 1613 'Shared_Dirty': 104, |
| 1536 'Private_Clean': 105, | 1614 'Private_Clean': 105, |
| 1537 'Private_Dirty': 106, | 1615 'Private_Dirty': 106, |
| 1538 }, | 1616 }, |
| 1539 self.device.GetMemoryUsageForPid(4321)) | 1617 self.device.GetMemoryUsageForPid(4321)) |
| 1540 | 1618 |
| 1541 | 1619 |
| 1542 class DeviceUtilsStrTest(DeviceUtilsTest): | |
| 1543 | |
| 1544 def testStr_returnsSerial(self): | |
| 1545 with self.assertCalls( | |
| 1546 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | |
| 1547 self.assertEqual('0123456789abcdef', str(self.device)) | |
| 1548 | |
| 1549 | |
| 1550 class DeviceUtilsClientCache(DeviceUtilsTest): | 1620 class DeviceUtilsClientCache(DeviceUtilsTest): |
| 1551 | 1621 |
| 1552 def testClientCache_twoCaches(self): | 1622 def testClientCache_twoCaches(self): |
| 1553 self.device._cache['test'] = 0 | 1623 self.device._cache['test'] = 0 |
| 1554 client_cache_one = self.device.GetClientCache('ClientOne') | 1624 client_cache_one = self.device.GetClientCache('ClientOne') |
| 1555 client_cache_one['test'] = 1 | 1625 client_cache_one['test'] = 1 |
| 1556 client_cache_two = self.device.GetClientCache('ClientTwo') | 1626 client_cache_two = self.device.GetClientCache('ClientTwo') |
| 1557 client_cache_two['test'] = 2 | 1627 client_cache_two['test'] = 2 |
| 1558 self.assertEqual(self.device._cache, {'test': 0}) | 1628 self.assertEqual(self.device._cache, {'test': 0}) |
| 1559 self.assertEqual(client_cache_one, {'test': 1}) | 1629 self.assertEqual(client_cache_one, {'test': 1}) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1621 devices = device_utils.DeviceUtils.HealthyDevices() | 1691 devices = device_utils.DeviceUtils.HealthyDevices() |
| 1622 self.assertEquals(1, len(devices)) | 1692 self.assertEquals(1, len(devices)) |
| 1623 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) | 1693 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) |
| 1624 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) | 1694 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) |
| 1625 | 1695 |
| 1626 | 1696 |
| 1627 if __name__ == '__main__': | 1697 if __name__ == '__main__': |
| 1628 logging.getLogger().setLevel(logging.DEBUG) | 1698 logging.getLogger().setLevel(logging.DEBUG) |
| 1629 unittest.main(verbosity=2) | 1699 unittest.main(verbosity=2) |
| 1630 | 1700 |
| OLD | NEW |