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