Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <android/hardware/thermal/2.0/IThermal.h> |
| 18 | #include <android/hardware/thermal/2.0/IThermalChangedCallback.h> |
| 19 | #include <android/hardware/thermal/2.0/types.h> |
| 20 | |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <hidl/GtestPrinter.h> |
| 23 | #include <hidl/ServiceManagement.h> |
| 24 | |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 25 | #include <VtsHalHidlTargetCallbackBase.h> |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 26 | |
| 27 | using ::android::sp; |
Wei Wang | c4c2559 | 2018-10-25 19:22:25 -0700 | [diff] [blame] | 28 | using ::android::hardware::hidl_enum_range; |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 29 | using ::android::hardware::hidl_vec; |
| 30 | using ::android::hardware::Return; |
| 31 | using ::android::hardware::Void; |
| 32 | using ::android::hardware::thermal::V1_0::ThermalStatus; |
| 33 | using ::android::hardware::thermal::V1_0::ThermalStatusCode; |
| 34 | using ::android::hardware::thermal::V2_0::CoolingDevice; |
| 35 | using ::android::hardware::thermal::V2_0::CoolingType; |
| 36 | using ::android::hardware::thermal::V2_0::IThermal; |
| 37 | using ::android::hardware::thermal::V2_0::IThermalChangedCallback; |
| 38 | using ::android::hardware::thermal::V2_0::Temperature; |
| 39 | using ::android::hardware::thermal::V2_0::TemperatureThreshold; |
| 40 | using ::android::hardware::thermal::V2_0::TemperatureType; |
| 41 | using ::android::hardware::thermal::V2_0::ThrottlingSeverity; |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 42 | |
| 43 | constexpr char kCallbackNameNotifyThrottling[] = "notifyThrottling"; |
| 44 | static const Temperature kThrottleTemp = { |
| 45 | .type = TemperatureType::SKIN, |
| 46 | .name = "test temperature sensor", |
| 47 | .value = 98.6, |
| 48 | .throttlingStatus = ThrottlingSeverity::CRITICAL, |
| 49 | }; |
| 50 | |
| 51 | class ThermalCallbackArgs { |
| 52 | public: |
| 53 | Temperature temperature; |
| 54 | }; |
| 55 | |
| 56 | // Callback class for receiving thermal event notifications from main class |
| 57 | class ThermalCallback : public ::testing::VtsHalHidlTargetCallbackBase<ThermalCallbackArgs>, |
| 58 | public IThermalChangedCallback { |
| 59 | public: |
| 60 | Return<void> notifyThrottling(const Temperature& temperature) override { |
| 61 | ThermalCallbackArgs args; |
| 62 | args.temperature = temperature; |
| 63 | NotifyFromCallback(kCallbackNameNotifyThrottling, args); |
| 64 | return Void(); |
| 65 | } |
| 66 | }; |
| 67 | |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 68 | // The main test class for THERMAL HIDL HAL 2.0. |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 69 | class ThermalHidlTest : public testing::TestWithParam<std::string> { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 70 | public: |
| 71 | virtual void SetUp() override { |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 72 | mThermal = IThermal::getService(GetParam()); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 73 | ASSERT_NE(mThermal, nullptr); |
| 74 | mThermalCallback = new (std::nothrow) ThermalCallback(); |
| 75 | ASSERT_NE(mThermalCallback, nullptr); |
| 76 | auto ret = mThermal->registerThermalChangedCallback( |
| 77 | mThermalCallback, false, TemperatureType::SKIN, |
| 78 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 79 | ASSERT_TRUE(ret.isOk()); |
| 80 | // Expect to fail if register again |
| 81 | ret = mThermal->registerThermalChangedCallback( |
| 82 | mThermalCallback, false, TemperatureType::SKIN, |
| 83 | [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); }); |
| 84 | ASSERT_TRUE(ret.isOk()); |
| 85 | } |
| 86 | |
| 87 | virtual void TearDown() override { |
| 88 | auto ret = mThermal->unregisterThermalChangedCallback( |
| 89 | mThermalCallback, |
| 90 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 91 | ASSERT_TRUE(ret.isOk()); |
| 92 | // Expect to fail if unregister again |
| 93 | ret = mThermal->unregisterThermalChangedCallback( |
| 94 | mThermalCallback, |
| 95 | [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); }); |
| 96 | ASSERT_TRUE(ret.isOk()); |
| 97 | } |
| 98 | |
| 99 | protected: |
| 100 | sp<IThermal> mThermal; |
| 101 | sp<ThermalCallback> mThermalCallback; |
| 102 | }; // class ThermalHidlTest |
| 103 | |
| 104 | // Test ThermalChangedCallback::notifyThrottling(). |
| 105 | // This just calls into and back from our local ThermalChangedCallback impl. |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 106 | TEST_P(ThermalHidlTest, NotifyThrottlingTest) { |
Wei Wang | 3ec02dc | 2021-08-27 11:20:39 -0700 | [diff] [blame] | 107 | sp<ThermalCallback> thermalCallback = new (std::nothrow) ThermalCallback(); |
| 108 | auto ret = thermalCallback->notifyThrottling(kThrottleTemp); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(ret.isOk()); |
Wei Wang | 3ec02dc | 2021-08-27 11:20:39 -0700 | [diff] [blame] | 110 | auto res = thermalCallback->WaitForCallback(kCallbackNameNotifyThrottling); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 111 | EXPECT_TRUE(res.no_timeout); |
| 112 | ASSERT_TRUE(res.args); |
| 113 | EXPECT_EQ(kThrottleTemp, res.args->temperature); |
| 114 | } |
| 115 | |
| 116 | // Test Thermal->registerThermalChangedCallback. |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 117 | TEST_P(ThermalHidlTest, RegisterThermalChangedCallbackTest) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 118 | // Expect to fail with same callback |
| 119 | auto ret = mThermal->registerThermalChangedCallback( |
Wei Wang | 84cf5c9 | 2019-02-20 11:39:14 -0800 | [diff] [blame] | 120 | mThermalCallback, false, TemperatureType::SKIN, |
| 121 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); }); |
| 122 | ASSERT_TRUE(ret.isOk()); |
| 123 | // Expect to fail with null callback |
| 124 | ret = mThermal->registerThermalChangedCallback( |
| 125 | nullptr, false, TemperatureType::SKIN, |
| 126 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); }); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 127 | ASSERT_TRUE(ret.isOk()); |
| 128 | sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback(); |
| 129 | // Expect to succeed with different callback |
| 130 | ret = mThermal->registerThermalChangedCallback( |
| 131 | localThermalCallback, false, TemperatureType::SKIN, |
| 132 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 133 | ASSERT_TRUE(ret.isOk()); |
Wei Wang | 84cf5c9 | 2019-02-20 11:39:14 -0800 | [diff] [blame] | 134 | // Remove the local callback |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 135 | ret = mThermal->unregisterThermalChangedCallback( |
| 136 | localThermalCallback, |
| 137 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 138 | ASSERT_TRUE(ret.isOk()); |
Wei Wang | 84cf5c9 | 2019-02-20 11:39:14 -0800 | [diff] [blame] | 139 | // Expect to fail with null callback |
| 140 | ret = mThermal->unregisterThermalChangedCallback(nullptr, [](ThermalStatus status) { |
| 141 | EXPECT_EQ(ThermalStatusCode::FAILURE, status.code); |
| 142 | }); |
| 143 | ASSERT_TRUE(ret.isOk()); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Test Thermal->unregisterThermalChangedCallback. |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 147 | TEST_P(ThermalHidlTest, UnregisterThermalChangedCallbackTest) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 148 | sp<ThermalCallback> localThermalCallback = new (std::nothrow) ThermalCallback(); |
| 149 | // Expect to fail as the callback was not registered before |
| 150 | auto ret = mThermal->unregisterThermalChangedCallback( |
| 151 | localThermalCallback, |
| 152 | [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); }); |
| 153 | ASSERT_TRUE(ret.isOk()); |
| 154 | // Register a local callback |
| 155 | ret = mThermal->registerThermalChangedCallback( |
| 156 | localThermalCallback, false, TemperatureType::SKIN, |
| 157 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 158 | ASSERT_TRUE(ret.isOk()); |
| 159 | // Expect to succeed with callback removed |
| 160 | ret = mThermal->unregisterThermalChangedCallback( |
| 161 | localThermalCallback, |
| 162 | [](ThermalStatus status) { EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); }); |
| 163 | ASSERT_TRUE(ret.isOk()); |
| 164 | // Expect to fail as the callback has been unregistered already |
| 165 | ret = mThermal->unregisterThermalChangedCallback( |
| 166 | localThermalCallback, |
| 167 | [](ThermalStatus status) { EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); }); |
| 168 | ASSERT_TRUE(ret.isOk()); |
| 169 | } |
| 170 | |
| 171 | // Sanity test for Thermal::getCurrentTemperatures(). |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 172 | TEST_P(ThermalHidlTest, TemperatureTest) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 173 | mThermal->getCurrentTemperatures(false, TemperatureType::SKIN, |
| 174 | [](ThermalStatus status, hidl_vec<Temperature> temperatures) { |
| 175 | if (temperatures.size()) { |
| 176 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 177 | } else { |
| 178 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 179 | } |
Wei Wang | dd62969 | 2019-04-05 10:54:20 -0700 | [diff] [blame] | 180 | for (int i = 0; i < temperatures.size(); ++i) { |
| 181 | EXPECT_LT(0u, temperatures[i].name.size()); |
| 182 | } |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 183 | }); |
Wei Wang | c4c2559 | 2018-10-25 19:22:25 -0700 | [diff] [blame] | 184 | auto types = hidl_enum_range<TemperatureType>(); |
| 185 | for (const auto& type : types) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 186 | mThermal->getCurrentTemperatures( |
| 187 | true, type, [&type](ThermalStatus status, hidl_vec<Temperature> temperatures) { |
| 188 | if (temperatures.size()) { |
| 189 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 190 | } else { |
| 191 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 192 | } |
| 193 | for (int i = 0; i < temperatures.size(); ++i) { |
| 194 | EXPECT_EQ(type, temperatures[i].type); |
Wei Wang | dd62969 | 2019-04-05 10:54:20 -0700 | [diff] [blame] | 195 | EXPECT_LT(0u, temperatures[i].name.size()); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 196 | } |
| 197 | }); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Sanity test for Thermal::getTemperatureThresholds(). |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 202 | TEST_P(ThermalHidlTest, TemperatureThresholdTest) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 203 | mThermal->getTemperatureThresholds( |
| 204 | false, TemperatureType::SKIN, |
| 205 | [](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) { |
| 206 | if (temperatures.size()) { |
| 207 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 208 | } else { |
| 209 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 210 | } |
| 211 | }); |
| 212 | for (int i = static_cast<int>(TemperatureType::UNKNOWN); |
| 213 | i <= static_cast<int>(TemperatureType::POWER_AMPLIFIER); ++i) { |
| 214 | auto type = static_cast<TemperatureType>(i); |
| 215 | mThermal->getTemperatureThresholds( |
| 216 | true, type, [&type](ThermalStatus status, hidl_vec<TemperatureThreshold> temperatures) { |
| 217 | if (temperatures.size()) { |
| 218 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 219 | } else { |
| 220 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 221 | } |
| 222 | for (int i = 0; i < temperatures.size(); ++i) { |
| 223 | EXPECT_EQ(type, temperatures[i].type); |
| 224 | } |
| 225 | }); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Sanity test for Thermal::getCurrentCoolingDevices(). |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 230 | TEST_P(ThermalHidlTest, CoolingDeviceTest) { |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 231 | mThermal->getCurrentCoolingDevices( |
| 232 | false, CoolingType::CPU, [](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) { |
| 233 | if (cooling_devices.size()) { |
| 234 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 235 | } else { |
| 236 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 237 | } |
Wei Wang | dd62969 | 2019-04-05 10:54:20 -0700 | [diff] [blame] | 238 | for (int i = 0; i < cooling_devices.size(); ++i) { |
| 239 | EXPECT_LT(0u, cooling_devices[i].name.size()); |
| 240 | } |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 241 | }); |
| 242 | for (int i = 0; i <= static_cast<int>(CoolingType::COMPONENT); ++i) { |
| 243 | auto type = static_cast<CoolingType>(i); |
| 244 | mThermal->getCurrentCoolingDevices( |
| 245 | true, type, [&type](ThermalStatus status, hidl_vec<CoolingDevice> cooling_devices) { |
| 246 | if (cooling_devices.size()) { |
| 247 | EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code); |
| 248 | } else { |
| 249 | EXPECT_NE(ThermalStatusCode::SUCCESS, status.code); |
| 250 | } |
| 251 | for (int i = 0; i < cooling_devices.size(); ++i) { |
| 252 | EXPECT_EQ(type, cooling_devices[i].type); |
Wei Wang | dd62969 | 2019-04-05 10:54:20 -0700 | [diff] [blame] | 253 | EXPECT_LT(0u, cooling_devices[i].name.size()); |
Wei Wang | 84ce54e | 2018-10-18 13:56:03 -0700 | [diff] [blame] | 254 | } |
| 255 | }); |
| 256 | } |
| 257 | } |
| 258 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 259 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalHidlTest); |
Dan Shi | 030bef3 | 2019-10-16 14:45:16 -0700 | [diff] [blame] | 260 | INSTANTIATE_TEST_SUITE_P( |
| 261 | PerInstance, ThermalHidlTest, |
| 262 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IThermal::descriptor)), |
| 263 | android::hardware::PrintInstanceNameToString); |