1 /*
2 * Copyright (c) 2022-2024 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_stub_test.h"
17
18 #include <memory>
19 #include "dscreen_constants.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace DistributedHardware {
SetUpTestCase(void)26 void DScreenSinkStubTest::SetUpTestCase(void)
27 {
28 }
29
TearDownTestCase(void)30 void DScreenSinkStubTest::TearDownTestCase(void)
31 {
32 }
33
SetUp(void)34 void DScreenSinkStubTest::SetUp(void)
35 {
36 }
37
TearDown(void)38 void DScreenSinkStubTest::TearDown(void)
39 {
40 }
41
InitSink(const std::string & params)42 int32_t DScreenSinkStubTest::TestDScreenSinkStub::InitSink(const std::string ¶ms)
43 {
44 (void)params;
45 return DH_SUCCESS;
46 }
47
ReleaseSink()48 int32_t DScreenSinkStubTest::TestDScreenSinkStub::ReleaseSink()
49 {
50 return DH_SUCCESS;
51 }
52
SubscribeLocalHardware(const std::string & dhId,const std::string & param)53 int32_t DScreenSinkStubTest::TestDScreenSinkStub::SubscribeLocalHardware(const std::string &dhId,
54 const std::string ¶m)
55 {
56 (void)dhId;
57 (void)param;
58 return DH_SUCCESS;
59 }
60
UnsubscribeLocalHardware(const std::string & dhId)61 int32_t DScreenSinkStubTest::TestDScreenSinkStub::UnsubscribeLocalHardware(const std::string &dhId)
62 {
63 (void)dhId;
64 return DH_SUCCESS;
65 }
66
DScreenNotify(const std::string & devId,int32_t eventCode,const std::string & eventContent)67 void DScreenSinkStubTest::TestDScreenSinkStub::DScreenNotify(const std::string &devId, int32_t eventCode,
68 const std::string &eventContent)
69 {
70 devId_ = devId;
71 eventCode_ = eventCode;
72 eventContent_ = eventContent;
73 }
74
75 /**
76 * @tc.name: InitSink_001
77 * @tc.desc: Invoke the InitSink ipc interface.
78 * @tc.type: FUNC
79 * @tc.require: Issue Number
80 */
81 HWTEST_F(DScreenSinkStubTest, InitSink_001, TestSize.Level1)
82 {
83 sptr<IRemoteObject> sinkStubPtr(new TestDScreenSinkStub());
84 DScreenSinkProxy sinkProxy(sinkStubPtr);
85
86 int32_t ret = sinkProxy.InitSink("params000");
87 EXPECT_EQ(DH_SUCCESS, ret);
88
89 ret = sinkProxy.InitSink("");
90 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
91 }
92
93 /**
94 * @tc.name: ReleaseSink_001
95 * @tc.desc: Invoke the ReleaseSink ipc interface.
96 * @tc.type: FUNC
97 * @tc.require: Issue Number
98 */
99 HWTEST_F(DScreenSinkStubTest, ReleaseSink_001, TestSize.Level1)
100 {
101 sptr<IRemoteObject> sinkStubPtr(new TestDScreenSinkStub());
102 DScreenSinkProxy sinkProxy(sinkStubPtr);
103
104 int32_t ret = sinkProxy.ReleaseSink();
105 EXPECT_EQ(DH_SUCCESS, ret);
106 }
107
108 /**
109 * @tc.name: SubscribeLocalHardware_001
110 * @tc.desc: Invoke the SubscribeLocalHardware ipc interface.
111 * @tc.type: FUNC
112 * @tc.require: Issue Number
113 */
114 HWTEST_F(DScreenSinkStubTest, SubscribeLocalHardware_001, TestSize.Level1)
115 {
116 sptr<IRemoteObject> sinkStubPtr(new TestDScreenSinkStub());
117 DScreenSinkProxy sinkProxy(sinkStubPtr);
118
119 int32_t ret = sinkProxy.SubscribeLocalHardware("dhId000", "param000");
120 EXPECT_EQ(DH_SUCCESS, ret);
121
122 ret = sinkProxy.SubscribeLocalHardware("", "param000");
123 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
124
125 ret = sinkProxy.SubscribeLocalHardware("dhId000", "");
126 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
127
128 std::string dhId = R"(dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dh
129 Id000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000d
130 hId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000)";
131 ret = sinkProxy.SubscribeLocalHardware(dhId, "param000");
132 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
133 }
134
135 /**
136 * @tc.name: UnsubscribeLocalHardware_001
137 * @tc.desc: Invoke the UnsubscribeLocalHardware ipc interface.
138 * @tc.type: FUNC
139 * @tc.require: Issue Number
140 */
141 HWTEST_F(DScreenSinkStubTest, UnsubscribeLocalHardware_001, TestSize.Level1)
142 {
143 sptr<IRemoteObject> sinkStubPtr(new TestDScreenSinkStub());
144 DScreenSinkProxy sinkProxy(sinkStubPtr);
145
146 int32_t ret = sinkProxy.UnsubscribeLocalHardware("dhId000");
147 EXPECT_EQ(DH_SUCCESS, ret);
148
149 ret = sinkProxy.UnsubscribeLocalHardware("");
150 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
151
152 std::string dhId = R"(dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dh
153 Id000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000d
154 hId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000)";
155 ret = sinkProxy.UnsubscribeLocalHardware(dhId);
156 EXPECT_EQ(ERR_DH_SCREEN_INPUT_PARAM_INVALID, ret);
157 }
158
159 /**
160 * @tc.name: DScreenNotify_001
161 * @tc.desc: Invoke the DScreenNotify ipc interface.
162 * @tc.type: FUNC
163 * @tc.require: Issue Number
164 */
165 HWTEST_F(DScreenSinkStubTest, DScreenNotify_001, TestSize.Level1)
166 {
167 sptr<IRemoteObject> sinkStubPtr(new TestDScreenSinkStub());
168 DScreenSinkProxy sinkProxy(sinkStubPtr);
169
170 sinkProxy.DScreenNotify("", 0, "eventContent000");
171
172 sinkProxy.DScreenNotify("devId000", 0, "");
173
174 std::string longString = R"(dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId00
175 Id000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000d0dh
176 hId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId000dhId0000dh)";
177 sinkProxy.DScreenNotify(longString, 0, "eventContent000");
178
179 std::string devId = "devId000";
180 int32_t eventCode = 0;
181 std::string eventContent = "eventContent000";
182 sinkProxy.DScreenNotify(devId, eventCode, eventContent);
183 EXPECT_STREQ(devId.c_str(), ((sptr<TestDScreenSinkStub> &)sinkStubPtr)->devId_.c_str());
184 EXPECT_EQ(eventCode, ((sptr<TestDScreenSinkStub> &)sinkStubPtr)->eventCode_);
185 EXPECT_STREQ(eventContent.c_str(), ((sptr<TestDScreenSinkStub> &)sinkStubPtr)->eventContent_.c_str());
186 }
187
188 /**
189 * @tc.name: DScreenNotifyInner_001
190 * @tc.desc: Verify the DScreenNotifyInner function.
191 * @tc.type: FUNC
192 * @tc.require: Issue Number
193 */
194 HWTEST_F(DScreenSinkStubTest, DScreenNotifyInner_001, TestSize.Level1)
195 {
196 sptr<DScreenSinkStub> sinkStub(new TestDScreenSinkStub());
197 MessageParcel data;
198 MessageParcel reply;
199 MessageOption option;
200 std::string devId = "";
201 int32_t eventCode = -1;
202 std::string eventContent = "";
203 data.WriteString(devId);
204 data.WriteInt32(eventCode);
205 data.WriteString(eventContent);
206 int32_t ret = sinkStub->DScreenNotifyInner(data, reply, option);
207 EXPECT_EQ(ret, ERR_DH_SCREEN_INPUT_PARAM_INVALID);
208 }
209
210 /**
211 * @tc.name: DScreenNotifyInner_002
212 * @tc.desc: Verify the DScreenNotifyInner function.
213 * @tc.type: FUNC
214 * @tc.require: Issue Number
215 */
216 HWTEST_F(DScreenSinkStubTest, DScreenNotifyInner_002, TestSize.Level1)
217 {
218 sptr<DScreenSinkStub> sinkStub(new TestDScreenSinkStub());
219 MessageParcel data;
220 MessageParcel reply;
221 MessageOption option;
222 std::string exceedDidMaxSizeStr(DID_MAX_SIZE + 1, 'a');
223 int32_t eventCode = -1;
224 std::string eventContent = "";
225 data.WriteString(exceedDidMaxSizeStr);
226 data.WriteInt32(eventCode);
227 data.WriteString(eventContent);
228 int32_t ret = sinkStub->DScreenNotifyInner(data, reply, option);
229 EXPECT_EQ(ret, ERR_DH_SCREEN_INPUT_PARAM_INVALID);
230 }
231
232 /**
233 * @tc.name: DScreenNotifyInner_003
234 * @tc.desc: Verify the DScreenNotifyInner function.
235 * @tc.type: FUNC
236 * @tc.require: Issue Number
237 */
238 HWTEST_F(DScreenSinkStubTest, DScreenNotifyInner_003, TestSize.Level1)
239 {
240 sptr<DScreenSinkStub> sinkStub(new TestDScreenSinkStub());
241 MessageParcel data;
242 MessageParcel reply;
243 MessageOption option;
244 std::string devId = "test";
245 int32_t eventCode = -1;
246 std::string eventContent = "";
247 data.WriteString(devId);
248 data.WriteInt32(eventCode);
249 data.WriteString(eventContent);
250 int32_t ret = sinkStub->DScreenNotifyInner(data, reply, option);
251 EXPECT_EQ(ret, ERR_DH_SCREEN_INPUT_PARAM_INVALID);
252 }
253
254 /**
255 * @tc.name: DScreenNotifyInner_004
256 * @tc.desc: Verify the DScreenNotifyInner function.
257 * @tc.type: FUNC
258 * @tc.require: Issue Number
259 */
260 HWTEST_F(DScreenSinkStubTest, DScreenNotifyInner_004, TestSize.Level1)
261 {
262 sptr<DScreenSinkStub> sinkStub(new TestDScreenSinkStub());
263 MessageParcel data;
264 MessageParcel reply;
265 MessageOption option;
266 std::string devId = "test";
267 int32_t eventCode = -1;
268 std::string exceedParamMaxSizeStr(PARAM_MAX_SIZE + 1, 'a');
269 data.WriteString(devId);
270 data.WriteInt32(eventCode);
271 data.WriteString(exceedParamMaxSizeStr);
272 int32_t ret = sinkStub->DScreenNotifyInner(data, reply, option);
273 EXPECT_EQ(ret, ERR_DH_SCREEN_INPUT_PARAM_INVALID);
274 }
275
276 /**
277 * @tc.name: DScreenNotifyInner_005
278 * @tc.desc: Verify the DScreenNotifyInner function.
279 * @tc.type: FUNC
280 * @tc.require: Issue Number
281 */
282 HWTEST_F(DScreenSinkStubTest, DScreenNotifyInner_005, TestSize.Level1)
283 {
284 sptr<DScreenSinkStub> sinkStub(new TestDScreenSinkStub());
285 MessageParcel data;
286 MessageParcel reply;
287 MessageOption option;
288 std::string devId = "test";
289 int32_t eventCode = -1;
290 std::string eventContent = "test";
291 data.WriteString(devId);
292 data.WriteInt32(eventCode);
293 data.WriteString(eventContent);
294 int32_t ret = sinkStub->DScreenNotifyInner(data, reply, option);
295 EXPECT_EQ(ret, DH_SUCCESS);
296 }
297 } // namespace DistributedHardware
298 } // namespace OHOS
299