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 <pointer_event.h>
18 #include "session/host/include/main_session.h"
19 
20 #include "common/include/session_permission.h"
21 #include "key_event.h"
22 #include "mock/mock_session_stage.h"
23 #include "session/host/include/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 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace Rosen {
35 class MainSessionTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41     SessionInfo info;
42     sptr<MainSession::SpecificSessionCallback> specificCallback = nullptr;
43     sptr<MainSession> mainSession_;
44 private:
45     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
46 };
47 
SetUpTestCase()48 void MainSessionTest::SetUpTestCase()
49 {
50 }
51 
TearDownTestCase()52 void MainSessionTest::TearDownTestCase()
53 {
54 }
55 
SetUp()56 void MainSessionTest::SetUp()
57 {
58     SessionInfo info;
59     info.abilityName_ = "testMainSession1";
60     info.moduleName_ = "testMainSession2";
61     info.bundleName_ = "testMainSession3";
62     mainSession_ = new (std::nothrow) MainSession(info, specificCallback);
63     EXPECT_NE(nullptr, mainSession_);
64 }
65 
TearDown()66 void MainSessionTest::TearDown()
67 {
68     mainSession_ = nullptr;
69 }
70 
CreateRSSurfaceNode()71 RSSurfaceNode::SharedPtr MainSessionTest::CreateRSSurfaceNode()
72 {
73     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
74     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
75     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
76     return surfaceNode;
77 }
78 
79 namespace {
80 
81 /**
82  * @tc.name: MainSession01
83  * @tc.desc: check func MainSession
84  * @tc.type: FUNC
85  */
HWTEST_F(MainSessionTest, MainSession01, Function | SmallTest | Level1)86 HWTEST_F(MainSessionTest, MainSession01, Function | SmallTest | Level1)
87 {
88     MainSession* pMainSession = nullptr;
89     sptr<MainSession::SpecificSessionCallback> pSpecificCallback = nullptr;
90 
91     SessionInfo info;
92     info.persistentId_ = -1;
93     info.abilityName_ = "";
94     info.moduleName_ = "";
95     info.bundleName_ = "";
96     pMainSession = new (std::nothrow) MainSession(info, pSpecificCallback);
97     EXPECT_NE(nullptr, pMainSession);
98 
99     info.persistentId_ = 0;
100     pMainSession = new (std::nothrow) MainSession(info, pSpecificCallback);
101     EXPECT_NE(nullptr, pMainSession);
102 
103     info.persistentId_ = -1;
104     info.abilityName_ = "MainSession01";
105     info.moduleName_ = "MainSession02";
106     info.bundleName_ = "MainSession03";
107     pSpecificCallback = new(std::nothrow) MainSession::SpecificSessionCallback;
108     pMainSession = new (std::nothrow) MainSession(info, pSpecificCallback);
109     EXPECT_NE(nullptr, pMainSession);
110 
111     info.persistentId_ = 0;
112     pMainSession = new (std::nothrow) MainSession(info, pSpecificCallback);
113     EXPECT_NE(nullptr, pMainSession);
114 }
115 
116 /**
117  * @tc.name: TransferKeyEvent01
118  * @tc.desc: check func TransferKeyEvent
119  * @tc.type: FUNC
120  */
HWTEST_F(MainSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)121 HWTEST_F(MainSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)
122 {
123     mainSession_->state_ = SessionState::STATE_END;
124 
125     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, mainSession_->TransferKeyEvent(nullptr));
126 }
127 
128 /**
129  * @tc.name: TransferKeyEvent02
130  * @tc.desc: check func TransferKeyEvent
131  * @tc.type: FUNC
132  */
HWTEST_F(MainSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)133 HWTEST_F(MainSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)
134 {
135     mainSession_->state_ = SessionState::STATE_CONNECT;
136     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
137     mainSession_->ClearDialogVector();
138     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, mainSession_->TransferKeyEvent(keyEvent));
139 }
140 
141 /**
142  * @tc.name: TransferKeyEvent03
143  * @tc.desc: check func TransferKeyEvent
144  * @tc.type: FUNC
145  */
HWTEST_F(MainSessionTest, TransferKeyEvent03, Function | SmallTest | Level1)146 HWTEST_F(MainSessionTest, TransferKeyEvent03, Function | SmallTest | Level1)
147 {
148     mainSession_->state_ = SessionState::STATE_CONNECT;
149     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
150     ASSERT_NE(keyEvent, nullptr);
151     SessionInfo info;
152     info.abilityName_ = "testDialogSession1";
153     info.moduleName_ = "testDialogSession2";
154     info.bundleName_ = "testDialogSession3";
155     sptr<Session> dialogSession = new (std::nothrow) SystemSession(info, nullptr);
156     dialogSession->SetSessionState(SessionState::STATE_ACTIVE);
157     mainSession_->BindDialogToParentSession(dialogSession);
158 
159     ASSERT_EQ(WSError::WS_ERROR_INVALID_PERMISSION, mainSession_->TransferKeyEvent(keyEvent));
160 }
161 
162 
163 /**
164  * @tc.name: ProcessPointDownSession01
165  * @tc.desc: check func ProcessPointDownSession
166  * @tc.type: FUNC
167  */
HWTEST_F(MainSessionTest, ProcessPointDownSession01, Function | SmallTest | Level1)168 HWTEST_F(MainSessionTest, ProcessPointDownSession01, Function | SmallTest | Level1)
169 {
170     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(100, 200));
171     mainSession_->ClearDialogVector();
172     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(10, 20));
173 }
174 /**
175  * @tc.name: ProcessPointDownSession02
176  * @tc.desc: check func ProcessPointDownSession
177  * @tc.type: FUNC
178  */
HWTEST_F(MainSessionTest, ProcessPointDownSession02, Function | SmallTest | Level1)179 HWTEST_F(MainSessionTest, ProcessPointDownSession02, Function | SmallTest | Level1)
180 {
181     mainSession_->BindDialogToParentSession(mainSession_);
182     EXPECT_EQ(WSError::WS_OK, mainSession_->ProcessPointDownSession(10, 20));
183 }
184 
185 /**
186  * @tc.name: SetTopmost01
187  * @tc.desc: check func SetTopmost
188  * @tc.type: FUNC
189  */
HWTEST_F(MainSessionTest, SetTopmost01, Function | SmallTest | Level1)190 HWTEST_F(MainSessionTest, SetTopmost01, Function | SmallTest | Level1)
191 {
192     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(true));
193     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(false));
194 }
195 /**
196  * @tc.name: SetTopmost02
197  * @tc.desc: check func SetTopmost
198  * @tc.type: FUNC
199  */
HWTEST_F(MainSessionTest, SetTopmost02, Function | SmallTest | Level1)200 HWTEST_F(MainSessionTest, SetTopmost02, Function | SmallTest | Level1)
201 {
202     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
203     mainSession_->SetSessionProperty(property);
204     ASSERT_TRUE(mainSession_->GetSessionProperty() != nullptr);
205     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(true));
206 
207     sptr<SceneSession::SessionChangeCallback> sessionChangeCallback = new SceneSession::SessionChangeCallback();
208     ASSERT_TRUE(sessionChangeCallback != nullptr);
209 
210     mainSession_->RegisterSessionChangeCallback(sessionChangeCallback);
211     EXPECT_EQ(WSError::WS_OK, mainSession_->SetTopmost(false));
212 }
213 
214 /**
215  * @tc.name: UpdatePointerArea01
216  * @tc.desc: check func UpdatePointerArea
217  * @tc.type: FUNC
218  */
HWTEST_F(MainSessionTest, UpdatePointerArea01, Function | SmallTest | Level1)219 HWTEST_F(MainSessionTest, UpdatePointerArea01, Function | SmallTest | Level1)
220 {
221     WSRect Rect={0, 0, 50, 50};
222     mainSession_->UpdateWindowMode(WindowMode::WINDOW_MODE_FLOATING);
223     mainSession_->UpdatePointerArea(Rect);
224     mainSession_->UpdateWindowMode(WindowMode::WINDOW_MODE_UNDEFINED);
225     mainSession_->UpdatePointerArea(Rect);
226 }
227 
228 /**
229  * @tc.name: CheckPointerEventDispatch03
230  * @tc.desc: check func CheckPointerEventDispatch
231  * @tc.type: FUNC
232  */
HWTEST_F(MainSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)233 HWTEST_F(MainSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)
234 {
235     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
236 
237     mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
238     mainSession_->CheckPointerEventDispatch(pointerEvent);
239 
240     mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
241     mainSession_->CheckPointerEventDispatch(pointerEvent);
242 
243     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
244     mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
245     mainSession_->CheckPointerEventDispatch(pointerEvent);
246 
247     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_DOWN);
248     mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
249     mainSession_->CheckPointerEventDispatch(pointerEvent);
250 }
251 
252 /**
253  * @tc.name: RectCheck03
254  * @tc.desc: check func RectCheck
255  * @tc.type: FUNC
256  */
HWTEST_F(MainSessionTest, RectCheck03, Function | SmallTest | Level1)257 HWTEST_F(MainSessionTest, RectCheck03, Function | SmallTest | Level1)
258 {
259     ASSERT_NE(mainSession_, nullptr);
260     SessionInfo info;
261     info.abilityName_ = "testMainSessionRectCheck";
262     info.moduleName_ = "testMainSessionRectCheck";
263     info.bundleName_ = "testMainSessionRectCheck";
264     sptr<Session> session = new (std::nothrow) Session(info);
265     EXPECT_NE(nullptr, session);
266     mainSession_->parentSession_ = session;
267     uint32_t curWidth = 100;
268     uint32_t curHeight = 200;
269     mainSession_->RectCheck(curWidth, curHeight);
270 
271     curWidth = 300;
272     curHeight = 200;
273     mainSession_->RectCheck(curWidth, curHeight);
274 
275     curWidth = 1930;
276     curHeight = 200;
277     mainSession_->RectCheck(curWidth, curHeight);
278 
279     curWidth = 330;
280     curHeight = 200;
281     mainSession_->RectCheck(curWidth, curHeight);
282 
283     curWidth = 330;
284     curHeight = 1930;
285     mainSession_->RectCheck(curWidth, curHeight);
286 }
287 
288 /**
289  * @tc.name: SetExitSplitOnBackground
290  * @tc.desc: check func SetExitSplitOnBackground
291  * @tc.type: FUNC
292  */
HWTEST_F(MainSessionTest, SetExitSplitOnBackground, Function | SmallTest | Level1)293 HWTEST_F(MainSessionTest, SetExitSplitOnBackground, Function | SmallTest | Level1)
294 {
295     bool isExitSplitOnBackground = true;
296     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
297     ASSERT_EQ(true, isExitSplitOnBackground);
298 }
299 
300 /**
301  * @tc.name: IsExitSplitOnBackground01
302  * @tc.desc: check func IsExitSplitOnBackground
303  * @tc.type: FUNC
304  */
HWTEST_F(MainSessionTest, IsExitSplitOnBackground01, Function | SmallTest | Level1)305 HWTEST_F(MainSessionTest, IsExitSplitOnBackground01, Function | SmallTest | Level1)
306 {
307     bool isExitSplitOnBackground = true;
308     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
309     bool ret = mainSession_->IsExitSplitOnBackground();
310     ASSERT_EQ(true, ret);
311 }
312 
313 /**
314  * @tc.name: IsExitSplitOnBackground02
315  * @tc.desc: check func IsExitSplitOnBackground
316  * @tc.type: FUNC
317  */
HWTEST_F(MainSessionTest, IsExitSplitOnBackground02, Function | SmallTest | Level1)318 HWTEST_F(MainSessionTest, IsExitSplitOnBackground02, Function | SmallTest | Level1)
319 {
320     bool isExitSplitOnBackground = false;
321     mainSession_->SetExitSplitOnBackground(isExitSplitOnBackground);
322     bool ret = mainSession_->IsExitSplitOnBackground();
323     ASSERT_EQ(false, ret);
324 }
325 /**
326  * @tc.name: NotifyClientToUpdateInteractive01
327  * @tc.desc: check func NotifyClientToUpdateInteractive
328  * @tc.type: FUNC
329  */
HWTEST_F(MainSessionTest, NotifyClientToUpdateInteractive01, Function | SmallTest | Level1)330 HWTEST_F(MainSessionTest, NotifyClientToUpdateInteractive01, Function | SmallTest | Level1)
331 {
332     mainSession_->NotifyClientToUpdateInteractive(true);
333     ASSERT_TRUE(true); // exec success
334 }
335 /**
336  * @tc.name: NotifyClientToUpdateInteractive02
337  * @tc.desc: check func NotifyClientToUpdateInteractive
338  * @tc.type: FUNC
339  */
HWTEST_F(MainSessionTest, NotifyClientToUpdateInteractive02, Function | SmallTest | Level1)340 HWTEST_F(MainSessionTest, NotifyClientToUpdateInteractive02, Function | SmallTest | Level1)
341 {
342     auto surfaceNode = CreateRSSurfaceNode();
343     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
344     ASSERT_NE(nullptr, property);
345     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
346     EXPECT_NE(nullptr, mockSessionStage);
347     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
348     EXPECT_NE(nullptr, testWindowEventChannel);
349 
350     auto result = mainSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
351     ASSERT_EQ(result, WSError::WS_OK);
352 
353     mainSession_->NotifyClientToUpdateInteractive(true);
354     ASSERT_TRUE(true); // exec success
355 
356     mainSession_->SetSessionState(SessionState::STATE_ACTIVE);
357     mainSession_->NotifyClientToUpdateInteractive(true);
358     ASSERT_TRUE(true); // exec success
359 
360     mainSession_->SetSessionState(SessionState::STATE_FOREGROUND);
361     mainSession_->NotifyClientToUpdateInteractive(true);
362     ASSERT_TRUE(true); // exec success
363 
364     mainSession_->SetSessionState(SessionState::STATE_DISCONNECT);
365     bool visible = mainSession_->UpdateVisibilityInner(true);
366     ASSERT_TRUE(visible);
367     mainSession_->NotifyClientToUpdateInteractive(true);
368     ASSERT_TRUE(true); // exec success
369 }
370 
371 /**
372  * @tc.name: OnRestoreMainWindow
373  * @tc.desc: OnRestoreMainWindow function01
374  * @tc.type: FUNC
375  */
HWTEST_F(MainSessionTest, OnRestoreMainWindow, Function | SmallTest | Level2)376 HWTEST_F(MainSessionTest, OnRestoreMainWindow, Function | SmallTest | Level2)
377 {
378     SessionInfo info;
379     info.abilityName_ = "OnRestoreMainWindow";
380     info.bundleName_ = "OnRestoreMainWindow";
381     sptr<SceneSession> session = sptr<SceneSession>::MakeSptr(info, nullptr);
382     EXPECT_NE(session, nullptr);
383     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
384 
385     sptr<SceneSession::SessionChangeCallback> sessionChangeCallback =
386         sptr<SceneSession::SessionChangeCallback>::MakeSptr();
387     session->RegisterSessionChangeCallback(sessionChangeCallback);
388     sessionChangeCallback->onRestoreMainWindowFunc_ = nullptr;
389     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
390 
391     NotifyRestoreMainWindowFunc func = []() {
392         return;
393     };
394     sessionChangeCallback->onRestoreMainWindowFunc_ = func;
395     EXPECT_EQ(WSError::WS_OK, session->OnRestoreMainWindow());
396 }
397 
398 }
399 }
400 }