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