Peiyong Lin | 5696075 | 2022-09-30 21:52:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 <chrono> |
| 19 | #include <cmath> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <thread> |
| 23 | #include <vector> |
| 24 | |
| 25 | #define LOG_TAG "thermal_aidl_hal_test" |
| 26 | |
| 27 | #include <aidl/Gtest.h> |
| 28 | #include <aidl/Vintf.h> |
| 29 | #include <aidl/android/hardware/thermal/BnThermal.h> |
| 30 | #include <aidl/android/hardware/thermal/BnThermalChangedCallback.h> |
| 31 | #include <android-base/logging.h> |
| 32 | #include <android-base/properties.h> |
| 33 | #include <android/binder_ibinder.h> |
| 34 | #include <android/binder_interface_utils.h> |
| 35 | #include <android/binder_manager.h> |
| 36 | #include <android/binder_process.h> |
| 37 | #include <android/binder_status.h> |
| 38 | #include <gtest/gtest.h> |
| 39 | |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | namespace aidl::android::hardware::thermal { |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | using ::android::sp; |
| 47 | using android::hardware::thermal::CoolingDevice; |
| 48 | using android::hardware::thermal::IThermal; |
| 49 | using android::hardware::thermal::Temperature; |
| 50 | using android::hardware::thermal::TemperatureType; |
| 51 | |
| 52 | using namespace std::string_literals; |
| 53 | using namespace std::chrono_literals; |
| 54 | |
| 55 | static const Temperature kThrottleTemp = { |
| 56 | .type = TemperatureType::SKIN, |
| 57 | .name = "test temperature sensor", |
| 58 | .value = 98.6, |
| 59 | .throttlingStatus = ThrottlingSeverity::CRITICAL, |
| 60 | }; |
| 61 | |
| 62 | // Callback class for receiving thermal event notifications from main class |
| 63 | class ThermalCallback : public BnThermalChangedCallback { |
| 64 | public: |
| 65 | ndk::ScopedAStatus notifyThrottling(const Temperature&) override { |
| 66 | { |
| 67 | std::lock_guard<std::mutex> lock(mMutex); |
| 68 | mInvoke = true; |
| 69 | } |
| 70 | mNotifyThrottling.notify_all(); |
| 71 | return ndk::ScopedAStatus::ok(); |
| 72 | } |
| 73 | |
| 74 | template <typename R, typename P> |
| 75 | [[nodiscard]] bool waitForCallback(std::chrono::duration<R, P> duration) { |
| 76 | std::unique_lock<std::mutex> lock(mMutex); |
| 77 | bool r = mNotifyThrottling.wait_for(lock, duration, [this] { return this->mInvoke; }); |
| 78 | mInvoke = false; |
| 79 | return r; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | std::mutex mMutex; |
| 84 | std::condition_variable mNotifyThrottling; |
| 85 | bool mInvoke = false; |
| 86 | }; |
| 87 | |
| 88 | // The main test class for THERMAL HIDL HAL. |
| 89 | class ThermalAidlTest : public testing::TestWithParam<std::string> { |
| 90 | public: |
| 91 | void SetUp() override { |
| 92 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 93 | ASSERT_NE(binder, nullptr); |
| 94 | mThermal = IThermal::fromBinder(ndk::SpAIBinder(binder)); |
| 95 | |
| 96 | mThermalCallback = ndk::SharedRefBase::make<ThermalCallback>(); |
| 97 | ASSERT_NE(mThermalCallback, nullptr); |
| 98 | auto ret = mThermal->registerThermalChangedCallback(mThermalCallback); |
| 99 | ASSERT_TRUE(ret.isOk()); |
| 100 | // Expect to fail if register again |
| 101 | ret = mThermal->registerThermalChangedCallback(mThermalCallback); |
| 102 | ASSERT_FALSE(ret.isOk()); |
| 103 | ASSERT_TRUE(ret.getStatus() == STATUS_INVALID_OPERATION); |
| 104 | } |
| 105 | |
| 106 | void TearDown() override { |
| 107 | auto ret = mThermal->unregisterThermalChangedCallback(mThermalCallback); |
| 108 | ASSERT_TRUE(ret.isOk()); |
| 109 | // Expect to fail if unregister again |
| 110 | ret = mThermal->unregisterThermalChangedCallback(mThermalCallback); |
| 111 | ASSERT_FALSE(ret.isOk()); |
| 112 | ASSERT_TRUE(ret.getStatus() == STATUS_INVALID_OPERATION); |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | std::shared_ptr<IThermal> mThermal; |
| 117 | std::shared_ptr<ThermalCallback> mThermalCallback; |
| 118 | }; |
| 119 | |
| 120 | // Test ThermalChangedCallback::notifyThrottling(). |
| 121 | // This just calls into and back from our local ThermalChangedCallback impl. |
| 122 | TEST_P(ThermalAidlTest, NotifyThrottlingTest) { |
| 123 | std::shared_ptr<ThermalCallback> thermalCallback = ndk::SharedRefBase::make<ThermalCallback>(); |
| 124 | auto ret = thermalCallback->notifyThrottling(kThrottleTemp); |
| 125 | ASSERT_TRUE(ret.isOk()); |
| 126 | ASSERT_TRUE(thermalCallback->waitForCallback(200ms)); |
| 127 | } |
| 128 | |
| 129 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalAidlTest); |
| 130 | INSTANTIATE_TEST_SUITE_P( |
| 131 | Thermal, ThermalAidlTest, |
| 132 | testing::ValuesIn(::android::getAidlHalInstanceNames(IThermal::descriptor)), |
| 133 | ::android::PrintInstanceNameToString); |
| 134 | |
| 135 | } // namespace |
| 136 | |
| 137 | int main(int argc, char** argv) { |
| 138 | ::testing::InitGoogleTest(&argc, argv); |
| 139 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 140 | ABinderProcess_startThreadPool(); |
| 141 | return RUN_ALL_TESTS(); |
| 142 | } |
| 143 | |
| 144 | } // namespace aidl::android::hardware::thermal |