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 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 (mock.call.pylib.constants.GetOutDirectory(), '/foo/bar'), | 635 (mock.call.pylib.constants.GetOutDirectory(), '/foo/bar'), |
636 (mock.call.os.path.exists(mock.ANY), True), | 636 (mock.call.os.path.exists(mock.ANY), True), |
637 (self.call.adb.Push(mock.ANY, mock.ANY), '')): | 637 (self.call.adb.Push(mock.ANY, mock.ANY), '')): |
638 self.assertNotEqual('', self.device.GetDevicePieWrapper()) | 638 self.assertNotEqual('', self.device.GetDevicePieWrapper()) |
639 | 639 |
640 | 640 |
641 @mock.patch('time.sleep', mock.Mock()) | 641 @mock.patch('time.sleep', mock.Mock()) |
642 class DeviceUtilsKillAllTest(DeviceUtilsTest): | 642 class DeviceUtilsKillAllTest(DeviceUtilsTest): |
643 | 643 |
644 def testKillAll_noMatchingProcesses(self): | 644 def testKillAll_noMatchingProcesses(self): |
645 with self.assertCall(self.call.adb.Shell('ps'), | 645 with self.assertCall(self.call.device.GetPids('test_process'), []): |
646 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'): | |
647 with self.assertRaises(device_errors.CommandFailedError): | 646 with self.assertRaises(device_errors.CommandFailedError): |
648 self.device.KillAll('test_process') | 647 self.device.KillAll('test_process') |
649 | 648 |
650 def testKillAll_nonblocking(self): | 649 def testKillAll_nonblocking(self): |
651 with self.assertCalls( | 650 with self.assertCalls( |
652 (self.call.adb.Shell('ps'), | 651 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
653 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
654 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
655 (self.call.adb.Shell('kill -9 1234'), '')): | 652 (self.call.adb.Shell('kill -9 1234'), '')): |
656 self.assertEquals(1, | 653 self.assertEquals( |
657 self.device.KillAll('some.process', blocking=False)) | 654 1, self.device.KillAll('some.process', blocking=False)) |
658 | 655 |
659 def testKillAll_blocking(self): | 656 def testKillAll_blocking(self): |
660 with self.assertCalls( | 657 with self.assertCalls( |
661 (self.call.adb.Shell('ps'), | 658 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
662 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
663 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
664 (self.call.adb.Shell('kill -9 1234'), ''), | 659 (self.call.adb.Shell('kill -9 1234'), ''), |
665 (self.call.adb.Shell('ps'), | 660 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
666 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 661 (self.call.device.GetPids('some.process'), [])): |
667 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | 662 self.assertEquals( |
668 (self.call.adb.Shell('ps'), | 663 1, self.device.KillAll('some.process', blocking=True)) |
669 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): | |
670 self.assertEquals(1, | |
671 self.device.KillAll('some.process', blocking=True)) | |
672 | 664 |
673 def testKillAll_root(self): | 665 def testKillAll_root(self): |
674 with self.assertCalls( | 666 with self.assertCalls( |
675 (self.call.adb.Shell('ps'), | 667 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
676 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
677 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
678 (self.call.device.NeedsSU(), True), | 668 (self.call.device.NeedsSU(), True), |
679 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): | 669 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
680 self.assertEquals(1, | 670 self.assertEquals( |
681 self.device.KillAll('some.process', as_root=True)) | 671 1, self.device.KillAll('some.process', as_root=True)) |
682 | 672 |
683 def testKillAll_sigterm(self): | 673 def testKillAll_sigterm(self): |
684 with self.assertCalls( | 674 with self.assertCalls( |
685 (self.call.adb.Shell('ps'), | 675 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
686 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
687 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
688 (self.call.adb.Shell('kill -15 1234'), '')): | 676 (self.call.adb.Shell('kill -15 1234'), '')): |
689 self.assertEquals(1, | 677 self.assertEquals( |
690 self.device.KillAll('some.process', signum=signal.SIGTERM)) | 678 1, self.device.KillAll('some.process', signum=signal.SIGTERM)) |
691 | 679 |
692 | 680 |
693 class DeviceUtilsStartActivityTest(DeviceUtilsTest): | 681 class DeviceUtilsStartActivityTest(DeviceUtilsTest): |
694 | 682 |
695 def testStartActivity_actionOnly(self): | 683 def testStartActivity_actionOnly(self): |
696 test_intent = intent.Intent(action='android.intent.action.VIEW') | 684 test_intent = intent.Intent(action='android.intent.action.VIEW') |
697 with self.assertCall( | 685 with self.assertCall( |
698 self.call.adb.Shell('am start ' | 686 self.call.adb.Shell('am start ' |
699 '-a android.intent.action.VIEW'), | 687 '-a android.intent.action.VIEW'), |
700 'Starting: Intent { act=android.intent.action.VIEW }'): | 688 'Starting: Intent { act=android.intent.action.VIEW }'): |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 with self.assertRaises(device_errors.CommandFailedError): | 1096 with self.assertRaises(device_errors.CommandFailedError): |
1109 self.device._ReadFileWithPull('/path/to/device/file') | 1097 self.device._ReadFileWithPull('/path/to/device/file') |
1110 | 1098 |
1111 def testReadFile_exists(self): | 1099 def testReadFile_exists(self): |
1112 with self.assertCalls( | 1100 with self.assertCalls( |
1113 (self.call.device.RunShellCommand( | 1101 (self.call.device.RunShellCommand( |
1114 ['ls', '-l', '/read/this/test/file'], | 1102 ['ls', '-l', '/read/this/test/file'], |
1115 as_root=False, check_return=True), | 1103 as_root=False, check_return=True), |
1116 ['-rw-rw---- root foo 256 1970-01-01 00:00 file']), | 1104 ['-rw-rw---- root foo 256 1970-01-01 00:00 file']), |
1117 (self.call.device.RunShellCommand( | 1105 (self.call.device.RunShellCommand( |
1118 ['cat', '/read/this/test/file'], as_root=False, check_return=True), | 1106 ['cat', '/read/this/test/file'], |
| 1107 as_root=False, check_return=True, large_output=False), |
1119 ['this is a test file'])): | 1108 ['this is a test file'])): |
1120 self.assertEqual('this is a test file\n', | 1109 self.assertEqual('this is a test file\n', |
1121 self.device.ReadFile('/read/this/test/file')) | 1110 self.device.ReadFile('/read/this/test/file')) |
1122 | 1111 |
1123 def testReadFile_doesNotExist(self): | 1112 def testReadFile_doesNotExist(self): |
1124 with self.assertCall( | 1113 with self.assertCall( |
1125 self.call.device.RunShellCommand( | 1114 self.call.device.RunShellCommand( |
1126 ['ls', '-l', '/this/file/does.not.exist'], | 1115 ['ls', '-l', '/this/file/does.not.exist'], |
1127 as_root=False, check_return=True), | 1116 as_root=False, check_return=True), |
1128 self.CommandError('File does not exist')): | 1117 self.CommandError('File does not exist')): |
1129 with self.assertRaises(device_errors.CommandFailedError): | 1118 with self.assertRaises(device_errors.CommandFailedError): |
1130 self.device.ReadFile('/this/file/does.not.exist') | 1119 self.device.ReadFile('/this/file/does.not.exist') |
1131 | 1120 |
| 1121 def testReadFile_zeroSize(self): |
| 1122 with self.assertCalls( |
| 1123 (self.call.device.RunShellCommand( |
| 1124 ['ls', '-l', '/this/file/has/zero/size'], |
| 1125 as_root=False, check_return=True), |
| 1126 ['-r--r--r-- root foo 0 1970-01-01 00:00 zero_size_file']), |
| 1127 (self.call.device.RunShellCommand( |
| 1128 ['cat', '/this/file/has/zero/size'], |
| 1129 as_root=False, check_return=True, large_output=True), |
| 1130 ['but it has contents'])): |
| 1131 self.assertEqual('but it has contents\n', |
| 1132 self.device.ReadFile('/this/file/has/zero/size')) |
| 1133 |
1132 def testReadFile_withSU(self): | 1134 def testReadFile_withSU(self): |
1133 with self.assertCalls( | 1135 with self.assertCalls( |
1134 (self.call.device.RunShellCommand( | 1136 (self.call.device.RunShellCommand( |
1135 ['ls', '-l', '/this/file/can.be.read.with.su'], | 1137 ['ls', '-l', '/this/file/can.be.read.with.su'], |
1136 as_root=True, check_return=True), | 1138 as_root=True, check_return=True), |
1137 ['-rw------- root root 256 1970-01-01 00:00 can.be.read.with.su']), | 1139 ['-rw------- root root 256 1970-01-01 00:00 can.be.read.with.su']), |
1138 (self.call.device.RunShellCommand( | 1140 (self.call.device.RunShellCommand( |
1139 ['cat', '/this/file/can.be.read.with.su'], | 1141 ['cat', '/this/file/can.be.read.with.su'], |
1140 as_root=True, check_return=True), | 1142 as_root=True, check_return=True, large_output=False), |
1141 ['this is a test file', 'read with su'])): | 1143 ['this is a test file', 'read with su'])): |
1142 self.assertEqual( | 1144 self.assertEqual( |
1143 'this is a test file\nread with su\n', | 1145 'this is a test file\nread with su\n', |
1144 self.device.ReadFile('/this/file/can.be.read.with.su', | 1146 self.device.ReadFile('/this/file/can.be.read.with.su', |
1145 as_root=True)) | 1147 as_root=True)) |
1146 | 1148 |
1147 def testReadFile_withPull(self): | 1149 def testReadFile_withPull(self): |
1148 contents = 'a' * 123456 | 1150 contents = 'a' * 123456 |
1149 with self.assertCalls( | 1151 with self.assertCalls( |
1150 (self.call.device.RunShellCommand( | 1152 (self.call.device.RunShellCommand( |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1380 with self.assertCalls( | 1382 with self.assertCalls( |
1381 (self.call.adb.Shell('setprop test.property new_value'), ''), | 1383 (self.call.adb.Shell('setprop test.property new_value'), ''), |
1382 (self.call.adb.Shell('getprop test.property'), 'old_value')): | 1384 (self.call.adb.Shell('getprop test.property'), 'old_value')): |
1383 with self.assertRaises(device_errors.CommandFailedError): | 1385 with self.assertRaises(device_errors.CommandFailedError): |
1384 self.device.SetProp('test.property', 'new_value', check=True) | 1386 self.device.SetProp('test.property', 'new_value', check=True) |
1385 | 1387 |
1386 | 1388 |
1387 class DeviceUtilsGetPidsTest(DeviceUtilsTest): | 1389 class DeviceUtilsGetPidsTest(DeviceUtilsTest): |
1388 | 1390 |
1389 def testGetPids_noMatches(self): | 1391 def testGetPids_noMatches(self): |
1390 with self.assertCall(self.call.adb.Shell('ps'), | 1392 with self.assertCall( |
1391 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 1393 self.call.device.RunShellCommand( |
1392 'user 1000 100 1024 1024 ffffffff 00000000 no.match\n'): | 1394 'ps', check_return=True, large_output=True), |
| 1395 ['USER PID PPID VSIZE RSS WCHAN PC NAME', |
| 1396 'user 1000 100 1024 1024 ffffffff 00000000 no.match']): |
1393 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1397 self.assertEqual({}, self.device.GetPids('does.not.match')) |
1394 | 1398 |
1395 def testGetPids_oneMatch(self): | 1399 def testGetPids_oneMatch(self): |
1396 with self.assertCall(self.call.adb.Shell('ps'), | 1400 with self.assertCall( |
1397 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 1401 self.call.device.RunShellCommand( |
1398 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\n' | 1402 'ps', check_return=True, large_output=True), |
1399 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n'): | 1403 ['USER PID PPID VSIZE RSS WCHAN PC NAME', |
| 1404 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match', |
| 1405 'user 1001 100 1024 1024 ffffffff 00000000 one.match']): |
1400 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) | 1406 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) |
1401 | 1407 |
1402 def testGetPids_mutlipleMatches(self): | 1408 def testGetPids_mutlipleMatches(self): |
1403 with self.assertCall(self.call.adb.Shell('ps'), | 1409 with self.assertCall( |
1404 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 1410 self.call.device.RunShellCommand( |
1405 'user 1000 100 1024 1024 ffffffff 00000000 not\n' | 1411 'ps', check_return=True, large_output=True), |
1406 'user 1001 100 1024 1024 ffffffff 00000000 one.match\n' | 1412 ['USER PID PPID VSIZE RSS WCHAN PC NAME', |
1407 'user 1002 100 1024 1024 ffffffff 00000000 two.match\n' | 1413 'user 1000 100 1024 1024 ffffffff 00000000 not', |
1408 'user 1003 100 1024 1024 ffffffff 00000000 three.match\n'): | 1414 'user 1001 100 1024 1024 ffffffff 00000000 one.match', |
| 1415 'user 1002 100 1024 1024 ffffffff 00000000 two.match', |
| 1416 'user 1003 100 1024 1024 ffffffff 00000000 three.match']): |
1409 self.assertEqual( | 1417 self.assertEqual( |
1410 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, | 1418 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, |
1411 self.device.GetPids('match')) | 1419 self.device.GetPids('match')) |
1412 | 1420 |
1413 def testGetPids_exactMatch(self): | 1421 def testGetPids_exactMatch(self): |
1414 with self.assertCall(self.call.adb.Shell('ps'), | 1422 with self.assertCall( |
1415 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 1423 self.call.device.RunShellCommand( |
1416 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\n' | 1424 'ps', check_return=True, large_output=True), |
1417 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\n'): | 1425 ['USER PID PPID VSIZE RSS WCHAN PC NAME', |
| 1426 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match', |
| 1427 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']): |
1418 self.assertEqual( | 1428 self.assertEqual( |
1419 {'not.exact.match': '1000', 'exact.match': '1234'}, | 1429 {'not.exact.match': '1000', 'exact.match': '1234'}, |
1420 self.device.GetPids('exact.match')) | 1430 self.device.GetPids('exact.match')) |
1421 | 1431 |
1422 | 1432 |
1423 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): | 1433 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): |
1424 | 1434 |
1425 def testTakeScreenshot_fileNameProvided(self): | 1435 def testTakeScreenshot_fileNameProvided(self): |
1426 with self.assertCalls( | 1436 with self.assertCalls( |
1427 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | 1437 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
1428 self.adb, suffix='.png'), | 1438 self.adb, suffix='.png'), |
1429 MockTempFile('/tmp/path/temp-123.png')), | 1439 MockTempFile('/tmp/path/temp-123.png')), |
1430 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), | 1440 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), |
1431 ''), | 1441 ''), |
1432 self.call.device.PullFile('/tmp/path/temp-123.png', | 1442 self.call.device.PullFile('/tmp/path/temp-123.png', |
1433 '/test/host/screenshot.png')): | 1443 '/test/host/screenshot.png')): |
1434 self.device.TakeScreenshot('/test/host/screenshot.png') | 1444 self.device.TakeScreenshot('/test/host/screenshot.png') |
1435 | 1445 |
1436 | 1446 |
1437 class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest): | 1447 class DeviceUtilsGetMemoryUsageForPidTest(DeviceUtilsTest): |
1438 | 1448 |
1439 def setUp(self): | 1449 def setUp(self): |
1440 super(DeviceUtilsGetMemoryUsageForPidTest, self).setUp() | 1450 super(DeviceUtilsGetMemoryUsageForPidTest, self).setUp() |
1441 | 1451 |
1442 def testGetMemoryUsageForPid_validPid(self): | 1452 def testGetMemoryUsageForPid_validPid(self): |
1443 with self.assertCalls( | 1453 with self.assertCalls( |
1444 (self.call.device.RunShellCommand( | 1454 (self.call.device.RunShellCommand( |
1445 ['showmap', '1234'], as_root=True, check_return=True), | 1455 ['showmap', '1234'], |
| 1456 as_root=True, check_return=True, large_output=True), |
1446 ['100 101 102 103 104 105 106 107 TOTAL']), | 1457 ['100 101 102 103 104 105 106 107 TOTAL']), |
1447 (self.call.device.ReadFile('/proc/1234/status', as_root=True), | 1458 (self.call.device.ReadFile('/proc/1234/status', as_root=True), |
1448 'VmHWM: 1024 kB\n')): | 1459 'VmHWM: 1024 kB\n')): |
1449 self.assertEqual( | 1460 self.assertEqual( |
1450 { | 1461 { |
1451 'Size': 100, | 1462 'Size': 100, |
1452 'Rss': 101, | 1463 'Rss': 101, |
1453 'Pss': 102, | 1464 'Pss': 102, |
1454 'Shared_Clean': 103, | 1465 'Shared_Clean': 103, |
1455 'Shared_Dirty': 104, | 1466 'Shared_Dirty': 104, |
1456 'Private_Clean': 105, | 1467 'Private_Clean': 105, |
1457 'Private_Dirty': 106, | 1468 'Private_Dirty': 106, |
1458 'VmHWM': 1024 | 1469 'VmHWM': 1024 |
1459 }, | 1470 }, |
1460 self.device.GetMemoryUsageForPid(1234)) | 1471 self.device.GetMemoryUsageForPid(1234)) |
1461 | 1472 |
1462 def testGetMemoryUsageForPid_noSmaps(self): | 1473 def testGetMemoryUsageForPid_noSmaps(self): |
1463 with self.assertCalls( | 1474 with self.assertCalls( |
1464 (self.call.device.RunShellCommand( | 1475 (self.call.device.RunShellCommand( |
1465 ['showmap', '4321'], as_root=True, check_return=True), | 1476 ['showmap', '4321'], |
| 1477 as_root=True, check_return=True, large_output=True), |
1466 ['cannot open /proc/4321/smaps: No such file or directory']), | 1478 ['cannot open /proc/4321/smaps: No such file or directory']), |
1467 (self.call.device.ReadFile('/proc/4321/status', as_root=True), | 1479 (self.call.device.ReadFile('/proc/4321/status', as_root=True), |
1468 'VmHWM: 1024 kb\n')): | 1480 'VmHWM: 1024 kb\n')): |
1469 self.assertEquals({'VmHWM': 1024}, self.device.GetMemoryUsageForPid(4321)) | 1481 self.assertEquals({'VmHWM': 1024}, self.device.GetMemoryUsageForPid(4321)) |
1470 | 1482 |
1471 def testGetMemoryUsageForPid_noStatus(self): | 1483 def testGetMemoryUsageForPid_noStatus(self): |
1472 with self.assertCalls( | 1484 with self.assertCalls( |
1473 (self.call.device.RunShellCommand( | 1485 (self.call.device.RunShellCommand( |
1474 ['showmap', '4321'], as_root=True, check_return=True), | 1486 ['showmap', '4321'], |
| 1487 as_root=True, check_return=True, large_output=True), |
1475 ['100 101 102 103 104 105 106 107 TOTAL']), | 1488 ['100 101 102 103 104 105 106 107 TOTAL']), |
1476 (self.call.device.ReadFile('/proc/4321/status', as_root=True), | 1489 (self.call.device.ReadFile('/proc/4321/status', as_root=True), |
1477 self.CommandError())): | 1490 self.CommandError())): |
1478 self.assertEquals( | 1491 self.assertEquals( |
1479 { | 1492 { |
1480 'Size': 100, | 1493 'Size': 100, |
1481 'Rss': 101, | 1494 'Rss': 101, |
1482 'Pss': 102, | 1495 'Pss': 102, |
1483 'Shared_Clean': 103, | 1496 'Shared_Clean': 103, |
1484 'Shared_Dirty': 104, | 1497 'Shared_Dirty': 104, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1540 self.assertEqual(client_cache_one, {'test': 1}) | 1553 self.assertEqual(client_cache_one, {'test': 1}) |
1541 self.assertEqual(client_cache_two, {'test': 1}) | 1554 self.assertEqual(client_cache_two, {'test': 1}) |
1542 self.device._ClearCache() | 1555 self.device._ClearCache() |
1543 self.assertEqual(client_cache_one, {}) | 1556 self.assertEqual(client_cache_one, {}) |
1544 self.assertEqual(client_cache_two, {}) | 1557 self.assertEqual(client_cache_two, {}) |
1545 | 1558 |
1546 if __name__ == '__main__': | 1559 if __name__ == '__main__': |
1547 logging.getLogger().setLevel(logging.DEBUG) | 1560 logging.getLogger().setLevel(logging.DEBUG) |
1548 unittest.main(verbosity=2) | 1561 unittest.main(verbosity=2) |
1549 | 1562 |
OLD | NEW |