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