blob: 3893014e20f01c545afbb5845e282828c9df9e20 [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
21#include <VtsHalHidlTargetCallbackBase.h>
22#include <VtsHalHidlTargetTestBase.h>
23#include <VtsHalHidlTargetTestEnvBase.h>
24
25using ::android::sp;
Wei Wangc4c25592018-10-25 19:22:25 -070026using ::android::hardware::hidl_enum_range;
Wei Wang84ce54e2018-10-18 13:56:03 -070027using ::android::hardware::hidl_vec;
28using ::android::hardware::Return;
29using ::android::hardware::Void;
30using ::android::hardware::thermal::V1_0::ThermalStatus;
31using ::android::hardware::thermal::V1_0::ThermalStatusCode;
32using ::android::hardware::thermal::V2_0::CoolingDevice;
33using ::android::hardware::thermal::V2_0::CoolingType;
34using ::android::hardware::thermal::V2_0::IThermal;
35using ::android::hardware::thermal::V2_0::IThermalChangedCallback;
36using ::android::hardware::thermal::V2_0::Temperature;
37using ::android::hardware::thermal::V2_0::TemperatureThreshold;
38using ::android::hardware::thermal::V2_0::TemperatureType;
39using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
Wei Wang84ce54e2018-10-18 13:56:03 -070040
41constexpr char kCallbackNameNotifyThrottling[] = "notifyThrottling";
42static const Temperature kThrottleTemp = {
43 .type = TemperatureType::SKIN,
44 .name = "test temperature sensor",
45 .value = 98.6,
46 .throttlingStatus = ThrottlingSeverity::CRITICAL,
47};
48
49class ThermalCallbackArgs {
50 public:
51 Temperature temperature;
52};
53
54// Callback class for receiving thermal event notifications from main class
55class ThermalCallback : public ::testing::VtsHalHidlTargetCallbackBase<ThermalCallbackArgs>,
56 public IThermalChangedCallback {
57 public:
58 Return<void> notifyThrottling(const Temperature& temperature) override {
59 ThermalCallbackArgs args;
60 args.temperature = temperature;
61 NotifyFromCallback(kCallbackNameNotifyThrottling, args);
62 return Void();
63 }
64};
65
66// Test environment for Thermal HIDL HAL.
67class ThermalHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
68 public:
69 // get the test environment singleton
70 static ThermalHidlEnvironment* Instance() {
71 static ThermalHidlEnvironment* instance = new ThermalHidlEnvironment;
72 return instance;
73 }
74
75 void registerTestServices() override { registerTestService<IThermal>(); }
76
77 private:
78 ThermalHidlEnvironment() {}
79};
80
81// The main test class for THERMAL HIDL HAL 2.0.
82class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase {
83 public:
84 virtual void SetUp() override {
85 mThermal = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>(
86 ThermalHidlEnvironment::Instance()->getServiceName<IThermal>());
87 ASSERT_NE(mThermal, nullptr);
88 mThermalCallback = new (std::nothrow) ThermalCallback();
89 ASSERT_NE(mThermalCallback, nullptr);
90 auto ret = mThermal->registerThermalChangedCallback(
91 mThermalCallback, false, TemperatureType::SKIN,
92 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
93 ASSERT_TRUE(ret.isOk());
94 // Expect to fail if register again
95 ret = mThermal->registerThermalChangedCallback(
96 mThermalCallback, false, TemperatureType::SKIN,
97 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
98 ASSERT_TRUE(ret.isOk());
99 }
100
101 virtual void TearDown() override {
102 auto ret = mThermal->unregisterThermalChangedCallback(
103 mThermalCallback,
104 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
105 ASSERT_TRUE(ret.isOk());
106 // Expect to fail if unregister again
107 ret = mThermal->unregisterThermalChangedCallback(
108 mThermalCallback,
109 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
110 ASSERT_TRUE(ret.isOk());
111 }
112
113 protected:
114 sp<IThermal> mThermal;
115 sp<ThermalCallback> mThermalCallback;
116}; // class ThermalHidlTest
117
118// Test ThermalChangedCallback::notifyThrottling().
119// This just calls into and back from our local ThermalChangedCallback impl.
120// Note: a real thermal throttling event from the Thermal HAL could be
121// inadvertently received here.
122TEST_F(ThermalHidlTest, NotifyThrottlingTest) {
123 auto ret = mThermalCallback->notifyThrottling(kThrottleTemp);
124 ASSERT_TRUE(ret.isOk());
125 auto res = mThermalCallback->WaitForCallback(kCallbackNameNotifyThrottling);
126 EXPECT_TRUE(res.no_timeout);
127 ASSERT_TRUE(res.args);
128 EXPECT_EQ(kThrottleTemp, res.args->temperature);
129}
130
131// Test Thermal->registerThermalChangedCallback.
132TEST_F(ThermalHidlTest, RegisterThermalChangedCallbackTest) {
133 // Expect to fail with same callback
134 auto ret = mThermal->registerThermalChangedCallback(
135 mThermalCallback, false, TemperatureType::SKIN,
136 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
137 ASSERT_TRUE(ret.isOk());
138 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
139 // Expect to succeed with different callback
140 ret = mThermal->registerThermalChangedCallback(
141 localThermalCallback, false, TemperatureType::SKIN,
142 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
143 ASSERT_TRUE(ret.isOk());
144 // Remove the local callback.
145 ret = mThermal->unregisterThermalChangedCallback(
146 localThermalCallback,
147 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
148 ASSERT_TRUE(ret.isOk());
149}
150
151// Test Thermal->unregisterThermalChangedCallback.
152TEST_F(ThermalHidlTest, UnregisterThermalChangedCallbackTest) {
153 sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback();
154 // Expect to fail as the callback was not registered before
155 auto ret = mThermal->unregisterThermalChangedCallback(
156 localThermalCallback,
157 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
158 ASSERT_TRUE(ret.isOk());
159 // Register a local callback
160 ret = mThermal->registerThermalChangedCallback(
161 localThermalCallback, false, TemperatureType::SKIN,
162 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
163 ASSERT_TRUE(ret.isOk());
164 // Expect to succeed with callback removed
165 ret = mThermal->unregisterThermalChangedCallback(
166 localThermalCallback,
167 [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); });
168 ASSERT_TRUE(ret.isOk());
169 // Expect to fail as the callback has been unregistered already
170 ret = mThermal->unregisterThermalChangedCallback(
171 localThermalCallback,
172 [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); });
173 ASSERT_TRUE(ret.isOk());
174}
175
176// Sanity test for Thermal::getCurrentTemperatures().
177TEST_F(ThermalHidlTest, TemperatureTest) {
178 mThermal->getCurrentTemperatures(false, TemperatureType::SKIN,
179 [](ThermalStatus status, hidl_vec<Temperature> temperatures) {
180 if (temperatures.size()) {
181 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
182 } else {
183 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
184 }
185 });
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);
197 }
198 });
199 }
200}
201
202// Sanity test for Thermal::getTemperatureThresholds().
203TEST_F(ThermalHidlTest, TemperatureThresholdTest) {
204 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().
231TEST_F(ThermalHidlTest, CoolingDeviceTest) {
232 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 }
239 });
240 for (int i = 0; i <= static_cast<int>(CoolingType::COMPONENT); ++i) {
241 auto type = static_cast<CoolingType>(i);
242 mThermal->getCurrentCoolingDevices(
243 true, type, [&type](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) {
244 if (cooling_devices.size()) {
245 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
246 } else {
247 EXPECT_NE(ThermalStatusCode::SUCCESS, status.code);
248 }
249 for (int i = 0; i < cooling_devices.size(); ++i) {
250 EXPECT_EQ(type, cooling_devices[i].type);
251 }
252 });
253 }
254}
255
256int main(int argc, char** argv) {
257 ::testing::AddGlobalTestEnvironment(ThermalHidlEnvironment::Instance());
258 ::testing::InitGoogleTest(&argc, argv);
259 ThermalHidlEnvironment::Instance()->init(&argc, argv);
260 int status = RUN_ALL_TESTS();
261 cout << "Test result = " << status << std::endl;
262 return status;
263}