thermal_state_observer_mac_unittest.mm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2020 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. #include "base/power_monitor/thermal_state_observer_mac.h"
  5. #import <Foundation/Foundation.h>
  6. #include <IOKit/pwr_mgt/IOPMLib.h>
  7. #include <notify.h>
  8. #include <memory>
  9. #include <queue>
  10. #include "base/bind.h"
  11. #include "base/logging.h"
  12. #include "base/power_monitor/power_monitor.h"
  13. #include "base/power_monitor/power_monitor_source.h"
  14. #include "base/synchronization/waitable_event.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. using DeviceThermalState = base::PowerThermalObserver::DeviceThermalState;
  18. using ::testing::MockFunction;
  19. using ::testing::Mock;
  20. using ::testing::Invoke;
  21. namespace base {
  22. void IgnoreStateChange(DeviceThermalState state) {}
  23. void IgnoreSpeedLimitChange(int speed_limit) {}
  24. // Verifies that a NSProcessInfoThermalStateDidChangeNotification produces the
  25. // adequate OnStateChange() call.
  26. TEST(ThermalStateObserverMacTest, StateChange) NS_AVAILABLE_MAC(10_10_3) {
  27. MockFunction<void(DeviceThermalState)> function;
  28. // ThermalStateObserverMac sends the current thermal state on construction.
  29. EXPECT_CALL(function, Call);
  30. ThermalStateObserverMac observer(
  31. BindRepeating(&MockFunction<void(DeviceThermalState)>::Call,
  32. Unretained(&function)),
  33. BindRepeating(IgnoreSpeedLimitChange), "ignored key");
  34. Mock::VerifyAndClearExpectations(&function);
  35. EXPECT_CALL(function, Call(DeviceThermalState::kCritical));
  36. observer.state_for_testing_ = DeviceThermalState::kCritical;
  37. [NSNotificationCenter.defaultCenter
  38. postNotificationName:NSProcessInfoThermalStateDidChangeNotification
  39. object:nil
  40. userInfo:nil];
  41. }
  42. TEST(ThermalStateObserverMacTest, SpeedChange) {
  43. MockFunction<void(int)> function;
  44. // ThermalStateObserverMac sends the current speed limit state on
  45. // construction.
  46. static constexpr const char* kTestNotificationKey =
  47. "ThermalStateObserverMacTest_SpeedChange";
  48. EXPECT_CALL(function, Call);
  49. ThermalStateObserverMac observer(
  50. BindRepeating(IgnoreStateChange),
  51. BindRepeating(&MockFunction<void(int)>::Call, Unretained(&function)),
  52. kTestNotificationKey);
  53. Mock::VerifyAndClearExpectations(&function);
  54. EXPECT_CALL(function, Call).WillOnce(Invoke([] {
  55. CFRunLoopStop(CFRunLoopGetCurrent());
  56. }));
  57. notify_post(kTestNotificationKey);
  58. CFRunLoopRun();
  59. }
  60. } // namespace base