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  */
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  */
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  */
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  */
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  */
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  */
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: RegisterBufferAvailableCallback
152  * @tc.desc: check func RegisterBufferAvailableCallback
153  * @tc.type: FUNC
154  */
155 HWTEST_F(SCBSystemSessionTest, RegisterBufferAvailableCallback, Function | SmallTest | Level3)
156 {
157     ASSERT_NE(scbSystemSession_, nullptr);
__anon9f19e3f00202() 158     scbSystemSession_->RegisterBufferAvailableCallback([]() {});
159 }
160 
161 /**
162  * @tc.name: BindKeyboardSession01
163  * @tc.desc: check func BindKeyboardSession
164  * @tc.type: FUNC
165  */
166 HWTEST_F(SCBSystemSessionTest, BindKeyboardSession01, Function | SmallTest | Level3)
167 {
168     ASSERT_NE(nullptr, scbSystemSession_);
169     scbSystemSession_->BindKeyboardSession(nullptr);
170 }
171 
172 /**
173  * @tc.name: BindKeyboardSession02
174  * @tc.desc: check func BindKeyboardSession
175  * @tc.type: FUNC
176  */
177 HWTEST_F(SCBSystemSessionTest, BindKeyboardSession02, Function | SmallTest | Level3)
178 {
179     ASSERT_NE(nullptr, scbSystemSession_);
180     SessionInfo info;
181     info.bundleName_ = "IntentionEventManager";
182     info.moduleName_ = "InputEventListener";
183     info.isSystem_ = true;
184     sptr<SceneSession::SpecificSessionCallback> callback =
185         new SceneSession::SpecificSessionCallback();
186     sptr<SceneSession> session = new SceneSession(info, callback);
187     scbSystemSession_->BindKeyboardSession(session);
188 }
189 
190 /**
191  * @tc.name: NotifyClientToUpdateRect
192  * @tc.desc: check func NotifyClientToUpdateRect
193  * @tc.type: FUNC
194  */
195 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect, Function | SmallTest | Level3)
196 {
197     ASSERT_NE(nullptr, scbSystemSession_);
198     scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
199 }
200 
201 /**
202  * @tc.name: SetSystemSceneBlockingFocus
203  * @tc.desc: check func SetSystemSceneBlockingFocus
204  * @tc.type: FUNC
205  */
206 HWTEST_F(SCBSystemSessionTest, SetSystemSceneBlockingFocus01, Function | SmallTest | Level3)
207 {
208     ASSERT_NE(nullptr, scbSystemSession_);
209     WSError ret = scbSystemSession_->SetSystemSceneBlockingFocus(true);
210     ASSERT_EQ(WSError::WS_OK, ret);
211 }
212 
213 /**
214  * @tc.name: PresentFocusIfPointDown
215  * @tc.desc: check func PresentFocusIfPointDown
216  * @tc.type: FUNC
217  */
218 HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown01, Function | SmallTest | Level3)
219 {
220     ASSERT_NE(nullptr, scbSystemSession_);
221     scbSystemSession_->PresentFocusIfPointDown();
222 }
223 
224 /**
225  * @tc.name: GetKeyboardSession
226  * @tc.desc: check func GetKeyboardSession
227  * @tc.type: FUNC
228  */
229 HWTEST_F(SCBSystemSessionTest, GetKeyboardSession01, Function | SmallTest | Level3)
230 {
231     ASSERT_NE(nullptr, scbSystemSession_);
232     scbSystemSession_->GetKeyboardSession();
233 }
234 
235 /**
236  * @tc.name: ProcessPointDownSession
237  * @tc.desc: check func ProcessPointDownSession
238  * @tc.type: FUNC
239  */
240 HWTEST_F(SCBSystemSessionTest, ProcessPointDownSession, Function | SmallTest | Level3)
241 {
242     int32_t posX = 0;
243     int32_t posY = 0;
244     WSError ret = scbSystemSession_->ProcessPointDownSession(posX, posY);
245     ASSERT_EQ(WSError::WS_OK, ret);
246 }
247 
248 /**
249  * @tc.name: NotifyClientToUpdateRect02
250  * @tc.desc: check func NotifyClientToUpdateRect
251  * @tc.type: FUNC
252  */
253 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level3)
254 {
255     sptr<SCBSystemSession::SpecificSessionCallback> specificCallback1 =
256         new (std::nothrow) SCBSystemSession::SpecificSessionCallback();
257     ASSERT_NE(specificCallback1, nullptr);
258     sptr<SCBSystemSession> scbSystemSession = new (std::nothrow) SCBSystemSession(info, specificCallback1);
259     ASSERT_NE(scbSystemSession, nullptr);
260     UpdateAvoidAreaCallback onUpdateAvoidArea;
261     ClearDisplayStatusBarTemporarilyFlags onClearDisplayStatusBarTemporarilyFlags;
262     scbSystemSession->specificCallback_ = specificCallback1;
263     scbSystemSession->specificCallback_->onUpdateAvoidArea_ = onUpdateAvoidArea;
264     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ =
265         onClearDisplayStatusBarTemporarilyFlags;
266     auto ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
267     ASSERT_EQ(WSError::WS_OK, ret);
268 
269     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ = nullptr;
270     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
271     ASSERT_EQ(WSError::WS_OK, ret);
272 
273     scbSystemSession->specificCallback_->onUpdateAvoidArea_ = nullptr;
274     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
275     ASSERT_EQ(WSError::WS_OK, ret);
276 
277     scbSystemSession->specificCallback_->onClearDisplayStatusBarTemporarilyFlags_ =
278         onClearDisplayStatusBarTemporarilyFlags;
279     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
280     ASSERT_EQ(WSError::WS_OK, ret);
281 
282     scbSystemSession->specificCallback_ = nullptr;
283     ret = scbSystemSession->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
284     ASSERT_EQ(WSError::WS_OK, ret);
285 }
286 
287 /**
288  * @tc.name: NotifyClientToUpdateRect03
289  * @tc.desc: check func NotifyClientToUpdateRect
290  * @tc.type: FUNC
291  */
292 HWTEST_F(SCBSystemSessionTest, NotifyClientToUpdateRect03, Function | SmallTest | Level1)
293 {
294     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
295     ASSERT_NE(property, nullptr);
296     KeyboardPanelRectUpdateCallback keyboardPanelRectUpdateCallback;
297     property->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
298 
299     auto ret = scbSystemSession_->SetSessionProperty(property);
300     ASSERT_EQ(WSError::WS_OK, ret);
301     scbSystemSession_->keyboardPanelRectUpdateCallback_ = keyboardPanelRectUpdateCallback;
302     scbSystemSession_->isKeyboardPanelEnabled_ = true;
303     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
304     ASSERT_EQ(WSError::WS_OK, ret);
305 
306     scbSystemSession_->isKeyboardPanelEnabled_ = false;
307     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
308     ASSERT_EQ(WSError::WS_OK, ret);
309 
310     scbSystemSession_->keyboardPanelRectUpdateCallback_ = nullptr;
311     scbSystemSession_->isKeyboardPanelEnabled_ = true;
312     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
313     ASSERT_EQ(WSError::WS_OK, ret);
314 
315     scbSystemSession_->isKeyboardPanelEnabled_ = false;
316     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
317     ASSERT_EQ(WSError::WS_OK, ret);
318 
319     property->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
320     ret = scbSystemSession_->SetSessionProperty(property);
321     ASSERT_EQ(WSError::WS_OK, ret);
322     scbSystemSession_->keyboardPanelRectUpdateCallback_ = keyboardPanelRectUpdateCallback;
323     scbSystemSession_->isKeyboardPanelEnabled_ = true;
324     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
325     ASSERT_EQ(WSError::WS_OK, ret);
326 
327     scbSystemSession_->isKeyboardPanelEnabled_ = false;
328     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
329     ASSERT_EQ(WSError::WS_OK, ret);
330 
331     scbSystemSession_->keyboardPanelRectUpdateCallback_ = nullptr;
332     scbSystemSession_->isKeyboardPanelEnabled_ = true;
333     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
334     ASSERT_EQ(WSError::WS_OK, ret);
335 
336     scbSystemSession_->isKeyboardPanelEnabled_ = false;
337     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
338     ASSERT_EQ(WSError::WS_OK, ret);
339 
340     scbSystemSession_->reason_ = SizeChangeReason::DRAG;
341     ret = scbSystemSession_->NotifyClientToUpdateRect("SCBSystemSessionTest", nullptr);
342     ASSERT_EQ(WSError::WS_OK, ret);
343 }
344 
345 /**
346  * @tc.name: PresentFocusIfPointDown02
347  * @tc.desc: check func PresentFocusIfPointDown
348  * @tc.type: FUNC
349  */
350 HWTEST_F(SCBSystemSessionTest, PresentFocusIfPointDown02, Function | SmallTest | Level3)
351 {
352     scbSystemSession_->isFocused_ = true;
353     auto ret = scbSystemSession_->SetFocusable(false);
354     ASSERT_EQ(WSError::WS_OK, ret);
355     scbSystemSession_->PresentFocusIfPointDown();
356     scbSystemSession_->PresentFoucusIfNeed(2);
357     ASSERT_EQ(scbSystemSession_->isFocused_, true);
358 
359     scbSystemSession_->isFocused_ = false;
360     ret = scbSystemSession_->SetFocusable(false);
361     ASSERT_EQ(WSError::WS_OK, ret);
362     scbSystemSession_->PresentFocusIfPointDown();
363     scbSystemSession_->PresentFoucusIfNeed(2);
364     ASSERT_EQ(scbSystemSession_->isFocused_, false);
365 
366     scbSystemSession_->isFocused_ = true;
367     ret = scbSystemSession_->SetFocusable(true);
368     ASSERT_EQ(WSError::WS_OK, ret);
369     scbSystemSession_->PresentFocusIfPointDown();
370     scbSystemSession_->PresentFoucusIfNeed(2);
371     ASSERT_EQ(scbSystemSession_->isFocused_, true);
372 }
373 
374 /**
375  * @tc.name: PresentFoucusIfNeed
376  * @tc.desc: check func PresentFoucusIfNeed
377  * @tc.type: FUNC
378  */
379 HWTEST_F(SCBSystemSessionTest, PresentFoucusIfNeed, Function | SmallTest | Level3)
380 {
381     int32_t pointerAction = 8;
382     scbSystemSession_->PresentFoucusIfNeed(pointerAction);
383     ASSERT_EQ(pointerAction, 8);
384 
385     pointerAction = 100;
386     scbSystemSession_->PresentFoucusIfNeed(pointerAction);
387     ASSERT_EQ(pointerAction, 100);
388 }
389 } //namespace
390 } //namespace Rosen
391 } //namespace OHOS