| 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 1433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1444 'Rss': 101, | 1444 'Rss': 101, |
| 1445 'Pss': 102, | 1445 'Pss': 102, |
| 1446 'Shared_Clean': 103, | 1446 'Shared_Clean': 103, |
| 1447 'Shared_Dirty': 104, | 1447 'Shared_Dirty': 104, |
| 1448 'Private_Clean': 105, | 1448 'Private_Clean': 105, |
| 1449 'Private_Dirty': 106, | 1449 'Private_Dirty': 106, |
| 1450 }, | 1450 }, |
| 1451 self.device.GetMemoryUsageForPid(4321)) | 1451 self.device.GetMemoryUsageForPid(4321)) |
| 1452 | 1452 |
| 1453 | 1453 |
| 1454 class DeviceUtilsGetBatteryInfoTest(DeviceUtilsTest): | |
| 1455 def testGetBatteryInfo_normal(self): | |
| 1456 with self.assertCall( | |
| 1457 self.call.device.RunShellCommand( | |
| 1458 ['dumpsys', 'battery'], check_return=True), | |
| 1459 [ | |
| 1460 'Current Battery Service state:', | |
| 1461 ' AC powered: false', | |
| 1462 ' USB powered: true', | |
| 1463 ' level: 100', | |
| 1464 ' temperature: 321', | |
| 1465 ]): | |
| 1466 self.assertEquals( | |
| 1467 { | |
| 1468 'AC powered': 'false', | |
| 1469 'USB powered': 'true', | |
| 1470 'level': '100', | |
| 1471 'temperature': '321', | |
| 1472 }, | |
| 1473 self.device.GetBatteryInfo()) | |
| 1474 | |
| 1475 | |
| 1476 def testGetBatteryInfo_nothing(self): | |
| 1477 with self.assertCall( | |
| 1478 self.call.device.RunShellCommand( | |
| 1479 ['dumpsys', 'battery'], check_return=True), []): | |
| 1480 self.assertEquals({}, self.device.GetBatteryInfo()) | |
| 1481 | |
| 1482 | |
| 1483 class DeviceUtilsGetChargingTest(DeviceUtilsTest): | |
| 1484 def testGetCharging_usb(self): | |
| 1485 with self.assertCall( | |
| 1486 self.call.device.GetBatteryInfo(), {'USB powered': 'true'}): | |
| 1487 self.assertTrue(self.device.GetCharging()) | |
| 1488 | |
| 1489 def testGetCharging_usbFalse(self): | |
| 1490 with self.assertCall( | |
| 1491 self.call.device.GetBatteryInfo(), {'USB powered': 'false'}): | |
| 1492 self.assertFalse(self.device.GetCharging()) | |
| 1493 | |
| 1494 def testGetCharging_ac(self): | |
| 1495 with self.assertCall( | |
| 1496 self.call.device.GetBatteryInfo(), {'AC powered': 'true'}): | |
| 1497 self.assertTrue(self.device.GetCharging()) | |
| 1498 | |
| 1499 def testGetCharging_wireless(self): | |
| 1500 with self.assertCall( | |
| 1501 self.call.device.GetBatteryInfo(), {'Wireless powered': 'true'}): | |
| 1502 self.assertTrue(self.device.GetCharging()) | |
| 1503 | |
| 1504 def testGetCharging_unknown(self): | |
| 1505 with self.assertCall( | |
| 1506 self.call.device.GetBatteryInfo(), {'level': '42'}): | |
| 1507 self.assertFalse(self.device.GetCharging()) | |
| 1508 | |
| 1509 | |
| 1510 class DeviceUtilsSetChargingTest(DeviceUtilsTest): | |
| 1511 | |
| 1512 @mock.patch('time.sleep', mock.Mock()) | |
| 1513 def testSetCharging_enabled(self): | |
| 1514 with self.assertCalls( | |
| 1515 (self.call.device.FileExists(mock.ANY), True), | |
| 1516 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | |
| 1517 (self.call.device.GetCharging(), False), | |
| 1518 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | |
| 1519 (self.call.device.GetCharging(), True)): | |
| 1520 self.device.SetCharging(True) | |
| 1521 | |
| 1522 def testSetCharging_alreadyEnabled(self): | |
| 1523 with self.assertCalls( | |
| 1524 (self.call.device.FileExists(mock.ANY), True), | |
| 1525 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | |
| 1526 (self.call.device.GetCharging(), True)): | |
| 1527 self.device.SetCharging(True) | |
| 1528 | |
| 1529 @mock.patch('time.sleep', mock.Mock()) | |
| 1530 def testSetCharging_disabled(self): | |
| 1531 with self.assertCalls( | |
| 1532 (self.call.device.FileExists(mock.ANY), True), | |
| 1533 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | |
| 1534 (self.call.device.GetCharging(), True), | |
| 1535 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | |
| 1536 (self.call.device.GetCharging(), False)): | |
| 1537 self.device.SetCharging(False) | |
| 1538 | |
| 1539 | |
| 1540 class DeviceUtilsSetBatteryMeasurementTest(DeviceUtilsTest): | |
| 1541 | |
| 1542 def testBatteryMeasurement(self): | |
| 1543 with self.assertCalls( | |
| 1544 (self.call.device.RunShellCommand( | |
| 1545 mock.ANY, retries=0, single_line=True, | |
| 1546 timeout=10, check_return=True), '22'), | |
| 1547 (self.call.device.RunShellCommand( | |
| 1548 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), | |
| 1549 (self.call.device.RunShellCommand( | |
| 1550 ['dumpsys', 'batterystats', '--charged', '--checkin'], | |
| 1551 check_return=True), []), | |
| 1552 (self.call.device.RunShellCommand( | |
| 1553 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), | |
| 1554 (self.call.device.GetCharging(), False), | |
| 1555 (self.call.device.RunShellCommand( | |
| 1556 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), | |
| 1557 (self.call.device.RunShellCommand( | |
| 1558 ['dumpsys', 'battery', 'reset'], check_return=True), []), | |
| 1559 (self.call.device.GetCharging(), True)): | |
| 1560 with self.device.BatteryMeasurement(): | |
| 1561 pass | |
| 1562 | |
| 1563 | |
| 1564 class DeviceUtilsStrTest(DeviceUtilsTest): | 1454 class DeviceUtilsStrTest(DeviceUtilsTest): |
| 1565 | 1455 |
| 1566 def testStr_returnsSerial(self): | 1456 def testStr_returnsSerial(self): |
| 1567 with self.assertCalls( | 1457 with self.assertCalls( |
| 1568 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | 1458 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): |
| 1569 self.assertEqual('0123456789abcdef', str(self.device)) | 1459 self.assertEqual('0123456789abcdef', str(self.device)) |
| 1570 | 1460 |
| 1571 | 1461 |
| 1572 class DeviceUtilsParallelTest(mock_calls.TestCase): | 1462 class DeviceUtilsParallelTest(mock_calls.TestCase): |
| 1573 | 1463 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1613 self.assertEqual(client_cache_one, {'test': 1}) | 1503 self.assertEqual(client_cache_one, {'test': 1}) |
| 1614 self.assertEqual(client_cache_two, {'test': 1}) | 1504 self.assertEqual(client_cache_two, {'test': 1}) |
| 1615 self.device._ClearCache() | 1505 self.device._ClearCache() |
| 1616 self.assertEqual(client_cache_one, {}) | 1506 self.assertEqual(client_cache_one, {}) |
| 1617 self.assertEqual(client_cache_two, {}) | 1507 self.assertEqual(client_cache_two, {}) |
| 1618 | 1508 |
| 1619 if __name__ == '__main__': | 1509 if __name__ == '__main__': |
| 1620 logging.getLogger().setLevel(logging.DEBUG) | 1510 logging.getLogger().setLevel(logging.DEBUG) |
| 1621 unittest.main(verbosity=2) | 1511 unittest.main(verbosity=2) |
| 1622 | 1512 |
| OLD | NEW |