blob: dfa11a1dc177c2e79910d3d0c11e2bb879615334 [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
24#include <android-base/logging.h>
25#include <android/hardware/thermal/1.0/IThermal.h>
26#include <android/hardware/thermal/1.0/types.h>
Yuexi Maed2bb4e2017-03-10 00:44:45 -080027#include <VtsHalHidlTargetTestBase.h>
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010028#include <unistd.h>
29
30using ::android::hardware::hidl_string;
31using ::android::hardware::hidl_vec;
32using ::android::hardware::thermal::V1_0::CoolingDevice;
33using ::android::hardware::thermal::V1_0::CpuUsage;
34using ::android::hardware::thermal::V1_0::IThermal;
35using ::android::hardware::thermal::V1_0::Temperature;
36using ::android::hardware::thermal::V1_0::TemperatureType;
37using ::android::hardware::thermal::V1_0::ThermalStatus;
38using ::android::hardware::thermal::V1_0::ThermalStatusCode;
39using ::android::hardware::Return;
40using ::android::hardware::Void;
41using ::android::sp;
42
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010043#define MONITORING_OPERATION_NUMBER 10
44
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010045#define MAX_DEVICE_TEMPERATURE 200
46#define MAX_FAN_SPEED 20000
47
48// The main test class for THERMAL HIDL HAL.
Yuexi Maed2bb4e2017-03-10 00:44:45 -080049class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase {
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010050 public:
51 virtual void SetUp() override {
Yuexi Maed2bb4e2017-03-10 00:44:45 -080052 thermal_ = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>();
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010053 ASSERT_NE(thermal_, nullptr);
54 baseSize_ = 0;
55 names_.clear();
56 }
57
58 virtual void TearDown() override {}
59
60 protected:
61 // Check validity of temperatures returned by Thremal HAL.
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 {
Polina Bondarenko2ff86c42017-04-05 16:51:23 +020071 // 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);
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010077 }
78 }
79 baseSize_ = size;
80 }
81
82 // Check validity of CPU usages returned by Thermal HAL.
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 {
Polina Bondarenko2ff86c42017-04-05 16:51:23 +020093 // Names are not guaranteed to be unique because of the current
94 // default Thermal HAL implementation.
95 names_.push_back(cpuUsages[i].name);
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010096 }
97 }
98 baseSize_ = size;
99 }
100
101 // Check validity of cooling devices information returned by Thermal HAL.
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.
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 ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100128 isnan(temperature.currentValue));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100129
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 ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100135 isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100136
137 // .throttlingThreshold must not exceed .shutdownThreshold if defined.
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100138 EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold ||
139 isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100140 }
141
142 // Check validity of CPU usage returned by Thermal HAL.
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.
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().
165TEST_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().
180TEST_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().
195TEST_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
209int main(int argc, char** argv) {
210 ::testing::InitGoogleTest(&argc, argv);
211 int status = RUN_ALL_TESTS();
Yifan Hongf9d30342016-11-30 13:45:34 -0800212 LOG(INFO) << "Test result = " << status;
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100213 return status;
214}