• 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()
23       const override;
24   PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const override;
25 
26   // Sends asynchronous notifications to registered observers.
27   void Suspend();
28   void Resume();
29   void SetBatteryPowerStatus(
30       PowerStateObserver::BatteryPowerStatus battery_power_status);
31 
32   // Sends asynchronous notifications to registered observers and ensures they
33   // are executed (i.e. RunUntilIdle()).
34   void GeneratePowerStateEvent(
35       PowerStateObserver::BatteryPowerStatus battery_power_status);
36   void GenerateSuspendEvent();
37   void GenerateResumeEvent();
38   void GenerateThermalThrottlingEvent(
39       PowerThermalObserver::DeviceThermalState new_thermal_state);
40   void GenerateSpeedLimitEvent(int speed_limit);
41 
42  protected:
43   PowerStateObserver::BatteryPowerStatus test_power_status_ =
44       PowerStateObserver::BatteryPowerStatus::kUnknown;
45   PowerThermalObserver::DeviceThermalState current_thermal_state_ =
46       PowerThermalObserver::DeviceThermalState::kUnknown;
47   int current_speed_limit_ = PowerThermalObserver::kSpeedLimitMax;
48 };
49 
50 PowerThermalObserver::DeviceThermalState
GetCurrentThermalState() const51 PowerMonitorTestSource::GetCurrentThermalState() const {
52   return current_thermal_state_;
53 }
54 
Suspend()55 void PowerMonitorTestSource::Suspend() {
56   ProcessPowerEvent(SUSPEND_EVENT);
57 }
58 
Resume()59 void PowerMonitorTestSource::Resume() {
60   ProcessPowerEvent(RESUME_EVENT);
61 }
62 
SetBatteryPowerStatus(PowerStateObserver::BatteryPowerStatus battery_power_status)63 void PowerMonitorTestSource::SetBatteryPowerStatus(
64     PowerStateObserver::BatteryPowerStatus battery_power_status) {
65   test_power_status_ = battery_power_status;
66   ProcessPowerEvent(POWER_STATE_EVENT);
67 }
68 
GeneratePowerStateEvent(PowerStateObserver::BatteryPowerStatus battery_power_status)69 void PowerMonitorTestSource::GeneratePowerStateEvent(
70     PowerStateObserver::BatteryPowerStatus battery_power_status) {
71   SetBatteryPowerStatus(battery_power_status);
72   RunLoop().RunUntilIdle();
73 }
74 
GenerateSuspendEvent()75 void PowerMonitorTestSource::GenerateSuspendEvent() {
76   Suspend();
77   RunLoop().RunUntilIdle();
78 }
79 
GenerateResumeEvent()80 void PowerMonitorTestSource::GenerateResumeEvent() {
81   Resume();
82   RunLoop().RunUntilIdle();
83 }
84 
85 PowerStateObserver::BatteryPowerStatus
GetBatteryPowerStatus() const86 PowerMonitorTestSource::GetBatteryPowerStatus() const {
87   return test_power_status_;
88 }
89 
GenerateThermalThrottlingEvent(PowerThermalObserver::DeviceThermalState new_thermal_state)90 void PowerMonitorTestSource::GenerateThermalThrottlingEvent(
91     PowerThermalObserver::DeviceThermalState new_thermal_state) {
92   ProcessThermalEvent(new_thermal_state);
93   current_thermal_state_ = new_thermal_state;
94   RunLoop().RunUntilIdle();
95 }
96 
GenerateSpeedLimitEvent(int speed_limit)97 void PowerMonitorTestSource::GenerateSpeedLimitEvent(int speed_limit) {
98   ProcessSpeedLimitEvent(speed_limit);
99   current_speed_limit_ = speed_limit;
100   RunLoop().RunUntilIdle();
101 }
102 
ScopedPowerMonitorTestSource()103 ScopedPowerMonitorTestSource::ScopedPowerMonitorTestSource() {
104   auto power_monitor_test_source = std::make_unique<PowerMonitorTestSource>();
105   power_monitor_test_source_ = power_monitor_test_source.get();
106   base::PowerMonitor::GetInstance()->Initialize(
107       std::move(power_monitor_test_source));
108 }
109 
~ScopedPowerMonitorTestSource()110 ScopedPowerMonitorTestSource::~ScopedPowerMonitorTestSource() {
111   base::PowerMonitor::GetInstance()->ShutdownForTesting();
112 }
113 
114 PowerThermalObserver::DeviceThermalState
GetCurrentThermalState() const115 ScopedPowerMonitorTestSource::GetCurrentThermalState() const {
116   return power_monitor_test_source_->GetCurrentThermalState();
117 }
118 
119 PowerStateObserver::BatteryPowerStatus
GetBatteryPowerStatus() const120 ScopedPowerMonitorTestSource::GetBatteryPowerStatus() const {
121   return power_monitor_test_source_->GetBatteryPowerStatus();
122 }
123 
Suspend()124 void ScopedPowerMonitorTestSource::Suspend() {
125   power_monitor_test_source_->Suspend();
126 }
127 
Resume()128 void ScopedPowerMonitorTestSource::Resume() {
129   power_monitor_test_source_->Resume();
130 }
131 
SetBatteryPowerStatus(PowerStateObserver::BatteryPowerStatus battery_power_status)132 void ScopedPowerMonitorTestSource::SetBatteryPowerStatus(
133     PowerStateObserver::BatteryPowerStatus battery_power_status) {
134   power_monitor_test_source_->SetBatteryPowerStatus(battery_power_status);
135 }
136 
GenerateSuspendEvent()137 void ScopedPowerMonitorTestSource::GenerateSuspendEvent() {
138   power_monitor_test_source_->GenerateSuspendEvent();
139 }
140 
GenerateResumeEvent()141 void ScopedPowerMonitorTestSource::GenerateResumeEvent() {
142   power_monitor_test_source_->GenerateResumeEvent();
143 }
144 
GeneratePowerStateEvent(PowerStateObserver::BatteryPowerStatus battery_power_status)145 void ScopedPowerMonitorTestSource::GeneratePowerStateEvent(
146     PowerStateObserver::BatteryPowerStatus battery_power_status) {
147   power_monitor_test_source_->GeneratePowerStateEvent(battery_power_status);
148 }
149 
GenerateThermalThrottlingEvent(PowerThermalObserver::DeviceThermalState new_thermal_state)150 void ScopedPowerMonitorTestSource::GenerateThermalThrottlingEvent(
151     PowerThermalObserver::DeviceThermalState new_thermal_state) {
152   power_monitor_test_source_->GenerateThermalThrottlingEvent(new_thermal_state);
153 }
154 
GenerateSpeedLimitEvent(int speed_limit)155 void ScopedPowerMonitorTestSource::GenerateSpeedLimitEvent(int speed_limit) {
156   power_monitor_test_source_->GenerateSpeedLimitEvent(speed_limit);
157 }
158 
159 PowerMonitorTestObserver::PowerMonitorTestObserver() = default;
160 PowerMonitorTestObserver::~PowerMonitorTestObserver() = default;
161 
OnBatteryPowerStatusChange(PowerStateObserver::BatteryPowerStatus battery_power_status)162 void PowerMonitorTestObserver::OnBatteryPowerStatusChange(
163     PowerStateObserver::BatteryPowerStatus battery_power_status) {
164   last_power_status_ = battery_power_status;
165   power_state_changes_++;
166 }
167 
OnSuspend()168 void PowerMonitorTestObserver::OnSuspend() {
169   suspends_++;
170 }
171 
OnResume()172 void PowerMonitorTestObserver::OnResume() {
173   resumes_++;
174 }
175 
OnThermalStateChange(PowerThermalObserver::DeviceThermalState new_state)176 void PowerMonitorTestObserver::OnThermalStateChange(
177     PowerThermalObserver::DeviceThermalState new_state) {
178   thermal_state_changes_++;
179   last_thermal_state_ = new_state;
180 }
181 
OnSpeedLimitChange(int speed_limit)182 void PowerMonitorTestObserver::OnSpeedLimitChange(int speed_limit) {
183   speed_limit_changes_++;
184   last_speed_limit_ = speed_limit;
185 }
186 
187 }  // namespace test
188 }  // namespace base
189