• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/power_monitor_test.h"
6 
7 #include "base/power_monitor/power_monitor.h"
8 #include "base/power_monitor/power_monitor_source.h"
9 #include "base/power_monitor/power_observer.h"
10 #include "base/run_loop.h"
11 #include "base/task/current_thread.h"
12 
13 namespace base {
14 namespace test {
15 
16 class PowerMonitorTestSource : public PowerMonitorSource {
17  public:
18   PowerMonitorTestSource() = default;
19   ~PowerMonitorTestSource() override = default;
20 
21   // Retrieve current states.
22   PowerThermalObserver::DeviceThermalState GetCurrentThermalState() override;
23   bool IsOnBatteryPower() override;
24 
25   // Sends asynchronous notifications to registered observers.
26   void Suspend();
27   void Resume();
28   void SetOnBatteryPower(bool on_battery_power);
29 
30   // Sends asynchronous notifications to registered observers and ensures they
31   // are executed (i.e. RunUntilIdle()).
32   void GeneratePowerStateEvent(bool on_battery_power);
33   void GenerateSuspendEvent();
34   void GenerateResumeEvent();
35   void GenerateThermalThrottlingEvent(
36       PowerThermalObserver::DeviceThermalState new_thermal_state);
37   void GenerateSpeedLimitEvent(int speed_limit);
38 
39  protected:
40   bool test_on_battery_power_ = false;
41   PowerThermalObserver::DeviceThermalState current_thermal_state_ =
42       PowerThermalObserver::DeviceThermalState::kUnknown;
43   int current_speed_limit_ = PowerThermalObserver::kSpeedLimitMax;
44 };
45 
46 PowerThermalObserver::DeviceThermalState
GetCurrentThermalState()47 PowerMonitorTestSource::GetCurrentThermalState() {
48   return current_thermal_state_;
49 }
50 
Suspend()51 void PowerMonitorTestSource::Suspend() {
52   ProcessPowerEvent(SUSPEND_EVENT);
53 }
54 
Resume()55 void PowerMonitorTestSource::Resume() {
56   ProcessPowerEvent(RESUME_EVENT);
57 }
58 
SetOnBatteryPower(bool on_battery_power)59 void PowerMonitorTestSource::SetOnBatteryPower(bool on_battery_power) {
60   test_on_battery_power_ = on_battery_power;
61   ProcessPowerEvent(POWER_STATE_EVENT);
62 }
63 
GeneratePowerStateEvent(bool on_battery_power)64 void PowerMonitorTestSource::GeneratePowerStateEvent(bool on_battery_power) {
65   SetOnBatteryPower(on_battery_power);
66   RunLoop().RunUntilIdle();
67 }
68 
GenerateSuspendEvent()69 void PowerMonitorTestSource::GenerateSuspendEvent() {
70   Suspend();
71   RunLoop().RunUntilIdle();
72 }
73 
GenerateResumeEvent()74 void PowerMonitorTestSource::GenerateResumeEvent() {
75   Resume();
76   RunLoop().RunUntilIdle();
77 }
78 
IsOnBatteryPower()79 bool PowerMonitorTestSource::IsOnBatteryPower() {
80   return test_on_battery_power_;
81 }
82 
GenerateThermalThrottlingEvent(PowerThermalObserver::DeviceThermalState new_thermal_state)83 void PowerMonitorTestSource::GenerateThermalThrottlingEvent(
84     PowerThermalObserver::DeviceThermalState new_thermal_state) {
85   ProcessThermalEvent(new_thermal_state);
86   current_thermal_state_ = new_thermal_state;
87   RunLoop().RunUntilIdle();
88 }
89 
GenerateSpeedLimitEvent(int speed_limit)90 void PowerMonitorTestSource::GenerateSpeedLimitEvent(int speed_limit) {
91   ProcessSpeedLimitEvent(speed_limit);
92   current_speed_limit_ = speed_limit;
93   RunLoop().RunUntilIdle();
94 }
95 
ScopedPowerMonitorTestSource()96 ScopedPowerMonitorTestSource::ScopedPowerMonitorTestSource() {
97   auto power_monitor_test_source = std::make_unique<PowerMonitorTestSource>();
98   power_monitor_test_source_ = power_monitor_test_source.get();
99   base::PowerMonitor::Initialize(std::move(power_monitor_test_source));
100 }
101 
~ScopedPowerMonitorTestSource()102 ScopedPowerMonitorTestSource::~ScopedPowerMonitorTestSource() {
103   base::PowerMonitor::ShutdownForTesting();
104 }
105 
106 PowerThermalObserver::DeviceThermalState
GetCurrentThermalState()107 ScopedPowerMonitorTestSource::GetCurrentThermalState() {
108   return power_monitor_test_source_->GetCurrentThermalState();
109 }
110 
IsOnBatteryPower()111 bool ScopedPowerMonitorTestSource::IsOnBatteryPower() {
112   return power_monitor_test_source_->IsOnBatteryPower();
113 }
114 
Suspend()115 void ScopedPowerMonitorTestSource::Suspend() {
116   power_monitor_test_source_->Suspend();
117 }
118 
Resume()119 void ScopedPowerMonitorTestSource::Resume() {
120   power_monitor_test_source_->Resume();
121 }
122 
SetOnBatteryPower(bool on_battery_power)123 void ScopedPowerMonitorTestSource::SetOnBatteryPower(bool on_battery_power) {
124   power_monitor_test_source_->SetOnBatteryPower(on_battery_power);
125 }
126 
GenerateSuspendEvent()127 void ScopedPowerMonitorTestSource::GenerateSuspendEvent() {
128   power_monitor_test_source_->GenerateSuspendEvent();
129 }
130 
GenerateResumeEvent()131 void ScopedPowerMonitorTestSource::GenerateResumeEvent() {
132   power_monitor_test_source_->GenerateResumeEvent();
133 }
134 
GeneratePowerStateEvent(bool on_battery_power)135 void ScopedPowerMonitorTestSource::GeneratePowerStateEvent(
136     bool on_battery_power) {
137   power_monitor_test_source_->GeneratePowerStateEvent(on_battery_power);
138 }
139 
GenerateThermalThrottlingEvent(PowerThermalObserver::DeviceThermalState new_thermal_state)140 void ScopedPowerMonitorTestSource::GenerateThermalThrottlingEvent(
141     PowerThermalObserver::DeviceThermalState new_thermal_state) {
142   power_monitor_test_source_->GenerateThermalThrottlingEvent(new_thermal_state);
143 }
144 
GenerateSpeedLimitEvent(int speed_limit)145 void ScopedPowerMonitorTestSource::GenerateSpeedLimitEvent(int speed_limit) {
146   power_monitor_test_source_->GenerateSpeedLimitEvent(speed_limit);
147 }
148 
149 PowerMonitorTestObserver::PowerMonitorTestObserver() = default;
150 PowerMonitorTestObserver::~PowerMonitorTestObserver() = default;
151 
OnPowerStateChange(bool on_battery_power)152 void PowerMonitorTestObserver::OnPowerStateChange(bool on_battery_power) {
153   last_power_state_ = on_battery_power;
154   power_state_changes_++;
155 }
156 
OnSuspend()157 void PowerMonitorTestObserver::OnSuspend() {
158   suspends_++;
159 }
160 
OnResume()161 void PowerMonitorTestObserver::OnResume() {
162   resumes_++;
163 }
164 
OnThermalStateChange(PowerThermalObserver::DeviceThermalState new_state)165 void PowerMonitorTestObserver::OnThermalStateChange(
166     PowerThermalObserver::DeviceThermalState new_state) {
167   thermal_state_changes_++;
168   last_thermal_state_ = new_state;
169 }
170 
OnSpeedLimitChange(int speed_limit)171 void PowerMonitorTestObserver::OnSpeedLimitChange(int speed_limit) {
172   speed_limit_changes_++;
173   last_speed_limit_ = speed_limit;
174 }
175 
176 }  // namespace test
177 }  // namespace base
178