blob: eb91e3133c2a655730c0a1ec818c25adb20c8f10 [file] [log] [blame]
Peiyong Lin56960752022-09-30 21:52:52 +00001/*
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
42namespace aidl::android::hardware::thermal {
43
44namespace {
45
46using ::android::sp;
47using android::hardware::thermal::CoolingDevice;
48using android::hardware::thermal::IThermal;
49using android::hardware::thermal::Temperature;
50using android::hardware::thermal::TemperatureType;
51
52using namespace std::string_literals;
53using namespace std::chrono_literals;
54
55static 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
63class 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.
89class 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);
Xiang Wangd43e8732023-02-07 12:10:33 -080098 auto status = mThermal->registerThermalChangedCallback(mThermalCallback);
99 ASSERT_TRUE(status.isOk());
Peiyong Lin56960752022-09-30 21:52:52 +0000100 // Expect to fail if register again
Xiang Wangd43e8732023-02-07 12:10:33 -0800101 status = mThermal->registerThermalChangedCallback(mThermalCallback);
102 ASSERT_FALSE(status.isOk());
103 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
Peiyong Lin56960752022-09-30 21:52:52 +0000104 }
105
106 void TearDown() override {
Xiang Wangd43e8732023-02-07 12:10:33 -0800107 auto status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
108 ASSERT_TRUE(status.isOk());
Peiyong Lin56960752022-09-30 21:52:52 +0000109 // Expect to fail if unregister again
Xiang Wangd43e8732023-02-07 12:10:33 -0800110 status = mThermal->unregisterThermalChangedCallback(mThermalCallback);
111 ASSERT_FALSE(status.isOk());
112 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
Peiyong Lin56960752022-09-30 21:52:52 +0000113 }
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.
122TEST_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
129GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalAidlTest);
130INSTANTIATE_TEST_SUITE_P(
131 Thermal, ThermalAidlTest,
132 testing::ValuesIn(::android::getAidlHalInstanceNames(IThermal::descriptor)),
133 ::android::PrintInstanceNameToString);
134
135} // namespace
Xiang Wangdd0edc62023-02-08 16:47:06 -0800136} // namespace aidl::android::hardware::thermal
Peiyong Lin56960752022-09-30 21:52:52 +0000137
138int main(int argc, char** argv) {
139 ::testing::InitGoogleTest(&argc, argv);
140 ABinderProcess_setThreadPoolMaxThreadCount(1);
141 ABinderProcess_startThreadPool();
142 return RUN_ALL_TESTS();
143}