blob: 6c5aca7c246af18383e9526b2c9fff79cad6a278 [file] [log] [blame]
Wei Wang84ce54e2018-10-18 13:56:03 -07001/*
2 * Copyright (C) 2018 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#include <android/hardware/thermal/2.0/IThermal.h>
18#include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
19#include <android/hardware/thermal/2.0/types.h>
20
Dan Shi030bef32019-10-16 14:45:16 -070021#include <gtest/gtest.h>
22#include <hidl/GtestPrinter.h>
23#include <hidl/ServiceManagement.h>
24
Wei Wang84ce54e2018-10-18 13:56:03 -070025#include <VtsHalHidlTargetCallbackBase.h>
Wei Wang84ce54e2018-10-18 13:56:03 -070026
27using ::android::sp;
Wei Wangc4c25592018-10-25 19:22:25 -070028using ::android::hardware::hidl_enum_range;
Wei Wang84ce54e2018-10-18 13:56:03 -070029using ::android::hardware::hidl_vec;
30using ::android::hardware::Return;
31using ::android::hardware::Void;
32using ::android::hardware::thermal::V1_0::ThermalStatus;
33using ::android::hardware::thermal::V1_0::ThermalStatusCode;
34using ::android::hardware::thermal::V2_0::CoolingDevice;
35using ::android::hardware::thermal::V2_0::CoolingType;
36using ::android::hardware::thermal::V2_0::IThermal;
37using ::android::hardware::thermal::V2_0::IThermalChangedCallback;
38using ::android::hardware::thermal::V2_0::Temperature;
39using ::android::hardware::thermal::V2_0::TemperatureThreshold;
40using ::android::hardware::thermal::V2_0::TemperatureType;
41using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
Wei Wang84ce54e2018-10-18 13:56:03 -070042
43constexpr char kCallbackNameNotifyThrottling[] = "notifyThrottling";
44static const Temperature kThrottleTemp = {
45 .type = TemperatureType::SKIN,
46 .name = "test temperature sensor",
47 .value = 98.6,
48 .throttlingStatus = ThrottlingSeverity::CRITICAL,
49};
50
51class ThermalCallbackArgs {
52 public:
53 Temperature temperature;
54};
55
56// Callback class for receiving thermal event notifications from main class
57class ThermalCallback : public ::testing::VtsHalHidlTargetCallbackBase<ThermalCallbackArgs>,
58 public IThermalChangedCallback {
59 public:
60 Return<void> notifyThrottling(const Temperature& temperature) override {
61 ThermalCallbackArgs args;
62 args.temperature = temperature;
63 NotifyFromCallback(kCallbackNameNotifyThrottling, args);
64 return Void();
65 }
66};
67
Wei Wang84ce54e2018-10-18 13:56:03 -070068// The main test class for THERMAL HIDL HAL 2.0.
Dan Shi030bef32019-10-16 14:45:16 -070069class ThermalHidlTest : public testing::TestWithParam<std::string> {
Wei Wang84ce54e2018-10-18 13:56:03 -070070 public:
71 virtual void SetUp() override {
Dan Shi030bef32019-10-16 14:45:16 -070072 mThermal = IThermal::getService(GetParam());
Wei Wang84ce54e2018-10-18 13:56:03 -070073 ASSERT_NE(mThermal, nullptr);
74 mThermalCallback = new (std::nothrow) ThermalCallback();
75 ASSERT_NE(mThermalCallback, nullptr);
76 auto ret = mThermal->registerThermalChangedCallback(
77 mThermalCallback, false, TemperatureType::SKIN,
78 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
79 ASSERT_TRUE(ret.isOk());
80 // Expect to fail if register again
81 ret = mThermal->registerThermalChangedCallback(
82 mThermalCallback, false, TemperatureType::SKIN,
83 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
84 ASSERT_TRUE(ret.isOk());
85 }
86
87 virtual void TearDown() override {
88 auto ret = mThermal->unregisterThermalChangedCallback(
89 mThermalCallback,
90 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
91 ASSERT_TRUE(ret.isOk());
92 // Expect to fail if unregister again
93 ret = mThermal->unregisterThermalChangedCallback(
94 mThermalCallback,
95 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
96 ASSERT_TRUE(ret.isOk());
97 }
98
99 protected:
100 sp<IThermal> mThermal;
101 sp<ThermalCallback> mThermalCallback;
102}; // class ThermalHidlTest
103
104// Test ThermalChangedCallback::notifyThrottling().
105// This just calls into and back from our local ThermalChangedCallback impl.
106// Note: a real thermal throttling event from the Thermal HAL could be
107// inadvertently received here.
Dan Shi030bef32019-10-16 14:45:16 -0700108TEST_P(ThermalHidlTest, NotifyThrottlingTest) {
Wei Wangfa6a0122021-08-27 11:20:39 -0700109 sp<ThermalCallback> thermalCallback = new (std::nothrow) ThermalCallback();
110 auto ret = thermalCallback->notifyThrottling(kThrottleTemp);
Wei Wang84ce54e2018-10-18 13:56:03 -0700111 ASSERT_TRUE(ret.isOk());
Wei Wangfa6a0122021-08-27 11:20:39 -0700112 auto res = thermalCallback->WaitForCallback(kCallbackNameNotifyThrottling);
Wei Wang84ce54e2018-10-18 13:56:03 -0700113 EXPECT_TRUE(res.no_timeout);
114 ASSERT_TRUE(res.args);
115 EXPECT_EQ(kThrottleTemp, res.args->temperature);
116}
117
118// Test Thermal->registerThermalChangedCallback.
Dan Shi030bef32019-10-16 14:45:16 -0700119TEST_P(ThermalHidlTest, RegisterThermalChangedCallbackTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700120 // Expect to fail with same callback
121 auto ret = mThermal->registerThermalChangedCallback(
Wei Wang84cf5c92019-02-20 11:39:14 -0800122 mThermalCallback, false, TemperatureType::SKIN,
123 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); });
124 ASSERT_TRUE(ret.isOk());
125 // Expect to fail with null callback
126 ret = mThermal->registerThermalChangedCallback(
127 nullptr, false, TemperatureType::SKIN,
128 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); });
Wei Wang84ce54e2018-10-18 13:56:03 -0700129 ASSERT_TRUE(ret.isOk());
130 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
131 // Expect to succeed with different callback
132 ret = mThermal->registerThermalChangedCallback(
133 localThermalCallback, false, TemperatureType::SKIN,
134 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
135 ASSERT_TRUE(ret.isOk());
Wei Wang84cf5c92019-02-20 11:39:14 -0800136 // Remove the local callback
Wei Wang84ce54e2018-10-18 13:56:03 -0700137 ret = mThermal->unregisterThermalChangedCallback(
138 localThermalCallback,
139 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
140 ASSERT_TRUE(ret.isOk());
Wei Wang84cf5c92019-02-20 11:39:14 -0800141 // Expect to fail with null callback
142 ret = mThermal->unregisterThermalChangedCallback(nullptr, [](ThermalStatus status) {
143 EXPECT_EQ(ThermalStatusCode::FAILURE, status.code);
144 });
145 ASSERT_TRUE(ret.isOk());
Wei Wang84ce54e2018-10-18 13:56:03 -0700146}
147
148// Test Thermal->unregisterThermalChangedCallback.
Dan Shi030bef32019-10-16 14:45:16 -0700149TEST_P(ThermalHidlTest, UnregisterThermalChangedCallbackTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700150 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
151 // Expect to fail as the callback was not registered before
152 auto ret = mThermal->unregisterThermalChangedCallback(
153 localThermalCallback,
154 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
155 ASSERT_TRUE(ret.isOk());
156 // Register a local callback
157 ret = mThermal->registerThermalChangedCallback(
158 localThermalCallback, false, TemperatureType::SKIN,
159 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
160 ASSERT_TRUE(ret.isOk());
161 // Expect to succeed with callback removed
162 ret = mThermal->unregisterThermalChangedCallback(
163 localThermalCallback,
164 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
165 ASSERT_TRUE(ret.isOk());
166 // Expect to fail as the callback has been unregistered already
167 ret = mThermal->unregisterThermalChangedCallback(
168 localThermalCallback,
169 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
170 ASSERT_TRUE(ret.isOk());
171}
172
173// Sanity test for Thermal::getCurrentTemperatures().
Dan Shi030bef32019-10-16 14:45:16 -0700174TEST_P(ThermalHidlTest, TemperatureTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700175 mThermal->getCurrentTemperatures(false, TemperatureType::SKIN,
176 [](ThermalStatus status, hidl_vec<Temperature> temperatures) {
177 if (temperatures.size()) {
178 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
179 } else {
180 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
181 }
Wei Wangdd629692019-04-05 10:54:20 -0700182 for (int i = 0; i < temperatures.size(); ++i) {
183 EXPECT_LT(0u, temperatures[i].name.size());
184 }
Wei Wang84ce54e2018-10-18 13:56:03 -0700185 });
Wei Wangc4c25592018-10-25 19:22:25 -0700186 auto types = hidl_enum_range<TemperatureType>();
187 for (const auto& type : types) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700188 mThermal->getCurrentTemperatures(
189 true, type, [&type](ThermalStatus status, hidl_vec<Temperature> temperatures) {
190 if (temperatures.size()) {
191 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
192 } else {
193 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
194 }
195 for (int i = 0; i < temperatures.size(); ++i) {
196 EXPECT_EQ(type, temperatures[i].type);
Wei Wangdd629692019-04-05 10:54:20 -0700197 EXPECT_LT(0u, temperatures[i].name.size());
Wei Wang84ce54e2018-10-18 13:56:03 -0700198 }
199 });
200 }
201}
202
203// Sanity test for Thermal::getTemperatureThresholds().
Dan Shi030bef32019-10-16 14:45:16 -0700204TEST_P(ThermalHidlTest, TemperatureThresholdTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700205 mThermal->getTemperatureThresholds(
206 false, TemperatureType::SKIN,
207 [](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) {
208 if (temperatures.size()) {
209 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
210 } else {
211 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
212 }
213 });
214 for (int i = static_cast<int>(TemperatureType::UNKNOWN);
215 i <= static_cast<int>(TemperatureType::POWER_AMPLIFIER); ++i) {
216 auto type = static_cast<TemperatureType>(i);
217 mThermal->getTemperatureThresholds(
218 true, type, [&type](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) {
219 if (temperatures.size()) {
220 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
221 } else {
222 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
223 }
224 for (int i = 0; i < temperatures.size(); ++i) {
225 EXPECT_EQ(type, temperatures[i].type);
226 }
227 });
228 }
229}
230
231// Sanity test for Thermal::getCurrentCoolingDevices().
Dan Shi030bef32019-10-16 14:45:16 -0700232TEST_P(ThermalHidlTest, CoolingDeviceTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700233 mThermal->getCurrentCoolingDevices(
234 false, CoolingType::CPU, [](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) {
235 if (cooling_devices.size()) {
236 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
237 } else {
238 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
239 }
Wei Wangdd629692019-04-05 10:54:20 -0700240 for (int i = 0; i < cooling_devices.size(); ++i) {
241 EXPECT_LT(0u, cooling_devices[i].name.size());
242 }
Wei Wang84ce54e2018-10-18 13:56:03 -0700243 });
244 for (int i = 0; i <= static_cast<int>(CoolingType::COMPONENT); ++i) {
245 auto type = static_cast<CoolingType>(i);
246 mThermal->getCurrentCoolingDevices(
247 true, type, [&type](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) {
248 if (cooling_devices.size()) {
249 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
250 } else {
251 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
252 }
253 for (int i = 0; i < cooling_devices.size(); ++i) {
254 EXPECT_EQ(type, cooling_devices[i].type);
Wei Wangdd629692019-04-05 10:54:20 -0700255 EXPECT_LT(0u, cooling_devices[i].name.size());
Wei Wang84ce54e2018-10-18 13:56:03 -0700256 }
257 });
258 }
259}
260
Dan Shiba4d5322020-07-28 13:09:30 -0700261GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalHidlTest);
Dan Shi030bef32019-10-16 14:45:16 -0700262INSTANTIATE_TEST_SUITE_P(
263 PerInstance, ThermalHidlTest,
264 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IThermal::descriptor)),
265 android::hardware::PrintInstanceNameToString);