1 /*
2 * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <ipc_types.h>
18 #include <pointer_event.h>
19 #include "iremote_object_mocker.h"
20 #include "mock/mock_session_stub.h"
21 #include "session/host/include/zidl/session_stub.h"
22 #include "ability_start_setting.h"
23 #include "parcel/accessibility_event_info_parcel.h"
24 #include "session/host/include/zidl/session_ipc_interface_code.h"
25 #include "want.h"
26 #include "ws_common.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace Rosen {
33 namespace {
34 const std::string UNDEFINED = "undefined";
35 }
36
37 namespace OHOS::Accessibility {
38 class AccessibilityEventInfo;
39 }
40 class SessionStubTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46
47 private:
48 sptr<SessionStub> session_ = nullptr;
49 };
50
SetUpTestCase()51 void SessionStubTest::SetUpTestCase()
52 {
53 }
54
TearDownTestCase()55 void SessionStubTest::TearDownTestCase()
56 {
57 }
58
SetUp()59 void SessionStubTest::SetUp()
60 {
61 session_ = new (std::nothrow) SessionStubMocker();
62 EXPECT_NE(nullptr, session_);
63 }
64
TearDown()65 void SessionStubTest::TearDown()
66 {
67 session_ = nullptr;
68 }
69
70 namespace {
71 /**
72 * @tc.name: OnRemoteRequest01
73 * @tc.desc: sessionStub OnRemoteRequest01
74 * @tc.type: FUNC
75 * @tc.require: #I6JLSI
76 */
77 HWTEST_F(SessionStubTest, OnRemoteRequest01, Function | SmallTest | Level2)
78 {
79 uint32_t code = 1;
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option = {MessageOption::TF_SYNC};
83 auto res = session_->OnRemoteRequest(code, data, reply, option);
84 ASSERT_EQ(ERR_NONE, res);
85 data.WriteInterfaceToken(u"OHOS.ISession");
86 res = session_->OnRemoteRequest(code, data, reply, option);
87 ASSERT_EQ(ERR_NONE, res);
88 }
89
90 /**
91 * @tc.name: ProcessRemoteRequestTest01
92 * @tc.desc: sessionStub ProcessRemoteRequestTest01
93 * @tc.type: FUNC
94 * @tc.require: #I6JLSI
95 */
96 HWTEST_F(SessionStubTest, ProcessRemoteRequestTest01, Function | SmallTest | Level2)
97 {
98 MessageParcel data;
99 MessageParcel reply;
100 MessageOption option = {MessageOption::TF_SYNC};
101 data.WriteBool(true);
102 auto res = session_->ProcessRemoteRequest(
103 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_WINDOW_ANIMATION_FLAG), data, reply, option);
104 ASSERT_EQ(ERR_NONE, res);
105
106 data.WriteBool(false);
107 data.WriteBool(true);
108 data.WriteString("");
109 res = session_->ProcessRemoteRequest(
110 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_FOREGROUND), data, reply, option);
111 ASSERT_EQ(ERR_NONE, res);
112
113 data.WriteBool(true);
114 data.WriteString("");
115 res = session_->ProcessRemoteRequest(
116 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_BACKGROUND), data, reply, option);
117 ASSERT_EQ(ERR_NONE, res);
118
119 data.WriteBool(true);
120 data.WriteString("");
121 res = session_->ProcessRemoteRequest(
122 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_DISCONNECT), data, reply, option);
123 ASSERT_EQ(ERR_NONE, res);
124 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
125 EXPECT_NE(data.WriteRemoteObject(iRemoteObjectMocker), false);
126 res = session_->ProcessRemoteRequest(
127 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_CONNECT), data, reply, option);
128 ASSERT_EQ(ERR_INVALID_DATA, res);
129 ASSERT_EQ(data.WriteUint32(1), true);
130 res = session_->ProcessRemoteRequest(
131 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_SESSION_EVENT), data, reply, option);
132 ASSERT_EQ(ERR_NONE, res);
133 AAFwk::Want options;
134 EXPECT_NE(data.WriteString("HandleSessionException"), false);
135 EXPECT_NE(data.WriteParcelable(&options), false);
136 res = session_->ProcessRemoteRequest(
137 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_TERMINATE), data, reply, option);
138 ASSERT_EQ(ERR_NONE, res);
139 res = session_->ProcessRemoteRequest(
140 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_SESSION_RECT), data, reply, option);
141 ASSERT_EQ(ERR_NONE, res);
142 res = session_->ProcessRemoteRequest(
143 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_RAISE_TO_APP_TOP), data, reply, option);
144 ASSERT_EQ(ERR_NONE, res);
145 }
146
147 /**
148 * @tc.name: ProcessRemoteRequestTest02
149 * @tc.desc: sessionStub ProcessRemoteRequestTest02
150 * @tc.type: FUNC
151 * @tc.require: #I6JLSI
152 */
153 HWTEST_F(SessionStubTest, ProcessRemoteRequestTest02, Function | SmallTest | Level2)
154 {
155 MessageParcel data;
156 MessageParcel reply;
157 MessageOption option = {MessageOption::TF_SYNC};
158 data.WriteBool(true);
159 auto res = session_->ProcessRemoteRequest(
160 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_WINDOW_ANIMATION_FLAG), data, reply, option);
161 ASSERT_EQ(ERR_NONE, res);
162 res = session_->ProcessRemoteRequest(
163 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_RAISE_APP_MAIN_WINDOW), data, reply, option);
164 ASSERT_EQ(ERR_NONE, res);
165 res = session_->ProcessRemoteRequest(
166 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_BACKPRESSED), data, reply, option);
167 ASSERT_EQ(ERR_INVALID_DATA, res);
168 res = session_->ProcessRemoteRequest(
169 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_MARK_PROCESSED), data, reply, option);
170 ASSERT_EQ(ERR_INVALID_DATA, res);
171 res = session_->ProcessRemoteRequest(
172 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_SET_MAXIMIZE_MODE), data, reply, option);
173 ASSERT_EQ(ERR_NONE, res);
174 res = session_->ProcessRemoteRequest(
175 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_MAXIMIZE_MODE), data, reply, option);
176 ASSERT_EQ(ERR_NONE, res);
177 res = session_->ProcessRemoteRequest(
178 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_NEED_AVOID), data, reply, option);
179 ASSERT_EQ(ERR_NONE, res);
180 res = session_->ProcessRemoteRequest(
181 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_GET_AVOID_AREA), data, reply, option);
182 ASSERT_EQ(ERR_NONE, res);
183 res = session_->ProcessRemoteRequest(
184 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_SET_ASPECT_RATIO), data, reply, option);
185 ASSERT_EQ(ERR_NONE, res);
186 res = session_->ProcessRemoteRequest(
187 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_UPDATE_CUSTOM_ANIMATION), data, reply, option);
188 ASSERT_EQ(ERR_NONE, res);
189 res = session_->ProcessRemoteRequest(
190 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_TRANSFER_ABILITY_RESULT), data, reply, option);
191 ASSERT_EQ(ERR_INVALID_VALUE, res);
192 res = session_->ProcessRemoteRequest(
193 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_TRANSFER_EXTENSION_DATA), data, reply, option);
194 ASSERT_EQ(ERR_INVALID_VALUE, res);
195 res = session_->ProcessRemoteRequest(
196 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_NOTIFY_EXTENSION_DIED), data, reply, option);
197 ASSERT_EQ(ERR_NONE, res);
198 res = session_->ProcessRemoteRequest(
199 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_NOTIFY_EXTENSION_TIMEOUT), data, reply, option);
200 ASSERT_EQ(ERR_INVALID_DATA, res);
201 ASSERT_EQ(data.WriteBool(true), true);
202 ASSERT_EQ(data.WriteBool(true), true);
203 res = session_->ProcessRemoteRequest(
204 static_cast<uint32_t>(SessionInterfaceCode::TRANS_ID_TITLE_AND_DOCK_HOVER_SHOW_CHANGE), data, reply, option);
205 ASSERT_EQ(ERR_NONE, res);
206 }
207
208 /**
209 * @tc.name: sessionStubTest02
210 * @tc.desc: sessionStub sessionStubTest02
211 * @tc.type: FUNC
212 * @tc.require: #I6JLSI
213 */
214 HWTEST_F(SessionStubTest, sessionStubTest02, Function | SmallTest | Level2)
215 {
216 MessageParcel data;
217 MessageParcel reply;
218 data.WriteBool(true);
219 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
220 EXPECT_NE(data.WriteRemoteObject(iRemoteObjectMocker), false);
221 ASSERT_EQ(data.WriteUint32(1), true);
222 AAFwk::Want options;
223 EXPECT_NE(data.WriteString("HandleSessionException"), false);
224 EXPECT_NE(data.WriteParcelable(&options), false);
225 ASSERT_EQ(data.WriteUint64(2), true);
226 auto res = session_->HandleRaiseAboveTarget(data, reply);
227 ASSERT_EQ(ERR_NONE, res);
228 res = session_->HandleRaiseAppMainWindowToTop(data, reply);
229 ASSERT_EQ(ERR_NONE, res);
230 res = session_->HandleBackPressed(data, reply);
231 if (!data.ReadBool()) {
232 ASSERT_EQ(ERR_INVALID_DATA, res);
233 }
234 res = session_->HandleMarkProcessed(data, reply);
235 ASSERT_EQ(ERR_NONE, res);
236 res = session_->HandleSetGlobalMaximizeMode(data, reply);
237 ASSERT_EQ(ERR_NONE, res);
238 res = session_->HandleGetGlobalMaximizeMode(data, reply);
239 ASSERT_EQ(ERR_NONE, res);
240 res = session_->HandleNeedAvoid(data, reply);
241 ASSERT_EQ(ERR_NONE, res);
242 res = session_->HandleGetAvoidAreaByType(data, reply);
243 ASSERT_EQ(ERR_NONE, res);
244 res = session_->HandleSetAspectRatio(data, reply);
245 ASSERT_EQ(ERR_NONE, res);
246 res = session_->HandleUpdateWindowSceneAfterCustomAnimation(data, reply);
247 ASSERT_EQ(ERR_NONE, res);
248 session_->HandleTransferAbilityResult(data, reply);
249 res = session_->HandleTransferExtensionData(data, reply);
250 ASSERT_EQ(ERR_INVALID_VALUE, res);
251 res = session_->HandleNotifyExtensionDied(data, reply);
252 ASSERT_EQ(ERR_NONE, res);
253 res = session_->HandleNotifyExtensionTimeout(data, reply);
254 ASSERT_EQ(ERR_NONE, res);
255 }
256
257 /**
258 * @tc.name: HandleTriggerBindModalUIExtension001
259 * @tc.desc: sessionStub sessionStubTest
260 * @tc.type: FUNC
261 * @tc.require: #I6JLSI
262 */
263 HWTEST_F(SessionStubTest, HandleTriggerBindModalUIExtension001, Function | SmallTest | Level2)
264 {
265 MessageParcel data;
266 MessageParcel reply;
267 data.WriteBool(true);
268 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
269 auto res = session_->HandleTriggerBindModalUIExtension(data, reply);
270 ASSERT_EQ(ERR_NONE, res);
271 }
272
273 /**
274 * @tc.name: HandleTransferAccessibilityEvent002
275 * @tc.desc: sessionStub sessionStubTest
276 * @tc.type: FUNC
277 * @tc.require: #I6JLSI
278 */
279 HWTEST_F(SessionStubTest, HandleTransferAccessibilityEvent003, Function | SmallTest | Level2)
280 {
281 MessageParcel data;
282 MessageParcel reply;
283 data.WriteBool(true);
284 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
285 auto res = session_->HandleTransferAccessibilityEvent(data, reply);
286 ASSERT_EQ(ERR_INVALID_DATA, res);
287 }
288
289 /**
290 * @tc.name: HandleNotifyPiPWindowPrepareClose003
291 * @tc.desc: sessionStub sessionStubTest
292 * @tc.type: FUNC
293 * @tc.require: #I6JLSI
294 */
295 HWTEST_F(SessionStubTest, HandleNotifyPiPWindowPrepareClose003, Function | SmallTest | Level2)
296 {
297 MessageParcel data;
298 MessageParcel reply;
299 data.WriteBool(true);
300 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
301 auto res = session_->HandleNotifyPiPWindowPrepareClose(data, reply);
302 ASSERT_EQ(ERR_NONE, res);
303 }
304
305 /**
306 * @tc.name: HandleUpdatePiPRect004
307 * @tc.desc: sessionStub sessionStubTest
308 * @tc.type: FUNC
309 * @tc.require: #I6JLSI
310 */
311 HWTEST_F(SessionStubTest, HandleUpdatePiPRect004, Function | SmallTest | Level2)
312 {
313 MessageParcel data;
314 MessageParcel reply;
315 data.WriteBool(true);
316 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
317 auto res = session_->HandleUpdatePiPRect(data, reply);
318 ASSERT_EQ(ERR_NONE, res);
319 }
320
321 /**
322 * @tc.name: HandleUpdatePiPControlStatus
323 * @tc.desc: sessionStub sessionStubTest
324 * @tc.type: FUNC
325 * @tc.require: #I6JLSI
326 */
327 HWTEST_F(SessionStubTest, HandleUpdatePiPControlStatus, Function | SmallTest | Level2)
328 {
329 ASSERT_NE(session_, nullptr);
330 MessageParcel data;
331 MessageParcel reply;
332 auto controlType = WsPiPControlType::VIDEO_PLAY_PAUSE;
333 auto status = WsPiPControlStatus::PLAY;
334 data.WriteUint32(static_cast<uint32_t>(controlType));
335 data.WriteInt32(static_cast<int32_t>(status));
336 auto res = session_->HandleUpdatePiPControlStatus(data, reply);
337 ASSERT_EQ(ERR_NONE, res);
338 }
339
340 /**
341 * @tc.name: HandleSetAutoStartPiP
342 * @tc.desc: sessionStub sessionStubTest
343 * @tc.type: FUNC
344 * @tc.require: #I6JLSI
345 */
346 HWTEST_F(SessionStubTest, HandleSetAutoStartPiP, Function | SmallTest | Level2)
347 {
348 ASSERT_NE(session_, nullptr);
349 MessageParcel data;
350 MessageParcel reply;
351 ASSERT_EQ(ERR_INVALID_DATA, session_->HandleSetAutoStartPiP(data, reply));
352 bool isAutoStartValid = true;
353 uint32_t priority = 0;
354 data.WriteInt32(10);
355 ASSERT_EQ(ERR_INVALID_DATA, session_->HandleSetAutoStartPiP(data, reply));
356 data.WriteBool(isAutoStartValid);
357 data.WriteUint32(priority);
358 ASSERT_EQ(ERR_NONE, session_->HandleSetAutoStartPiP(data, reply));
359 }
360
361 /**
362 * @tc.name: HandleProcessPointDownSession006
363 * @tc.desc: sessionStub sessionStubTest
364 * @tc.type: FUNC
365 * @tc.require: #I6JLSI
366 */
367 HWTEST_F(SessionStubTest, HandleProcessPointDownSession006, Function | SmallTest | Level2)
368 {
369 MessageParcel data;
370 MessageParcel reply;
371 data.WriteBool(true);
372 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
373 auto res = session_->HandleProcessPointDownSession(data, reply);
374 ASSERT_EQ(ERR_NONE, res);
375 }
376
377 /**
378 * @tc.name: HandleSendPointerEvenForMoveDrag007
379 * @tc.desc: sessionStub sessionStubTest
380 * @tc.type: FUNC
381 * @tc.require: #I6JLSI
382 */
383 HWTEST_F(SessionStubTest, HandleSendPointerEvenForMoveDrag007, Function | SmallTest | Level2)
384 {
385 MessageParcel data;
386 MessageParcel reply;
387 data.WriteBool(true);
388 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
389 auto res = session_->HandleSendPointerEvenForMoveDrag(data, reply);
390 ASSERT_EQ(ERR_INVALID_DATA, res);
391 }
392
393 /**
394 * @tc.name: HandleUpdateRectChangeListenerRegistered008
395 * @tc.desc: sessionStub sessionStubTest
396 * @tc.type: FUNC
397 * @tc.require: #I6JLSI
398 */
399 HWTEST_F(SessionStubTest, HandleUpdateRectChangeListenerRegistered008, Function | SmallTest | Level2)
400 {
401 MessageParcel data;
402 MessageParcel reply;
403 data.WriteBool(true);
404 sptr<IRemoteObjectMocker> iRemoteObjectMocker = new IRemoteObjectMocker();
405 auto res = session_->HandleUpdateRectChangeListenerRegistered(data, reply);
406 ASSERT_EQ(ERR_NONE, res);
407 }
408
409 /**
410 * @tc.name: HandleNotifySyncOn012
411 * @tc.desc: sessionStub sessionStubTest
412 * @tc.type: FUNC
413 * @tc.require: #I6JLSI
414 */
415 HWTEST_F(SessionStubTest, HandleNotifySyncOn012, Function | SmallTest | Level2)
416 {
417 MessageParcel data;
418 MessageParcel reply;
419 data.WriteBool(true);
420 auto res = session_->HandleNotifySyncOn(data, reply);
421 ASSERT_EQ(ERR_NONE, res);
422 }
423
424 /**
425 * @tc.name: HandleNotifyAsyncOn013
426 * @tc.desc: sessionStub sessionStubTest
427 * @tc.type: FUNC
428 * @tc.require: #I6JLSI
429 */
430 HWTEST_F(SessionStubTest, HandleNotifyAsyncOn013, Function | SmallTest | Level2)
431 {
432 MessageParcel data;
433 MessageParcel reply;
434 data.WriteBool(true);
435 auto res = session_->HandleNotifyAsyncOn(data, reply);
436 ASSERT_EQ(ERR_NONE, res);
437 }
438
439 /**
440 * @tc.name: HandleRequestFocus
441 * @tc.desc: sessionStub HandleRequestFocusTest
442 * @tc.type: FUNC
443 * @tc.require: #IAPLFA
444 */
445 HWTEST_F(SessionStubTest, HandleRequestFocus, Function | SmallTest | Level2)
446 {
447 MessageParcel data;
448 MessageParcel reply;
449 data.WriteBool(false);
450 ASSERT_NE(session_, nullptr);
451 auto res = session_->HandleRequestFocus(data, reply);
452 ASSERT_EQ(ERR_NONE, res);
453 }
454
455 /**
456 * @tc.name: HandleGetAppForceLandscapeConfig
457 * @tc.desc: sessionStub sessionStubTest
458 * @tc.type: FUNC
459 * @tc.require: #I6JLSI
460 */
461 HWTEST_F(SessionStubTest, HandleGetAppForceLandscapeConfig, Function | SmallTest | Level2)
462 {
463 MessageParcel data;
464 MessageParcel reply;
465 ASSERT_NE(session_, nullptr);
466 auto res = session_->HandleGetAppForceLandscapeConfig(data, reply);
467 ASSERT_EQ(ERR_NONE, res);
468 }
469
470 /**
471 * @tc.name: HandleUpdateClientRect01
472 * @tc.desc: sessionStub sessionStubTest
473 * @tc.type: FUNC
474 */
475 HWTEST_F(SessionStubTest, HandleUpdateClientRect01, Function | SmallTest | Level2)
476 {
477 MessageParcel data;
478 MessageParcel reply;
479 ASSERT_NE(session_, nullptr);
480 auto res = session_->HandleUpdateClientRect(data, reply);
481 ASSERT_EQ(ERR_INVALID_DATA, res);
482
483 data.WriteInt32(100);
484 data.WriteInt32(100);
485 data.WriteUint32(800);
486 data.WriteUint32(800);
487 res = session_->HandleUpdateClientRect(data, reply);
488 ASSERT_EQ(ERR_NONE, res);
489 }
490
491 /**
492 * @tc.name: HandleSetSystemEnableDrag_TestReadBool
493 * @tc.desc: Check whether the enableDrag is read successfully.
494 * @tc.type: FUNC
495 */
496 HWTEST_F(SessionStubTest, HandleSetSystemEnableDrag_TestReadBool, Function | SmallTest | Level2)
497 {
498 MessageParcel data;
499 MessageParcel reply;
500 ASSERT_NE(session_, nullptr);
501 auto res = session_->HandleSetSystemEnableDrag(data, reply);
502 ASSERT_EQ(ERR_INVALID_DATA, res);
503
504 data.WriteBool(true);
505 res = session_->HandleSetSystemEnableDrag(data, reply);
506 ASSERT_EQ(ERR_NONE, res);
507 }
508 }
509 } // namespace Rosen
510 } // namespace OHOS
511