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 #ifndef BASE_TEST_POWER_MONITOR_TEST_H_ 6 #define BASE_TEST_POWER_MONITOR_TEST_H_ 7 8 #include "base/memory/raw_ptr.h" 9 #include "base/power_monitor/power_monitor.h" 10 #include "base/power_monitor/power_monitor_source.h" 11 #include "base/power_monitor/power_observer.h" 12 13 namespace base { 14 15 namespace test { 16 17 // Use PowerMonitorTestSource via ScopedPowerMonitorTestSource wrapper when you 18 // need to simulate power events (suspend and resume). 19 class PowerMonitorTestSource; 20 21 // ScopedPowerMonitorTestSource initializes the PowerMonitor with a Mock 22 // PowerMonitorSource. Mock power notifications can be simulated through this 23 // helper class. 24 // 25 // Example: 26 // base::test::ScopedPowerMonitorTestSource power_monitor_source; 27 // power_monitor_source.Suspend(); 28 // [...] 29 // power_monitor_source.Resume(); 30 class ScopedPowerMonitorTestSource { 31 public: 32 ScopedPowerMonitorTestSource(); 33 ~ScopedPowerMonitorTestSource(); 34 35 ScopedPowerMonitorTestSource(const ScopedPowerMonitorTestSource&) = delete; 36 ScopedPowerMonitorTestSource& operator=(const ScopedPowerMonitorTestSource&) = 37 delete; 38 39 // Retrieve current states. 40 PowerThermalObserver::DeviceThermalState GetCurrentThermalState(); 41 bool IsOnBatteryPower(); 42 43 // Sends asynchronous notifications to registered observers. 44 void Suspend(); 45 void Resume(); 46 void SetOnBatteryPower(bool on_battery_power); 47 48 void GenerateSuspendEvent(); 49 void GenerateResumeEvent(); 50 void GeneratePowerStateEvent(bool on_battery_power); 51 void GenerateThermalThrottlingEvent( 52 PowerThermalObserver::DeviceThermalState new_thermal_state); 53 void GenerateSpeedLimitEvent(int speed_limit); 54 55 private: 56 // Owned by PowerMonitor. 57 raw_ptr<PowerMonitorTestSource, DanglingUntriaged> 58 power_monitor_test_source_ = nullptr; 59 }; 60 61 class PowerMonitorTestObserver : public PowerSuspendObserver, 62 public PowerThermalObserver, 63 public PowerStateObserver { 64 public: 65 PowerMonitorTestObserver(); 66 ~PowerMonitorTestObserver() override; 67 68 // PowerStateObserver overrides. 69 void OnPowerStateChange(bool on_battery_power) override; 70 // PowerSuspendObserver overrides. 71 void OnSuspend() override; 72 void OnResume() override; 73 // PowerThermalObserver overrides. 74 void OnThermalStateChange( 75 PowerThermalObserver::DeviceThermalState new_state) override; 76 void OnSpeedLimitChange(int speed_limit) override; 77 78 // Test status counts. power_state_changes()79 int power_state_changes() const { return power_state_changes_; } suspends()80 int suspends() const { return suspends_; } resumes()81 int resumes() const { return resumes_; } thermal_state_changes()82 int thermal_state_changes() const { return thermal_state_changes_; } speed_limit_changes()83 int speed_limit_changes() const { return speed_limit_changes_; } 84 last_power_state()85 bool last_power_state() const { return last_power_state_; } last_thermal_state()86 PowerThermalObserver::DeviceThermalState last_thermal_state() const { 87 return last_thermal_state_; 88 } last_speed_limit()89 int last_speed_limit() const { return last_speed_limit_; } 90 91 private: 92 // Count of OnPowerStateChange notifications. 93 int power_state_changes_ = 0; 94 // Count of OnSuspend notifications. 95 int suspends_ = 0; 96 // Count of OnResume notifications. 97 int resumes_ = 0; 98 // Count of OnThermalStateChange notifications. 99 int thermal_state_changes_ = 0; 100 // Count of OnSpeedLimitChange notifications. 101 int speed_limit_changes_ = 0; 102 103 // Last power state we were notified of. 104 bool last_power_state_ = false; 105 // Last power thermal we were notified of. 106 PowerThermalObserver::DeviceThermalState last_thermal_state_ = 107 PowerThermalObserver::DeviceThermalState::kUnknown; 108 // Last speed limit we were notified of. 109 int last_speed_limit_ = PowerThermalObserver::kSpeedLimitMax; 110 }; 111 112 } // namespace test 113 } // namespace base 114 115 #endif // BASE_TEST_POWER_MONITOR_TEST_H_ 116