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() const; 41 PowerStateObserver::BatteryPowerStatus GetBatteryPowerStatus() const; 42 43 // Sends asynchronous notifications to registered observers. 44 void Suspend(); 45 void Resume(); 46 void SetBatteryPowerStatus( 47 PowerStateObserver::BatteryPowerStatus battery_power_status); 48 49 void GenerateSuspendEvent(); 50 void GenerateResumeEvent(); 51 void GeneratePowerStateEvent( 52 PowerStateObserver::BatteryPowerStatus battery_power_status); 53 void GenerateThermalThrottlingEvent( 54 PowerThermalObserver::DeviceThermalState new_thermal_state); 55 void GenerateSpeedLimitEvent(int speed_limit); 56 57 private: 58 // Owned by PowerMonitor. 59 raw_ptr<PowerMonitorTestSource, DanglingUntriaged> 60 power_monitor_test_source_ = nullptr; 61 }; 62 63 class PowerMonitorTestObserver : public PowerSuspendObserver, 64 public PowerThermalObserver, 65 public PowerStateObserver { 66 public: 67 PowerMonitorTestObserver(); 68 ~PowerMonitorTestObserver() override; 69 70 // PowerStateObserver overrides. 71 void OnBatteryPowerStatusChange( 72 PowerStateObserver::BatteryPowerStatus battery_power_status) override; 73 // PowerSuspendObserver overrides. 74 void OnSuspend() override; 75 void OnResume() override; 76 // PowerThermalObserver overrides. 77 void OnThermalStateChange( 78 PowerThermalObserver::DeviceThermalState new_state) override; 79 void OnSpeedLimitChange(int speed_limit) override; 80 81 // Test status counts. power_state_changes()82 int power_state_changes() const { return power_state_changes_; } suspends()83 int suspends() const { return suspends_; } resumes()84 int resumes() const { return resumes_; } thermal_state_changes()85 int thermal_state_changes() const { return thermal_state_changes_; } speed_limit_changes()86 int speed_limit_changes() const { return speed_limit_changes_; } 87 last_power_status()88 PowerStateObserver::BatteryPowerStatus last_power_status() const { 89 return last_power_status_; 90 } last_thermal_state()91 PowerThermalObserver::DeviceThermalState last_thermal_state() const { 92 return last_thermal_state_; 93 } last_speed_limit()94 int last_speed_limit() const { return last_speed_limit_; } 95 96 private: 97 // Count of OnPowerStateChange notifications. 98 int power_state_changes_ = 0; 99 // Count of OnSuspend notifications. 100 int suspends_ = 0; 101 // Count of OnResume notifications. 102 int resumes_ = 0; 103 // Count of OnThermalStateChange notifications. 104 int thermal_state_changes_ = 0; 105 // Count of OnSpeedLimitChange notifications. 106 int speed_limit_changes_ = 0; 107 108 // Last power state we were notified of. 109 PowerStateObserver::BatteryPowerStatus last_power_status_ = 110 PowerStateObserver::BatteryPowerStatus::kUnknown; 111 // Last power thermal we were notified of. 112 PowerThermalObserver::DeviceThermalState last_thermal_state_ = 113 PowerThermalObserver::DeviceThermalState::kUnknown; 114 // Last speed limit we were notified of. 115 int last_speed_limit_ = PowerThermalObserver::kSpeedLimitMax; 116 }; 117 118 } // namespace test 119 } // namespace base 120 121 #endif // BASE_TEST_POWER_MONITOR_TEST_H_ 122