power_monitor_test.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_POWER_MONITOR_TEST_H_
  5. #define BASE_TEST_POWER_MONITOR_TEST_H_
  6. #include "base/power_monitor/power_monitor.h"
  7. #include "base/power_monitor/power_monitor_source.h"
  8. #include "base/power_monitor/power_observer.h"
  9. namespace base {
  10. namespace test {
  11. // Use PowerMonitorTestSource via ScopedPowerMonitorTestSource wrapper when you
  12. // need to simulate power events (suspend and resume).
  13. class PowerMonitorTestSource;
  14. // ScopedPowerMonitorTestSource initializes the PowerMonitor with a Mock
  15. // PowerMonitorSource. Mock power notifications can be simulated through this
  16. // helper class.
  17. //
  18. // Example:
  19. // base::test::ScopedPowerMonitorTestSource power_monitor_source;
  20. // power_monitor_source.Suspend();
  21. // [...]
  22. // power_monitor_source.Resume();
  23. class ScopedPowerMonitorTestSource {
  24. public:
  25. ScopedPowerMonitorTestSource();
  26. ~ScopedPowerMonitorTestSource();
  27. ScopedPowerMonitorTestSource(const ScopedPowerMonitorTestSource&) = delete;
  28. ScopedPowerMonitorTestSource& operator=(const ScopedPowerMonitorTestSource&) =
  29. delete;
  30. // Retrieve current states.
  31. PowerThermalObserver::DeviceThermalState GetCurrentThermalState();
  32. bool IsOnBatteryPower();
  33. // Sends asynchronous notifications to registered observers.
  34. void Suspend();
  35. void Resume();
  36. void SetOnBatteryPower(bool on_battery_power);
  37. void GenerateSuspendEvent();
  38. void GenerateResumeEvent();
  39. void GeneratePowerStateEvent(bool on_battery_power);
  40. void GenerateThermalThrottlingEvent(
  41. PowerThermalObserver::DeviceThermalState new_thermal_state);
  42. void GenerateSpeedLimitEvent(int speed_limit);
  43. private:
  44. // Owned by PowerMonitor.
  45. PowerMonitorTestSource* power_monitor_test_source_ = nullptr;
  46. };
  47. class PowerMonitorTestObserver : public PowerSuspendObserver,
  48. public PowerThermalObserver,
  49. public PowerStateObserver {
  50. public:
  51. PowerMonitorTestObserver();
  52. ~PowerMonitorTestObserver() override;
  53. // PowerStateObserver overrides.
  54. void OnPowerStateChange(bool on_battery_power) override;
  55. // PowerSuspendObserver overrides.
  56. void OnSuspend() override;
  57. void OnResume() override;
  58. // PowerThermalObserver overrides.
  59. void OnThermalStateChange(
  60. PowerThermalObserver::DeviceThermalState new_state) override;
  61. void OnSpeedLimitChange(int speed_limit) override;
  62. // Test status counts.
  63. int power_state_changes() const { return power_state_changes_; }
  64. int suspends() const { return suspends_; }
  65. int resumes() const { return resumes_; }
  66. int thermal_state_changes() const { return thermal_state_changes_; }
  67. int speed_limit_changes() const { return speed_limit_changes_; }
  68. bool last_power_state() const { return last_power_state_; }
  69. PowerThermalObserver::DeviceThermalState last_thermal_state() const {
  70. return last_thermal_state_;
  71. }
  72. int last_speed_limit() const { return last_speed_limit_; }
  73. private:
  74. // Count of OnPowerStateChange notifications.
  75. int power_state_changes_ = 0;
  76. // Count of OnSuspend notifications.
  77. int suspends_ = 0;
  78. // Count of OnResume notifications.
  79. int resumes_ = 0;
  80. // Count of OnThermalStateChange notifications.
  81. int thermal_state_changes_ = 0;
  82. // Count of OnSpeedLimitChange notifications.
  83. int speed_limit_changes_ = 0;
  84. // Last power state we were notified of.
  85. bool last_power_state_ = false;
  86. // Last power thermal we were notified of.
  87. PowerThermalObserver::DeviceThermalState last_thermal_state_ =
  88. PowerThermalObserver::DeviceThermalState::kUnknown;
  89. // Last speed limit we were notified of.
  90. int last_speed_limit_ = PowerThermalObserver::kSpeedLimitMax;
  91. };
  92. } // namespace test
  93. } // namespace base
  94. #endif // BASE_TEST_POWER_MONITOR_TEST_H_