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
18 #include "extension_session.h"
19 #include "accessibility_event_info.h"
20 #include "session_info.h"
21 #include "interfaces/include/ws_common.h"
22 #include "key_event.h"
23 #include "mock/mock_session_stage.h"
24 #include "mock/mock_window_event_channel.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 class ExtensionSessionTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 sptr<ExtensionSession> extensionSession_ = nullptr;
38 sptr<SessionStageMocker> mockSessionStage_ = nullptr;
39 sptr<WindowEventChannelMocker> mockEventChannel_ = nullptr;
40 sptr<ExtensionSession::ExtensionSessionEventCallback> extSessionEventCallback_ = nullptr;
41 };
42
SetUpTestCase()43 void ExtensionSessionTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void ExtensionSessionTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void ExtensionSessionTest::SetUp()
52 {
53 SessionInfo info;
54 info.abilityName_ = "ExtensionSessionTest";
55 info.bundleName_ = "ExtensionSessionTest";
56 extensionSession_ = new (std::nothrow) ExtensionSession(info);
57 ASSERT_NE(extensionSession_, nullptr);
58
59 mockSessionStage_ = new (std::nothrow) SessionStageMocker();
60 ASSERT_NE(mockSessionStage_, nullptr);
61
62 mockEventChannel_ = new (std::nothrow) WindowEventChannelMocker(mockSessionStage_);
63 ASSERT_NE(mockEventChannel_, nullptr);
64
65 extSessionEventCallback_ = new (std::nothrow) ExtensionSession::ExtensionSessionEventCallback();
66 ASSERT_NE(extSessionEventCallback_, nullptr);
67 }
68
TearDown()69 void ExtensionSessionTest::TearDown()
70 {
71 extensionSession_ = nullptr;
72 mockSessionStage_ = nullptr;
73 mockEventChannel_ = nullptr;
74 extSessionEventCallback_ = nullptr;
75 }
76
77 namespace {
78 /**
79 * @tc.name: Connect
80 * @tc.desc: test function : Connect
81 * @tc.type: FUNC
82 */
83 HWTEST_F(ExtensionSessionTest, Connect, Function | SmallTest | Level1)
84 {
85 SystemSessionConfig sessionConfig;
86 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
87 auto res = extensionSession_->Connect(mockSessionStage_, mockEventChannel_, nullptr, sessionConfig, nullptr,
88 nullptr, "");
89 ASSERT_EQ(res, WSError::WS_OK);
90
91 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
92 res = extensionSession_->Connect(mockSessionStage_, nullptr, nullptr, sessionConfig, nullptr, nullptr, "");
93 ASSERT_EQ(res, WSError::WS_ERROR_NULLPTR);
94 }
95
96 /**
97 * @tc.name: RegisterExtensionSessionEventCallback
98 * @tc.desc: test function : RegisterExtensionSessionEventCallback
99 * @tc.type: FUNC
100 */
101 HWTEST_F(ExtensionSessionTest, RegisterExtensionSessionEventCallback, Function | SmallTest | Level1)
102 {
103 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
104 ASSERT_NE(nullptr, extensionSession_->GetExtensionSessionEventCallback());
105 }
106
107 /**
108 * @tc.name: GetExtensionSessionEventCallback
109 * @tc.desc: test function : GetExtensionSessionEventCallback
110 * @tc.type: FUNC
111 */
112 HWTEST_F(ExtensionSessionTest, GetExtensionSessionEventCallback, Function | SmallTest | Level1)
113 {
114 ASSERT_NE(nullptr, extensionSession_->GetExtensionSessionEventCallback());
115 }
116
117 /**
118 * @tc.name: TransferAbilityResult
119 * @tc.desc: test function : TransferAbilityResult
120 * @tc.type: FUNC
121 */
122 HWTEST_F(ExtensionSessionTest, TransferAbilityResult, Function | SmallTest | Level1)
123 {
124 MockFunction<void(uint32_t, const AAFwk::Want&)> mockTransferAbilityResultFunc;
125 extSessionEventCallback_->transferAbilityResultFunc_ = mockTransferAbilityResultFunc.AsStdFunction();
126 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
127 uint32_t test = 0;
128 AAFwk::Want want;
129 EXPECT_CALL(mockTransferAbilityResultFunc, Call(_, _)).Times(1);
130 WSError result = extensionSession_->TransferAbilityResult(test, want);
131 ASSERT_EQ(result, WSError::WS_OK);
132
133 extSessionEventCallback_->transferAbilityResultFunc_ = nullptr;
134 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
135 EXPECT_CALL(mockTransferAbilityResultFunc, Call(_, _)).Times(0);
136 result = extensionSession_->TransferAbilityResult(test, want);
137 ASSERT_EQ(result, WSError::WS_OK);
138
139 extSessionEventCallback_ = nullptr;
140 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
141 EXPECT_CALL(mockTransferAbilityResultFunc, Call(_, _)).Times(0);
142 result = extensionSession_->TransferAbilityResult(test, want);
143 ASSERT_EQ(result, WSError::WS_OK);
144 }
145
146 /**
147 * @tc.name: TransferExtensionData
148 * @tc.desc: test function : TransferExtensionData
149 * @tc.type: FUNC
150 */
151 HWTEST_F(ExtensionSessionTest, TransferExtensionData, Function | SmallTest | Level1)
152 {
153 MockFunction<void(const AAFwk::WantParams&)> mockTransferExtensionDataFunc;
154 extSessionEventCallback_->transferExtensionDataFunc_ = mockTransferExtensionDataFunc.AsStdFunction();
155 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
156 AAFwk::WantParams wantParams;
157 EXPECT_CALL(mockTransferExtensionDataFunc, Call(_)).Times(1);
158 WSError result = extensionSession_->TransferExtensionData(wantParams);
159 ASSERT_EQ(result, WSError::WS_OK);
160
161 extSessionEventCallback_->transferExtensionDataFunc_ = nullptr;
162 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
163 EXPECT_CALL(mockTransferExtensionDataFunc, Call(_)).Times(0);
164 result = extensionSession_->TransferExtensionData(wantParams);
165 ASSERT_EQ(result, WSError::WS_OK);
166
167 extSessionEventCallback_ = nullptr;
168 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
169 EXPECT_CALL(mockTransferExtensionDataFunc, Call(_)).Times(0);
170 result = extensionSession_->TransferExtensionData(wantParams);
171 ASSERT_EQ(result, WSError::WS_OK);
172 }
173
174 /**
175 * @tc.name: TransferComponentData
176 * @tc.desc: test function : TransferComponentData
177 * @tc.type: FUNC
178 */
179 HWTEST_F(ExtensionSessionTest, TransferComponentData, Function | SmallTest | Level1)
180 {
181 extensionSession_->sessionStage_ = mockSessionStage_;
182
183 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
184 AAFwk::WantParams wantParams;
185 EXPECT_CALL(*mockSessionStage_, NotifyTransferComponentData).Times(0);
186 WSError result = extensionSession_->TransferComponentData(wantParams);
187 ASSERT_EQ(result, WSError::WS_ERROR_INVALID_SESSION);
188
189 extensionSession_->state_ = SessionState::STATE_CONNECT;
190 EXPECT_CALL(*mockSessionStage_, NotifyTransferComponentData).Times(1);
191 result = extensionSession_->TransferComponentData(wantParams);
192 ASSERT_EQ(result, WSError::WS_OK);
193 }
194
195 /**
196 * @tc.name: TransferComponentDataSync
197 * @tc.desc: test function : TransferComponentDataSync
198 * @tc.type: FUNC
199 */
200 HWTEST_F(ExtensionSessionTest, TransferComponentDataSync, Function | SmallTest | Level1)
201 {
202 extensionSession_->sessionStage_ = mockSessionStage_;
203
204 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
205 AAFwk::WantParams wantParams;
206 AAFwk::WantParams reWantParams;
207 EXPECT_CALL(*mockSessionStage_, NotifyTransferComponentDataSync).Times(0);
208 auto res = extensionSession_->TransferComponentDataSync(wantParams, reWantParams);
209 ASSERT_EQ(res, WSErrorCode::WS_ERROR_TRANSFER_DATA_FAILED);
210
211 extensionSession_->state_ = SessionState::STATE_CONNECT;
212 EXPECT_CALL(*mockSessionStage_, NotifyTransferComponentDataSync).Times(1).WillOnce(Return(WSErrorCode::WS_OK));
213 res = extensionSession_->TransferComponentDataSync(wantParams, reWantParams);
214 ASSERT_EQ(res, WSErrorCode::WS_OK);
215 }
216
217 /**
218 * @tc.name: NotifySyncOn
219 * @tc.desc: test function : NotifySyncOn
220 * @tc.type: FUNC
221 */
222 HWTEST_F(ExtensionSessionTest, NotifySyncOn, Function | SmallTest | Level1)
223 {
224 MockFunction<void()> mockNotifySyncOnFunc;
225 extSessionEventCallback_->notifySyncOnFunc_ = mockNotifySyncOnFunc.AsStdFunction();
226 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
227 EXPECT_CALL(mockNotifySyncOnFunc, Call()).Times(1);
228 extensionSession_->NotifySyncOn();
229
230 extSessionEventCallback_->notifySyncOnFunc_ = nullptr;
231 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
232 EXPECT_CALL(mockNotifySyncOnFunc, Call()).Times(0);
233 extensionSession_->NotifySyncOn();
234
235 extSessionEventCallback_ = nullptr;
236 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
237 EXPECT_CALL(mockNotifySyncOnFunc, Call()).Times(0);
238 extensionSession_->NotifySyncOn();
239 }
240
241 /**
242 * @tc.name: NotifyAsyncOn
243 * @tc.desc: test function : NotifyAsyncOn
244 * @tc.type: FUNC
245 */
246 HWTEST_F(ExtensionSessionTest, NotifyAsyncOn, Function | SmallTest | Level1)
247 {
248 MockFunction<void()> mockNotifyAsyncOnFunc;
249 extSessionEventCallback_->notifyAsyncOnFunc_ = mockNotifyAsyncOnFunc.AsStdFunction();
250 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
251 EXPECT_CALL(mockNotifyAsyncOnFunc, Call()).Times(1);
252 extensionSession_->NotifyAsyncOn();
253
254 extSessionEventCallback_->notifyAsyncOnFunc_ = nullptr;
255 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
256 EXPECT_CALL(mockNotifyAsyncOnFunc, Call()).Times(0);
257 extensionSession_->NotifyAsyncOn();
258
259 extSessionEventCallback_ = nullptr;
260 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
261 EXPECT_CALL(mockNotifyAsyncOnFunc, Call()).Times(0);
262 extensionSession_->NotifyAsyncOn();
263 }
264
265 /**
266 * @tc.name: NotifyDensityFollowHost01
267 * @tc.desc: normal test
268 * @tc.type: FUNC
269 */
270 HWTEST_F(ExtensionSessionTest, NotifyDensityFollowHost01, Function | SmallTest | Level1)
271 {
272 extensionSession_->state_ = SessionState::STATE_CONNECT;
273 extensionSession_->sessionStage_ = mockSessionStage_;
274
275 bool isFollowHost = true;
276 float densityValue = 1.0f;
277 EXPECT_CALL(*mockSessionStage_, NotifyDensityFollowHost(isFollowHost, densityValue));
278 WSError res = extensionSession_->NotifyDensityFollowHost(isFollowHost, densityValue);
279 ASSERT_EQ(WSError::WS_OK, res);
280 }
281
282 /**
283 * @tc.name: NotifyDensityFollowHost02
284 * @tc.desc: session is invalid
285 * @tc.type: FUNC
286 */
287 HWTEST_F(ExtensionSessionTest, NotifyDensityFollowHost02, Function | SmallTest | Level1)
288 {
289 bool isFollowHost = true;
290 float densityValue = 1.0f;
291 WSError res = extensionSession_->NotifyDensityFollowHost(isFollowHost, densityValue);
292 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
293 }
294
295 /**
296 * @tc.name: NotifyDensityFollowHost03
297 * @tc.desc: sessionStage_ is invalid
298 * @tc.type: FUNC
299 */
300 HWTEST_F(ExtensionSessionTest, NotifyDensityFollowHost03, Function | SmallTest | Level1)
301 {
302 extensionSession_->state_ = SessionState::STATE_CONNECT;
303 extensionSession_->sessionStage_ = nullptr;
304
305 bool isFollowHost = true;
306 float densityValue = 1.0f;
307 WSError res = extensionSession_->NotifyDensityFollowHost(isFollowHost, densityValue);
308 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, res);
309 }
310
311 /**
312 * @tc.name: TriggerBindModalUIExtension
313 * @tc.desc: test function : TriggerBindModalUIExtension
314 * @tc.type: FUNC
315 */
316 HWTEST_F(ExtensionSessionTest, TriggerBindModalUIExtension, Function | SmallTest | Level1)
317 {
318 extensionSession_->isFirstTriggerBindModal_ = false;
319 MockFunction<void()> mockNotifyBindModalFunc;
320 extSessionEventCallback_->notifyBindModalFunc_ = mockNotifyBindModalFunc.AsStdFunction();
321 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
322 EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
323 extensionSession_->TriggerBindModalUIExtension();
324
325 extensionSession_->isFirstTriggerBindModal_ = true;
326 EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(1);
327 extensionSession_->TriggerBindModalUIExtension();
328
329 extSessionEventCallback_->notifyBindModalFunc_ = nullptr;
330 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
331 EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
332 extensionSession_->TriggerBindModalUIExtension();
333
334 extSessionEventCallback_ = nullptr;
335 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
336 EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
337 extensionSession_->TriggerBindModalUIExtension();
338 }
339
340 /**
341 * @tc.name: TransferAccessibilityEvent
342 * @tc.desc: test function : TransferAccessibilityEvent
343 * @tc.type: FUNC
344 */
345 HWTEST_F(ExtensionSessionTest, TransferAccessibilityEvent, Function | SmallTest | Level1)
346 {
347 OHOS::Accessibility::AccessibilityEventInfo info1;
348 int64_t uiExtensionIdLevel = 6;
349 WSError result = extensionSession_->TransferAccessibilityEvent(info1, uiExtensionIdLevel);
350 ASSERT_EQ(result, WSError::WS_OK);
351 }
352
353 /**
354 * @tc.name: TransferAccessibilityHoverEvent01
355 * @tc.desc: TransferAccessibilityHoverEvent01
356 * @tc.type: FUNC
357 */
358 HWTEST_F(ExtensionSessionTest, TransferAccessibilityHoverEvent01, Function | SmallTest | Level1)
359 {
360 extensionSession_->windowEventChannel_ = mockEventChannel_;
361 EXPECT_CALL(*mockEventChannel_, TransferAccessibilityHoverEvent);
362 float pointX = 0.0f;
363 float pointY = 0.0f;
364 int32_t sourceType = 0;
365 int32_t eventType = 0;
366 int64_t timeMs = 0;
367 WSError result = extensionSession_->TransferAccessibilityHoverEvent(
368 pointX, pointY, sourceType, eventType, timeMs);
369 ASSERT_EQ(result, WSError::WS_OK);
370 }
371
372 /**
373 * @tc.name: TransferAccessibilityHoverEvent02
374 * @tc.desc: TransferAccessibilityHoverEvent02
375 * @tc.type: FUNC
376 */
377 HWTEST_F(ExtensionSessionTest, TransferAccessibilityHoverEvent02, Function | SmallTest | Level1)
378 {
379 extensionSession_->windowEventChannel_ = nullptr;
380 float pointX = 0.0f;
381 float pointY = 0.0f;
382 int32_t sourceType = 0;
383 int32_t eventType = 0;
384 int64_t timeMs = 0;
385 WSError result = extensionSession_->TransferAccessibilityHoverEvent(
386 pointX, pointY, sourceType, eventType, timeMs);
387 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
388 }
389
390 /**
391 * @tc.name: TransferAccessibilityChildTreeRegister01
392 * @tc.desc: TransferAccessibilityChildTreeRegister01
393 * @tc.type: FUNC
394 */
395 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeRegister01, Function | SmallTest | Level1)
396 {
397 extensionSession_->windowEventChannel_ = mockEventChannel_;
398 EXPECT_CALL(*mockEventChannel_, TransferAccessibilityChildTreeRegister);
399 uint32_t windowId = 0;
400 int32_t treeId = 0;
401 int64_t accessibilityId = 0;
402 WSError result = extensionSession_->TransferAccessibilityChildTreeRegister(windowId, treeId, accessibilityId);
403 ASSERT_EQ(result, WSError::WS_OK);
404 }
405
406 /**
407 * @tc.name: TransferAccessibilityChildTreeRegister02
408 * @tc.desc: TransferAccessibilityChildTreeRegister02
409 * @tc.type: FUNC
410 */
411 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeRegister02, Function | SmallTest | Level1)
412 {
413 extensionSession_->windowEventChannel_ = nullptr;
414 uint32_t windowId = 0;
415 int32_t treeId = 0;
416 int64_t accessibilityId = 0;
417 WSError result = extensionSession_->TransferAccessibilityChildTreeRegister(windowId, treeId, accessibilityId);
418 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
419 }
420
421 /**
422 * @tc.name: TransferAccessibilityChildTreeUnregister01
423 * @tc.desc: TransferAccessibilityChildTreeUnregister01
424 * @tc.type: FUNC
425 */
426 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeUnregister01, Function | SmallTest | Level1)
427 {
428 extensionSession_->windowEventChannel_ = mockEventChannel_;
429 EXPECT_CALL(*mockEventChannel_, TransferAccessibilityChildTreeUnregister);
430 WSError result = extensionSession_->TransferAccessibilityChildTreeUnregister();
431 ASSERT_EQ(result, WSError::WS_OK);
432 }
433
434 /**
435 * @tc.name: TransferAccessibilityChildTreeUnregister02
436 * @tc.desc: TransferAccessibilityChildTreeUnregister02
437 * @tc.type: FUNC
438 */
439 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeUnregister02, Function | SmallTest | Level1)
440 {
441 extensionSession_->windowEventChannel_ = nullptr;
442 WSError result = extensionSession_->TransferAccessibilityChildTreeUnregister();
443 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
444 }
445
446 /**
447 * @tc.name: TransferAccessibilityDumpChildInfo01
448 * @tc.desc: TransferAccessibilityDumpChildInfo01
449 * @tc.type: FUNC
450 */
451 HWTEST_F(ExtensionSessionTest, TransferAccessibilityDumpChildInfo01, Function | SmallTest | Level1)
452 {
453 extensionSession_->windowEventChannel_ = mockEventChannel_;
454 EXPECT_CALL(*mockEventChannel_, TransferAccessibilityDumpChildInfo);
455 std::vector<std::string> params;
456 std::vector<std::string> info;
457 WSError result = extensionSession_->TransferAccessibilityDumpChildInfo(params, info);
458 ASSERT_EQ(result, WSError::WS_OK);
459 }
460
461 /**
462 * @tc.name: TransferAccessibilityDumpChildInfo02
463 * @tc.desc: TransferAccessibilityDumpChildInfo02
464 * @tc.type: FUNC
465 */
466 HWTEST_F(ExtensionSessionTest, TransferAccessibilityDumpChildInfo02, Function | SmallTest | Level1)
467 {
468 extensionSession_->windowEventChannel_ = nullptr;
469 std::vector<std::string> params;
470 std::vector<std::string> info;
471 WSError result = extensionSession_->TransferAccessibilityDumpChildInfo(params, info);
472 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
473 }
474
475 /**
476 * @tc.name: TransferKeyEventForConsumed01
477 * @tc.desc: TransferKeyEventForConsumed not timeout
478 * @tc.type: FUNC
479 */
480 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed01, Function | SmallTest | Level1)
481 {
482 extensionSession_->windowEventChannel_ = mockEventChannel_;
483 extensionSession_->channelListener_ = new (std::nothrow) WindowEventChannelListener();
484 ASSERT_NE(extensionSession_->channelListener_, nullptr);
485 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync)
486 .WillOnce([](const std::shared_ptr<MMI::KeyEvent> &keyEvent,
487 bool isPreImeEvent,
__anon993f7d710202(const std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isPreImeEvent, const sptr<IRemoteObject> &listener) 488 const sptr<IRemoteObject> &listener) {
489 auto channelListener = iface_cast<IWindowEventChannelListener>(listener);
490 channelListener->OnTransferKeyEventForConsumed(keyEvent->GetId(), isPreImeEvent, true, WSError::WS_OK);
491 return WSError::WS_OK;
492 });
493
494 auto keyEvent = MMI::KeyEvent::Create();
495 ASSERT_NE(keyEvent, nullptr);
496 bool isConsumed = false;
497 bool isTimeout = false;
498 bool isPreImeEvent = false;
499 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
500 ASSERT_EQ(result, WSError::WS_OK);
501 ASSERT_EQ(isTimeout, false);
502 }
503
504 /**
505 * @tc.name: TransferKeyEventForConsumed02
506 * @tc.desc: TransferKeyEventForConsumed timeout
507 * @tc.type: FUNC
508 */
509 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed02, Function | SmallTest | Level1)
510 {
511 extensionSession_->windowEventChannel_ = mockEventChannel_;
512 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync);
513 extensionSession_->channelListener_ = new (std::nothrow) WindowEventChannelListener();
514 ASSERT_NE(extensionSession_->channelListener_, nullptr);
515
516 auto keyEvent = MMI::KeyEvent::Create();
517 ASSERT_NE(keyEvent, nullptr);
518 bool isConsumed = false;
519 bool isTimeout = false;
520 bool isPreImeEvent = false;
521 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
522 ASSERT_EQ(result, WSError::WS_OK);
523 ASSERT_EQ(isTimeout, true);
524 }
525
526 /**
527 * @tc.name: TransferKeyEventForConsumed03
528 * @tc.desc: TransferKeyEventForConsumed windowEventChannel_ nullptr
529 * @tc.type: FUNC
530 */
531 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed03, Function | SmallTest | Level1)
532 {
533 auto keyEvent = MMI::KeyEvent::Create();
534 ASSERT_NE(keyEvent, nullptr);
535 bool isConsumed = false;
536 bool isTimeout = false;
537 bool isPreImeEvent = false;
538 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
539 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
540 }
541
542 /**
543 * @tc.name: TransferKeyEventForConsumed04
544 * @tc.desc: TransferKeyEventForConsumed keyEvent nullptr
545 * @tc.type: FUNC
546 */
547 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed04, Function | SmallTest | Level1)
548 {
549 extensionSession_->windowEventChannel_ = mockEventChannel_;
550
551 auto keyEvent = nullptr;
552 bool isConsumed = false;
553 bool isTimeout = false;
554 bool isPreImeEvent = false;
555 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
556 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
557 }
558
559 /**
560 * @tc.name: TransferKeyEventForConsumed05
561 * @tc.desc: TransferKeyEventForConsumed channelListener_ nullptr
562 * @tc.type: FUNC
563 */
564 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed05, Function | SmallTest | Level1)
565 {
566 extensionSession_->windowEventChannel_ = mockEventChannel_;
567 extensionSession_->channelListener_ = nullptr;
568
569 auto keyEvent = MMI::KeyEvent::Create();
570 ASSERT_NE(keyEvent, nullptr);
571 bool isConsumed = false;
572 bool isTimeout = false;
573 bool isPreImeEvent = false;
574 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
575 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
576 }
577
578 /**
579 * @tc.name: TransferKeyEventForConsumed06
580 * @tc.desc: TransferKeyEventForConsumed not return OK
581 * @tc.type: FUNC
582 */
583 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed06, Function | SmallTest | Level1)
584 {
585 extensionSession_->windowEventChannel_ = mockEventChannel_;
586 extensionSession_->channelListener_ = new (std::nothrow) WindowEventChannelListener();
587 ASSERT_NE(extensionSession_->channelListener_, nullptr);
588 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).WillOnce(Return(WSError::WS_DO_NOTHING));
589
590 auto keyEvent = MMI::KeyEvent::Create();
591 ASSERT_NE(keyEvent, nullptr);
592 bool isConsumed = false;
593 bool isTimeout = false;
594 bool isPreImeEvent = false;
595 WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
596 ASSERT_EQ(result, WSError::WS_DO_NOTHING);
597 }
598
599 /**
600 * @tc.name: WindowEventChannelListenerOnRemoteRequest01
601 * @tc.desc: WindowEventChannelListenerOnRemoteRequest01 test
602 * @tc.type: FUNC
603 */
604 HWTEST_F(ExtensionSessionTest, WindowEventChannelListenerOnRemoteRequest01, Function | SmallTest | Level1)
605 {
606 MessageParcel data;
607 MessageParcel reply;
608 MessageOption option;
609 data.WriteInterfaceToken(WindowEventChannelListener::GetDescriptor());
610 data.WriteBool(true);
611 data.WriteInt32(0);
612 uint32_t code = static_cast<uint32_t>(IWindowEventChannelListener::WindowEventChannelListenerMessage::
613 TRANS_ID_ON_TRANSFER_KEY_EVENT_FOR_CONSUMED_ASYNC);
614 WindowEventChannelListener listener;
615 ASSERT_EQ(listener.OnRemoteRequest(code, data, reply, option), 0);
616 }
617
618 /**
619 * @tc.name: WindowEventChannelListenerOnRemoteRequest02
620 * @tc.desc: WindowEventChannelListenerOnRemoteRequest02 test
621 * @tc.type: FUNC
622 */
623 HWTEST_F(ExtensionSessionTest, WindowEventChannelListenerOnRemoteRequest02, Function | SmallTest | Level1)
624 {
625 MessageParcel data;
626 MessageParcel reply;
627 MessageOption option;
628 data.WriteInterfaceToken(WindowEventChannelListener::GetDescriptor());
629 data.WriteBool(true);
630 data.WriteInt32(0);
631 uint32_t code = static_cast<uint32_t>(10001);
632 WindowEventChannelListener listener;
633 ASSERT_EQ(listener.OnRemoteRequest(code, data, reply, option), IPC_STUB_UNKNOW_TRANS_ERR);
634 }
635
636 /**
637 * @tc.name: ChannelDeathRecipientOnRemoteDied01
638 * @tc.desc: ChannelDeathRecipientOnRemoteDied01 test
639 * @tc.type: FUNC
640 */
641 HWTEST_F(ExtensionSessionTest, ChannelDeathRecipientOnRemoteDied01, Function | SmallTest | Level1)
642 {
643 sptr<WindowEventChannelListener> listener = new (std::nothrow) WindowEventChannelListener();
644 EXPECT_NE(nullptr, listener);
645 sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
646 deathRecipient = new (std::nothrow) ChannelDeathRecipient(listener);
647 EXPECT_NE(nullptr, deathRecipient);
648 sptr<IRemoteObject> wptrDeath = nullptr;
649 wptrDeath = new (std::nothrow) WindowEventChannel(nullptr);
650 ASSERT_NE(nullptr, wptrDeath);
651 deathRecipient->OnRemoteDied(wptrDeath);
652 EXPECT_NE(nullptr, deathRecipient);
653 }
654
655 /**
656 * @tc.name: ChannelDeathRecipientOnRemoteDied02
657 * @tc.desc: ChannelDeathRecipientOnRemoteDied02 test
658 * @tc.type: FUNC
659 */
660 HWTEST_F(ExtensionSessionTest, ChannelDeathRecipientOnRemoteDied02, Function | SmallTest | Level1)
661 {
662 sptr<WindowEventChannelListener> listener = new (std::nothrow) WindowEventChannelListener();
663 EXPECT_NE(nullptr, listener);
664 sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
665 deathRecipient = new (std::nothrow) ChannelDeathRecipient(listener);
666 EXPECT_NE(nullptr, deathRecipient);
667 deathRecipient->OnRemoteDied(nullptr);
668 EXPECT_NE(nullptr, deathRecipient);
669 }
670
671 /**
672 * @tc.name: TransferKeyEventAsync
673 * @tc.desc: TransferKeyEventAsync
674 * @tc.type: FUNC
675 */
676 HWTEST_F(ExtensionSessionTest, TransferKeyEventAsync, Function | SmallTest | Level1)
677 {
678 extensionSession_->windowEventChannel_ = mockEventChannel_;
679 extensionSession_->channelListener_ = new (std::nothrow) WindowEventChannelListener();
680 ASSERT_NE(extensionSession_->channelListener_, nullptr);
681
682 auto keyEvent = MMI::KeyEvent::Create();
683 ASSERT_NE(keyEvent, nullptr);
684 bool isPreImeEvent = false;
685 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(1).WillOnce(Return(WSError::WS_OK));
686 WSError result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
687 ASSERT_EQ(result, WSError::WS_OK);
688
689 keyEvent = nullptr;
690 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(0);
691 result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
692 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
693
694 extensionSession_->windowEventChannel_ = nullptr;
695 EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(0);
696 result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
697 ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
698 }
699
700 /**
701 * @tc.name: UpdateAvoidArea
702 * @tc.desc: test function : UpdateAvoidArea
703 * @tc.type: FUNC
704 */
705 HWTEST_F(ExtensionSessionTest, UpdateAvoidArea, Function | SmallTest | Level1)
706 {
707 extensionSession_->sessionStage_ = mockSessionStage_;
708
709 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
710 sptr<AvoidArea> avoidArea = new (std::nothrow) AvoidArea();
711 ASSERT_NE(avoidArea, nullptr);
712 AvoidAreaType type = AvoidAreaType::TYPE_SYSTEM;
713 EXPECT_CALL(*mockSessionStage_, UpdateAvoidArea).Times(0);
714 WSError res = extensionSession_->UpdateAvoidArea(avoidArea, type);
715 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
716
717 extensionSession_->state_ = SessionState::STATE_CONNECT;
718 EXPECT_CALL(*mockSessionStage_, UpdateAvoidArea).Times(1).WillOnce(Return(WSError::WS_OK));
719 res = extensionSession_->UpdateAvoidArea(avoidArea, type);
720 ASSERT_EQ(WSError::WS_OK, res);
721 }
722
723 /**
724 * @tc.name: GetAvoidAreaByType
725 * @tc.desc: test function : GetAvoidAreaByType
726 * @tc.type: FUNC
727 */
728 HWTEST_F(ExtensionSessionTest, GetAvoidAreaByType, Function | SmallTest | Level1)
729 {
730 MockFunction<AvoidArea(AvoidAreaType type)> mockNotifyGetAvoidAreaByTypeFunc;
731 extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ = mockNotifyGetAvoidAreaByTypeFunc.AsStdFunction();
732 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
733 AvoidAreaType type = AvoidAreaType::TYPE_SYSTEM;
__anon993f7d710302(AvoidAreaType type) 734 EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_)).Times(1).WillOnce([](AvoidAreaType type) {
735 return AvoidArea();
736 });
737 auto res = extensionSession_->GetAvoidAreaByType(type);
738 ASSERT_EQ(res, AvoidArea());
739
740 extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ = nullptr;
741 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
742 EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_)).Times(0);
743 res = extensionSession_->GetAvoidAreaByType(type);
744 ASSERT_EQ(res, AvoidArea());
745
746 extSessionEventCallback_ = nullptr;
747 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
748 EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_)).Times(0);
749 res = extensionSession_->GetAvoidAreaByType(type);
750 ASSERT_EQ(res, AvoidArea());
751 }
752
753 /**
754 * @tc.name: Background
755 * @tc.desc: test function : Background
756 * @tc.type: FUNC
757 */
758 HWTEST_F(ExtensionSessionTest, Background, Function | SmallTest | Level1)
759 {
760 ASSERT_NE(extensionSession_->property_, nullptr);
761 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
762 extensionSession_->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
763 auto res = extensionSession_->Background();
764 ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
765
766 extensionSession_->state_ = SessionState::STATE_ACTIVE;
767 res = extensionSession_->Background();
768 ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
769
770 extensionSession_->property_->type_ = WindowType::WINDOW_TYPE_UI_EXTENSION;
771 res = extensionSession_->Background();
772 ASSERT_EQ(res, WSError::WS_OK);
773 ASSERT_FALSE(extensionSession_->isActive_);
774 ASSERT_EQ(extensionSession_->state_, SessionState::STATE_BACKGROUND);
775
776 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
777 res = extensionSession_->Background();
778 ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
779 }
780
781 /**
782 * @tc.name: NotifyExtensionEventAsync
783 * @tc.desc: test function : NotifyExtensionEventAsync
784 * @tc.type: FUNC
785 */
786 HWTEST_F(ExtensionSessionTest, NotifyExtensionEventAsync, Function | SmallTest | Level1)
787 {
788 ASSERT_NE(nullptr, extSessionEventCallback_);
789 MockFunction<void(uint32_t)> mockNotifyExtensionEventFunc;
790 extSessionEventCallback_->notifyExtensionEventFunc_ = mockNotifyExtensionEventFunc.AsStdFunction();
791 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
792 EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(1);
793 extensionSession_->NotifyExtensionEventAsync(0);
794
795 extSessionEventCallback_->notifyExtensionEventFunc_ = nullptr;
796 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
797 EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(0);
798 extensionSession_->NotifyExtensionEventAsync(0);
799
800 extSessionEventCallback_ = nullptr;
801 extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
802 EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(0);
803 extensionSession_->NotifyExtensionEventAsync(0);
804 }
805
806 /**
807 * @tc.name: NotifyDumpInfo
808 * @tc.desc: test function : NotifyDumpInfo
809 * @tc.type: FUNC
810 */
811 HWTEST_F(ExtensionSessionTest, NotifyDumpInfo, Function | SmallTest | Level1)
812 {
813 extensionSession_->sessionStage_ = mockSessionStage_;
814 extensionSession_->state_ = SessionState::STATE_DISCONNECT;
815 std::vector<std::string> params;
816 std::vector<std::string> info;
817 WSError res = extensionSession_->NotifyDumpInfo(params, info);
818 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
819
820 extensionSession_->state_ = SessionState::STATE_CONNECT;
821 EXPECT_CALL(*mockSessionStage_, NotifyDumpInfo).Times(1).WillOnce(Return(WSError::WS_OK));
822 res = extensionSession_->NotifyDumpInfo(params, info);
823 ASSERT_EQ(WSError::WS_OK, res);
824
825 extensionSession_->sessionStage_ = nullptr;
826 res = extensionSession_->NotifyDumpInfo(params, info);
827 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, res);
828 }
829 }
830 }
831 }