1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
7 from telemetry
.internal
.platform
import power_monitor
as power_monitor
8 from telemetry
.internal
.platform
.power_monitor
import (
9 android_power_monitor_controller
)
11 from devil
.android
import battery_utils
13 # pylint: disable=import-error, unused-argument
16 class AndroidPowerMonitorControllerTest(unittest
.TestCase
):
17 @mock.patch
.object(battery_utils
, 'BatteryUtils')
18 def testComposition(self
, _
):
20 class P1(power_monitor
.PowerMonitor
):
21 def StartMonitoringPower(self
, browser
):
22 raise NotImplementedError()
23 def StopMonitoringPower(self
):
24 raise NotImplementedError()
26 class P2(power_monitor
.PowerMonitor
):
27 def __init__(self
, value
):
28 super(P2
, self
).__init
__()
29 self
._value
= {'P2': value
}
30 def CanMonitorPower(self
):
32 def StartMonitoringPower(self
, browser
):
34 def StopMonitoringPower(self
):
37 class P3(power_monitor
.PowerMonitor
):
38 def __init__(self
, value
):
39 super(P3
, self
).__init
__()
40 self
._value
= {'P3': value
}
41 def CanMonitorPower(self
):
43 def StartMonitoringPower(self
, browser
):
45 def StopMonitoringPower(self
):
48 battery
= battery_utils
.BatteryUtils(None)
49 controller
= android_power_monitor_controller
.AndroidPowerMonitorController(
50 [P1(), P2(1), P3(2)], battery
)
51 self
.assertEqual(controller
.CanMonitorPower(), True)
52 controller
.StartMonitoringPower(None)
53 controller_returns
= controller
.StopMonitoringPower()
54 self
.assertEqual(controller_returns
['P2'], 1)
55 self
.assertEqual(controller_returns
['P3'], 2)
57 @mock.patch
.object(battery_utils
, 'BatteryUtils')
58 def testReenableChargingIfNeeded(self
, mock_battery
):
59 battery
= battery_utils
.BatteryUtils(None)
60 battery
.GetCharging
.return_value
= False
61 android_power_monitor_controller
._ReenableChargingIfNeeded
(battery
)
63 def testMergePowerResultsOneEmpty(self
):
64 dict_one
= {'platform_info': {}, 'component_utilization': {}}
65 dict_two
= {'test': 1, 'component_utilization': {'test': 2}}
68 'component_utilization': {'test': 2},
71 (android_power_monitor_controller
.AndroidPowerMonitorController
.
72 _MergePowerResults(dict_one
, dict_two
))
73 self
.assertDictEqual(dict_one
, results
)
75 def testMergePowerResultsSameEntry(self
):
78 'component_utilization': {'test': 2},
79 'platform_info': {'test2': 'a'}
81 dict_two
= {'test': 3, 'platform_info': {'test': 4}}
84 'component_utilization': {'test': 2},
85 'platform_info': {'test': 4, 'test2': 'a'}
87 (android_power_monitor_controller
.AndroidPowerMonitorController
.
88 _MergePowerResults(dict_one
, dict_two
))
89 self
.assertDictEqual(dict_one
, results
)