1 /*
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
24 #include <android-base/logging.h>
25 #include <android/hardware/thermal/1.0/IThermal.h>
26 #include <android/hardware/thermal/1.0/types.h>
27 #include <VtsHalHidlTargetTestBase.h>
28 #include <unistd.h>
29
30 using ::android::hardware::hidl_string;
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::thermal::V1_0::CoolingDevice;
33 using ::android::hardware::thermal::V1_0::CpuUsage;
34 using ::android::hardware::thermal::V1_0::IThermal;
35 using ::android::hardware::thermal::V1_0::Temperature;
36 using ::android::hardware::thermal::V1_0::TemperatureType;
37 using ::android::hardware::thermal::V1_0::ThermalStatus;
38 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
39 using ::android::hardware::Return;
40 using ::android::hardware::Void;
41 using ::android::sp;
42
43 #define MONITORING_OPERATION_NUMBER 10
44
45 #define MAX_DEVICE_TEMPERATURE 200
46 #define MAX_FAN_SPEED 20000
47
48 // The main test class for THERMAL HIDL HAL.
49 class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase {
50 public:
SetUp()51 virtual void SetUp() override {
52 thermal_ = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>();
53 ASSERT_NE(thermal_, nullptr);
54 baseSize_ = 0;
55 names_.clear();
56 }
57
TearDown()58 virtual void TearDown() override {}
59
60 protected:
61 // Check validity of temperatures returned by Thremal HAL.
checkTemperatures(const hidl_vec<Temperature> temperatures)62 void checkTemperatures(const hidl_vec<Temperature> temperatures) {
63 size_t size = temperatures.size();
64 EXPECT_LE(baseSize_, size);
65
66 for (size_t i = 0; i < size; ++i) {
67 checkDeviceTemperature(temperatures[i]);
68 if (i < baseSize_) {
69 EXPECT_EQ(names_[i], temperatures[i].name.c_str());
70 } else {
71 // Names must be unique only for known temperature types.
72 if (temperatures[i].type != TemperatureType::UNKNOWN) {
73 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
74 temperatures[i].name.c_str()));
75 }
76 names_.push_back(temperatures[i].name);
77 }
78 }
79 baseSize_ = size;
80 }
81
82 // Check validity of CPU usages returned by Thermal HAL.
checkCpuUsages(const hidl_vec<CpuUsage> & cpuUsages)83 void checkCpuUsages(const hidl_vec<CpuUsage>& cpuUsages) {
84 size_t size = cpuUsages.size();
85 // A number of CPU's does not change.
86 if (baseSize_ != 0) EXPECT_EQ(baseSize_, size);
87
88 for (size_t i = 0; i < size; ++i) {
89 checkCpuUsage(cpuUsages[i]);
90 if (i < baseSize_) {
91 EXPECT_EQ(names_[i], cpuUsages[i].name.c_str());
92 } else {
93 // Names are not guaranteed to be unique because of the current
94 // default Thermal HAL implementation.
95 names_.push_back(cpuUsages[i].name);
96 }
97 }
98 baseSize_ = size;
99 }
100
101 // Check validity of cooling devices information returned by Thermal HAL.
checkCoolingDevices(const hidl_vec<CoolingDevice> coolingDevices)102 void checkCoolingDevices(const hidl_vec<CoolingDevice> coolingDevices) {
103 size_t size = coolingDevices.size();
104 EXPECT_LE(baseSize_, size);
105
106 for (size_t i = 0; i < size; ++i) {
107 checkCoolingDevice(coolingDevices[i]);
108 if (i < baseSize_) {
109 EXPECT_EQ(names_[i], coolingDevices[i].name.c_str());
110 } else {
111 // Names must be unique.
112 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
113 coolingDevices[i].name.c_str()));
114 names_.push_back(coolingDevices[i].name);
115 }
116 }
117 baseSize_ = size;
118 }
119
120 sp<IThermal> thermal_;
121
122 private:
123 // Check validity of temperature returned by Thermal HAL.
checkDeviceTemperature(const Temperature & temperature)124 void checkDeviceTemperature(const Temperature& temperature) {
125 // .currentValue of known type is in Celsius and must be reasonable.
126 EXPECT_TRUE(temperature.type == TemperatureType::UNKNOWN ||
127 std::abs(temperature.currentValue) < MAX_DEVICE_TEMPERATURE ||
128 isnan(temperature.currentValue));
129
130 // .name must not be empty.
131 EXPECT_LT(0u, temperature.name.size());
132
133 // .currentValue must not exceed .shutdwonThreshold if defined.
134 EXPECT_TRUE(temperature.currentValue < temperature.shutdownThreshold ||
135 isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold));
136
137 // .throttlingThreshold must not exceed .shutdownThreshold if defined.
138 EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold ||
139 isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold));
140 }
141
142 // Check validity of CPU usage returned by Thermal HAL.
checkCpuUsage(const CpuUsage & cpuUsage)143 void checkCpuUsage(const CpuUsage& cpuUsage) {
144 // .active must be less than .total if CPU is online.
145 EXPECT_TRUE(!cpuUsage.isOnline ||
146 (cpuUsage.active >= 0 && cpuUsage.total >= 0 &&
147 cpuUsage.total >= cpuUsage.active));
148
149 // .name must be not empty.
150 EXPECT_LT(0u, cpuUsage.name.size());
151 }
152
153 // Check validity of a cooling device information returned by Thermal HAL.
checkCoolingDevice(const CoolingDevice & coolingDevice)154 void checkCoolingDevice(const CoolingDevice& coolingDevice) {
155 EXPECT_LE(0, coolingDevice.currentValue);
156 EXPECT_GT(MAX_FAN_SPEED, coolingDevice.currentValue);
157 EXPECT_LT(0u, coolingDevice.name.size());
158 }
159
160 size_t baseSize_;
161 std::vector<hidl_string> names_;
162 };
163
164 // Sanity test for Thermal::getTemperatures().
TEST_F(ThermalHidlTest,TemperatureTest)165 TEST_F(ThermalHidlTest, TemperatureTest) {
166 hidl_vec<Temperature> passed;
167 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
168 thermal_->getTemperatures(
169 [&passed](ThermalStatus status, hidl_vec<Temperature> temperatures) {
170 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
171 passed = temperatures;
172 });
173
174 checkTemperatures(passed);
175 sleep(1);
176 }
177 }
178
179 // Sanity test for Thermal::getCpuUsages().
TEST_F(ThermalHidlTest,CpuUsageTest)180 TEST_F(ThermalHidlTest, CpuUsageTest) {
181 hidl_vec<CpuUsage> passed;
182 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
183 thermal_->getCpuUsages(
184 [&passed](ThermalStatus status, hidl_vec<CpuUsage> cpuUsages) {
185 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
186 passed = cpuUsages;
187 });
188
189 checkCpuUsages(passed);
190 sleep(1);
191 }
192 }
193
194 // Sanity test for Thermal::getCoolingDevices().
TEST_F(ThermalHidlTest,CoolingDeviceTest)195 TEST_F(ThermalHidlTest, CoolingDeviceTest) {
196 hidl_vec<CoolingDevice> passed;
197 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
198 thermal_->getCoolingDevices([&passed](
199 ThermalStatus status, hidl_vec<CoolingDevice> coolingDevices) {
200 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
201 passed = coolingDevices;
202 });
203
204 checkCoolingDevices(passed);
205 sleep(1);
206 }
207 }
208
main(int argc,char ** argv)209 int main(int argc, char** argv) {
210 ::testing::InitGoogleTest(&argc, argv);
211 int status = RUN_ALL_TESTS();
212 LOG(INFO) << "Test result = " << status;
213 return status;
214 }
215