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 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 
24 #include <VtsHalHidlTargetCallbackBase.h>
25 
26 using ::android::hardware::thermal::V1_0::Temperature;
27 using ::android::hardware::thermal::V1_0::TemperatureType;
28 using ::android::hardware::thermal::V1_1::IThermal;
29 using ::android::hardware::thermal::V1_1::IThermalCallback;
30 using ::android::hardware::Return;
31 using ::android::hardware::Void;
32 using ::android::sp;
33 
34 constexpr char kCallbackNameNotifyThrottling[] = "notifyThrottling";
35 static const Temperature kThrottleTemp = {
36     .type = TemperatureType::CPU,
37     .name = "test temperature sensor",
38     .currentValue = 98.6,
39     .throttlingThreshold = 58,
40     .shutdownThreshold = 60,
41     .vrThrottlingThreshold = 59,
42 };
43 
44 class ThermalCallbackArgs {
45    public:
46      bool isThrottling;
47      Temperature temperature;
48 };
49 
50 // Callback class for receiving thermal event notifications from main class
51 class ThermalCallback
52     : public ::testing::VtsHalHidlTargetCallbackBase<ThermalCallbackArgs>,
53       public IThermalCallback {
54    public:
55     virtual ~ThermalCallback() = default;
56 
notifyThrottling(bool isThrottling,const Temperature & temperature)57     Return<void> notifyThrottling(bool isThrottling,
58                                   const Temperature& temperature) override {
59         ThermalCallbackArgs args;
60         args.isThrottling = isThrottling;
61         args.temperature = temperature;
62         NotifyFromCallback(kCallbackNameNotifyThrottling, args);
63         return Void();
64     }
65 };
66 
67 // The main test class for THERMAL HIDL HAL 1.1.
68 class ThermalHidlTest : public testing::TestWithParam<std::string> {
69    public:
SetUp()70     virtual void SetUp() override {
71         mThermal = IThermal::getService(GetParam());
72         ASSERT_NE(mThermal, nullptr);
73         mThermalCallback = new(std::nothrow) ThermalCallback();
74         ASSERT_NE(mThermalCallback, nullptr);
75         auto ret = mThermal->registerThermalCallback(mThermalCallback);
76         ASSERT_TRUE(ret.isOk());
77     }
78 
TearDown()79     virtual void TearDown() override {
80         auto ret = mThermal->registerThermalCallback(nullptr);
81         ASSERT_TRUE(ret.isOk());
82     }
83 
84    protected:
85     sp<IThermal> mThermal;
86     sp<ThermalCallback> mThermalCallback;
87 }; // class ThermalHidlTest
88 
89 // Test ThermalCallback::notifyThrottling().
90 // This just calls into and back from our local ThermalCallback impl.
91 // Note: a real thermal throttling event from the Thermal HAL could be
92 // inadvertently received here.
TEST_P(ThermalHidlTest,NotifyThrottlingTest)93 TEST_P(ThermalHidlTest, NotifyThrottlingTest) {
94     auto ret = mThermalCallback->notifyThrottling(true, kThrottleTemp);
95     ASSERT_TRUE(ret.isOk());
96     auto res = mThermalCallback->WaitForCallback(kCallbackNameNotifyThrottling);
97     EXPECT_TRUE(res.no_timeout);
98     ASSERT_TRUE(res.args);
99     EXPECT_EQ(true, res.args->isThrottling);
100     EXPECT_EQ(kThrottleTemp, res.args->temperature);
101 }
102 
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ThermalHidlTest);
104 INSTANTIATE_TEST_SUITE_P(
105         PerInstance, ThermalHidlTest,
106         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IThermal::descriptor)),
107         android::hardware::PrintInstanceNameToString);