1 /*
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 
26 using android::binder::Status;
27 
28 using namespace testing;
29 using namespace android;
30 using namespace android::os;
31 
32 class MockIThermalService : public IThermalService {
33 public:
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));
70 };
71 
72 class NativeThermalUnitTest : public Test {
73 public:
SetUp()74     void SetUp() override {
75         mMockIThermalService = new StrictMock<MockIThermalService>();
76         AThermal_setIThermalServiceForTesting(mMockIThermalService);
77         mThermalManager = AThermal_acquireManager();
78     }
79 
TearDown()80     void TearDown() override {
81         AThermal_setIThermalServiceForTesting(nullptr);
82         AThermal_releaseManager(mThermalManager);
83     }
84 
85     StrictMock<MockIThermalService>* mMockIThermalService = nullptr;
86     AThermalManager* mThermalManager = nullptr;
87 };
88 
checkThermalHeadroomThresholds(const std::vector<float> & expected,const AThermalHeadroomThreshold * thresholds,size_t size)89 static void checkThermalHeadroomThresholds(const std::vector<float>& expected,
90                                            const AThermalHeadroomThreshold* thresholds,
91                                            size_t size) {
92     if (thresholds == nullptr) {
93         FAIL() << "Unexpected null thresholds pointer";
94     }
95     for (int i = 0; i < (int)size; i++) {
96         auto t = thresholds[i];
97         ASSERT_EQ(i, t.thermalStatus) << "threshold " << i << " should have status " << i;
98         ASSERT_EQ(expected[i], t.headroom)
99                 << "threshold " << i << " should have headroom " << expected[i];
100     }
101 }
102 
TEST_F(NativeThermalUnitTest,TestGetThermalHeadroomThresholds)103 TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholds) {
104     std::vector<float> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9};
105     EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
106             .Times(Exactly(1))
107             .WillRepeatedly(DoAll(SetArgPointee<0>(expected), Return(Status())));
108     const AThermalHeadroomThreshold* thresholds1 = nullptr;
109     size_t size1;
110     ASSERT_EQ(OK, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds1, &size1));
111     checkThermalHeadroomThresholds(expected, thresholds1, size1);
112     // following calls should be cached
113     EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_)).Times(0);
114 
115     const AThermalHeadroomThreshold* thresholds2 = nullptr;
116     size_t size2;
117     ASSERT_EQ(OK, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds2, &size2));
118     checkThermalHeadroomThresholds(expected, thresholds2, size2);
119 }
120 
TEST_F(NativeThermalUnitTest,TestGetThermalHeadroomThresholdsFailedWithServerError)121 TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithServerError) {
122     const AThermalHeadroomThreshold* thresholds = nullptr;
123     size_t size;
124     EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
125             .Times(Exactly(1))
126             .WillOnce(Return(
127                     Status::fromExceptionCode(binder::Status::Exception::EX_ILLEGAL_ARGUMENT)));
128     ASSERT_EQ(EPIPE, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, &size));
129     ASSERT_EQ(nullptr, thresholds);
130 }
131 
TEST_F(NativeThermalUnitTest,TestGetThermalHeadroomThresholdsFailedWithFeatureDisabled)132 TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithFeatureDisabled) {
133     const AThermalHeadroomThreshold* thresholds = nullptr;
134     size_t size;
135     EXPECT_CALL(*mMockIThermalService, getThermalHeadroomThresholds(_))
136             .Times(Exactly(1))
137             .WillOnce(Return(Status::fromExceptionCode(
138                     binder::Status::Exception::EX_UNSUPPORTED_OPERATION)));
139     ASSERT_EQ(ENOSYS, AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, &size));
140     ASSERT_EQ(nullptr, thresholds);
141 }
142 
TEST_F(NativeThermalUnitTest,TestGetThermalHeadroomThresholdsFailedWithNullPtr)143 TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithNullPtr) {
144     const AThermalHeadroomThreshold* thresholds = nullptr;
145     size_t size;
146     size_t* nullSize = nullptr;
147     ASSERT_EQ(EINVAL,
148               AThermal_getThermalHeadroomThresholds(mThermalManager, &thresholds, nullSize));
149     ASSERT_EQ(nullptr, thresholds);
150     ASSERT_EQ(EINVAL, AThermal_getThermalHeadroomThresholds(mThermalManager, nullptr, &size));
151 }
152 
TEST_F(NativeThermalUnitTest,TestGetThermalHeadroomThresholdsFailedWithNonEmptyPtr)153 TEST_F(NativeThermalUnitTest, TestGetThermalHeadroomThresholdsFailedWithNonEmptyPtr) {
154     const AThermalHeadroomThreshold* initialized = new AThermalHeadroomThreshold[1];
155     size_t size;
156     ASSERT_EQ(EINVAL, AThermal_getThermalHeadroomThresholds(mThermalManager, &initialized, &size));
157     delete[] initialized;
158 }
159