• 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 {
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_ = sptr<ExtensionSession>::MakeSptr(info);
57     ASSERT_NE(extensionSession_, nullptr);
58 
59     mockSessionStage_ = sptr<SessionStageMocker>::MakeSptr();
60     ASSERT_NE(mockSessionStage_, nullptr);
61 
62     mockEventChannel_ = sptr<WindowEventChannelMocker>::MakeSptr(mockSessionStage_);
63     ASSERT_NE(mockEventChannel_, nullptr);
64 
65     extSessionEventCallback_ = sptr<ExtensionSession::ExtensionSessionEventCallback>::MakeSptr();
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: UpdateSessionViewportConfig1
313  * @tc.desc: normal test
314  * @tc.type: FUNC
315  */
316 HWTEST_F(ExtensionSessionTest, UpdateSessionViewportConfig1, Function | SmallTest | Level1)
317 {
318     ASSERT_NE(extensionSession_, nullptr);
319     extensionSession_->sessionStage_ = mockSessionStage_;
320     extensionSession_->state_ = SessionState::STATE_CONNECT;
321     SessionViewportConfig config;
322     EXPECT_CALL(*mockSessionStage_, UpdateSessionViewportConfig).Times(1).WillOnce(Return(WSError::WS_OK));
323     WSError res = extensionSession_->UpdateSessionViewportConfig(config);
324     ASSERT_EQ(WSError::WS_OK, res);
325 }
326 
327 /**
328  * @tc.name: UpdateSessionViewportConfig2
329  * @tc.desc: session is invalid
330  * @tc.type: FUNC
331  */
332 HWTEST_F(ExtensionSessionTest, UpdateSessionViewportConfig2, Function | SmallTest | Level1)
333 {
334     ASSERT_NE(extensionSession_, nullptr);
335     SessionViewportConfig config;
336     WSError res = extensionSession_->UpdateSessionViewportConfig(config);
337     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
338 }
339 
340 /**
341  * @tc.name: UpdateSessionViewportConfig3
342  * @tc.desc: sessionStage_ is null
343  * @tc.type: FUNC
344  */
345 HWTEST_F(ExtensionSessionTest, UpdateSessionViewportConfig3, Function | SmallTest | Level1)
346 {
347     ASSERT_NE(extensionSession_, nullptr);
348     extensionSession_->sessionStage_ = nullptr;
349     extensionSession_->state_ = SessionState::STATE_CONNECT;
350     SessionViewportConfig config;
351     WSError res = extensionSession_->UpdateSessionViewportConfig(config);
352     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, res);
353 }
354 
355 /**
356  * @tc.name: TriggerBindModalUIExtension
357  * @tc.desc: test function : TriggerBindModalUIExtension
358  * @tc.type: FUNC
359  */
360 HWTEST_F(ExtensionSessionTest, TriggerBindModalUIExtension, Function | SmallTest | Level1)
361 {
362     extensionSession_->isFirstTriggerBindModal_ = false;
363     MockFunction<void()> mockNotifyBindModalFunc;
364     extSessionEventCallback_->notifyBindModalFunc_ = mockNotifyBindModalFunc.AsStdFunction();
365     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
366     EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
367     extensionSession_->TriggerBindModalUIExtension();
368 
369     extensionSession_->isFirstTriggerBindModal_ = true;
370     EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(1);
371     extensionSession_->TriggerBindModalUIExtension();
372 
373     extSessionEventCallback_->notifyBindModalFunc_ = nullptr;
374     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
375     EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
376     extensionSession_->TriggerBindModalUIExtension();
377 
378     extSessionEventCallback_ = nullptr;
379     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
380     EXPECT_CALL(mockNotifyBindModalFunc, Call()).Times(0);
381     extensionSession_->TriggerBindModalUIExtension();
382 }
383 
384 /**
385  * @tc.name: TransferAccessibilityEvent
386  * @tc.desc: test function : TransferAccessibilityEvent
387  * @tc.type: FUNC
388  */
389 HWTEST_F(ExtensionSessionTest, TransferAccessibilityEvent, Function | SmallTest | Level1)
390 {
391     OHOS::Accessibility::AccessibilityEventInfo info1;
392     int64_t uiExtensionIdLevel = 6;
393     WSError result = extensionSession_->TransferAccessibilityEvent(info1, uiExtensionIdLevel);
394     ASSERT_EQ(result, WSError::WS_OK);
395 }
396 
397 /**
398  * @tc.name: TransferAccessibilityHoverEvent01
399  * @tc.desc: TransferAccessibilityHoverEvent01
400  * @tc.type: FUNC
401  */
402 HWTEST_F(ExtensionSessionTest, TransferAccessibilityHoverEvent01, Function | SmallTest | Level1)
403 {
404     extensionSession_->windowEventChannel_ = mockEventChannel_;
405     EXPECT_CALL(*mockEventChannel_, TransferAccessibilityHoverEvent);
406     float pointX = 0.0f;
407     float pointY = 0.0f;
408     int32_t sourceType = 0;
409     int32_t eventType = 0;
410     int64_t timeMs = 0;
411     WSError result = extensionSession_->TransferAccessibilityHoverEvent(
412         pointX, pointY, sourceType, eventType, timeMs);
413     ASSERT_EQ(result, WSError::WS_OK);
414 }
415 
416 /**
417  * @tc.name: TransferAccessibilityHoverEvent02
418  * @tc.desc: TransferAccessibilityHoverEvent02
419  * @tc.type: FUNC
420  */
421 HWTEST_F(ExtensionSessionTest, TransferAccessibilityHoverEvent02, Function | SmallTest | Level1)
422 {
423     extensionSession_->windowEventChannel_ = nullptr;
424     float pointX = 0.0f;
425     float pointY = 0.0f;
426     int32_t sourceType = 0;
427     int32_t eventType = 0;
428     int64_t timeMs = 0;
429     WSError result = extensionSession_->TransferAccessibilityHoverEvent(
430         pointX, pointY, sourceType, eventType, timeMs);
431     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
432 }
433 
434 /**
435  * @tc.name: TransferAccessibilityChildTreeRegister01
436  * @tc.desc: TransferAccessibilityChildTreeRegister01
437  * @tc.type: FUNC
438  */
439 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeRegister01, Function | SmallTest | Level1)
440 {
441     extensionSession_->windowEventChannel_ = mockEventChannel_;
442     EXPECT_CALL(*mockEventChannel_, TransferAccessibilityChildTreeRegister);
443     uint32_t windowId = 0;
444     int32_t treeId = 0;
445     int64_t accessibilityId = 0;
446     WSError result = extensionSession_->TransferAccessibilityChildTreeRegister(windowId, treeId, accessibilityId);
447     ASSERT_EQ(result, WSError::WS_OK);
448 }
449 
450 /**
451  * @tc.name: TransferAccessibilityChildTreeRegister02
452  * @tc.desc: TransferAccessibilityChildTreeRegister02
453  * @tc.type: FUNC
454  */
455 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeRegister02, Function | SmallTest | Level1)
456 {
457     extensionSession_->windowEventChannel_ = nullptr;
458     uint32_t windowId = 0;
459     int32_t treeId = 0;
460     int64_t accessibilityId = 0;
461     WSError result = extensionSession_->TransferAccessibilityChildTreeRegister(windowId, treeId, accessibilityId);
462     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
463 }
464 
465 /**
466  * @tc.name: TransferAccessibilityChildTreeUnregister01
467  * @tc.desc: TransferAccessibilityChildTreeUnregister01
468  * @tc.type: FUNC
469  */
470 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeUnregister01, Function | SmallTest | Level1)
471 {
472     extensionSession_->windowEventChannel_ = mockEventChannel_;
473     EXPECT_CALL(*mockEventChannel_, TransferAccessibilityChildTreeUnregister);
474     WSError result = extensionSession_->TransferAccessibilityChildTreeUnregister();
475     ASSERT_EQ(result, WSError::WS_OK);
476 }
477 
478 /**
479  * @tc.name: TransferAccessibilityChildTreeUnregister02
480  * @tc.desc: TransferAccessibilityChildTreeUnregister02
481  * @tc.type: FUNC
482  */
483 HWTEST_F(ExtensionSessionTest, TransferAccessibilityChildTreeUnregister02, Function | SmallTest | Level1)
484 {
485     extensionSession_->windowEventChannel_ = nullptr;
486     WSError result = extensionSession_->TransferAccessibilityChildTreeUnregister();
487     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
488 }
489 
490 /**
491  * @tc.name: TransferAccessibilityDumpChildInfo01
492  * @tc.desc: TransferAccessibilityDumpChildInfo01
493  * @tc.type: FUNC
494  */
495 HWTEST_F(ExtensionSessionTest, TransferAccessibilityDumpChildInfo01, Function | SmallTest | Level1)
496 {
497     extensionSession_->windowEventChannel_ = mockEventChannel_;
498     EXPECT_CALL(*mockEventChannel_, TransferAccessibilityDumpChildInfo);
499     std::vector<std::string> params;
500     std::vector<std::string> info;
501     WSError result = extensionSession_->TransferAccessibilityDumpChildInfo(params, info);
502     ASSERT_EQ(result, WSError::WS_OK);
503 }
504 
505 /**
506  * @tc.name: TransferAccessibilityDumpChildInfo02
507  * @tc.desc: TransferAccessibilityDumpChildInfo02
508  * @tc.type: FUNC
509  */
510 HWTEST_F(ExtensionSessionTest, TransferAccessibilityDumpChildInfo02, Function | SmallTest | Level1)
511 {
512     extensionSession_->windowEventChannel_ = nullptr;
513     std::vector<std::string> params;
514     std::vector<std::string> info;
515     WSError result = extensionSession_->TransferAccessibilityDumpChildInfo(params, info);
516     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
517 }
518 
519 /**
520  * @tc.name: TransferKeyEventForConsumed01
521  * @tc.desc: TransferKeyEventForConsumed not timeout
522  * @tc.type: FUNC
523  */
524 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed01, Function | SmallTest | Level1)
525 {
526     extensionSession_->windowEventChannel_ = mockEventChannel_;
527     extensionSession_->channelListener_ = sptr<WindowEventChannelListener>::MakeSptr();
528     ASSERT_NE(extensionSession_->channelListener_, nullptr);
529     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync)
530         .WillOnce([](const std::shared_ptr<MMI::KeyEvent>& keyEvent,
531                      bool isPreImeEvent,
__anon4770ff790202(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isPreImeEvent, const sptr<IRemoteObject>& listener) 532                      const sptr<IRemoteObject>& listener) {
533             auto channelListener = iface_cast<IWindowEventChannelListener>(listener);
534             channelListener->OnTransferKeyEventForConsumed(keyEvent->GetId(), isPreImeEvent, true, WSError::WS_OK);
535             return WSError::WS_OK;
536         });
537 
538     auto keyEvent = MMI::KeyEvent::Create();
539     ASSERT_NE(keyEvent, nullptr);
540     bool isConsumed = false;
541     bool isTimeout = false;
542     bool isPreImeEvent = false;
543     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
544     ASSERT_EQ(result, WSError::WS_OK);
545     ASSERT_EQ(isTimeout, false);
546 }
547 
548 /**
549  * @tc.name: TransferKeyEventForConsumed02
550  * @tc.desc: TransferKeyEventForConsumed timeout
551  * @tc.type: FUNC
552  */
553 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed02, Function | SmallTest | Level1)
554 {
555     extensionSession_->windowEventChannel_ = mockEventChannel_;
556     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync);
557     extensionSession_->channelListener_ = sptr<WindowEventChannelListener>::MakeSptr();
558     ASSERT_NE(extensionSession_->channelListener_, nullptr);
559 
560     auto keyEvent = MMI::KeyEvent::Create();
561     ASSERT_NE(keyEvent, nullptr);
562     bool isConsumed = false;
563     bool isTimeout = false;
564     bool isPreImeEvent = false;
565     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
566     ASSERT_EQ(result, WSError::WS_OK);
567     ASSERT_EQ(isTimeout, true);
568 }
569 
570 /**
571  * @tc.name: TransferKeyEventForConsumed03
572  * @tc.desc: TransferKeyEventForConsumed windowEventChannel_ nullptr
573  * @tc.type: FUNC
574  */
575 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed03, Function | SmallTest | Level1)
576 {
577     auto keyEvent = MMI::KeyEvent::Create();
578     ASSERT_NE(keyEvent, nullptr);
579     bool isConsumed = false;
580     bool isTimeout = false;
581     bool isPreImeEvent = false;
582     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
583     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
584 }
585 
586 /**
587  * @tc.name: TransferKeyEventForConsumed04
588  * @tc.desc: TransferKeyEventForConsumed keyEvent nullptr
589  * @tc.type: FUNC
590  */
591 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed04, Function | SmallTest | Level1)
592 {
593     extensionSession_->windowEventChannel_ = mockEventChannel_;
594 
595     auto keyEvent = nullptr;
596     bool isConsumed = false;
597     bool isTimeout = false;
598     bool isPreImeEvent = false;
599     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
600     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
601 }
602 
603 /**
604  * @tc.name: TransferKeyEventForConsumed05
605  * @tc.desc: TransferKeyEventForConsumed channelListener_ nullptr
606  * @tc.type: FUNC
607  */
608 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed05, Function | SmallTest | Level1)
609 {
610     extensionSession_->windowEventChannel_ = mockEventChannel_;
611     extensionSession_->channelListener_ = nullptr;
612 
613     auto keyEvent = MMI::KeyEvent::Create();
614     ASSERT_NE(keyEvent, nullptr);
615     bool isConsumed = false;
616     bool isTimeout = false;
617     bool isPreImeEvent = false;
618     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
619     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
620 }
621 
622 /**
623  * @tc.name: TransferKeyEventForConsumed06
624  * @tc.desc: TransferKeyEventForConsumed not return OK
625  * @tc.type: FUNC
626  */
627 HWTEST_F(ExtensionSessionTest, TransferKeyEventForConsumed06, Function | SmallTest | Level1)
628 {
629     extensionSession_->windowEventChannel_ = mockEventChannel_;
630     extensionSession_->channelListener_ = sptr<WindowEventChannelListener>::MakeSptr();
631     ASSERT_NE(extensionSession_->channelListener_, nullptr);
632     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).WillOnce(Return(WSError::WS_DO_NOTHING));
633 
634     auto keyEvent = MMI::KeyEvent::Create();
635     ASSERT_NE(keyEvent, nullptr);
636     bool isConsumed = false;
637     bool isTimeout = false;
638     bool isPreImeEvent = false;
639     WSError result = extensionSession_->TransferKeyEventForConsumed(keyEvent, isConsumed, isTimeout, isPreImeEvent);
640     ASSERT_EQ(result, WSError::WS_DO_NOTHING);
641 }
642 
643 /**
644  * @tc.name: WindowEventChannelListenerOnRemoteRequest01
645  * @tc.desc: WindowEventChannelListenerOnRemoteRequest01 test
646  * @tc.type: FUNC
647  */
648 HWTEST_F(ExtensionSessionTest, WindowEventChannelListenerOnRemoteRequest01, Function | SmallTest | Level1)
649 {
650     MessageParcel data;
651     MessageParcel reply;
652     MessageOption option;
653     data.WriteInterfaceToken(WindowEventChannelListener::GetDescriptor());
654     data.WriteInt32(0);
655     data.WriteBool(true);
656     data.WriteBool(true);
657     data.WriteInt32(0);
658     uint32_t code = static_cast<uint32_t>(IWindowEventChannelListener::WindowEventChannelListenerMessage::
659         TRANS_ID_ON_TRANSFER_KEY_EVENT_FOR_CONSUMED_ASYNC);
660     WindowEventChannelListener listener;
661     ASSERT_EQ(listener.OnRemoteRequest(code, data, reply, option), 0);
662 }
663 
664 /**
665  * @tc.name: WindowEventChannelListenerOnRemoteRequest02
666  * @tc.desc: WindowEventChannelListenerOnRemoteRequest02 test
667  * @tc.type: FUNC
668  */
669 HWTEST_F(ExtensionSessionTest, WindowEventChannelListenerOnRemoteRequest02, Function | SmallTest | Level1)
670 {
671     MessageParcel data;
672     MessageParcel reply;
673     MessageOption option;
674     data.WriteInterfaceToken(WindowEventChannelListener::GetDescriptor());
675     data.WriteBool(true);
676     data.WriteInt32(0);
677     uint32_t code = static_cast<uint32_t>(10001);
678     WindowEventChannelListener listener;
679     ASSERT_EQ(listener.OnRemoteRequest(code, data, reply, option), IPC_STUB_UNKNOW_TRANS_ERR);
680 }
681 
682 /**
683  * @tc.name: ChannelDeathRecipientOnRemoteDied01
684  * @tc.desc: ChannelDeathRecipientOnRemoteDied01 test
685  * @tc.type: FUNC
686  */
687 HWTEST_F(ExtensionSessionTest, ChannelDeathRecipientOnRemoteDied01, Function | SmallTest | Level1)
688 {
689     sptr<WindowEventChannelListener> listener = sptr<WindowEventChannelListener>::MakeSptr();
690     EXPECT_NE(nullptr, listener);
691     sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
692     deathRecipient = sptr<ChannelDeathRecipient>::MakeSptr(listener);
693     EXPECT_NE(nullptr, deathRecipient);
694     sptr<IRemoteObject> wptrDeath = nullptr;
695     wptrDeath = sptr<WindowEventChannel>::MakeSptr(nullptr);
696     ASSERT_NE(nullptr, wptrDeath);
697     deathRecipient->OnRemoteDied(wptrDeath);
698     EXPECT_NE(nullptr, deathRecipient);
699 }
700 
701 /**
702  * @tc.name: ChannelDeathRecipientOnRemoteDied02
703  * @tc.desc: ChannelDeathRecipientOnRemoteDied02 test
704  * @tc.type: FUNC
705  */
706 HWTEST_F(ExtensionSessionTest, ChannelDeathRecipientOnRemoteDied02, Function | SmallTest | Level1)
707 {
708     sptr<WindowEventChannelListener> listener = sptr<WindowEventChannelListener>::MakeSptr();
709     EXPECT_NE(nullptr, listener);
710     sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
711     deathRecipient = sptr<ChannelDeathRecipient>::MakeSptr(listener);
712     EXPECT_NE(nullptr, deathRecipient);
713     deathRecipient->OnRemoteDied(nullptr);
714     EXPECT_NE(nullptr, deathRecipient);
715 }
716 
717 /**
718  * @tc.name: TransferKeyEventAsync
719  * @tc.desc: TransferKeyEventAsync
720  * @tc.type: FUNC
721  */
722 HWTEST_F(ExtensionSessionTest, TransferKeyEventAsync, Function | SmallTest | Level1)
723 {
724     extensionSession_->windowEventChannel_ = mockEventChannel_;
725     extensionSession_->channelListener_ = sptr<WindowEventChannelListener>::MakeSptr();
726     ASSERT_NE(extensionSession_->channelListener_, nullptr);
727 
728     auto keyEvent = MMI::KeyEvent::Create();
729     ASSERT_NE(keyEvent, nullptr);
730     bool isPreImeEvent = false;
731     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(1).WillOnce(Return(WSError::WS_OK));
732     WSError result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
733     ASSERT_EQ(result, WSError::WS_OK);
734 
735     keyEvent = nullptr;
736     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(0);
737     result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
738     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
739 
740     extensionSession_->windowEventChannel_ = nullptr;
741     EXPECT_CALL(*mockEventChannel_, TransferKeyEventForConsumedAsync).Times(0);
742     result = extensionSession_->TransferKeyEventAsync(keyEvent, isPreImeEvent);
743     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
744 }
745 
746 /**
747  * @tc.name: UpdateAvoidArea
748  * @tc.desc: test function : UpdateAvoidArea
749  * @tc.type: FUNC
750  */
751 HWTEST_F(ExtensionSessionTest, UpdateAvoidArea, Function | SmallTest | Level1)
752 {
753     extensionSession_->sessionStage_ = mockSessionStage_;
754 
755     extensionSession_->state_ = SessionState::STATE_DISCONNECT;
756     sptr<AvoidArea> avoidArea = sptr<AvoidArea>::MakeSptr();
757     ASSERT_NE(avoidArea, nullptr);
758     AvoidAreaType type = AvoidAreaType::TYPE_SYSTEM;
759     EXPECT_CALL(*mockSessionStage_, UpdateAvoidArea).Times(0);
760     WSError res = extensionSession_->UpdateAvoidArea(avoidArea, type);
761     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
762 
763     extensionSession_->state_ = SessionState::STATE_CONNECT;
764     EXPECT_CALL(*mockSessionStage_, UpdateAvoidArea).Times(1).WillOnce(Return(WSError::WS_OK));
765     res = extensionSession_->UpdateAvoidArea(avoidArea, type);
766     ASSERT_EQ(WSError::WS_OK, res);
767 }
768 
769 /**
770  * @tc.name: GetAvoidAreaByType
771  * @tc.desc: test function : GetAvoidAreaByType
772  * @tc.type: FUNC
773  */
774 HWTEST_F(ExtensionSessionTest, GetAvoidAreaByType, Function | SmallTest | Level1)
775 {
776     MockFunction<AvoidArea(AvoidAreaType type, int32_t apiVersion)> mockNotifyGetAvoidAreaByTypeFunc;
777     extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ = mockNotifyGetAvoidAreaByTypeFunc.AsStdFunction();
778     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
779     AvoidAreaType typeSystem = AvoidAreaType::TYPE_SYSTEM;
780     AvoidAreaType typeCutout = AvoidAreaType::TYPE_CUTOUT;
781     AvoidAreaType typeSystemGesture = AvoidAreaType::TYPE_SYSTEM_GESTURE;
782     AvoidAreaType typeKeyboard = AvoidAreaType::TYPE_KEYBOARD;
783     AvoidAreaType typeNavigationIndicator = AvoidAreaType::TYPE_NAVIGATION_INDICATOR;
784     AvoidArea expectedAvoidArea;
785     expectedAvoidArea.topRect_ = {10, 20, 30, 40};
786     EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_, _)).Times(5).WillRepeatedly(Return(expectedAvoidArea));
787     auto res = extensionSession_->GetAvoidAreaByType(typeSystem);
788     ASSERT_EQ(res, expectedAvoidArea);
789     res = extensionSession_->GetAvoidAreaByType(typeCutout);
790     ASSERT_EQ(res, expectedAvoidArea);
791     res = extensionSession_->GetAvoidAreaByType(typeSystemGesture);
792     ASSERT_EQ(res, expectedAvoidArea);
793     res = extensionSession_->GetAvoidAreaByType(typeKeyboard);
794     ASSERT_EQ(res, expectedAvoidArea);
795     res = extensionSession_->GetAvoidAreaByType(typeNavigationIndicator);
796     ASSERT_EQ(res, expectedAvoidArea);
797 
798     extSessionEventCallback_->notifyGetAvoidAreaByTypeFunc_ = nullptr;
799     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
800     EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_, _)).Times(0);
801     res = extensionSession_->GetAvoidAreaByType(typeSystem);
802     ASSERT_EQ(res, AvoidArea());
803     res = extensionSession_->GetAvoidAreaByType(typeCutout);
804     ASSERT_EQ(res, AvoidArea());
805     res = extensionSession_->GetAvoidAreaByType(typeSystemGesture);
806     ASSERT_EQ(res, AvoidArea());
807     res = extensionSession_->GetAvoidAreaByType(typeKeyboard);
808     ASSERT_EQ(res, AvoidArea());
809     res = extensionSession_->GetAvoidAreaByType(typeNavigationIndicator);
810     ASSERT_EQ(res, AvoidArea());
811 
812     extSessionEventCallback_ = nullptr;
813     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
814     EXPECT_CALL(mockNotifyGetAvoidAreaByTypeFunc, Call(_, _)).Times(0);
815     res = extensionSession_->GetAvoidAreaByType(typeSystem);
816     ASSERT_EQ(res, AvoidArea());
817     res = extensionSession_->GetAvoidAreaByType(typeCutout);
818     ASSERT_EQ(res, AvoidArea());
819     res = extensionSession_->GetAvoidAreaByType(typeSystemGesture);
820     ASSERT_EQ(res, AvoidArea());
821     res = extensionSession_->GetAvoidAreaByType(typeKeyboard);
822     ASSERT_EQ(res, AvoidArea());
823     res = extensionSession_->GetAvoidAreaByType(typeNavigationIndicator);
824     ASSERT_EQ(res, AvoidArea());
825 }
826 
827 /**
828  * @tc.name: Background
829  * @tc.desc: test function : Background
830  * @tc.type: FUNC
831  */
832 HWTEST_F(ExtensionSessionTest, Background, Function | SmallTest | Level1)
833 {
834     ASSERT_NE(extensionSession_->property_, nullptr);
835     extensionSession_->state_ = SessionState::STATE_DISCONNECT;
836     extensionSession_->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
837     auto res = extensionSession_->Background();
838     ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
839 
840     extensionSession_->state_ = SessionState::STATE_ACTIVE;
841     res = extensionSession_->Background();
842     ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
843 
844     extensionSession_->property_->type_ = WindowType::WINDOW_TYPE_UI_EXTENSION;
845     res = extensionSession_->Background();
846     ASSERT_EQ(res, WSError::WS_OK);
847     ASSERT_FALSE(extensionSession_->isActive_);
848     ASSERT_EQ(extensionSession_->state_, SessionState::STATE_BACKGROUND);
849 
850     extensionSession_->state_ = SessionState::STATE_DISCONNECT;
851     res = extensionSession_->Background();
852     ASSERT_EQ(res, WSError::WS_ERROR_INVALID_SESSION);
853 }
854 
855 /**
856  * @tc.name: NotifyExtensionEventAsync
857  * @tc.desc: test function : NotifyExtensionEventAsync
858  * @tc.type: FUNC
859  */
860 HWTEST_F(ExtensionSessionTest, NotifyExtensionEventAsync, Function | SmallTest | Level1)
861 {
862     ASSERT_NE(nullptr, extSessionEventCallback_);
863     MockFunction<void(uint32_t)> mockNotifyExtensionEventFunc;
864     extSessionEventCallback_->notifyExtensionEventFunc_ = mockNotifyExtensionEventFunc.AsStdFunction();
865     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
866     EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(1);
867     extensionSession_->NotifyExtensionEventAsync(0);
868 
869     extSessionEventCallback_->notifyExtensionEventFunc_ = nullptr;
870     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
871     EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(0);
872     extensionSession_->NotifyExtensionEventAsync(0);
873 
874     extSessionEventCallback_ = nullptr;
875     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
876     EXPECT_CALL(mockNotifyExtensionEventFunc, Call(_)).Times(0);
877     extensionSession_->NotifyExtensionEventAsync(0);
878 }
879 
880 /**
881  * @tc.name: NotifyDumpInfo
882  * @tc.desc: test function : NotifyDumpInfo
883  * @tc.type: FUNC
884  */
885 HWTEST_F(ExtensionSessionTest, NotifyDumpInfo, Function | SmallTest | Level1)
886 {
887     extensionSession_->sessionStage_ = mockSessionStage_;
888     extensionSession_->state_ = SessionState::STATE_DISCONNECT;
889     std::vector<std::string> params;
890     std::vector<std::string> info;
891     WSError res = extensionSession_->NotifyDumpInfo(params, info);
892     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, res);
893 
894     extensionSession_->state_ = SessionState::STATE_CONNECT;
895     EXPECT_CALL(*mockSessionStage_, NotifyDumpInfo).Times(1).WillOnce(Return(WSError::WS_OK));
896     res = extensionSession_->NotifyDumpInfo(params, info);
897     ASSERT_EQ(WSError::WS_OK, res);
898 
899     extensionSession_->sessionStage_ = nullptr;
900     res = extensionSession_->NotifyDumpInfo(params, info);
901     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, res);
902 }
903 
904 /**
905  * @tc.name: GetStatusBarHeight
906  * @tc.desc: test function : GetStatusBarHeight
907  * @tc.type: FUNC
908  */
909 HWTEST_F(ExtensionSessionTest, GetStatusBarHeight, Function | SmallTest | Level1)
910 {
911     ASSERT_NE(nullptr, extSessionEventCallback_);
912     MockFunction<uint32_t()> mockGetStatusBarHeightFunc;
913     extSessionEventCallback_->getStatusBarHeightFunc_ = mockGetStatusBarHeightFunc.AsStdFunction();
914     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
915     EXPECT_CALL(mockGetStatusBarHeightFunc, Call()).Times(1);
916     extensionSession_->GetStatusBarHeight();
917 
918     extSessionEventCallback_->getStatusBarHeightFunc_ = nullptr;
919     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
920     EXPECT_CALL(mockGetStatusBarHeightFunc, Call()).Times(0);
921     extensionSession_->GetStatusBarHeight();
922 
923     extSessionEventCallback_ = nullptr;
924     extensionSession_->RegisterExtensionSessionEventCallback(extSessionEventCallback_);
925     EXPECT_CALL(mockGetStatusBarHeightFunc, Call()).Times(0);
926     extensionSession_->GetStatusBarHeight();
927 }
928 
929 /**
930  * @tc.name: TryUpdateExtensionPersistentId
931  * @tc.desc: test function : TryUpdateExtensionPersistentId
932  * @tc.type: FUNC
933  */
934 HWTEST_F(ExtensionSessionTest, TryUpdateExtensionPersistentId, Function | SmallTest | Level1)
935 {
936     SessionInfo info;
937     info.abilityName_ = "ExtensionSessionTest";
938     info.bundleName_ = "ExtensionSessionTest";
939     info.persistentId_ = INVALID_SESSION_ID;
940     sptr<ExtensionSession> extensionSessionA = sptr<ExtensionSession>::MakeSptr(info);
941     info.persistentId_ = extensionSessionA->GetPersistentId();
942     sptr<ExtensionSession> extensionSessionB = sptr<ExtensionSession>::MakeSptr(info);
943     ASSERT_EQ(info.persistentId_ + 1, extensionSessionB->GetPersistentId());
944     extensionSessionA.clear();
945     info.persistentId_ += 4096;
946     sptr<ExtensionSession> extensionSessionC = sptr<ExtensionSession>::MakeSptr(info);
947     ASSERT_EQ(info.persistentId_, extensionSessionC->GetPersistentId());
948 }
949 }
950 }
951 }