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 #define LOG_TAG "Hdmi_Connection_hal_test"
18
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 #include <aidl/android/hardware/tv/hdmi/connection/BnHdmiConnection.h>
22 #include <aidl/android/hardware/tv/hdmi/connection/BnHdmiConnectionCallback.h>
23 #include <android-base/logging.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <gtest/gtest.h>
27 #include <log/log.h>
28 #include <sstream>
29 #include <vector>
30
31 using ::aidl::android::hardware::tv::hdmi::connection::BnHdmiConnectionCallback;
32 using ::aidl::android::hardware::tv::hdmi::connection::HdmiPortInfo;
33 using ::aidl::android::hardware::tv::hdmi::connection::HdmiPortType;
34 using ::aidl::android::hardware::tv::hdmi::connection::HpdSignal;
35 using ::aidl::android::hardware::tv::hdmi::connection::IHdmiConnection;
36 using ::aidl::android::hardware::tv::hdmi::connection::IHdmiConnectionCallback;
37 using ::ndk::SpAIBinder;
38
39 #define INCORRECT_VENDOR_ID 0x00
40 #define TV_PHYSICAL_ADDRESS 0x0000
41
42 // The main test class for TV HDMI Connection HAL.
43 class HdmiConnectionTest : public ::testing::TestWithParam<std::string> {
serviceDied(void *)44 static void serviceDied(void* /* cookie */) {
45 ALOGE("VtsHalTvHdmiConnectionAidlTargetTest died");
46 }
47
48 public:
SetUp()49 void SetUp() override {
50 hdmiConnection = IHdmiConnection::fromBinder(
51 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
52 ASSERT_NE(hdmiConnection, nullptr);
53 ALOGI("%s: getService() for hdmiConnection is %s", __func__,
54 hdmiConnection->isRemote() ? "remote" : "local");
55
56 hdmiConnectionCallback = ::ndk::SharedRefBase::make<HdmiConnectionCallback>();
57 ASSERT_NE(hdmiConnectionCallback, nullptr);
58 hdmiConnectionDeathRecipient =
59 ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(&serviceDied));
60 ASSERT_EQ(AIBinder_linkToDeath(hdmiConnection->asBinder().get(),
61 hdmiConnectionDeathRecipient.get(), 0),
62 STATUS_OK);
63 }
64
65 class HdmiConnectionCallback : public BnHdmiConnectionCallback {
66 public:
onHotplugEvent(bool connected __unused,int32_t portId __unused)67 ::ndk::ScopedAStatus onHotplugEvent(bool connected __unused, int32_t portId __unused) {
68 return ::ndk::ScopedAStatus::ok();
69 };
70 };
71
72 std::shared_ptr<IHdmiConnection> hdmiConnection;
73 std::shared_ptr<IHdmiConnectionCallback> hdmiConnectionCallback;
74 ::ndk::ScopedAIBinder_DeathRecipient hdmiConnectionDeathRecipient;
75 };
76
77 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HdmiConnectionTest);
78 INSTANTIATE_TEST_SUITE_P(
79 PerInstance, HdmiConnectionTest,
80 testing::ValuesIn(android::getAidlHalInstanceNames(IHdmiConnection::descriptor)),
81 android::PrintInstanceNameToString);
82
TEST_P(HdmiConnectionTest,SetCallback)83 TEST_P(HdmiConnectionTest, SetCallback) {
84 ASSERT_TRUE(hdmiConnection->setCallback(::ndk::SharedRefBase::make<HdmiConnectionCallback>())
85 .isOk());
86 }
87
TEST_P(HdmiConnectionTest,GetPortInfo)88 TEST_P(HdmiConnectionTest, GetPortInfo) {
89 std::vector<HdmiPortInfo> ports;
90 ASSERT_TRUE(hdmiConnection->getPortInfo(&ports).isOk());
91
92 bool cecSupportedOnDevice = false;
93 for (size_t i = 0; i < ports.size(); ++i) {
94 EXPECT_TRUE((ports[i].type == HdmiPortType::OUTPUT) ||
95 (ports[i].type == HdmiPortType::INPUT));
96 if (ports[i].type == HdmiPortType::OUTPUT && ports[i].portId <= 0) {
97 ALOGW("%s: Port id for output ports should start from 1", __func__);
98 }
99 cecSupportedOnDevice = cecSupportedOnDevice | ports[i].cecSupported;
100 }
101 EXPECT_NE(cecSupportedOnDevice, false) << "At least one port should support CEC";
102 }
103
TEST_P(HdmiConnectionTest,IsConnected)104 TEST_P(HdmiConnectionTest, IsConnected) {
105 std::vector<HdmiPortInfo> ports;
106 ASSERT_TRUE(hdmiConnection->getPortInfo(&ports).isOk());
107 for (size_t i = 0; i < ports.size(); ++i) {
108 bool connected;
109 ASSERT_TRUE(hdmiConnection->isConnected(ports[i].portId, &connected).isOk());
110 }
111 }
112
TEST_P(HdmiConnectionTest,HdpSignal)113 TEST_P(HdmiConnectionTest, HdpSignal) {
114 std::vector<HdmiPortInfo> ports;
115 ASSERT_TRUE(hdmiConnection->getPortInfo(&ports).isOk());
116 HpdSignal originalSignal;
117 HpdSignal signal = HpdSignal::HDMI_HPD_STATUS_BIT;
118 for (size_t i = 0; i < ports.size(); ++i) {
119 int32_t portId = ports[i].portId;
120 HpdSignal readSignal;
121 ASSERT_TRUE(hdmiConnection->getHpdSignal(portId, &originalSignal).isOk());
122 ASSERT_TRUE(hdmiConnection->setHpdSignal(signal, portId).isOk());
123 ASSERT_TRUE(hdmiConnection->getHpdSignal(portId, &readSignal).isOk());
124 EXPECT_EQ(readSignal, signal);
125 signal = HpdSignal::HDMI_HPD_PHYSICAL;
126 ASSERT_TRUE(hdmiConnection->setHpdSignal(signal, portId).isOk());
127 ASSERT_TRUE(hdmiConnection->getHpdSignal(portId, &readSignal).isOk());
128 EXPECT_EQ(readSignal, signal);
129 ASSERT_TRUE(hdmiConnection->setHpdSignal(originalSignal, portId).isOk());
130 }
131 }
132