1 /*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dscreen_sink_load_callback.h"
17 #include "dscreen_sink_handler_test.h"
18
19 using namespace testing;
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace DistributedHardware {
SetUpTestCase(void)24 void DScreenSinkHandlerTest::SetUpTestCase(void) {}
25
TearDownTestCase(void)26 void DScreenSinkHandlerTest::TearDownTestCase(void) {}
27
SetUp(void)28 void DScreenSinkHandlerTest::SetUp(void)
29 {
30 DScreenSinkHandler::GetInstance().InitSink("DScreenSinkHandlerTest");
31 }
32
TearDown(void)33 void DScreenSinkHandlerTest::TearDown(void)
34 {
35 DScreenSinkHandler::GetInstance().ReleaseSink();
36 }
37
38 /**
39 * @tc.name: InitSink_001
40 * @tc.desc: Verify the InitSink function.
41 * @tc.type: FUNC
42 * @tc.require: Issue Number
43 */
44 HWTEST_F(DScreenSinkHandlerTest, InitSink_001, TestSize.Level1)
45 {
46 const std::string param = "";
47 int32_t ret = DScreenSinkHandler::GetInstance().InitSink(param);
48 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
49 }
50
51 /**
52 * @tc.name: InitSink_002
53 * @tc.desc: Verify the InitSink function.
54 * @tc.type: FUNC
55 * @tc.require: Issue Number
56 */
57 HWTEST_F(DScreenSinkHandlerTest, InitSink_002, TestSize.Level1)
58 {
59 const std::string param = "DScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandler \
60 TestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandler \
61 TestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandler \
62 TestDScreenSinkHandlerTestDScreenSinkHandlerTestDScreenSinkHandlerTest";
63 int32_t ret = DScreenSinkHandler::GetInstance().InitSink(param);
64 EXPECT_EQ(DH_SUCCESS, ret);
65 }
66
67 /**
68 * @tc.name: FinishStartSA_001
69 * @tc.desc: Verify the FinishStartSA function.
70 * @tc.type: FUNC
71 * @tc.require: Issue Number
72 */
73 HWTEST_F(DScreenSinkHandlerTest, FinishStartSA_001, TestSize.Level1)
74 {
75 const std::string param = "";
76 sptr<IRemoteObject> remoteObject = nullptr;
77 DScreenSinkHandler::GetInstance().dScreenSinkProxy_ = nullptr;
78 DScreenSinkHandler::GetInstance().FinishStartSA(param, remoteObject);
79 EXPECT_EQ(DScreenSinkHandler::GetInstance().dScreenSinkProxy_, nullptr);
80 }
81
82 /**
83 * @tc.name: LocalHardware_001
84 * @tc.desc: Verify the SubscribeLocalHardware function.
85 * @tc.type: FUNC
86 * @tc.require: Issue Number
87 */
88 HWTEST_F(DScreenSinkHandlerTest, LocalHardware_001, TestSize.Level1)
89 {
90 const std::string dhId = "DScreenSinkHandlerTest";
91 const std::string param = "DScreenSinkHandlerTest";
92 int32_t ret = DScreenSinkHandler::GetInstance().InitSink(param);
93 EXPECT_EQ(DH_SUCCESS, ret);
94 sptr<DScreenSinkLoadCallback> loadCallback(new DScreenSinkLoadCallback(param));
95 loadCallback->OnLoadSystemAbilitySuccess(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, nullptr);
96 loadCallback->OnLoadSystemAbilitySuccess(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, nullptr);
97 ret = DScreenSinkHandler::GetInstance().SubscribeLocalHardware(dhId, param);
98 EXPECT_EQ(DH_SUCCESS, ret);
99
100 ret = DScreenSinkHandler::GetInstance().UnsubscribeLocalHardware(dhId);
101 EXPECT_EQ(DH_SUCCESS, ret);
102
103 DScreenSinkHandler::GetInstance().dScreenSinkProxy_ = nullptr;
104 ret = DScreenSinkHandler::GetInstance().UnsubscribeLocalHardware(dhId);
105 EXPECT_EQ(ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT, ret);
106 }
107
108 /**
109 * @tc.name: SubscribeLocalHardware_001
110 * @tc.desc: Verify the SubscribeLocalHardware function.
111 * @tc.type: FUNC
112 * @tc.require: Issue Number
113 */
114 HWTEST_F(DScreenSinkHandlerTest, SubscribeLocalHardware_001, TestSize.Level1)
115 {
116 const std::string dhId = "";
117 const std::string param = "DScreenSinkHandlerTest";
118 int32_t ret = DScreenSinkHandler::GetInstance().SubscribeLocalHardware(dhId, param);
119 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
120
121 ret = DScreenSinkHandler::GetInstance().UnsubscribeLocalHardware(dhId);
122 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
123 }
124
125 /**
126 * @tc.name: SubscribeLocalHardware_002
127 * @tc.desc: Verify the SubscribeLocalHardware function.
128 * @tc.type: FUNC
129 * @tc.require: Issue Number
130 */
131 HWTEST_F(DScreenSinkHandlerTest, SubscribeLocalHardware_002, TestSize.Level1)
132 {
133 const std::string dhId = "";
134 const std::string param = "DScreenSinkHandlerTest";
135 DScreenSinkHandler::GetInstance().dScreenSinkProxy_ = nullptr;
136 sptr<IRemoteObject> remoteObject = nullptr;
137 DScreenSinkHandler::GetInstance().OnRemoteSinkSvrDied(remoteObject);
138 ASSERT_NE(nullptr, DScreenSinkHandler::GetInstance().sinkSvrRecipient_);
139 DScreenSinkHandler::GetInstance().sinkSvrRecipient_->OnRemoteDied(remoteObject);
140 int32_t ret = DScreenSinkHandler::GetInstance().SubscribeLocalHardware(dhId, param);
141 EXPECT_EQ(ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT, ret);
142 }
143
144 /**
145 * @tc.name: OnRemoteSinkSvrDied_001
146 * @tc.desc: Verify the OnRemoteSinkSvrDied function.
147 * @tc.type: FUNC
148 * @tc.require: Issue Number
149 */
150 HWTEST_F(DScreenSinkHandlerTest, OnRemoteSinkSvrDied_001, TestSize.Level1)
151 {
152 sptr<ISystemAbilityManager> samgr =
153 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
154
155 sptr<IRemoteObject> remoteObject =
156 samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID);
157
158 wptr<IRemoteObject> remote(remoteObject);
159
160 ASSERT_NE(DScreenSinkHandler::GetInstance().sinkSvrRecipient_, nullptr);
161 DScreenSinkHandler::GetInstance().sinkSvrRecipient_->OnRemoteDied(remote);
162 EXPECT_EQ(nullptr, DScreenSinkHandler::GetInstance().dScreenSinkProxy_);
163 }
164 } // namespace DistributedHardware
165 } // namespace OHOS
166