1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "session/host/include/sub_session.h"
18
19 #include "common/include/session_permission.h"
20 #include "key_event.h"
21 #include "mock/mock_session_stage.h"
22 #include "session/host/include/session.h"
23 #include "session/host/include/main_session.h"
24 #include "session/host/include/system_session.h"
25 #include <ui/rs_surface_node.h>
26 #include "window_event_channel_base.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 #include "window_property.h"
30 #include "window_session_property.h"
31 #include "mock_sub_session.h"
32
33 using namespace testing;
34 using namespace testing::ext;
35
36 namespace OHOS {
37 namespace Rosen {
38 class SubSessionTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44 SessionInfo info;
45 sptr<SubSession::SpecificSessionCallback> specificCallback = nullptr;
46 private:
47 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
48 sptr<SubSession> subSession_;
49 SystemSessionConfig systemConfig_;
50 };
51
SetUpTestCase()52 void SubSessionTest::SetUpTestCase()
53 {
54 }
55
TearDownTestCase()56 void SubSessionTest::TearDownTestCase()
57 {
58 }
59
SetUp()60 void SubSessionTest::SetUp()
61 {
62 SessionInfo info;
63 info.abilityName_ = "testMainSession1";
64 info.moduleName_ = "testMainSession2";
65 info.bundleName_ = "testMainSession3";
66 subSession_ = sptr<SubSession>::MakeSptr(info, specificCallback);
67 EXPECT_NE(nullptr, subSession_);
68 }
69
TearDown()70 void SubSessionTest::TearDown()
71 {
72 subSession_ = nullptr;
73 }
74
CreateRSSurfaceNode()75 RSSurfaceNode::SharedPtr SubSessionTest::CreateRSSurfaceNode()
76 {
77 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
78 rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
79 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
80 return surfaceNode;
81 }
82
83 namespace {
84
85 /**
86 * @tc.name: TransferKeyEvent01
87 * @tc.desc: check func TransferKeyEvent
88 * @tc.type: FUNC
89 */
90 HWTEST_F(SubSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)
91 {
92 subSession_->state_ = SessionState::STATE_END;
93
94 ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, subSession_->TransferKeyEvent(nullptr));
95 }
96
97 /**
98 * @tc.name: TransferKeyEvent02
99 * @tc.desc: check func TransferKeyEvent
100 * @tc.type: FUNC
101 */
102 HWTEST_F(SubSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)
103 {
104 subSession_->state_ = SessionState::STATE_CONNECT;
105 std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
106
107 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
108 }
109
110 /**
111 * @tc.name: TransferKeyEvent03
112 * @tc.desc: check func TransferKeyEvent
113 * @tc.type: FUNC
114 */
115 HWTEST_F(SubSessionTest, TransferKeyEvent03, Function | SmallTest | Level1)
116 {
117 ASSERT_NE(subSession_, nullptr);
118 subSession_->state_ = SessionState::STATE_CONNECT;
119 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
120 ASSERT_NE(keyEvent, nullptr);
121 subSession_->SetParentSession(nullptr);
122 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
123 }
124
125 /**
126 * @tc.name: TransferKeyEvent04
127 * @tc.desc: check func TransferKeyEvent
128 * @tc.type: FUNC
129 */
130 HWTEST_F(SubSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)
131 {
132 SessionInfo sessionInfo;
133 sessionInfo.abilityName_ = "TransferKeyEvent04";
134 sessionInfo.moduleName_ = "TransferKeyEvent04";
135 sessionInfo.bundleName_ = "TransferKeyEvent04";
136 sptr<SubSession> session = sptr<SubSession>::MakeSptr(sessionInfo, specificCallback);
137 ASSERT_NE(session, nullptr);
138 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
139 ASSERT_NE(keyEvent, nullptr);
140
141 subSession_->SetParentSession(session);
142 subSession_->SetSessionState(SessionState::STATE_ACTIVE);
143 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
144 }
145
146 /**
147 * @tc.name: IsTopmost01
148 * @tc.desc: check func IsTopmost
149 * @tc.type: FUNC
150 */
151 HWTEST_F(SubSessionTest, IsTopmost01, Function | SmallTest | Level1)
152 {
153 subSession_->GetSessionProperty()->SetTopmost(false);
154 ASSERT_EQ(false, subSession_->IsTopmost());
155
156 subSession_->GetSessionProperty()->SetTopmost(true);
157 ASSERT_EQ(true, subSession_->IsTopmost());
158 }
159
160 /**
161 * @tc.name: IsTopmost02
162 * @tc.desc: check func IsTopmost
163 * @tc.type: FUNC
164 */
165 HWTEST_F(SubSessionTest, IsTopmost02, Function | SmallTest | Level1)
166 {
167 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
168 subSession_->SetSessionProperty(property);
169 ASSERT_TRUE(subSession_->GetSessionProperty() != nullptr);
170
171 subSession_->GetSessionProperty()->SetTopmost(true);
172 ASSERT_EQ(true, subSession_->IsTopmost());
173 }
174
175 /**
176 * @tc.name: CheckPointerEventDispatch01
177 * @tc.desc: check func CheckPointerEventDispatch
178 * @tc.type: FUNC
179 */
180 HWTEST_F(SubSessionTest, CheckPointerEventDispatch01, Function | SmallTest | Level1)
181 {
182 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
183 ASSERT_NE(nullptr, pointerEvent);
184 systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
185
186 ASSERT_TRUE(subSession_ != nullptr);
187 auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
188 ASSERT_TRUE(result);
189 }
190
191 /**
192 * @tc.name: CheckPointerEventDispatch02
193 * @tc.desc: check func CheckPointerEventDispatch
194 * @tc.type: FUNC
195 */
196 HWTEST_F(SubSessionTest, CheckPointerEventDispatch02, Function | SmallTest | Level1)
197 {
198 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
199 ASSERT_NE(nullptr, pointerEvent);
200 systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
201
202 ASSERT_TRUE(subSession_ != nullptr);
203 subSession_->SetSessionState(SessionState::STATE_FOREGROUND);
204 auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
205 ASSERT_TRUE(result);
206 }
207
208 /**
209 * @tc.name: CheckPointerEventDispatch03
210 * @tc.desc: check func CheckPointerEventDispatch
211 * @tc.type: FUNC
212 */
213 HWTEST_F(SubSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)
214 {
215 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
216 ASSERT_NE(nullptr, pointerEvent);
217 systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
218
219 ASSERT_TRUE(subSession_ != nullptr);
220 subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
221 subSession_->UpdateSessionState(SessionState::STATE_ACTIVE);
222 auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
223 ASSERT_TRUE(result);
224 }
225
226 /**
227 * @tc.name: CheckPointerEventDispatch04
228 * @tc.desc: check func CheckPointerEventDispatch
229 * @tc.type: FUNC
230 */
231 HWTEST_F(SubSessionTest, CheckPointerEventDispatch04, Function | SmallTest | Level1)
232 {
233 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
234 ASSERT_NE(nullptr, pointerEvent);
235 systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
236
237 ASSERT_TRUE(subSession_ != nullptr);
238 subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
239 subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
240 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
241 auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
242 ASSERT_TRUE(result);
243 }
244
245 /**
246 * @tc.name: CheckPointerEventDispatch05
247 * @tc.desc: check func CheckPointerEventDispatch
248 * @tc.type: FUNC
249 */
250 HWTEST_F(SubSessionTest, CheckPointerEventDispatch05, Function | SmallTest | Level1)
251 {
252 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
253 ASSERT_NE(nullptr, pointerEvent);
254 systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
255
256 ASSERT_TRUE(subSession_ != nullptr);
257 subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
258 subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
259 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
260 auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
261 ASSERT_TRUE(result);
262 }
263
264 /**
265 * @tc.name: IsModal01
266 * @tc.desc: check func IsModal
267 * @tc.type: FUNC
268 */
269 HWTEST_F(SubSessionTest, IsModal, Function | SmallTest | Level1)
270 {
271 ASSERT_FALSE(subSession_->IsModal());
272 }
273
274 /**
275 * @tc.name: RectCheck
276 * @tc.desc: test function : RectCheck
277 * @tc.type: FUNC
278 */
279 HWTEST_F(SubSessionTest, RectCheck, Function | SmallTest | Level1)
280 {
281 ASSERT_NE(subSession_, nullptr);
282 SessionInfo info;
283 info.abilityName_ = "testRectCheck";
284 info.moduleName_ = "testRectCheck";
285 info.bundleName_ = "testRectCheck";
286 sptr<Session> session = sptr<Session>::MakeSptr(info);
287 EXPECT_NE(nullptr, session);
288 subSession_->parentSession_ = session;
289 uint32_t curWidth = 100;
290 uint32_t curHeight = 200;
291 subSession_->RectCheck(curWidth, curHeight);
292
293 curWidth = 300;
294 curHeight = 200;
295 subSession_->RectCheck(curWidth, curHeight);
296
297 curWidth = 1930;
298 curHeight = 200;
299 subSession_->RectCheck(curWidth, curHeight);
300
301 curWidth = 330;
302 curHeight = 200;
303 subSession_->RectCheck(curWidth, curHeight);
304
305 curWidth = 330;
306 curHeight = 1930;
307 subSession_->RectCheck(curWidth, curHeight);
308 }
309
310 /**
311 * @tc.name: IsModal
312 * @tc.desc: IsModal function01
313 * @tc.type: FUNC
314 */
315 HWTEST_F(SubSessionTest, IsModal01, Function | SmallTest | Level2)
316 {
317 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
318 property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
319 property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
320 EXPECT_EQ(subSession_->IsModal(), false);
321 subSession_->SetSessionProperty(property);
322 EXPECT_EQ(subSession_->IsModal(), true);
323 }
324
325 /**
326 * @tc.name: IsApplicationModal
327 * @tc.desc: IsApplicationModal function01
328 * @tc.type: FUNC
329 */
330 HWTEST_F(SubSessionTest, IsApplicationModal, Function | SmallTest | Level2)
331 {
332 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
333 property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
334 property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
335 property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL);
336 EXPECT_EQ(subSession_->IsApplicationModal(), false);
337 subSession_->SetSessionProperty(property);
338 EXPECT_EQ(subSession_->IsApplicationModal(), true);
339 }
340
341 /**
342 * @tc.name: NotifySessionRectChange
343 * @tc.desc: NotifySessionRectChange function01
344 * @tc.type: FUNC
345 */
346 HWTEST_F(SubSessionTest, NotifySessionRectChange01, Function | SmallTest | Level2)
347 {
348 subSession_->shouldFollowParentWhenShow_ = true;
349 WSRect rect;
350 subSession_->NotifySessionRectChange(rect, SizeChangeReason::UNDEFINED, DISPLAY_ID_INVALID);
351 ASSERT_EQ(subSession_->shouldFollowParentWhenShow_, true);
352 subSession_->NotifySessionRectChange(rect, SizeChangeReason::DRAG_END, DISPLAY_ID_INVALID);
353 ASSERT_EQ(subSession_->shouldFollowParentWhenShow_, false);
354 }
355
356 /**
357 * @tc.name: UpdateSessionRectInner
358 * @tc.desc: UpdateSessionRectInner function01
359 * @tc.type: FUNC
360 */
361 HWTEST_F(SubSessionTest, UpdateSessionRectInner01, Function | SmallTest | Level2)
362 {
363 subSession_->shouldFollowParentWhenShow_ = true;
364 WSRect rect;
365 MoveConfiguration config;
366 config.displayId = DISPLAY_ID_INVALID;
367 subSession_->UpdateSessionRectInner(rect, SizeChangeReason::UNDEFINED, config);
368 ASSERT_EQ(subSession_->shouldFollowParentWhenShow_, true);
369 config.displayId = 123;
370 subSession_->UpdateSessionRectInner(rect, SizeChangeReason::DRAG_END, config);
371 ASSERT_EQ(subSession_->shouldFollowParentWhenShow_, false);
372 }
373
374 /**
375 * @tc.name: IsVisibleForeground
376 * @tc.desc: IsVisibleForeground Test
377 * @tc.type: FUNC
378 */
379 HWTEST_F(SubSessionTest, IsVisibleForeground, Function | SmallTest | Level2)
380 {
381 systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
382 subSession_->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
383 EXPECT_EQ(subSession_->IsVisibleForeground(), false);
384 subSession_->SetSessionState(SessionState::STATE_FOREGROUND);
385 EXPECT_EQ(subSession_->IsVisibleForeground(), false);
386 subSession_->isVisible_ = true;
387 EXPECT_EQ(subSession_->IsVisibleForeground(), true);
388
389 SessionInfo info;
390 info.abilityName_ = "IsVisibleForeground";
391 info.moduleName_ = "IsVisibleForeground";
392 info.bundleName_ = "IsVisibleForeground";
393 sptr<Session> parentSession = sptr<Session>::MakeSptr(info);
394 parentSession->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
395 subSession_->SetParentSession(parentSession);
396 EXPECT_EQ(subSession_->IsVisibleForeground(), false);
397 parentSession->SetSessionState(SessionState::STATE_FOREGROUND);
398 EXPECT_EQ(subSession_->IsVisibleForeground(), false);
399 parentSession->isVisible_ = true;
400 EXPECT_EQ(subSession_->IsVisibleForeground(), true);
401 }
402
403 /**
404 * @tc.name: NotifyFollowParentMultiScreenPolicy
405 * @tc.desc: NotifyFollowParentMultiScreenPolicy
406 * @tc.type: FUNC
407 */
408 HWTEST_F(SubSessionTest, NotifyFollowParentMultiScreenPolicy, Function | SmallTest | Level2)
409 {
410 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
411 property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
412 subSession_->SetSessionProperty(property);
413 EXPECT_EQ(subSession_->NotifyFollowParentMultiScreenPolicy(true), WSError::WS_OK);
414 EXPECT_EQ(subSession_->NotifyFollowParentMultiScreenPolicy(false), WSError::WS_OK);
415 }
416
417 /**
418 * @tc.name: IsFollowParentMultiScreenPolicy
419 * @tc.desc: IsFollowParentMultiScreenPolicy
420 * @tc.type: FUNC
421 */
422 HWTEST_F(SubSessionTest, IsFollowParentMultiScreenPolicy, Function | SmallTest | Level2)
423 {
424 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
425 property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
426 subSession_->SetSessionProperty(property);
427 EXPECT_EQ(subSession_->IsFollowParentMultiScreenPolicy(), false);
428 EXPECT_EQ(subSession_->NotifyFollowParentMultiScreenPolicy(true), WSError::WS_OK);
429 EXPECT_EQ(subSession_->IsFollowParentMultiScreenPolicy(), true);
430 EXPECT_EQ(subSession_->NotifyFollowParentMultiScreenPolicy(false), WSError::WS_OK);
431 EXPECT_EQ(subSession_->IsFollowParentMultiScreenPolicy(), false);
432 }
433
434 /**
435 * @tc.name: SetParentSessionCallback
436 * @tc.desc: SetParentSessionCallback
437 * @tc.type: FUNC
438 */
439 HWTEST_F(SubSessionTest, SetParentSessionCallback, Function | SmallTest | Level2)
440 {
441 SessionInfo info;
442 info.abilityName_ = "SetParentSessionCallback";
443 info.bundleName_ = "SetParentSessionCallback";
444 sptr<SubSession> subSession = sptr<SubSession>::MakeSptr(info, nullptr);
445 subSession->SetParentSessionCallback(nullptr);
446 EXPECT_EQ(subSession->setParentSessionFunc_, nullptr);
447
__anondc91f2c60202(int32_t oldParentWindowId, int32_t newParentWindowId) 448 NotifySetParentSessionFunc func = [](int32_t oldParentWindowId, int32_t newParentWindowId) {
449 return;
450 };
451 subSession->SetParentSessionCallback(std::move(func));
452 EXPECT_NE(subSession->setParentSessionFunc_, nullptr);
453 }
454
455 /**
456 * @tc.name: NotifySetParentSession
457 * @tc.desc: NotifySetParentSession
458 * @tc.type: FUNC
459 */
460 HWTEST_F(SubSessionTest, NotifySetParentSession, Function | SmallTest | Level2)
461 {
462 SessionInfo info;
463 info.abilityName_ = "NotifySetParentSession";
464 info.bundleName_ = "NotifySetParentSession";
465 sptr<SubSession> subSession = sptr<SubSession>::MakeSptr(info, nullptr);
466 int32_t oldParentWindowId = 1;
467 int32_t newParentWindowId = 2;
468 auto res = subSession->NotifySetParentSession(oldParentWindowId, newParentWindowId);
469 EXPECT_EQ(res, WMError::WM_OK);
470
__anondc91f2c60302(int32_t oldParentWindowId, int32_t newParentWindowId) 471 NotifySetParentSessionFunc func = [](int32_t oldParentWindowId, int32_t newParentWindowId) {
472 return;
473 };
474 subSession->SetParentSessionCallback(std::move(func));
475 res = subSession->NotifySetParentSession(oldParentWindowId, newParentWindowId);
476 EXPECT_EQ(res, WMError::WM_OK);
477 }
478 }
479 }
480 }