blob: 75536a6057503a8b4b59fed6b72997d9db1804c5 [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 Wang84ce54e2018-10-18 13:56:03 -0700109 auto ret = mThermalCallback->notifyThrottling(kThrottleTemp);
110 ASSERT_TRUE(ret.isOk());
111 auto res = mThermalCallback->WaitForCallback(kCallbackNameNotifyThrottling);
112 EXPECT_TRUE(res.no_timeout);
113 ASSERT_TRUE(res.args);
114 EXPECT_EQ(kThrottleTemp, res.args->temperature);
115}
116
117// Test Thermal->registerThermalChangedCallback.
Dan Shi030bef32019-10-16 14:45:16 -0700118TEST_P(ThermalHidlTest, RegisterThermalChangedCallbackTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700119 // Expect to fail with same callback
120 auto ret = mThermal->registerThermalChangedCallback(
Wei Wang84cf5c92019-02-20 11:39:14 -0800121 mThermalCallback, false, TemperatureType::SKIN,
122 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); });
123 ASSERT_TRUE(ret.isOk());
124 // Expect to fail with null callback
125 ret = mThermal->registerThermalChangedCallback(
126 nullptr, false, TemperatureType::SKIN,
127 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); });
Wei Wang84ce54e2018-10-18 13:56:03 -0700128 ASSERT_TRUE(ret.isOk());
129 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
130 // Expect to succeed with different callback
131 ret = mThermal->registerThermalChangedCallback(
132 localThermalCallback, false, TemperatureType::SKIN,
133 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
134 ASSERT_TRUE(ret.isOk());
Wei Wang84cf5c92019-02-20 11:39:14 -0800135 // Remove the local callback
Wei Wang84ce54e2018-10-18 13:56:03 -0700136 ret = mThermal->unregisterThermalChangedCallback(
137 localThermalCallback,
138 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
139 ASSERT_TRUE(ret.isOk());
Wei Wang84cf5c92019-02-20 11:39:14 -0800140 // Expect to fail with null callback
141 ret = mThermal->unregisterThermalChangedCallback(nullptr, [](ThermalStatus status) {
142 EXPECT_EQ(ThermalStatusCode::FAILURE, status.code);
143 });
144 ASSERT_TRUE(ret.isOk());
Wei Wang84ce54e2018-10-18 13:56:03 -0700145}
146
147// Test Thermal->unregisterThermalChangedCallback.
Dan Shi030bef32019-10-16 14:45:16 -0700148TEST_P(ThermalHidlTest, UnregisterThermalChangedCallbackTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700149 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
150 // Expect to fail as the callback was not registered before
151 auto ret = mThermal->unregisterThermalChangedCallback(
152 localThermalCallback,
153 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
154 ASSERT_TRUE(ret.isOk());
155 // Register a local callback
156 ret = mThermal->registerThermalChangedCallback(
157 localThermalCallback, false, TemperatureType::SKIN,
158 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
159 ASSERT_TRUE(ret.isOk());
160 // Expect to succeed with callback removed
161 ret = mThermal->unregisterThermalChangedCallback(
162 localThermalCallback,
163 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
164 ASSERT_TRUE(ret.isOk());
165 // Expect to fail as the callback has been unregistered already
166 ret = mThermal->unregisterThermalChangedCallback(
167 localThermalCallback,
168 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
169 ASSERT_TRUE(ret.isOk());
170}
171
172// Sanity test for Thermal::getCurrentTemperatures().
Dan Shi030bef32019-10-16 14:45:16 -0700173TEST_P(ThermalHidlTest, TemperatureTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700174 mThermal->getCurrentTemperatures(false, TemperatureType::SKIN,
175 [](ThermalStatus status, hidl_vec<Temperature> temperatures) {
176 if (temperatures.size()) {
177 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
178 } else {
179 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
180 }
Wei Wangdd629692019-04-05 10:54:20 -0700181 for (int i = 0; i < temperatures.size(); ++i) {
182 EXPECT_LT(0u, temperatures[i].name.size());
183 }
Wei Wang84ce54e2018-10-18 13:56:03 -0700184 });
Wei Wangc4c25592018-10-25 19:22:25 -0700185 auto types = hidl_enum_range<TemperatureType>();
186 for (const auto& type : types) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700187 mThermal->getCurrentTemperatures(
188 true, type, [&type](ThermalStatus status, hidl_vec<Temperature> temperatures) {
189 if (temperatures.size()) {
190 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
191 } else {
192 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
193 }
194 for (int i = 0; i < temperatures.size(); ++i) {
195 EXPECT_EQ(type, temperatures[i].type);
Wei Wangdd629692019-04-05 10:54:20 -0700196 EXPECT_LT(0u, temperatures[i].name.size());
Wei Wang84ce54e2018-10-18 13:56:03 -0700197 }
198 });
199 }
200}
201
202// Sanity test for Thermal::getTemperatureThresholds().
Dan Shi030bef32019-10-16 14:45:16 -0700203TEST_P(ThermalHidlTest, TemperatureThresholdTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700204 mThermal->getTemperatureThresholds(
205 false, TemperatureType::SKIN,
206 [](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) {
207 if (temperatures.size()) {
208 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
209 } else {
210 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
211 }
212 });
213 for (int i = static_cast<int>(TemperatureType::UNKNOWN);
214 i <= static_cast<int>(TemperatureType::POWER_AMPLIFIER); ++i) {
215 auto type = static_cast<TemperatureType>(i);
216 mThermal->getTemperatureThresholds(
217 true, type, [&type](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) {
218 if (temperatures.size()) {
219 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
220 } else {
221 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
222 }
223 for (int i = 0; i < temperatures.size(); ++i) {
224 EXPECT_EQ(type, temperatures[i].type);
225 }
226 });
227 }
228}
229
230// Sanity test for Thermal::getCurrentCoolingDevices().
Dan Shi030bef32019-10-16 14:45:16 -0700231TEST_P(ThermalHidlTest, CoolingDeviceTest) {
Wei Wang84ce54e2018-10-18 13:56:03 -0700232 mThermal->getCurrentCoolingDevices(
233 false, CoolingType::CPU, [](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) {
234 if (cooling_devices.size()) {
235 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
236 } else {
237 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
238 }
Wei Wangdd629692019-04-05 10:54:20 -0700239 for (int i = 0; i < cooling_devices.size(); ++i) {
240 EXPECT_LT(0u, cooling_devices[i].name.size());
241 }
Wei Wang84ce54e2018-10-18 13:56:03 -0700242 });
243 for (int i = 0; i <= static_cast<int>(CoolingType::COMPONENT); ++i) {
244 auto type = static_cast<CoolingType>(i);
245 mThermal->getCurrentCoolingDevices(
246 true, type, [&type](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) {
247 if (cooling_devices.size()) {
248 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
249 } else {
250 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
251 }
252 for (int i = 0; i < cooling_devices.size(); ++i) {
253 EXPECT_EQ(type, cooling_devices[i].type);
Wei Wangdd629692019-04-05 10:54:20 -0700254 EXPECT_LT(0u, cooling_devices[i].name.size());
Wei Wang84ce54e2018-10-18 13:56:03 -0700255 }
256 });
257 }
258}
259
Dan Shi030bef32019-10-16 14:45:16 -0700260INSTANTIATE_TEST_SUITE_P(
261 PerInstance, ThermalHidlTest,
262 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IThermal::descriptor)),
263 android::hardware::PrintInstanceNameToString);