• 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 #ifndef BASE_TEST_POWER_MONITOR_TEST_H_
6 #define BASE_TEST_POWER_MONITOR_TEST_H_
7 
8 #include "base/memory/raw_ptr_exclusion.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   // This field is not a raw_ptr<> because it was filtered by the rewriter for:
58   // #union
59   RAW_PTR_EXCLUSION PowerMonitorTestSource* power_monitor_test_source_ =
60       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 OnPowerStateChange(bool on_battery_power) override;
72   // PowerSuspendObserver overrides.
73   void OnSuspend() override;
74   void OnResume() override;
75   // PowerThermalObserver overrides.
76   void OnThermalStateChange(
77       PowerThermalObserver::DeviceThermalState new_state) override;
78   void OnSpeedLimitChange(int speed_limit) override;
79 
80   // Test status counts.
power_state_changes()81   int power_state_changes() const { return power_state_changes_; }
suspends()82   int suspends() const { return suspends_; }
resumes()83   int resumes() const { return resumes_; }
thermal_state_changes()84   int thermal_state_changes() const { return thermal_state_changes_; }
speed_limit_changes()85   int speed_limit_changes() const { return speed_limit_changes_; }
86 
last_power_state()87   bool last_power_state() const { return last_power_state_; }
last_thermal_state()88   PowerThermalObserver::DeviceThermalState last_thermal_state() const {
89     return last_thermal_state_;
90   }
last_speed_limit()91   int last_speed_limit() const { return last_speed_limit_; }
92 
93  private:
94   // Count of OnPowerStateChange notifications.
95   int power_state_changes_ = 0;
96   // Count of OnSuspend notifications.
97   int suspends_ = 0;
98   // Count of OnResume notifications.
99   int resumes_ = 0;
100   // Count of OnThermalStateChange notifications.
101   int thermal_state_changes_ = 0;
102   // Count of OnSpeedLimitChange notifications.
103   int speed_limit_changes_ = 0;
104 
105   // Last power state we were notified of.
106   bool last_power_state_ = false;
107   // Last power thermal we were notified of.
108   PowerThermalObserver::DeviceThermalState last_thermal_state_ =
109       PowerThermalObserver::DeviceThermalState::kUnknown;
110   // Last speed limit we were notified of.
111   int last_speed_limit_ = PowerThermalObserver::kSpeedLimitMax;
112 };
113 
114 }  // namespace test
115 }  // namespace base
116 
117 #endif  // BASE_TEST_POWER_MONITOR_TEST_H_
118