Home | History | Annotate | Download | only in functional
      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 <VtsHalHidlTargetTestBase.h>
     25 #include <VtsHalHidlTargetTestEnvBase.h>
     26 #include <android-base/logging.h>
     27 #include <android/hardware/thermal/1.0/IThermal.h>
     28 #include <android/hardware/thermal/1.0/types.h>
     29 #include <unistd.h>
     30 
     31 using ::android::hardware::hidl_string;
     32 using ::android::hardware::hidl_vec;
     33 using ::android::hardware::thermal::V1_0::CoolingDevice;
     34 using ::android::hardware::thermal::V1_0::CpuUsage;
     35 using ::android::hardware::thermal::V1_0::IThermal;
     36 using ::android::hardware::thermal::V1_0::Temperature;
     37 using ::android::hardware::thermal::V1_0::TemperatureType;
     38 using ::android::hardware::thermal::V1_0::ThermalStatus;
     39 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
     40 using ::android::hardware::Return;
     41 using ::android::hardware::Void;
     42 using ::android::sp;
     43 
     44 #define MONITORING_OPERATION_NUMBER 10
     45 
     46 #define MAX_DEVICE_TEMPERATURE 200
     47 #define MAX_FAN_SPEED 20000
     48 
     49 // Test environment for Thermal HIDL HAL.
     50 class 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 
     63 // The main test class for THERMAL HIDL HAL.
     64 class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     65  public:
     66   virtual void SetUp() override {
     67     thermal_ = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>(
     68         ThermalHidlEnvironment::Instance()->getServiceName<IThermal>());
     69     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 {
     87           // 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);
     93       }
     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 {
    109           // Names are not guaranteed to be unique because of the current
    110           // default Thermal HAL implementation.
    111           names_.push_back(cpuUsages[i].name);
    112       }
    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 ||
    144                 isnan(temperature.currentValue));
    145 
    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 ||
    151                 isnan(temperature.currentValue) || isnan(temperature.shutdownThreshold));
    152 
    153     // .throttlingThreshold must not exceed .shutdownThreshold if defined.
    154     EXPECT_TRUE(temperature.throttlingThreshold < temperature.shutdownThreshold ||
    155                 isnan(temperature.throttlingThreshold) || isnan(temperature.shutdownThreshold));
    156   }
    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().
    181 TEST_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().
    196 TEST_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().
    211 TEST_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 
    225 int main(int argc, char** argv) {
    226   ::testing::AddGlobalTestEnvironment(ThermalHidlEnvironment::Instance());
    227   ::testing::InitGoogleTest(&argc, argv);
    228   ThermalHidlEnvironment::Instance()->init(&argc, argv);
    229   int status = RUN_ALL_TESTS();
    230   LOG(INFO) << "Test result = " << status;
    231   return status;
    232 }
    233