Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 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> |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 27 | #include <VtsHalHidlTargetTestBase.h> |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 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 | |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 43 | #define MONITORING_OPERATION_NUMBER 10 |
| 44 | |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 45 | #define MAX_DEVICE_TEMPERATURE 200 |
| 46 | #define MAX_FAN_SPEED 20000 |
| 47 | |
| 48 | // The main test class for THERMAL HIDL HAL. |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 49 | class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 50 | public: |
| 51 | virtual void SetUp() override { |
Yuexi Ma | ed2bb4e | 2017-03-10 00:44:45 -0800 | [diff] [blame] | 52 | thermal_ = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>(); |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 53 | 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 Bondarenko | 93a2d68 | 2017-01-19 15:55:19 +0100 | [diff] [blame] | 127 | isnan(temperature.currentValue)); |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 128 | |
| 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 Bondarenko | 93a2d68 | 2017-01-19 15:55:19 +0100 | [diff] [blame] | 134 | isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold)); |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 135 | |
| 136 | // .throttlingThreshold must not exceed .shutdownThreshold if defined. |
Polina Bondarenko | 93a2d68 | 2017-01-19 15:55:19 +0100 | [diff] [blame] | 137 | EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold || |
| 138 | isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold)); |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 139 | } |
| 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(). |
| 164 | TEST_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(). |
| 179 | TEST_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(). |
| 194 | TEST_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 | |
| 208 | int main(int argc, char** argv) { |
| 209 | ::testing::InitGoogleTest(&argc, argv); |
| 210 | int status = RUN_ALL_TESTS(); |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 211 | LOG(INFO) << "Test result = " << status; |
Polina Bondarenko | 59d05eb | 2016-11-03 16:41:18 +0100 | [diff] [blame] | 212 | return status; |
| 213 | } |