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