blob: 6feec73b67f76cb45542b17368707edbd313bdad [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 Ma50d7e272017-02-28 01:46:51 -080027#include <VtsHalHidlTargetBaseTest.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 Ma50d7e272017-02-28 01:46:51 -080049class ThermalHidlTest : public ::testing::VtsHalHidlTargetBaseTest {
Polina Bondarenko59d05eb2016-11-03 16:41:18 +010050 public:
51 virtual void SetUp() override {
Yuexi Ma50d7e272017-02-28 01:46:51 -080052 thermal_ = ::testing::VtsHalHidlTargetBaseTest::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 {
71 // Names must be unique.
72 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
73 temperatures[i].name.c_str()));
74 names_.push_back(temperatures[i].name);
75 }
76 }
77 baseSize_ = size;
78 }
79
80 // Check validity of CPU usages returned by Thermal HAL.
81 void checkCpuUsages(const hidl_vec<CpuUsage>& cpuUsages) {
82 size_t size = cpuUsages.size();
83 // A number of CPU's does not change.
84 if (baseSize_ != 0) EXPECT_EQ(baseSize_, size);
85
86 for (size_t i = 0; i < size; ++i) {
87 checkCpuUsage(cpuUsages[i]);
88 if (i < baseSize_) {
89 EXPECT_EQ(names_[i], cpuUsages[i].name.c_str());
90 } else {
91 // Names must be unique.
92 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
93 cpuUsages[i].name.c_str()));
94 names_.push_back(cpuUsages[i].name);
95 }
96 }
97 baseSize_ = size;
98 }
99
100 // Check validity of cooling devices information returned by Thermal HAL.
101 void checkCoolingDevices(const hidl_vec<CoolingDevice> coolingDevices) {
102 size_t size = coolingDevices.size();
103 EXPECT_LE(baseSize_, size);
104
105 for (size_t i = 0; i < size; ++i) {
106 checkCoolingDevice(coolingDevices[i]);
107 if (i < baseSize_) {
108 EXPECT_EQ(names_[i], coolingDevices[i].name.c_str());
109 } else {
110 // Names must be unique.
111 EXPECT_EQ(names_.end(), std::find(names_.begin(), names_.end(),
112 coolingDevices[i].name.c_str()));
113 names_.push_back(coolingDevices[i].name);
114 }
115 }
116 baseSize_ = size;
117 }
118
119 sp<IThermal> thermal_;
120
121 private:
122 // Check validity of temperature returned by Thermal HAL.
123 void checkDeviceTemperature(const Temperature& temperature) {
124 // .currentValue of known type is in Celsius and must be reasonable.
125 EXPECT_TRUE(temperature.type == TemperatureType::UNKNOWN ||
126 std::abs(temperature.currentValue) < MAX_DEVICE_TEMPERATURE ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100127 isnan(temperature.currentValue));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100128
129 // .name must not be empty.
130 EXPECT_LT(0u, temperature.name.size());
131
132 // .currentValue must not exceed .shutdwonThreshold if defined.
133 EXPECT_TRUE(temperature.currentValue < temperature.shutdownThreshold ||
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100134 isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100135
136 // .throttlingThreshold must not exceed .shutdownThreshold if defined.
Polina Bondarenko93a2d682017-01-19 15:55:19 +0100137 EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold ||
138 isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold));
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100139 }
140
141 // Check validity of CPU usage returned by Thermal HAL.
142 void checkCpuUsage(const CpuUsage& cpuUsage) {
143 // .active must be less than .total if CPU is online.
144 EXPECT_TRUE(!cpuUsage.isOnline ||
145 (cpuUsage.active >= 0 && cpuUsage.total >= 0 &&
146 cpuUsage.total >= cpuUsage.active));
147
148 // .name must be not empty.
149 EXPECT_LT(0u, cpuUsage.name.size());
150 }
151
152 // Check validity of a cooling device information returned by Thermal HAL.
153 void checkCoolingDevice(const CoolingDevice& coolingDevice) {
154 EXPECT_LE(0, coolingDevice.currentValue);
155 EXPECT_GT(MAX_FAN_SPEED, coolingDevice.currentValue);
156 EXPECT_LT(0u, coolingDevice.name.size());
157 }
158
159 size_t baseSize_;
160 std::vector<hidl_string> names_;
161};
162
163// Sanity test for Thermal::getTemperatures().
164TEST_F(ThermalHidlTest, TemperatureTest) {
165 hidl_vec<Temperature> passed;
166 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
167 thermal_->getTemperatures(
168 [&passed](ThermalStatus status, hidl_vec<Temperature> temperatures) {
169 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
170 passed = temperatures;
171 });
172
173 checkTemperatures(passed);
174 sleep(1);
175 }
176}
177
178// Sanity test for Thermal::getCpuUsages().
179TEST_F(ThermalHidlTest, CpuUsageTest) {
180 hidl_vec<CpuUsage> passed;
181 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
182 thermal_->getCpuUsages(
183 [&passed](ThermalStatus status, hidl_vec<CpuUsage> cpuUsages) {
184 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
185 passed = cpuUsages;
186 });
187
188 checkCpuUsages(passed);
189 sleep(1);
190 }
191}
192
193// Sanity test for Thermal::getCoolingDevices().
194TEST_F(ThermalHidlTest, CoolingDeviceTest) {
195 hidl_vec<CoolingDevice> passed;
196 for (size_t i = 0; i < MONITORING_OPERATION_NUMBER; ++i) {
197 thermal_->getCoolingDevices([&passed](
198 ThermalStatus status, hidl_vec<CoolingDevice> coolingDevices) {
199 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
200 passed = coolingDevices;
201 });
202
203 checkCoolingDevices(passed);
204 sleep(1);
205 }
206}
207
208int main(int argc, char** argv) {
209 ::testing::InitGoogleTest(&argc, argv);
210 int status = RUN_ALL_TESTS();
Yifan Hongf9d30342016-11-30 13:45:34 -0800211 LOG(INFO) << "Test result = " << status;
Polina Bondarenko59d05eb2016-11-03 16:41:18 +0100212 return status;
213}