| 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 battery_utils.py | 7 Unit tests for the contents of battery_utils.py |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import logging | 10 import logging |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 battery_utils.BatteryUtils('') | 57 battery_utils.BatteryUtils('') |
| 58 | 58 |
| 59 | 59 |
| 60 class BatteryUtilsSetChargingTest(BatteryUtilsTest): | 60 class BatteryUtilsSetChargingTest(BatteryUtilsTest): |
| 61 | 61 |
| 62 @mock.patch('time.sleep', mock.Mock()) | 62 @mock.patch('time.sleep', mock.Mock()) |
| 63 def testSetCharging_enabled(self): | 63 def testSetCharging_enabled(self): |
| 64 with self.assertCalls( | 64 with self.assertCalls( |
| 65 (self.call.device.FileExists(mock.ANY), True), | 65 (self.call.device.FileExists(mock.ANY), True), |
| 66 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | 66 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 67 (self.call.device.GetCharging(), False), | 67 (self.call.battery.GetCharging(), False), |
| 68 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | 68 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 69 (self.call.device.GetCharging(), True)): | 69 (self.call.battery.GetCharging(), True)): |
| 70 self.battery.SetCharging(True) | 70 self.battery.SetCharging(True) |
| 71 | 71 |
| 72 def testSetCharging_alreadyEnabled(self): | 72 def testSetCharging_alreadyEnabled(self): |
| 73 with self.assertCalls( | 73 with self.assertCalls( |
| 74 (self.call.device.FileExists(mock.ANY), True), | 74 (self.call.device.FileExists(mock.ANY), True), |
| 75 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | 75 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 76 (self.call.device.GetCharging(), True)): | 76 (self.call.battery.GetCharging(), True)): |
| 77 self.battery.SetCharging(True) | 77 self.battery.SetCharging(True) |
| 78 | 78 |
| 79 @mock.patch('time.sleep', mock.Mock()) | 79 @mock.patch('time.sleep', mock.Mock()) |
| 80 def testSetCharging_disabled(self): | 80 def testSetCharging_disabled(self): |
| 81 with self.assertCalls( | 81 with self.assertCalls( |
| 82 (self.call.device.FileExists(mock.ANY), True), | 82 (self.call.device.FileExists(mock.ANY), True), |
| 83 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | 83 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 84 (self.call.device.GetCharging(), True), | 84 (self.call.battery.GetCharging(), True), |
| 85 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), | 85 (self.call.device.RunShellCommand(mock.ANY, check_return=True), []), |
| 86 (self.call.device.GetCharging(), False)): | 86 (self.call.battery.GetCharging(), False)): |
| 87 self.battery.SetCharging(False) | 87 self.battery.SetCharging(False) |
| 88 | 88 |
| 89 | 89 |
| 90 class BatteryUtilsSetBatteryMeasurementTest(BatteryUtilsTest): | 90 class BatteryUtilsSetBatteryMeasurementTest(BatteryUtilsTest): |
| 91 | 91 |
| 92 def testBatteryMeasurement(self): | 92 def testBatteryMeasurement(self): |
| 93 with self.assertCalls( | 93 with self.assertCalls( |
| 94 (self.call.device.RunShellCommand( | 94 (self.call.device.RunShellCommand( |
| 95 mock.ANY, retries=0, single_line=True, | 95 mock.ANY, retries=0, single_line=True, |
| 96 timeout=10, check_return=True), '22'), | 96 timeout=10, check_return=True), '22'), |
| 97 (self.call.device.RunShellCommand( | 97 (self.call.device.RunShellCommand( |
| 98 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), |
| 99 (self.call.device.RunShellCommand( |
| 98 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), | 100 ['dumpsys', 'batterystats', '--reset'], check_return=True), []), |
| 99 (self.call.device.RunShellCommand( | 101 (self.call.device.RunShellCommand( |
| 100 ['dumpsys', 'batterystats', '--charged', '--checkin'], | 102 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 101 check_return=True), []), | 103 check_return=True), []), |
| 102 (self.call.device.RunShellCommand( | 104 (self.call.device.RunShellCommand( |
| 103 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), | 105 ['dumpsys', 'battery', 'set', 'usb', '0'], check_return=True), []), |
| 104 (self.call.device.GetCharging(), False), | 106 (self.call.battery.GetCharging(), False), |
| 105 (self.call.device.RunShellCommand( | 107 (self.call.device.RunShellCommand( |
| 106 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), | 108 ['dumpsys', 'battery', 'set', 'usb', '1'], check_return=True), []), |
| 107 (self.call.device.RunShellCommand( | 109 (self.call.device.RunShellCommand( |
| 108 ['dumpsys', 'battery', 'reset'], check_return=True), []), | 110 ['dumpsys', 'battery', 'reset'], check_return=True), []), |
| 109 (self.call.device.GetCharging(), True)): | 111 (self.call.battery.GetCharging(), True)): |
| 110 with self.battery.BatteryMeasurement(): | 112 with self.battery.BatteryMeasurement(): |
| 111 pass | 113 pass |
| 112 | 114 |
| 113 | 115 |
| 114 class BatteryUtilsGetPowerData(BatteryUtilsTest): | 116 class BatteryUtilsGetPowerData(BatteryUtilsTest): |
| 115 | 117 |
| 116 _DUMPSYS_OUTPUT = [ | 118 _DUMPSYS_OUTPUT = [ |
| 117 '9,0,i,uid,1000,test_package1', | 119 '9,0,i,uid,1000,test_package1', |
| 118 '9,0,i,uid,1001,test_package2', | 120 '9,0,i,uid,1001,test_package2', |
| 119 '9,1000,l,pwi,uid,1', | 121 '9,1000,l,pwi,uid,1', |
| (...skipping 25 matching lines...) Expand all Loading... |
| 145 | 147 |
| 146 @mock.patch('time.sleep', mock.Mock()) | 148 @mock.patch('time.sleep', mock.Mock()) |
| 147 def testChargeDeviceToLevel(self): | 149 def testChargeDeviceToLevel(self): |
| 148 with self.assertCalls( | 150 with self.assertCalls( |
| 149 (self.call.battery.SetCharging(True)), | 151 (self.call.battery.SetCharging(True)), |
| 150 (self.call.battery.GetBatteryInfo(), {'level': '50'}), | 152 (self.call.battery.GetBatteryInfo(), {'level': '50'}), |
| 151 (self.call.battery.GetBatteryInfo(), {'level': '100'})): | 153 (self.call.battery.GetBatteryInfo(), {'level': '100'})): |
| 152 self.battery.ChargeDeviceToLevel(95) | 154 self.battery.ChargeDeviceToLevel(95) |
| 153 | 155 |
| 154 | 156 |
| 157 class DeviceUtilsGetBatteryInfoTest(BatteryUtilsTest): |
| 158 |
| 159 def testGetBatteryInfo_normal(self): |
| 160 with self.assertCall( |
| 161 self.call.device.RunShellCommand( |
| 162 ['dumpsys', 'battery'], check_return=True), |
| 163 [ |
| 164 'Current Battery Service state:', |
| 165 ' AC powered: false', |
| 166 ' USB powered: true', |
| 167 ' level: 100', |
| 168 ' temperature: 321', |
| 169 ]): |
| 170 self.assertEquals( |
| 171 { |
| 172 'AC powered': 'false', |
| 173 'USB powered': 'true', |
| 174 'level': '100', |
| 175 'temperature': '321', |
| 176 }, |
| 177 self.battery.GetBatteryInfo()) |
| 178 |
| 179 def testGetBatteryInfo_nothing(self): |
| 180 with self.assertCall( |
| 181 self.call.device.RunShellCommand( |
| 182 ['dumpsys', 'battery'], check_return=True), []): |
| 183 self.assertEquals({}, self.battery.GetBatteryInfo()) |
| 184 |
| 185 |
| 186 class DeviceUtilsGetChargingTest(BatteryUtilsTest): |
| 187 |
| 188 def testGetCharging_usb(self): |
| 189 with self.assertCall( |
| 190 self.call.battery.GetBatteryInfo(), {'USB powered': 'true'}): |
| 191 self.assertTrue(self.battery.GetCharging()) |
| 192 |
| 193 def testGetCharging_usbFalse(self): |
| 194 with self.assertCall( |
| 195 self.call.battery.GetBatteryInfo(), {'USB powered': 'false'}): |
| 196 self.assertFalse(self.battery.GetCharging()) |
| 197 |
| 198 def testGetCharging_ac(self): |
| 199 with self.assertCall( |
| 200 self.call.battery.GetBatteryInfo(), {'AC powered': 'true'}): |
| 201 self.assertTrue(self.battery.GetCharging()) |
| 202 |
| 203 def testGetCharging_wireless(self): |
| 204 with self.assertCall( |
| 205 self.call.battery.GetBatteryInfo(), {'Wireless powered': 'true'}): |
| 206 self.assertTrue(self.battery.GetCharging()) |
| 207 |
| 208 def testGetCharging_unknown(self): |
| 209 with self.assertCall( |
| 210 self.call.battery.GetBatteryInfo(), {'level': '42'}): |
| 211 self.assertFalse(self.battery.GetCharging()) |
| 212 |
| 213 |
| 155 if __name__ == '__main__': | 214 if __name__ == '__main__': |
| 156 logging.getLogger().setLevel(logging.DEBUG) | 215 logging.getLogger().setLevel(logging.DEBUG) |
| 157 unittest.main(verbosity=2) | 216 unittest.main(verbosity=2) |
| OLD | NEW |