Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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/1.1/IThermal.h> |
| 18 | #include <android/hardware/thermal/1.1/IThermalCallback.h> |
| 19 | #include <android/hardware/thermal/1.0/types.h> |
| 20 | |
| 21 | #include <VtsHalHidlTargetCallbackBase.h> |
| 22 | #include <VtsHalHidlTargetTestBase.h> |
Zhuoyao Zhang | e01b187 | 2018-02-08 21:10:21 -0800 | [diff] [blame^] | 23 | #include <VtsHalHidlTargetTestEnvBase.h> |
Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 24 | |
| 25 | using ::android::hardware::thermal::V1_0::Temperature; |
| 26 | using ::android::hardware::thermal::V1_0::TemperatureType; |
| 27 | using ::android::hardware::thermal::V1_1::IThermal; |
| 28 | using ::android::hardware::thermal::V1_1::IThermalCallback; |
| 29 | using ::android::hardware::Return; |
| 30 | using ::android::hardware::Void; |
| 31 | using ::android::sp; |
| 32 | |
| 33 | constexpr char kCallbackNameNotifyThrottling[] = "notifyThrottling"; |
| 34 | static const Temperature kThrottleTemp = { |
| 35 | .type = TemperatureType::CPU, |
| 36 | .name = "test temperature sensor", |
| 37 | .currentValue = 98.6, |
| 38 | .throttlingThreshold = 58, |
| 39 | .shutdownThreshold = 60, |
| 40 | .vrThrottlingThreshold = 59, |
| 41 | }; |
| 42 | |
| 43 | class ThermalCallbackArgs { |
| 44 | public: |
| 45 | bool isThrottling; |
| 46 | Temperature temperature; |
| 47 | }; |
| 48 | |
| 49 | // Callback class for receiving thermal event notifications from main class |
| 50 | class ThermalCallback |
| 51 | : public ::testing::VtsHalHidlTargetCallbackBase<ThermalCallbackArgs>, |
| 52 | public IThermalCallback { |
| 53 | public: |
| 54 | virtual ~ThermalCallback() = default; |
| 55 | |
| 56 | Return<void> notifyThrottling(bool isThrottling, |
| 57 | const Temperature& temperature) override { |
| 58 | ThermalCallbackArgs args; |
| 59 | args.isThrottling = isThrottling; |
| 60 | args.temperature = temperature; |
| 61 | NotifyFromCallback(kCallbackNameNotifyThrottling, args); |
| 62 | return Void(); |
| 63 | } |
| 64 | }; |
| 65 | |
Zhuoyao Zhang | e01b187 | 2018-02-08 21:10:21 -0800 | [diff] [blame^] | 66 | // Test environment for Thermal HIDL HAL. |
| 67 | class ThermalHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { |
| 68 | public: |
| 69 | // get the test environment singleton |
| 70 | static ThermalHidlEnvironment* Instance() { |
| 71 | static ThermalHidlEnvironment* instance = new ThermalHidlEnvironment; |
| 72 | return instance; |
| 73 | } |
| 74 | |
| 75 | virtual void registerTestServices() override { registerTestService<IThermal>(); } |
| 76 | private: |
| 77 | ThermalHidlEnvironment() {} |
| 78 | }; |
| 79 | |
Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 80 | // The main test class for THERMAL HIDL HAL 1.1. |
| 81 | class ThermalHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 82 | public: |
| 83 | virtual void SetUp() override { |
Zhuoyao Zhang | e01b187 | 2018-02-08 21:10:21 -0800 | [diff] [blame^] | 84 | mThermal = ::testing::VtsHalHidlTargetTestBase::getService<IThermal>( |
| 85 | ThermalHidlEnvironment::Instance()->getServiceName<IThermal>()); |
Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 86 | ASSERT_NE(mThermal, nullptr); |
| 87 | mThermalCallback = new(std::nothrow) ThermalCallback(); |
| 88 | ASSERT_NE(mThermalCallback, nullptr); |
| 89 | auto ret = mThermal->registerThermalCallback(mThermalCallback); |
| 90 | ASSERT_TRUE(ret.isOk()); |
| 91 | } |
| 92 | |
| 93 | virtual void TearDown() override { |
| 94 | auto ret = mThermal->registerThermalCallback(nullptr); |
| 95 | ASSERT_TRUE(ret.isOk()); |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | sp<IThermal> mThermal; |
| 100 | sp<ThermalCallback> mThermalCallback; |
| 101 | }; // class ThermalHidlTest |
| 102 | |
| 103 | // Test ThermalCallback::notifyThrottling(). |
| 104 | // This just calls into and back from our local ThermalCallback impl. |
| 105 | // Note: a real thermal throttling event from the Thermal HAL could be |
| 106 | // inadvertently received here. |
| 107 | TEST_F(ThermalHidlTest, NotifyThrottlingTest) { |
| 108 | auto ret = mThermalCallback->notifyThrottling(true, kThrottleTemp); |
| 109 | ASSERT_TRUE(ret.isOk()); |
| 110 | auto res = mThermalCallback->WaitForCallback(kCallbackNameNotifyThrottling); |
| 111 | EXPECT_TRUE(res.no_timeout); |
| 112 | ASSERT_TRUE(res.args); |
| 113 | EXPECT_EQ(true, res.args->isThrottling); |
| 114 | EXPECT_EQ(kThrottleTemp, res.args->temperature); |
| 115 | } |
| 116 | |
| 117 | int main(int argc, char** argv) { |
Zhuoyao Zhang | e01b187 | 2018-02-08 21:10:21 -0800 | [diff] [blame^] | 118 | ::testing::AddGlobalTestEnvironment(ThermalHidlEnvironment::Instance()); |
Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 119 | ::testing::InitGoogleTest(&argc, argv); |
Zhuoyao Zhang | e01b187 | 2018-02-08 21:10:21 -0800 | [diff] [blame^] | 120 | ThermalHidlEnvironment::Instance()->init(&argc, argv); |
Todd Poynor | 8d95f2e | 2017-05-23 20:28:15 -0700 | [diff] [blame] | 121 | int status = RUN_ALL_TESTS(); |
| 122 | cout << "Test result = " << status << std::endl; |
| 123 | return status; |
| 124 | } |