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/scb_system_session.h"
18 #include "common/include/session_permission.h"
19 #include "key_event.h"
20 #include "mock/mock_session_stage.h"
21 #include "session/host/include/session.h"
22 #include "window_helper.h"
23 #include "window_manager_hilog.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Rosen {
30 class SCBSystemSessionTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36     SessionInfo info;
37     sptr<SCBSystemSession::SpecificSessionCallback> specificCallback = nullptr;
38     sptr<SCBSystemSession> scbSystemSession_;
39 };
40 
SetUpTestCase()41 void SCBSystemSessionTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void SCBSystemSessionTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void SCBSystemSessionTest::SetUp()
50 {
51     SessionInfo info;
52     info.abilityName_ = "testSCBSystemSession1";
53     info.moduleName_ = "testSCBSystemSession2";
54     info.bundleName_ = "testSCBSystemSession3";
55     scbSystemSession_ = new (std::nothrow) SCBSystemSession(info, specificCallback);
56     EXPECT_NE(nullptr, scbSystemSession_);
57 }
58 
TearDown()59 void SCBSystemSessionTest::TearDown()
60 {
61     scbSystemSession_ = nullptr;
62 }
63 
64 namespace {
65 /**
66  * @tc.name: NotifyClientToUpdateRect01
67  * @tc.desc: check func NotifyClientToUpdateRect
68  * @tc.type: FUNC
69  */
HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level1)70 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level1)
71 {
72     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
73     ASSERT_NE(mockSessionStage, nullptr);
74     scbSystemSession_->sessionStage_ = mockSessionStage;
75     auto ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
76     ASSERT_EQ(WSError::WS_OK, ret);
77 }
78 
79 /**
80  * @tc.name: TransferKeyEvent01
81  * @tc.desc: check func TransferKeyEvent
82  * @tc.type: FUNC
83  */
HWTEST_F(SCBSystemSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)84 HWTEST_F(SCBSystemSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)
85 {
86     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, scbSystemSession_->TransferKeyEvent(nullptr));
87 }
88 
89 /**
90  * @tc.name: TransferKeyEvent02
91  * @tc.desc: check func TransferKeyEvent
92  * @tc.type: FUNC
93  */
HWTEST_F(SCBSystemSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)94 HWTEST_F(SCBSystemSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)
95 {
96     scbSystemSession_->state_ = SessionState::STATE_CONNECT;
97     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
98     ASSERT_NE(keyEvent, nullptr);
99     scbSystemSession_->windowEventChannel_ = nullptr;
100 
101     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, scbSystemSession_->TransferKeyEvent(keyEvent));
102 }
103 
104 /**
105  * @tc.name: UpdateFocus01
106  * @tc.desc: check func UpdateFocus
107  * @tc.type: FUNC
108  */
HWTEST_F(SCBSystemSessionTest, UpdateFocus01, Function | SmallTest | Level1)109 HWTEST_F(SCBSystemSessionTest, UpdateFocus01, Function | SmallTest | Level1)
110 {
111     bool isFocused = scbSystemSession_->isFocused_;
112 
113     ASSERT_EQ(WSError::WS_DO_NOTHING, scbSystemSession_->UpdateFocus(isFocused));
114 }
115 
116 /**
117  * @tc.name: UpdateFocus02
118  * @tc.desc: check func UpdateFocus
119  * @tc.type: FUNC
120  */
HWTEST_F(SCBSystemSessionTest, UpdateFocus02, Function | SmallTest | Level1)121 HWTEST_F(SCBSystemSessionTest, UpdateFocus02, Function | SmallTest | Level1)
122 {
123     scbSystemSession_->isFocused_ = true;
124     bool isFocused = scbSystemSession_->isFocused_;
125 
126     ASSERT_EQ(WSError::WS_OK, scbSystemSession_->UpdateFocus(!isFocused));
127 
128     scbSystemSession_->isFocused_ = false;
129     isFocused = scbSystemSession_->isFocused_;
130 
131     ASSERT_EQ(WSError::WS_OK, scbSystemSession_->UpdateFocus(!isFocused));
132 }
133 
134 /**
135  * @tc.name: UpdateWindowMode
136  * @tc.desc: check func UpdateWindowMode
137  * @tc.type: FUNC
138  */
HWTEST_F(SCBSystemSessionTest, UpdateWindowMode, Function | SmallTest | Level1)139 HWTEST_F(SCBSystemSessionTest, UpdateWindowMode, Function | SmallTest | Level1)
140 {
141     scbSystemSession_->PresentFocusIfPointDown();
142     scbSystemSession_->PresentFoucusIfNeed(2);
143     ASSERT_EQ(WSError::WS_OK, scbSystemSession_->SetSystemSceneBlockingFocus(true));
144     WSRect rect = {0, 0, 0, 0};
145     scbSystemSession_->UpdatePointerArea(rect);
146     auto ret = scbSystemSession_->UpdateWindowMode(WindowMode::WINDOW_MODE_UNDEFINED);
147     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, ret);
148 }
149 
150 /**
151  * @tc.name: BindKeyboardSession01
152  * @tc.desc: check func BindKeyboardSession
153  * @tc.type: FUNC
154  */
HWTEST_F(SCBSystemSessionTest, BindKeyboardSession01, Function | SmallTest | Level3)155 HWTEST_F(SCBSystemSessionTest, BindKeyboardSession01, Function | SmallTest | Level3)
156 {
157     ASSERT_NE(nullptr, scbSystemSession_);
158     scbSystemSession_->BindKeyboardSession(nullptr);
159 }
160 
161 /**
162  * @tc.name: BindKeyboardSession02
163  * @tc.desc: check func BindKeyboardSession
164  * @tc.type: FUNC
165  */
HWTEST_F(SCBSystemSessionTest, BindKeyboardSession02, Function | SmallTest | Level3)166 HWTEST_F(SCBSystemSessionTest, BindKeyboardSession02, Function | SmallTest | Level3)
167 {
168     ASSERT_NE(nullptr, scbSystemSession_);
169     SessionInfo info;
170     info.bundleName_ = "IntentionEventManager";
171     info.moduleName_ = "InputEventListener";
172     info.isSystem_ = true;
173     sptr<SceneSession::SpecificSessionCallback> callback =
174         new SceneSession::SpecificSessionCallback();
175     sptr<SceneSession> session = new SceneSession(info, callback);
176     scbSystemSession_->BindKeyboardSession(session);
177 }
178 
179 /**
180  * @tc.name: NotifyClientToUpdateRect
181  * @tc.desc: check func NotifyClientToUpdateRect
182  * @tc.type: FUNC
183  */
HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect, Function | SmallTest | Level3)184 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect, Function | SmallTest | Level3)
185 {
186     ASSERT_NE(nullptr, scbSystemSession_);
187     scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
188 }
189 
190 /**
191  * @tc.name: SetSystemSceneBlockingFocus
192  * @tc.desc: check func SetSystemSceneBlockingFocus
193  * @tc.type: FUNC
194  */
HWTEST_F(SCBSystemSessionTest, SetSystemSceneBlockingFocus01, Function | SmallTest | Level3)195 HWTEST_F(SCBSystemSessionTest, SetSystemSceneBlockingFocus01, Function | SmallTest | Level3)
196 {
197     ASSERT_NE(nullptr, scbSystemSession_);
198     WSError ret = scbSystemSession_->SetSystemSceneBlockingFocus(true);
199     ASSERT_EQ(WSError::WS_OK, ret);
200 }
201 
202 /**
203  * @tc.name: PresentFocusIfPointDown
204  * @tc.desc: check func PresentFocusIfPointDown
205  * @tc.type: FUNC
206  */
HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown01, Function | SmallTest | Level3)207 HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown01, Function | SmallTest | Level3)
208 {
209     ASSERT_NE(nullptr, scbSystemSession_);
210     scbSystemSession_->PresentFocusIfPointDown();
211 }
212 
213 /**
214  * @tc.name: GetKeyboardSession
215  * @tc.desc: check func GetKeyboardSession
216  * @tc.type: FUNC
217  */
HWTEST_F(SCBSystemSessionTest, GetKeyboardSession01, Function | SmallTest | Level3)218 HWTEST_F(SCBSystemSessionTest, GetKeyboardSession01, Function | SmallTest | Level3)
219 {
220     ASSERT_NE(nullptr, scbSystemSession_);
221     scbSystemSession_->GetKeyboardSession();
222 }
223 
224 /**
225  * @tc.name: ProcessPointDownSession
226  * @tc.desc: check func ProcessPointDownSession
227  * @tc.type: FUNC
228  */
HWTEST_F(SCBSystemSessionTest, ProcessPointDownSession, Function | SmallTest | Level3)229 HWTEST_F(SCBSystemSessionTest, ProcessPointDownSession, Function | SmallTest | Level3)
230 {
231     int32_t posX = 0;
232     int32_t posY = 0;
233     WSError ret = scbSystemSession_->ProcessPointDownSession(posX, posY);
234     ASSERT_EQ(WSError::WS_OK, ret);
235 }
236 
237 /**
238  * @tc.name: NotifyClientToUpdateRect02
239  * @tc.desc: check func NotifyClientToUpdateRect
240  * @tc.type: FUNC
241  */
HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level3)242 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level3)
243 {
244     sptr<SCBSystemSession::SpecificSessionCallback> specificCallback1 =
245         new (std::nothrow) SCBSystemSession::SpecificSessionCallback();
246     ASSERT_NE(specificCallback1, nullptr);
247     sptr<SCBSystemSession> scbSystemSession = new (std::nothrow) SCBSystemSession(info, specificCallback1);
248     ASSERT_NE(scbSystemSession, nullptr);
249     UpdateAvoidAreaCallback onUpdateAvoidArea;
250     ClearDisplayStatusBarTemporarilyFlags onClearDisplayStatusBarTemporarilyFlags;
251     scbSystemSession->specificCallback_ = specificCallback1;
252     scbSystemSession->specificCallback_->onUpdateAvoidArea_ = onUpdateAvoidArea;
253     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ =
254         onClearDisplayStatusBarTemporarilyFlags;
255     auto ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
256     ASSERT_EQ(WSError::WS_OK, ret);
257 
258     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ = nullptr;
259     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
260     ASSERT_EQ(WSError::WS_OK, ret);
261 
262     scbSystemSession->specificCallback_->onUpdateAvoidArea_ = nullptr;
263     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
264     ASSERT_EQ(WSError::WS_OK, ret);
265 
266     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ =
267         onClearDisplayStatusBarTemporarilyFlags;
268     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
269     ASSERT_EQ(WSError::WS_OK, ret);
270 
271     scbSystemSession->specificCallback_ = nullptr;
272     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
273     ASSERT_EQ(WSError::WS_OK, ret);
274 }
275 
276 /**
277  * @tc.name: NotifyClientToUpdateRect03
278  * @tc.desc: check func NotifyClientToUpdateRect
279  * @tc.type: FUNC
280  */
HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect03, Function | SmallTest | Level1)281 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect03, Function | SmallTest | Level1)
282 {
283     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
284     ASSERT_NE(property, nullptr);
285     KeyboardPanelRectUpdateCallback keyboardPanelRectUpdateCallback;
286     property->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
287 
288     auto ret = scbSystemSession_->SetSessionProperty(property);
289     ASSERT_EQ(WSError::WS_OK, ret);
290     scbSystemSession_->keyboardPanelRectUpdateCallback_ = keyboardPanelRectUpdateCallback;
291     scbSystemSession_->isKeyboardPanelEnabled_ = true;
292     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
293     ASSERT_EQ(WSError::WS_OK, ret);
294 
295     scbSystemSession_->isKeyboardPanelEnabled_ = false;
296     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
297     ASSERT_EQ(WSError::WS_OK, ret);
298 
299     scbSystemSession_->keyboardPanelRectUpdateCallback_ = nullptr;
300     scbSystemSession_->isKeyboardPanelEnabled_ = true;
301     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
302     ASSERT_EQ(WSError::WS_OK, ret);
303 
304     scbSystemSession_->isKeyboardPanelEnabled_ = false;
305     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
306     ASSERT_EQ(WSError::WS_OK, ret);
307 
308     property->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
309     ret = scbSystemSession_->SetSessionProperty(property);
310     ASSERT_EQ(WSError::WS_OK, ret);
311     scbSystemSession_->keyboardPanelRectUpdateCallback_ = keyboardPanelRectUpdateCallback;
312     scbSystemSession_->isKeyboardPanelEnabled_ = true;
313     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
314     ASSERT_EQ(WSError::WS_OK, ret);
315 
316     scbSystemSession_->isKeyboardPanelEnabled_ = false;
317     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
318     ASSERT_EQ(WSError::WS_OK, ret);
319 
320     scbSystemSession_->keyboardPanelRectUpdateCallback_ = nullptr;
321     scbSystemSession_->isKeyboardPanelEnabled_ = true;
322     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
323     ASSERT_EQ(WSError::WS_OK, ret);
324 
325     scbSystemSession_->isKeyboardPanelEnabled_ = false;
326     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
327     ASSERT_EQ(WSError::WS_OK, ret);
328 
329     scbSystemSession_->reason_ = SizeChangeReason::DRAG;
330     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
331     ASSERT_EQ(WSError::WS_OK, ret);
332 }
333 
334 /**
335  * @tc.name: PresentFocusIfPointDown02
336  * @tc.desc: check func PresentFocusIfPointDown
337  * @tc.type: FUNC
338  */
HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown02, Function | SmallTest | Level3)339 HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown02, Function | SmallTest | Level3)
340 {
341     scbSystemSession_->isFocused_ = true;
342     auto ret = scbSystemSession_->SetFocusable(false);
343     ASSERT_EQ(WSError::WS_OK, ret);
344     scbSystemSession_->PresentFocusIfPointDown();
345     scbSystemSession_->PresentFoucusIfNeed(2);
346     ASSERT_EQ(scbSystemSession_->isFocused_, true);
347 
348     scbSystemSession_->isFocused_ = false;
349     ret = scbSystemSession_->SetFocusable(false);
350     ASSERT_EQ(WSError::WS_OK, ret);
351     scbSystemSession_->PresentFocusIfPointDown();
352     scbSystemSession_->PresentFoucusIfNeed(2);
353     ASSERT_EQ(scbSystemSession_->isFocused_, false);
354 
355     scbSystemSession_->isFocused_ = true;
356     ret = scbSystemSession_->SetFocusable(true);
357     ASSERT_EQ(WSError::WS_OK, ret);
358     scbSystemSession_->PresentFocusIfPointDown();
359     scbSystemSession_->PresentFoucusIfNeed(2);
360     ASSERT_EQ(scbSystemSession_->isFocused_, true);
361 }
362 
363 /**
364  * @tc.name: PresentFoucusIfNeed
365  * @tc.desc: check func PresentFoucusIfNeed
366  * @tc.type: FUNC
367  */
HWTEST_F(SCBSystemSessionTest, PresentFoucusIfNeed, Function | SmallTest | Level3)368 HWTEST_F(SCBSystemSessionTest, PresentFoucusIfNeed, Function | SmallTest | Level3)
369 {
370     int32_t pointerAction = 8;
371     scbSystemSession_->PresentFoucusIfNeed(pointerAction);
372     ASSERT_EQ(pointerAction, 8);
373 
374     pointerAction = 100;
375     scbSystemSession_->PresentFoucusIfNeed(pointerAction);
376     ASSERT_EQ(pointerAction, 100);
377 }
378 } //namespace
379 } //namespace Rosen
380 } //namespace OHOS