| 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 # pylint: disable=W0613 | 10 # pylint: disable=W0613 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 198 |
| 199 @mock.patch('time.sleep', mock.Mock()) | 199 @mock.patch('time.sleep', mock.Mock()) |
| 200 def testChargeDeviceToLevel(self): | 200 def testChargeDeviceToLevel(self): |
| 201 with self.assertCalls( | 201 with self.assertCalls( |
| 202 (self.call.battery.SetCharging(True)), | 202 (self.call.battery.SetCharging(True)), |
| 203 (self.call.battery.GetBatteryInfo(), {'level': '50'}), | 203 (self.call.battery.GetBatteryInfo(), {'level': '50'}), |
| 204 (self.call.battery.GetBatteryInfo(), {'level': '100'})): | 204 (self.call.battery.GetBatteryInfo(), {'level': '100'})): |
| 205 self.battery.ChargeDeviceToLevel(95) | 205 self.battery.ChargeDeviceToLevel(95) |
| 206 | 206 |
| 207 | 207 |
| 208 class DeviceUtilsGetBatteryInfoTest(BatteryUtilsTest): | 208 class BatteryUtilsGetBatteryInfoTest(BatteryUtilsTest): |
| 209 | 209 |
| 210 def testGetBatteryInfo_normal(self): | 210 def testGetBatteryInfo_normal(self): |
| 211 with self.assertCall( | 211 with self.assertCall( |
| 212 self.call.device.RunShellCommand( | 212 self.call.device.RunShellCommand( |
| 213 ['dumpsys', 'battery'], check_return=True), | 213 ['dumpsys', 'battery'], check_return=True), |
| 214 [ | 214 [ |
| 215 'Current Battery Service state:', | 215 'Current Battery Service state:', |
| 216 ' AC powered: false', | 216 ' AC powered: false', |
| 217 ' USB powered: true', | 217 ' USB powered: true', |
| 218 ' level: 100', | 218 ' level: 100', |
| 219 ' temperature: 321', | 219 ' temperature: 321', |
| 220 ]): | 220 ]): |
| 221 self.assertEquals( | 221 self.assertEquals( |
| 222 { | 222 { |
| 223 'AC powered': 'false', | 223 'AC powered': 'false', |
| 224 'USB powered': 'true', | 224 'USB powered': 'true', |
| 225 'level': '100', | 225 'level': '100', |
| 226 'temperature': '321', | 226 'temperature': '321', |
| 227 }, | 227 }, |
| 228 self.battery.GetBatteryInfo()) | 228 self.battery.GetBatteryInfo()) |
| 229 | 229 |
| 230 def testGetBatteryInfo_nothing(self): | 230 def testGetBatteryInfo_nothing(self): |
| 231 with self.assertCall( | 231 with self.assertCall( |
| 232 self.call.device.RunShellCommand( | 232 self.call.device.RunShellCommand( |
| 233 ['dumpsys', 'battery'], check_return=True), []): | 233 ['dumpsys', 'battery'], check_return=True), []): |
| 234 self.assertEquals({}, self.battery.GetBatteryInfo()) | 234 self.assertEquals({}, self.battery.GetBatteryInfo()) |
| 235 | 235 |
| 236 | 236 |
| 237 class DeviceUtilsGetChargingTest(BatteryUtilsTest): | 237 class BatteryUtilsGetChargingTest(BatteryUtilsTest): |
| 238 | 238 |
| 239 def testGetCharging_usb(self): | 239 def testGetCharging_usb(self): |
| 240 with self.assertCall( | 240 with self.assertCall( |
| 241 self.call.battery.GetBatteryInfo(), {'USB powered': 'true'}): | 241 self.call.battery.GetBatteryInfo(), {'USB powered': 'true'}): |
| 242 self.assertTrue(self.battery.GetCharging()) | 242 self.assertTrue(self.battery.GetCharging()) |
| 243 | 243 |
| 244 def testGetCharging_usbFalse(self): | 244 def testGetCharging_usbFalse(self): |
| 245 with self.assertCall( | 245 with self.assertCall( |
| 246 self.call.battery.GetBatteryInfo(), {'USB powered': 'false'}): | 246 self.call.battery.GetBatteryInfo(), {'USB powered': 'false'}): |
| 247 self.assertFalse(self.battery.GetCharging()) | 247 self.assertFalse(self.battery.GetCharging()) |
| 248 | 248 |
| 249 def testGetCharging_ac(self): | 249 def testGetCharging_ac(self): |
| 250 with self.assertCall( | 250 with self.assertCall( |
| 251 self.call.battery.GetBatteryInfo(), {'AC powered': 'true'}): | 251 self.call.battery.GetBatteryInfo(), {'AC powered': 'true'}): |
| 252 self.assertTrue(self.battery.GetCharging()) | 252 self.assertTrue(self.battery.GetCharging()) |
| 253 | 253 |
| 254 def testGetCharging_wireless(self): | 254 def testGetCharging_wireless(self): |
| 255 with self.assertCall( | 255 with self.assertCall( |
| 256 self.call.battery.GetBatteryInfo(), {'Wireless powered': 'true'}): | 256 self.call.battery.GetBatteryInfo(), {'Wireless powered': 'true'}): |
| 257 self.assertTrue(self.battery.GetCharging()) | 257 self.assertTrue(self.battery.GetCharging()) |
| 258 | 258 |
| 259 def testGetCharging_unknown(self): | 259 def testGetCharging_unknown(self): |
| 260 with self.assertCall( | 260 with self.assertCall( |
| 261 self.call.battery.GetBatteryInfo(), {'level': '42'}): | 261 self.call.battery.GetBatteryInfo(), {'level': '42'}): |
| 262 self.assertFalse(self.battery.GetCharging()) | 262 self.assertFalse(self.battery.GetCharging()) |
| 263 | 263 |
| 264 | 264 |
| 265 class DeviceUtilsGetNetworkDataTest(BatteryUtilsTest): | 265 class BatteryUtilsGetNetworkDataTest(BatteryUtilsTest): |
| 266 | 266 |
| 267 def testGetNetworkData_noDataUsage(self): | 267 def testGetNetworkData_noDataUsage(self): |
| 268 with self.assertCalls( | 268 with self.assertCalls( |
| 269 (self.call.device.RunShellCommand( | 269 (self.call.device.RunShellCommand( |
| 270 ['dumpsys', 'batterystats', '-c'], check_return=True), | 270 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 271 _DUMPSYS_OUTPUT), | 271 _DUMPSYS_OUTPUT), |
| 272 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), | 272 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), |
| 273 self.ShellError()), | 273 self.ShellError()), |
| 274 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), | 274 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), |
| 275 self.ShellError())): | 275 self.ShellError())): |
| (...skipping 25 matching lines...) Expand all Loading... |
| 301 def testGetNetworkData_clearedCache(self): | 301 def testGetNetworkData_clearedCache(self): |
| 302 with self.assertCalls( | 302 with self.assertCalls( |
| 303 (self.call.device.RunShellCommand( | 303 (self.call.device.RunShellCommand( |
| 304 ['dumpsys', 'batterystats', '-c'], check_return=True), | 304 ['dumpsys', 'batterystats', '-c'], check_return=True), |
| 305 _DUMPSYS_OUTPUT), | 305 _DUMPSYS_OUTPUT), |
| 306 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), 1), | 306 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_snd'), 1), |
| 307 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), 2)): | 307 (self.call.device.ReadFile('/proc/uid_stat/1000/tcp_rcv'), 2)): |
| 308 self.battery._cache.clear() | 308 self.battery._cache.clear() |
| 309 self.assertEqual(self.battery.GetNetworkData('test_package1'), (1,2)) | 309 self.assertEqual(self.battery.GetNetworkData('test_package1'), (1,2)) |
| 310 | 310 |
| 311 class BatteryUtilsLetBatteryCoolToTemperatureTest(BatteryUtilsTest): |
| 312 |
| 313 @mock.patch('time.sleep', mock.Mock()) |
| 314 def testLetBatteryCoolToTemperature_startUnder(self): |
| 315 with self.assertCalls( |
| 316 (self.call.battery.GetBatteryInfo(), {'temperature': '500'})): |
| 317 self.battery.LetBatteryCoolToTemperature(600) |
| 318 |
| 319 @mock.patch('time.sleep', mock.Mock()) |
| 320 def testLetBatteryCoolToTemperature_startOver(self): |
| 321 with self.assertCalls( |
| 322 (self.call.battery.GetBatteryInfo(), {'temperature': '500'}), |
| 323 (self.call.battery.GetBatteryInfo(), {'temperature': '400'})): |
| 324 self.battery.LetBatteryCoolToTemperature(400) |
| 311 | 325 |
| 312 if __name__ == '__main__': | 326 if __name__ == '__main__': |
| 313 logging.getLogger().setLevel(logging.DEBUG) | 327 logging.getLogger().setLevel(logging.DEBUG) |
| 314 unittest.main(verbosity=2) | 328 unittest.main(verbosity=2) |
| OLD | NEW |