blob: 6f059ef5bbfac6ae50b6997974d09ea8b8ff25b8 [file] [log] [blame]
Polina Bondarenko59d05eb2016-11-03 16:41:18 +01001/*
2 * Copyright (C) 2016 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 <algorithm>
18#include <cmath>
19#include <string>
20#include <vector>
21
22#define LOG_TAG "thermal_hidl_hal_test"
23
Zhuoyao Zhange01b1872018-02-08 21:10:21 -080024#include <VtsHalHidlTargetTestBase.h>
25#include <VtsHalHidlTargetTestEnvBase.h>
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010026#include <android-base/logging.h>
27#include <android/hardware/thermal/1.0/IThermal.h>
28#include <android/hardware/thermal/1.0/types.h>
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010029#include <unistd.h>
30
31using ::android::hardware::hidl_string;
32using ::android::hardware::hidl_vec;
33using ::android::hardware::thermal::V1_0::CoolingDevice;
34using ::android::hardware::thermal::V1_0::CpuUsage;
35using ::android::hardware::thermal::V1_0::IThermal;
36using ::android::hardware::thermal::V1_0::Temperature;
37using ::android::hardware::thermal::V1_0::TemperatureType;
38using ::android::hardware::thermal::V1_0::ThermalStatus;
39using ::android::hardware::thermal::V1_0::ThermalStatusCode;
40using ::android::hardware::Return;
41using ::android::hardware::Void;
42using ::android::sp;
43
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010044#define MONITORING_OPERATION_NUMBER 10
45
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010046#define MAX_DEVICE_TEMPERATURE 200
47#define MAX_FAN_SPEED 20000
48
Zhuoyao Zhange01b1872018-02-08 21:10:21 -080049// Test environment for Thermal HIDL HAL.
50class ThermalHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
51 public:
52 // get the test environment singleton
53 static ThermalHidlEnvironment* Instance() {
54 static ThermalHidlEnvironment* instance = new ThermalHidlEnvironment;
55 return instance;
56 }
57
58 virtual void registerTestServices() override { registerTestService<IThermal>(); }
59 private:
60 ThermalHidlEnvironment() {}
61};
62
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010063// The main test class for THERMAL HIDL HAL.
Yuexi Maed2bb4e2017-03-10 00:44:45 -080064class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase {
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010065 public:
66 virtual void SetUp() override {
Zhuoyao Zhange01b1872018-02-08 21:10:21 -080067 thermal_ = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>(
68 ThermalHidlEnvironment::Instance()->getServiceName<IThermal>());
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010069 ASSERT_NE(thermal_, nullptr);
70 baseSize_ = 0;
71 names_.clear();
72 }
73
74 virtual void TearDown() override {}
75
76 protected:
77 // Check validity of temperatures returned by Thremal HAL.
78 void checkTemperatures(const hidl_vec<Temperature> temperatures) {
79 size_t size = temperatures.size();
80 EXPECT_LE(baseSize_, size);
81
82 for (size_t i = 0; i < size; ++i) {
83 checkDeviceTemperature(temperatures[i]);
84 if (i < baseSize_) {
85 EXPECT_EQ(names_[i], temperatures[i].name.c_str());
86 } else {
Polina Bondarenko2ff86c42017-04-05 16:51:23 +020087 // Names must be unique only for known temperature types.
88 if (temperatures[i].type != TemperatureType::UNKNOWN) {
89 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
90 temperatures[i].name.c_str()));
91 }
92 names_.push_back(temperatures[i].name);
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010093 }
94 }
95 baseSize_ = size;
96 }
97
98 // Check validity of CPU usages returned by Thermal HAL.
99 void checkCpuUsages(const hidl_vec<CpuUsage>& cpuUsages) {
100 size_t size = cpuUsages.size();
101 // A number of CPU's does not change.
102 if (baseSize_ != 0) EXPECT_EQ(baseSize_, size);
103
104 for (size_t i = 0; i < size; ++i) {
105 checkCpuUsage(cpuUsages[i]);
106 if (i < baseSize_) {
107 EXPECT_EQ(names_[i], cpuUsages[i].name.c_str());
108 } else {
Polina Bondarenko2ff86c42017-04-05 16:51:23 +0200109 // Names are not guaranteed to be unique because of the current
110 // default Thermal HAL implementation.
111 names_.push_back(cpuUsages[i].name);
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100112 }
113 }
114 baseSize_ = size;
115 }
116
117 // Check validity of cooling devices information returned by Thermal HAL.
118 void checkCoolingDevices(const hidl_vec<CoolingDevice> coolingDevices) {
119 size_t size = coolingDevices.size();
120 EXPECT_LE(baseSize_, size);
121
122 for (size_t i = 0; i < size; ++i) {
123 checkCoolingDevice(coolingDevices[i]);
124 if (i < baseSize_) {
125 EXPECT_EQ(names_[i], coolingDevices[i].name.c_str());
126 } else {
127 // Names must be unique.
128 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
129 coolingDevices[i].name.c_str()));
130 names_.push_back(coolingDevices[i].name);
131 }
132 }
133 baseSize_ = size;
134 }
135
136 sp<IThermal> thermal_;
137
138 private:
139 // Check validity of temperature returned by Thermal HAL.
140 void checkDeviceTemperature(const Temperature& temperature) {
141 // .currentValue of known type is in Celsius and must be reasonable.
142 EXPECT_TRUE(temperature.type == TemperatureType::UNKNOWN ||
143 std::abs(temperature.currentValue) < MAX_DEVICE_TEMPERATURE ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100144 isnan(temperature.currentValue));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100145
146 // .name must not be empty.
147 EXPECT_LT(0u, temperature.name.size());
148
149 // .currentValue must not exceed .shutdwonThreshold if defined.
150 EXPECT_TRUE(temperature.currentValue < temperature.shutdownThreshold ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100151 isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100152
153 // .throttlingThreshold must not exceed .shutdownThreshold if defined.
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100154 EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold ||
155 isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100156 }
157
158 // Check validity of CPU usage returned by Thermal HAL.
159 void checkCpuUsage(const CpuUsage& cpuUsage) {
160 // .active must be less than .total if CPU is online.
161 EXPECT_TRUE(!cpuUsage.isOnline ||
162 (cpuUsage.active >= 0 && cpuUsage.total >= 0 &&
163 cpuUsage.total >= cpuUsage.active));
164
165 // .name must be not empty.
166 EXPECT_LT(0u, cpuUsage.name.size());
167 }
168
169 // Check validity of a cooling device information returned by Thermal HAL.
170 void checkCoolingDevice(const CoolingDevice& coolingDevice) {
171 EXPECT_LE(0, coolingDevice.currentValue);
172 EXPECT_GT(MAX_FAN_SPEED, coolingDevice.currentValue);
173 EXPECT_LT(0u, coolingDevice.name.size());
174 }
175
176 size_t baseSize_;
177 std::vector<hidl_string> names_;
178};
179
180// Sanity test for Thermal::getTemperatures().
181TEST_F(ThermalHidlTest, TemperatureTest) {
182 hidl_vec<Temperature> passed;
183 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
184 thermal_->getTemperatures(
185 [&passed](ThermalStatus status, hidl_vec<Temperature> temperatures) {
186 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
187 passed = temperatures;
188 });
189
190 checkTemperatures(passed);
191 sleep(1);
192 }
193}
194
195// Sanity test for Thermal::getCpuUsages().
196TEST_F(ThermalHidlTest, CpuUsageTest) {
197 hidl_vec<CpuUsage> passed;
198 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
199 thermal_->getCpuUsages(
200 [&passed](ThermalStatus status, hidl_vec<CpuUsage> cpuUsages) {
201 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
202 passed = cpuUsages;
203 });
204
205 checkCpuUsages(passed);
206 sleep(1);
207 }
208}
209
210// Sanity test for Thermal::getCoolingDevices().
211TEST_F(ThermalHidlTest, CoolingDeviceTest) {
212 hidl_vec<CoolingDevice> passed;
213 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
214 thermal_->getCoolingDevices([&passed](
215 ThermalStatus status, hidl_vec<CoolingDevice> coolingDevices) {
216 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
217 passed = coolingDevices;
218 });
219
220 checkCoolingDevices(passed);
221 sleep(1);
222 }
223}
224
225int main(int argc, char** argv) {
Zhuoyao Zhange01b1872018-02-08 21:10:21 -0800226 ::testing::AddGlobalTestEnvironment(ThermalHidlEnvironment::Instance());
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100227 ::testing::InitGoogleTest(&argc, argv);
Zhuoyao Zhange01b1872018-02-08 21:10:21 -0800228 ThermalHidlEnvironment::Instance()->init(&argc, argv);
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100229 int status = RUN_ALL_TESTS();
Yifan Hongf9d30342016-11-30 13:45:34 -0800230 LOG(INFO) << "Test result = " << status;
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100231 return status;
232}