blob: 4e319fc41d7c48c96562e5a46d5bbdf21d255475 [file] [log] [blame]
Xiang Wang5f4e8192023-10-25 13:40:35 -07001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "NativeThermalUnitTest"
18
19#include <android/os/IThermalService.h>
20#include <android/thermal.h>
21#include <binder/IBinder.h>
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24#include <thermal_private.h>
25
26using android::binder::Status;
27
28using namespace testing;
29using namespace android;
30using namespace android::os;
31
32class MockIThermalService : public IThermalService {
33public:
34 MOCK_METHOD(Status, registerThermalEventListener,
35 (const ::android::sp<::android::os::IThermalEventListener>& listener,
36 bool* _aidl_return),
37 (override));
38 MOCK_METHOD(Status, registerThermalEventListenerWithType,
39 (const ::android::sp<::android::os::IThermalEventListener>& listener, int32_t type,
40 bool* _aidl_return),
41 (override));
42 MOCK_METHOD(Status, unregisterThermalEventListener,
43 (const ::android::sp<::android::os::IThermalEventListener>& listener,
44 bool* _aidl_return),
45 (override));
46 MOCK_METHOD(Status, getCurrentTemperatures,
47 (::std::vector<::android::os::Temperature> * _aidl_return), (override));
48 MOCK_METHOD(Status, getCurrentTemperaturesWithType,
49 (int32_t type, ::std::vector<::android::os::Temperature>* _aidl_return),
50 (override));
51 MOCK_METHOD(Status, registerThermalStatusListener,
52 (const ::android::sp<::android::os::IThermalStatusListener>& listener,
53 bool* _aidl_return),
54 (override));
55 MOCK_METHOD(Status, unregisterThermalStatusListener,
56 (const ::android::sp<::android::os::IThermalStatusListener>& listener,
57 bool* _aidl_return),
58 (override));
59 MOCK_METHOD(Status, getCurrentThermalStatus, (int32_t * _aidl_return), (override));
60 MOCK_METHOD(Status, getCurrentCoolingDevices,
61 (::std::vector<::android::os::CoolingDevice> * _aidl_return), (override));
62 MOCK_METHOD(Status, getCurrentCoolingDevicesWithType,
63 (int32_t type, ::std::vector<::android::os::CoolingDevice>* _aidl_return),
64 (override));
65 MOCK_METHOD(Status, getThermalHeadroom, (int32_t forecastSeconds, float* _aidl_return),
66 (override));
67 MOCK_METHOD(Status, getThermalHeadroomThresholds, (::std::vector<float> * _aidl_return),
68 (override));
69 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
Xiang Wang2b054632024-10-30 21:53:43 +000070 MOCK_METHOD(Status, registerThermalHeadroomListener,
71 (const ::android::sp<::android::os::IThermalHeadroomListener>& listener,
72 bool* _aidl_return),
73 (override));
74 MOCK_METHOD(Status, unregisterThermalHeadroomListener,
75 (const ::android::sp<::android::os::IThermalHeadroomListener>& listener,
76 bool* _aidl_return),
77 (override));
Xiang Wang5f4e8192023-10-25 13:40:35 -070078};
79
80class NativeThermalUnitTest : public Test {
81public:
82 void SetUp() override {
83 mMockIThermalService = new StrictMock<MockIThermalService>();
84 AThermal_setIThermalServiceForTesting(mMockIThermalService);
85 mThermalManager = AThermal_acquireManager();
86 }
87
88 void TearDown() override {
89 AThermal_setIThermalServiceForTesting(nullptr);
90 AThermal_releaseManager(mThermalManager);
91 }
92
93 StrictMock<MockIThermalService>* mMockIThermalService = nullptr;
94 AThermalManager* mThermalManager = nullptr;
95};
96
97static void checkThermalHeadroomThresholds(const std::vector<float>& expected,
98 const AThermalHeadroomThreshold* thresholds,
99 size_t size) {
100 if (thresholds == nullptr) {
101 FAIL() << "Unexpected null thresholds pointer";
102 }
103 for (int i = 0; i < (int)size; i++) {
104 auto t = thresholds[i];
105 ASSERT_EQ(i, t.thermalStatus) << "threshold " << i << " should have status " << i;
106 ASSERT_EQ(expected[i], t.headroom)
107 << "threshold " << i << " should have headroom " << expected[i];
108 }
109}
110
111TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholds) {
112 std::vector<float> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9};
113 EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
114 .Times(Exactly(1))
115 .WillRepeatedly(DoAll(SetArgPointee<0>(expected), Return(Status())));
116 const AThermalHeadroomThreshold* thresholds1 = nullptr;
117 size_t size1;
118 ASSERT_EQ(OK, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds1, &size1));
119 checkThermalHeadroomThresholds(expected, thresholds1, size1);
120 // following calls should be cached
121 EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_)).Times(0);
122
123 const AThermalHeadroomThreshold* thresholds2 = nullptr;
124 size_t size2;
125 ASSERT_EQ(OK, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds2, &size2));
126 checkThermalHeadroomThresholds(expected, thresholds2, size2);
127}
128
129TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithServerError) {
130 const AThermalHeadroomThreshold* thresholds = nullptr;
131 size_t size;
132 EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
133 .Times(Exactly(1))
134 .WillOnce(Return(
135 Status::fromExceptionCode(binder::Status::Exception::EX_ILLEGAL_ARGUMENT)));
136 ASSERT_EQ(EPIPE, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, &size));
137 ASSERT_EQ(nullptr, thresholds);
138}
139
140TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithFeatureDisabled) {
141 const AThermalHeadroomThreshold* thresholds = nullptr;
142 size_t size;
143 EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
144 .Times(Exactly(1))
145 .WillOnce(Return(Status::fromExceptionCode(
146 binder::Status::Exception::EX_UNSUPPORTED_OPERATION)));
147 ASSERT_EQ(ENOSYS, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, &size));
148 ASSERT_EQ(nullptr, thresholds);
149}
150
151TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithNullPtr) {
152 const AThermalHeadroomThreshold* thresholds = nullptr;
153 size_t size;
154 size_t* nullSize = nullptr;
155 ASSERT_EQ(EINVAL,
156 AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, nullSize));
157 ASSERT_EQ(nullptr, thresholds);
158 ASSERT_EQ(EINVAL, AThermal_getThermalHeadroomThresholds(mThermalManager, nullptr, &size));
159}
160
161TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithNonEmptyPtr) {
162 const AThermalHeadroomThreshold* initialized = new AThermalHeadroomThreshold[1];
163 size_t size;
164 ASSERT_EQ(EINVAL, AThermal_getThermalHeadroomThresholds(mThermalManager, &initialized, &size));
165 delete[] initialized;
166}