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 "session/host/include/keyboard_session.h"
17 #include <gtest/gtest.h>
18 #include <parameters.h>
19 
20 #include "common/include/session_permission.h"
21 #include "interfaces/include/ws_common.h"
22 #include "mock/mock_session_stage.h"
23 #include "mock/mock_keyboard_session.h"
24 #include "session/host/include/session.h"
25 #include "screen_session_manager_client/include/screen_session_manager_client.h"
26 #include "session/host/include/scene_session.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 namespace {
36 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "KeyboardSessionTest"};
37 }
38 
39 constexpr int WAIT_ASYNC_US = 1000000;
40 class KeyboardSessionTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp() override;
45     void TearDown() override;
46 private:
47     sptr<KeyboardSession> GetKeyboardSession(const std::string& abilityName, const std::string& bundleName);
48     sptr<SceneSession> GetSceneSession(const std::string& abilityName, const std::string& bundleName);
49     sptr<KSSceneSessionMocker> GetSceneSessionMocker(const std::string& abilityName, const std::string& bundleName);
50 };
51 
SetUpTestCase()52 void KeyboardSessionTest::SetUpTestCase()
53 {
54 }
55 
TearDownTestCase()56 void KeyboardSessionTest::TearDownTestCase()
57 {
58 }
59 
SetUp()60 void KeyboardSessionTest::SetUp()
61 {
62 }
63 
TearDown()64 void KeyboardSessionTest::TearDown()
65 {
66 }
67 
GetKeyboardSession(const std::string& abilityName, const std::string& bundleName)68 sptr<KeyboardSession> KeyboardSessionTest::GetKeyboardSession(const std::string& abilityName,
69     const std::string& bundleName)
70 {
71     SessionInfo info;
72     info.abilityName_ = abilityName;
73     info.bundleName_ = bundleName;
74     sptr<SceneSession::SpecificSessionCallback> specificCb =
75         new (std::nothrow) SceneSession::SpecificSessionCallback();
76     EXPECT_NE(specificCb, nullptr);
77     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
78         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
79     EXPECT_NE(keyboardCb, nullptr);
80     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
81     EXPECT_NE(keyboardSession, nullptr);
82 
83     sptr<WindowSessionProperty> keyboardProperty = new (std::nothrow) WindowSessionProperty();
84     EXPECT_NE(keyboardProperty, nullptr);
85     keyboardProperty->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
86     keyboardSession->SetSessionProperty(keyboardProperty);
87 
88     return keyboardSession;
89 }
90 
GetSceneSession(const std::string& abilityName, const std::string& bundleName)91 sptr<SceneSession> KeyboardSessionTest::GetSceneSession(const std::string& abilityName,
92     const std::string& bundleName)
93 {
94     SessionInfo info;
95     info.abilityName_ = abilityName;
96     info.bundleName_ = bundleName;
97     sptr<SceneSession::SpecificSessionCallback> specificCb =
98         new (std::nothrow) SceneSession::SpecificSessionCallback();
99     EXPECT_NE(specificCb, nullptr);
100     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
101 
102     return sceneSession;
103 }
104 
GetSceneSessionMocker(const std::string& abilityName, const std::string& bundleName)105 sptr<KSSceneSessionMocker> KeyboardSessionTest::GetSceneSessionMocker(const std::string& abilityName,
106     const std::string& bundleName)
107 {
108     SessionInfo info;
109     info.abilityName_ = abilityName;
110     info.bundleName_ = bundleName;
111     sptr<SceneSession::SpecificSessionCallback> specificCb =
112         new (std::nothrow) SceneSession::SpecificSessionCallback();
113     EXPECT_NE(specificCb, nullptr);
114     sptr<KSSceneSessionMocker> mockSession = sptr<KSSceneSessionMocker>::MakeSptr(info, nullptr);
115 
116     return mockSession;
117 }
118 
119 namespace {
120 /**
121  * @tc.name: Show
122  * @tc.desc: test function : Show
123  * @tc.type: FUNC
124  */
HWTEST_F(KeyboardSessionTest, GetKeyboardGravity, Function | SmallTest | Level1)125 HWTEST_F(KeyboardSessionTest, GetKeyboardGravity, Function | SmallTest | Level1)
126 {
127     SessionInfo info;
128     info.abilityName_ = "GetKeyboardGravity";
129     info.bundleName_ = "GetKeyboardGravity";
130     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
131     ASSERT_TRUE((keyboardSession != nullptr));
132     ASSERT_EQ(SessionGravity::SESSION_GRAVITY_DEFAULT, keyboardSession->GetKeyboardGravity());
133 
134     sptr<SceneSession::SpecificSessionCallback> specificCb =
135         new (std::nothrow) SceneSession::SpecificSessionCallback();
136     EXPECT_NE(specificCb, nullptr);
137     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, specificCb);
138     sptr<WindowSessionProperty> windowSessionProperty = new (std::nothrow) WindowSessionProperty();
139     EXPECT_NE(windowSessionProperty, nullptr);
140     sceneSession->property_ = windowSessionProperty;
141     keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, nullptr);
142     ASSERT_EQ(SessionGravity::SESSION_GRAVITY_DEFAULT, keyboardSession->GetKeyboardGravity());
143 }
144 
145 /**
146  * @tc.name: Show01
147  * @tc.desc: test function : Show
148  * @tc.type: FUNC
149  */
HWTEST_F(KeyboardSessionTest, Show01, Function | SmallTest | Level1)150 HWTEST_F(KeyboardSessionTest, Show01, Function | SmallTest | Level1)
151 {
152     SessionInfo info;
153     info.abilityName_ = "Show01";
154     info.bundleName_ = "Show01";
155     sptr<SceneSession::SpecificSessionCallback> specificCb =
156         new (std::nothrow) SceneSession::SpecificSessionCallback();
157     EXPECT_NE(specificCb, nullptr);
158     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
159         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
160     EXPECT_NE(keyboardCb, nullptr);
161     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
162     ASSERT_TRUE((keyboardSession != nullptr));
163     sptr<WindowSessionProperty> property = new WindowSessionProperty();
164     ASSERT_NE(nullptr, property);
165 
166     keyboardSession->isKeyboardPanelEnabled_ = true;
167     ASSERT_EQ(WSError::WS_OK, keyboardSession->Show(property));
168 
169     keyboardSession->isKeyboardPanelEnabled_ = false;
170     ASSERT_EQ(WSError::WS_OK, keyboardSession->Show(property));
171 }
172 
173 /**
174  * @tc.name: Show02
175  * @tc.desc: test function : Show
176  * @tc.type: FUNC
177  */
HWTEST_F(KeyboardSessionTest, Show02, Function | SmallTest | Level1)178 HWTEST_F(KeyboardSessionTest, Show02, Function | SmallTest | Level1)
179 {
180     SessionInfo info;
181     info.abilityName_ = "Show02";
182     info.bundleName_ = "Show02";
183     sptr<SceneSession::SpecificSessionCallback> specificCb =
184         new (std::nothrow) SceneSession::SpecificSessionCallback();
185     EXPECT_NE(specificCb, nullptr);
186     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
187         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
188     EXPECT_NE(keyboardCb, nullptr);
189     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
190     ASSERT_TRUE((keyboardSession != nullptr));
191     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, keyboardSession->Show(nullptr));
192 }
193 
194 /**
195  * @tc.name: Hide
196  * @tc.desc: test function : Hide
197  * @tc.type: FUNC
198  */
HWTEST_F(KeyboardSessionTest, Hide, Function | SmallTest | Level1)199 HWTEST_F(KeyboardSessionTest, Hide, Function | SmallTest | Level1)
200 {
201     SessionInfo info;
202     info.abilityName_ = "Hide";
203     info.bundleName_ = "Hide";
204     sptr<SceneSession::SpecificSessionCallback> specificCb =
205         new (std::nothrow) SceneSession::SpecificSessionCallback();
206     EXPECT_NE(specificCb, nullptr);
207     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
208         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
209     EXPECT_NE(keyboardCb, nullptr);
210     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
211     ASSERT_TRUE((keyboardSession != nullptr));
212 
213     ASSERT_EQ(WSError::WS_OK, keyboardSession->Hide());
214 }
215 
216 /**
217  * @tc.name: Disconnect
218  * @tc.desc: normal function
219  * @tc.type: FUNC
220  */
HWTEST_F(KeyboardSessionTest, Disconnect, Function | SmallTest | Level2)221 HWTEST_F(KeyboardSessionTest, Disconnect, Function | SmallTest | Level2)
222 {
223     SessionInfo info;
224     info.abilityName_ = "Disconnect";
225     info.bundleName_ = "Disconnect";
226     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
227     EXPECT_NE(keyboardSession, nullptr);
228     sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
229     property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
230     keyboardSession->SetSessionProperty(property);
231     keyboardSession->isActive_ = true;
232     auto result = keyboardSession->Disconnect();
233     ASSERT_EQ(result, WSError::WS_OK);
234 }
235 
236 /**
237  * @tc.name: NotifyClientToUpdateRect
238  * @tc.desc: NotifyClientToUpdateRect
239  * @tc.type: FUNC
240  */
HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect, Function | SmallTest | Level2)241 HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect, Function | SmallTest | Level2)
242 {
243     SessionInfo info;
244     info.abilityName_ = "NotifyClientToUpdateRect";
245     info.bundleName_ = "NotifyClientToUpdateRect";
246     sptr<SceneSession::SpecificSessionCallback> specificCb =
247         new (std::nothrow) SceneSession::SpecificSessionCallback();
248     EXPECT_NE(specificCb, nullptr);
249     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
250         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
251     EXPECT_NE(keyboardCb, nullptr);
252     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
253     EXPECT_NE(keyboardSession, nullptr);
254 
255     // NotifyClientToUpdateRectTask return not ok
256     WSError ret = keyboardSession->NotifyClientToUpdateRect("KeyboardSessionTest", nullptr);
257     ASSERT_EQ(ret, WSError::WS_OK);
258 
259     // NotifyClientToUpdateRectTask return ok and session->reason_ is UNDEFINED
260     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
261     ASSERT_NE(mockSessionStage, nullptr);
262     keyboardSession->sessionStage_ = mockSessionStage;
263     keyboardSession->state_ = SessionState::STATE_CONNECT;
264     ret = keyboardSession->NotifyClientToUpdateRect("KeyboardSessionTest", nullptr);
265     ASSERT_EQ(ret, WSError::WS_OK);
266 
267     // NotifyClientToUpdateRectTask return ok and session->reason_ is DRAG
268     keyboardSession->reason_ = SizeChangeReason::DRAG;
269     keyboardSession->dirtyFlags_ |= static_cast<uint32_t>(SessionUIDirtyFlag::RECT);
270     ret = keyboardSession->NotifyClientToUpdateRect("KeyboardSessionTest", nullptr);
271     ASSERT_EQ(ret, WSError::WS_OK);
272 }
273 
274 /**
275  * @tc.name: NotifyClientToUpdateRect01
276  * @tc.desc: NotifyClientToUpdateRect
277  * @tc.type: FUNC
278  */
HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level2)279 HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect01, Function | SmallTest | Level2)
280 {
281     SessionInfo info;
282     info.abilityName_ = "NotifyClientToUpdateRect01";
283     info.bundleName_ = "NotifyClientToUpdateRect01";
284     sptr<SceneSession::SpecificSessionCallback> specificCb =
285         new (std::nothrow) SceneSession::SpecificSessionCallback();
286     EXPECT_NE(specificCb, nullptr);
287     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
288         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
289     EXPECT_NE(keyboardCb, nullptr);
290     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
291     EXPECT_NE(keyboardSession, nullptr);
292     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
293     ASSERT_NE(mockSessionStage, nullptr);
294     keyboardSession->dirtyFlags_ |= static_cast<uint32_t>(SessionUIDirtyFlag::RECT);
295     keyboardSession->sessionStage_ = mockSessionStage;
296     auto ret = keyboardSession->NotifyClientToUpdateRect("KeyboardSessionTest", nullptr);
297     ASSERT_EQ(ret, WSError::WS_OK);
298 }
299 
300 /**
301  * @tc.name: NotifyClientToUpdateRect02
302  * @tc.desc: NotifyClientToUpdateRect
303  * @tc.type: FUNC
304  */
HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level2)305 HWTEST_F(KeyboardSessionTest, NotifyClientToUpdateRect02, Function | SmallTest | Level2)
306 {
307     SessionInfo info;
308     info.abilityName_ = "NotifyClientToUpdateRect02";
309     info.bundleName_ = "NotifyClientToUpdateRect02";
310     sptr<SceneSession::SpecificSessionCallback> specificCb =
311         new (std::nothrow) SceneSession::SpecificSessionCallback();
312     EXPECT_NE(specificCb, nullptr);
313     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
314         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
315     EXPECT_NE(keyboardCb, nullptr);
316     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
317     EXPECT_NE(keyboardSession, nullptr);
318     keyboardSession->dirtyFlags_ |= static_cast<uint32_t>(SessionUIDirtyFlag::RECT);
319     keyboardSession->reason_ = SizeChangeReason::MOVE;
320     keyboardSession->isKeyboardPanelEnabled_ = true;
321     sptr<WindowSessionProperty> windowSessionProperty = new (std::nothrow) WindowSessionProperty();
322     EXPECT_NE(windowSessionProperty, nullptr);
323     windowSessionProperty->SetWindowType(WindowType::WINDOW_TYPE_KEYBOARD_PANEL);
324     keyboardSession->SetSessionProperty(windowSessionProperty);
325     auto ret = keyboardSession->NotifyClientToUpdateRect("KeyboardSessionTest", nullptr);
326     ASSERT_EQ(ret, WSError::WS_OK);
327 }
328 
329 /**
330  * @tc.name: SetKeyboardSessionGravity
331  * @tc.desc: SetKeyboardSessionGravity
332  * @tc.type: FUNC
333  */
HWTEST_F(KeyboardSessionTest, SetKeyboardSessionGravity, Function | SmallTest | Level1)334 HWTEST_F(KeyboardSessionTest, SetKeyboardSessionGravity, Function | SmallTest | Level1)
335 {
336     SessionInfo info;
337     info.abilityName_ = "SetKeyboardSessionGravity";
338     info.bundleName_ = "SetKeyboardSessionGravity";
339     sptr<SceneSession::SpecificSessionCallback> specificCb =
340         new (std::nothrow) SceneSession::SpecificSessionCallback();
341     EXPECT_NE(specificCb, nullptr);
342     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
343         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
344     EXPECT_NE(keyboardCb, nullptr);
345     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
346     EXPECT_NE(keyboardSession, nullptr);
347 
348     auto ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
349     ASSERT_EQ(ret, WSError::WS_OK);
350 
351     sptr<SceneSession::SessionChangeCallback> sessionChangeCb =
352         new (std::nothrow) SceneSession::SessionChangeCallback();
353     EXPECT_NE(sessionChangeCb, nullptr);
354     keyboardSession->sessionChangeCallback_ = sessionChangeCb;
355     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
356     ASSERT_EQ(ret, WSError::WS_OK);
357 
358     keyboardSession->keyboardGravityChangeFunc_ = [](SessionGravity) {
359         return 0;
360     };
361     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
362     ASSERT_EQ(ret, WSError::WS_OK);
363 
364     keyboardSession->isKeyboardPanelEnabled_ = true;
365     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
366     ASSERT_EQ(ret, WSError::WS_OK);
367 
368     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
369     keyboardSession->isKeyboardPanelEnabled_ = false;
370     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
371     ASSERT_EQ(ret, WSError::WS_OK);
372 
373     keyboardSession->isKeyboardPanelEnabled_ = true;
374     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
375     ASSERT_EQ(ret, WSError::WS_OK);
376 
377     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
378     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_FLOAT, 0);
379     ASSERT_EQ(ret, WSError::WS_OK);
380 
381     keyboardSession->state_ = SessionState::STATE_DISCONNECT;
382     ret = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_FLOAT, 0);
383     ASSERT_EQ(ret, WSError::WS_OK);
384 }
385 
386 /**
387  * @tc.name: GetSceneSession01
388  * @tc.desc: GetSceneSession
389  * @tc.type: FUNC
390  */
HWTEST_F(KeyboardSessionTest, GetSceneSession01, Function | SmallTest | Level1)391 HWTEST_F(KeyboardSessionTest, GetSceneSession01, Function | SmallTest | Level1)
392 {
393     SessionInfo info;
394     info.abilityName_ = "GetSceneSession01";
395     info.bundleName_ = "GetSceneSession01";
396     sptr<SceneSession::SpecificSessionCallback> specificCb =
397         new (std::nothrow) SceneSession::SpecificSessionCallback();
398     EXPECT_NE(specificCb, nullptr);
399     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
400         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
401     EXPECT_NE(keyboardCb, nullptr);
402     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
403     EXPECT_NE(keyboardSession, nullptr);
404     info.windowType_ = 1;
405     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, specificCb);
406     EXPECT_NE(sceneSession, nullptr);
407     auto id = sceneSession->GetPersistentId();
408     EXPECT_NE(id, 0);
409     auto ret = keyboardSession->GetSceneSession(id);
410 
411     keyboardCb->onGetSceneSession_ = [](uint32_t) {
412         return nullptr;
413     };
414     EXPECT_NE(keyboardCb->onGetSceneSession_, nullptr);
415     ret = keyboardSession->GetSceneSession(id);
416 }
417 
418 /**
419  * @tc.name: NotifyOccupiedAreaChangeInfo
420  * @tc.desc: NotifyOccupiedAreaChangeInfo
421  * @tc.type: FUNC
422  */
HWTEST_F(KeyboardSessionTest, NotifyOccupiedAreaChangeInfo, Function | SmallTest | Level1)423 HWTEST_F(KeyboardSessionTest, NotifyOccupiedAreaChangeInfo, Function | SmallTest | Level1)
424 {
425     SessionInfo info;
426     info.abilityName_ = "NotifyOccupiedAreaChangeInfo";
427     info.bundleName_ = "NotifyOccupiedAreaChangeInfo";
428     sptr<SceneSession::SpecificSessionCallback> specificCb =
429         new (std::nothrow) SceneSession::SpecificSessionCallback();
430     EXPECT_NE(specificCb, nullptr);
431     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
432         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
433     EXPECT_NE(keyboardCb, nullptr);
434     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
435     EXPECT_NE(keyboardSession, nullptr);
436     sptr<SceneSession> callingSession = new (std::nothrow) SceneSession(info, nullptr);
437     WSRect rect = { 0, 0, 0, 0 };
438     WSRect occupiedArea = { 0, 0, 0, 0 };
439     keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
440 
441     WSRect lastSR = {1, 1, 1, 1};
442     callingSession->lastSafeRect = lastSR;
443     keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
444 
445     sptr<WindowSessionProperty> windowSessionProperty = new (std::nothrow) WindowSessionProperty();
446     EXPECT_NE(windowSessionProperty, nullptr);
447     keyboardSession->property_ = windowSessionProperty;
448     keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
449 }
450 
451 /**
452  * @tc.name: RestoreCallingSession
453  * @tc.desc: RestoreCallingSession
454  * @tc.type: FUNC
455  */
HWTEST_F(KeyboardSessionTest, RestoreCallingSession, Function | SmallTest | Level1)456 HWTEST_F(KeyboardSessionTest, RestoreCallingSession, Function | SmallTest | Level1)
457 {
458     SessionInfo info;
459     info.abilityName_ = "RestoreCallingSession";
460     info.bundleName_ = "RestoreCallingSession";
461     sptr<SceneSession::SpecificSessionCallback> specificCb =
462         new (std::nothrow) SceneSession::SpecificSessionCallback();
463     EXPECT_NE(specificCb, nullptr);
464     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
465         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
466     EXPECT_NE(keyboardCb, nullptr);
467     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
468     EXPECT_NE(keyboardSession, nullptr);
469 
470     // callingSession is nullptr
471     keyboardSession->RestoreCallingSession();
472 
473     // callingsession is not nullptr
474     info.windowType_ = 1; // 1 is main_window_type
475     sptr<SceneSession> callingSession = new (std::nothrow) SceneSession(info, specificCb);
476     EXPECT_NE(callingSession, nullptr);
477     ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
478     keyboardSession->keyboardCallback_->onGetSceneSession_ =
479         [callingSession](int32_t persistentId)->sptr<SceneSession> {
480         return callingSession;
481     };
482     keyboardSession->RestoreCallingSession();
483     ASSERT_EQ(callingSession->GetOriPosYBeforeRaisedByKeyboard(), 0); // 0: default value
484 
485     callingSession->SetOriPosYBeforeRaisedByKeyboard(100); // 100 is not default
486     ASSERT_NE(callingSession->property_, nullptr);
487     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
488     keyboardSession->RestoreCallingSession();
489     ASSERT_EQ(callingSession->GetOriPosYBeforeRaisedByKeyboard(), 0); // 0: default value
490 }
491 
492 /**
493  * @tc.name: UseFocusIdIfCallingSessionIdInvalid
494  * @tc.desc: UseFocusIdIfCallingSessionIdInvalid
495  * @tc.type: FUNC
496  */
HWTEST_F(KeyboardSessionTest, UseFocusIdIfCallingSessionIdInvalid, Function | SmallTest | Level1)497 HWTEST_F(KeyboardSessionTest, UseFocusIdIfCallingSessionIdInvalid, Function | SmallTest | Level1)
498 {
499     SessionInfo info;
500     info.abilityName_ = "UseFocusIdIfCallingSessionIdInvalid";
501     info.bundleName_ = "UseFocusIdIfCallingSessionIdInvalid";
502     sptr<SceneSession::SpecificSessionCallback> specificCb =
503         new (std::nothrow) SceneSession::SpecificSessionCallback();
504     EXPECT_NE(specificCb, nullptr);
505     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
506         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
507     EXPECT_NE(keyboardCb, nullptr);
508     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
509     EXPECT_NE(keyboardSession, nullptr);
510 
511     info.windowType_ = 1;
512     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, specificCb);
513     EXPECT_NE(sceneSession, nullptr);
514     auto id = sceneSession->GetPersistentId();
515     EXPECT_NE(id, 0);
516 
517     keyboardSession->GetSessionProperty()->SetCallingSessionId(id);
518     keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
519 }
520 
521 /**
522  * @tc.name: UpdateCallingSessionIdAndPosition
523  * @tc.desc: UpdateCallingSessionIdAndPosition
524  * @tc.type: FUNC
525  */
HWTEST_F(KeyboardSessionTest, UpdateCallingSessionIdAndPosition, Function | SmallTest | Level1)526 HWTEST_F(KeyboardSessionTest, UpdateCallingSessionIdAndPosition, Function | SmallTest | Level1)
527 {
528     SessionInfo info;
529     info.abilityName_ = "UpdateCallingSessionIdAndPosition";
530     info.bundleName_ = "UpdateCallingSessionIdAndPosition";
531     sptr<SceneSession::SpecificSessionCallback> specificCb =
532         new (std::nothrow) SceneSession::SpecificSessionCallback();
533     EXPECT_NE(specificCb, nullptr);
534     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
535         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
536     EXPECT_NE(keyboardCb, nullptr);
537     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
538     EXPECT_NE(keyboardSession, nullptr);
539 
540     info.windowType_ = 1;
541     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, specificCb);
542     EXPECT_NE(sceneSession, nullptr);
543     auto id = sceneSession->GetPersistentId();
544     EXPECT_NE(id, 0);
545 
546     keyboardSession->UpdateCallingSessionIdAndPosition(id);
547 }
548 
549 /**
550  * @tc.name: RelayoutKeyBoard
551  * @tc.desc: RelayoutKeyBoard
552  * @tc.type: FUNC
553  */
HWTEST_F(KeyboardSessionTest, RelayoutKeyBoard, Function | SmallTest | Level1)554 HWTEST_F(KeyboardSessionTest, RelayoutKeyBoard, Function | SmallTest | Level1)
555 {
556     SessionInfo info;
557     info.abilityName_ = "RelayoutKeyBoard";
558     info.bundleName_ = "RelayoutKeyBoard";
559     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
560     EXPECT_NE(keyboardSession, nullptr);
561 
562     keyboardSession->RelayoutKeyBoard();
563 }
564 
565 /**
566  * @tc.name: GetFocusedSessionId
567  * @tc.desc: GetFocusedSessionId
568  * @tc.type: FUNC
569  */
HWTEST_F(KeyboardSessionTest, GetFocusedSessionId, Function | SmallTest | Level1)570 HWTEST_F(KeyboardSessionTest, GetFocusedSessionId, Function | SmallTest | Level1)
571 {
572     SessionInfo info;
573     info.abilityName_ = "RelayoutKeyBoard";
574     info.bundleName_ = "RelayoutKeyBoard";
575     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
576         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
577     EXPECT_NE(keyboardCb, nullptr);
578     keyboardCb->onGetFocusedSessionId_ = []()
579     {
580         return 0;
581     };
582     EXPECT_NE(keyboardCb->onGetFocusedSessionId_, nullptr);
583     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, keyboardCb);
584     EXPECT_NE(keyboardSession, nullptr);
585     ASSERT_EQ(INVALID_WINDOW_ID, keyboardSession->GetFocusedSessionId());
586 
587     keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
588     EXPECT_NE(keyboardSession, nullptr);
589 
590     ASSERT_EQ(INVALID_WINDOW_ID, keyboardSession->GetFocusedSessionId());
591 }
592 
593 /**
594  * @tc.name: OnKeyboardPanelUpdated
595  * @tc.desc: OnKeyboardPanelUpdated
596  * @tc.type: FUNC
597  */
HWTEST_F(KeyboardSessionTest, OnKeyboardPanelUpdated, Function | SmallTest | Level1)598 HWTEST_F(KeyboardSessionTest, OnKeyboardPanelUpdated, Function | SmallTest | Level1)
599 {
600     WLOGFI("OnKeyboardPanelUpdated begin!");
601     int ret = 0;
602 
603     SessionInfo info;
604     info.abilityName_ = "OnKeyboardPanelUpdated";
605     info.bundleName_ = "OnKeyboardPanelUpdated";
606     sptr<SceneSession::SpecificSessionCallback> specificCb =
607         new (std::nothrow) SceneSession::SpecificSessionCallback();
608     EXPECT_NE(specificCb, nullptr);
609     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
610         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
611     EXPECT_NE(keyboardCb, nullptr);
612     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
613     EXPECT_NE(keyboardSession, nullptr);
614     keyboardSession->isKeyboardPanelEnabled_ = false;
615     keyboardSession->OnKeyboardPanelUpdated();
616 
617     keyboardSession->isKeyboardPanelEnabled_ = true;
618     keyboardSession->specificCallback_ = nullptr;
619     keyboardSession->OnKeyboardPanelUpdated();
620 
621     keyboardSession->specificCallback_ = specificCb;
622     auto onUpdateAvoidArea = specificCb->onUpdateAvoidArea_;
623     if (onUpdateAvoidArea == nullptr) {
624         onUpdateAvoidArea = [](const int32_t& id){};
625     }
626     specificCb->onUpdateAvoidArea_ = nullptr;
627     keyboardSession->OnKeyboardPanelUpdated();
628 
629     specificCb->onUpdateAvoidArea_ = onUpdateAvoidArea;
630     keyboardSession->OnKeyboardPanelUpdated();
631 
632     ASSERT_EQ(ret, 0);
633     WLOGFI("OnKeyboardPanelUpdated end!");
634 }
635 
636 /**
637  * @tc.name: SetCallingSessionId
638  * @tc.desc: SetCallingSessionId
639  * @tc.type: FUNC
640  */
HWTEST_F(KeyboardSessionTest, SetCallingSessionId, Function | SmallTest | Level1)641 HWTEST_F(KeyboardSessionTest, SetCallingSessionId, Function | SmallTest | Level1)
642 {
643     WLOGFI("SetCallingSessionId begin!");
644     SessionInfo info;
645     info.abilityName_ = "SetCallingSessionId";
646     info.bundleName_ = "SetCallingSessionId";
647     sptr<SceneSession::SpecificSessionCallback> specificCb =
648         new (std::nothrow) SceneSession::SpecificSessionCallback();
649     EXPECT_NE(specificCb, nullptr);
650     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
651         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
652     EXPECT_NE(keyboardCb, nullptr);
653     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
654     EXPECT_NE(keyboardSession, nullptr);
655 
656     // keyboardCallback_->onGetSceneSession_ is nullptr, getCallingSession is nullptr
657     keyboardSession->SetCallingSessionId(0);
658     ASSERT_EQ(keyboardSession->GetCallingSessionId(), INVALID_SESSION_ID);
659 
660     // getCallingSession is not nullptr
661     info.windowType_ = 1; // 1 is main_window_type
662     sptr<SceneSession> callingSession = new (std::nothrow) SceneSession(info, specificCb);
663     EXPECT_NE(callingSession, nullptr);
664     ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
665     keyboardSession->keyboardCallback_->onGetSceneSession_ =
666         [callingSession](int32_t persistenId)->sptr<SceneSession> {
667         if (persistenId != 100) { // callingSession's persistentId is 100
668             return nullptr;
669         }
670         return callingSession;
671     };
672     keyboardSession->keyboardCallback_->onGetFocusedSessionId_ = []()->int32_t {
673         return 100; // focusSession's persistentId is 100
674     };
675     keyboardSession->SetCallingSessionId(0);
676     ASSERT_EQ(keyboardSession->GetCallingSessionId(), 100); // 100 is callingSessionId
677 
678     ASSERT_NE(keyboardSession->property_, nullptr);
679     keyboardSession->property_->SetCallingSessionId(INVALID_SESSION_ID);
680     keyboardSession->keyboardCallback_->onCallingSessionIdChange_ = [](int32_t callingSessionid){};
681     keyboardSession->SetCallingSessionId(100);
682     ASSERT_EQ(keyboardSession->GetCallingSessionId(), 100); // 100 is callingSessionId
683 }
684 
685 /**
686  * @tc.name: GetCallingSessionId
687  * @tc.desc: GetCallingSessionId
688  * @tc.type: FUNC
689  */
HWTEST_F(KeyboardSessionTest, GetCallingSessionId, Function | SmallTest | Level1)690 HWTEST_F(KeyboardSessionTest, GetCallingSessionId, Function | SmallTest | Level1)
691 {
692     SessionInfo info;
693     info.abilityName_ = "GetCallingSessionId";
694     info.bundleName_ = "GetCallingSessionId";
695     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, nullptr, nullptr);
696     EXPECT_NE(keyboardSession, nullptr);
697     auto ret = keyboardSession->GetCallingSessionId();
698     ASSERT_EQ(ret, INVALID_WINDOW_ID);
699 }
700 
701 /**
702  * @tc.name: AdjustKeyboardLayout01
703  * @tc.desc: AdjustKeyboardLayout
704  * @tc.type: FUNC
705  */
HWTEST_F(KeyboardSessionTest, AdjustKeyboardLayout01, Function | SmallTest | Level1)706 HWTEST_F(KeyboardSessionTest, AdjustKeyboardLayout01, Function | SmallTest | Level1)
707 {
708     SessionInfo info;
709     info.abilityName_ = "AdjustKeyboardLayout01";
710     info.bundleName_ = "AdjustKeyboardLayout01";
711     sptr<SceneSession::SpecificSessionCallback> specificCb =
712         new (std::nothrow) SceneSession::SpecificSessionCallback();
713     EXPECT_NE(specificCb, nullptr);
714     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
715         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
716     EXPECT_NE(keyboardCb, nullptr);
717     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
718     EXPECT_NE(keyboardSession, nullptr);
719     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
720     property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
721     keyboardSession->SetSessionProperty(property);
722     keyboardSession->RegisterSessionChangeCallback(nullptr);
723 
724     KeyboardLayoutParams params;
725     ASSERT_EQ(keyboardSession->AdjustKeyboardLayout(params), WSError::WS_OK);
726 
727     sptr<SceneSession::SessionChangeCallback> sessionChangeCallback =
728         new (std::nothrow) SceneSession::SessionChangeCallback();
729     EXPECT_NE(sessionChangeCallback, nullptr);
730     keyboardSession->adjustKeyboardLayoutFunc_ = nullptr;
731     keyboardSession->RegisterSessionChangeCallback(sessionChangeCallback);
732     ASSERT_EQ(keyboardSession->AdjustKeyboardLayout(params), WSError::WS_OK);
733 
734     keyboardSession->adjustKeyboardLayoutFunc_ = [](const KeyboardLayoutParams& params){};
735     ASSERT_EQ(keyboardSession->AdjustKeyboardLayout(params), WSError::WS_OK);
736 }
737 
738 /**
739  * @tc.name: AdjustKeyboardLayout01
740  * @tc.desc: AdjustKeyboardLayout
741  * @tc.type: FUNC
742  */
HWTEST_F(KeyboardSessionTest, AdjustKeyboardLayout02, Function | SmallTest | Level1)743 HWTEST_F(KeyboardSessionTest, AdjustKeyboardLayout02, Function | SmallTest | Level1)
744 {
745     SessionInfo info;
746     info.abilityName_ = "AdjustKeyboardLayout02";
747     info.bundleName_ = "AdjustKeyboardLayout02";
748     sptr<SceneSession::SpecificSessionCallback> specificCb =
749         new (std::nothrow) SceneSession::SpecificSessionCallback();
750     EXPECT_NE(specificCb, nullptr);
751     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
752         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
753     EXPECT_NE(keyboardCb, nullptr);
754     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
755     EXPECT_NE(keyboardSession, nullptr);
756     keyboardSession->SetSessionProperty(nullptr);
757 
758     KeyboardLayoutParams params;
759     ASSERT_EQ(keyboardSession->AdjustKeyboardLayout(params), WSError::WS_OK);
760 }
761 
762 /**
763  * @tc.name: CheckIfNeedRaiseCallingSession
764  * @tc.desc: CheckIfNeedRaiseCallingSession
765  * @tc.type: FUNC
766  */
HWTEST_F(KeyboardSessionTest, CheckIfNeedRaiseCallingSession, Function | SmallTest | Level1)767 HWTEST_F(KeyboardSessionTest, CheckIfNeedRaiseCallingSession, Function | SmallTest | Level1)
768 {
769     WLOGFI("CheckIfNeedRaiseCallingSession begin!");
770     SessionInfo info;
771     info.abilityName_ = "CheckIfNeedRaiseCallingSession";
772     info.bundleName_ = "CheckIfNeedRaiseCallingSession";
773     sptr<SceneSession::SpecificSessionCallback> specificCb =
774         new (std::nothrow) SceneSession::SpecificSessionCallback();
775     EXPECT_NE(specificCb, nullptr);
776     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
777         new (std::nothrow) KeyboardSession::KeyboardSessionCallback();
778     EXPECT_NE(keyboardCb, nullptr);
779     sptr<KeyboardSession> keyboardSession = new (std::nothrow) KeyboardSession(info, specificCb, keyboardCb);
780     EXPECT_NE(keyboardSession, nullptr);
781     sptr<WindowSessionProperty> property = new(std::nothrow) WindowSessionProperty();
782     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
783     keyboardSession->SetSessionProperty(property);
784 
785     ASSERT_FALSE(keyboardSession->CheckIfNeedRaiseCallingSession(nullptr, true));
786 
787     sptr<SceneSession> sceneSession = new (std::nothrow) SceneSession(info, specificCb);
788     EXPECT_NE(sceneSession, nullptr);
789     property->sessionGravity_ = SessionGravity::SESSION_GRAVITY_FLOAT;
790     ASSERT_FALSE(keyboardSession->CheckIfNeedRaiseCallingSession(sceneSession, true));
791 
792     property->sessionGravity_ = SessionGravity::SESSION_GRAVITY_BOTTOM;
793     ASSERT_TRUE(keyboardSession->CheckIfNeedRaiseCallingSession(sceneSession, false));
794 
795     property->SetWindowType(WindowType::SYSTEM_WINDOW_BASE);
796 
797     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
798     ASSERT_FALSE(keyboardSession->CheckIfNeedRaiseCallingSession(sceneSession, true));
799 
800     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
801     keyboardSession->CheckIfNeedRaiseCallingSession(sceneSession, true);
802 
803     WLOGFI("CheckIfNeedRaiseCallingSession end!");
804 }
805 
806 /**
807  * @tc.name: OpenKeyboardSyncTransaction
808  * @tc.desc: OpenKeyboardSyncTransaction
809  * @tc.type: FUNC
810  */
HWTEST_F(KeyboardSessionTest, OpenKeyboardSyncTransaction, Function | SmallTest | Level1)811 HWTEST_F(KeyboardSessionTest, OpenKeyboardSyncTransaction, Function | SmallTest | Level1)
812 {
813     std::string abilityName = "OpenKeyboardSyncTransaction";
814     std::string bundleName = "OpenKeyboardSyncTransaction";
815     sptr<KeyboardSession> keyboardSession = GetKeyboardSession(abilityName, bundleName);
816 
817     // isKeyBoardSyncTransactionOpen_ is false
818     keyboardSession->OpenKeyboardSyncTransaction();
819 
820     // isKeyBoardSyncTransactionOpen_ is true
821     keyboardSession->OpenKeyboardSyncTransaction();
822 }
823 
824 /**
825  * @tc.name: CloseKeyboardSyncTransaction1
826  * @tc.desc: CloseKeyboardSyncTransaction1
827  * @tc.type: FUNC
828  */
HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction1, Function | SmallTest | Level1)829 HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction1, Function | SmallTest | Level1)
830 {
831     std::string abilityName = "CloseKeyboardSyncTransaction1";
832     std::string bundleName = "CloseKeyboardSyncTransaction1";
833     sptr<KeyboardSession> keyboardSession = GetKeyboardSession(abilityName, bundleName);
834 
835     WSRect keyboardPanelRect = { 0, 0, 0, 0 };
836     bool isKeyboardShow = true;
837     bool isRotating = false;
838 
839     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, isKeyboardShow, isRotating);
840 }
841 
842 /**
843  * @tc.name: CloseKeyboardSyncTransaction2
844  * @tc.desc: CloseKeyboardSyncTransaction2
845  * @tc.type: FUNC
846  */
HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction2, Function | SmallTest | Level1)847 HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction2, Function | SmallTest | Level1)
848 {
849     std::string abilityName = "CloseKeyboardSyncTransaction2";
850     std::string bundleName = "CloseKeyboardSyncTransaction2";
851     sptr<KeyboardSession> keyboardSession = GetKeyboardSession(abilityName, bundleName);
852 
853     WSRect keyboardPanelRect = { 0, 0, 0, 0 };
854     bool isKeyboardShow = true;
855     bool isRotating = false;
856 
857     // isKeyBoardSyncTransactionOpen_ is true
858     keyboardSession->OpenKeyboardSyncTransaction();
859     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, isKeyboardShow, isRotating);
860 }
861 
862 /**
863  * @tc.name: BindKeyboardPanelSession
864  * @tc.desc: BindKeyboardPanelSession
865  * @tc.type: FUNC
866  */
HWTEST_F(KeyboardSessionTest, BindKeyboardPanelSession, Function | SmallTest | Level1)867 HWTEST_F(KeyboardSessionTest, BindKeyboardPanelSession, Function | SmallTest | Level1)
868 {
869     SessionInfo info;
870     info.abilityName_ = "BindKeyboardPanelSession";
871     info.bundleName_ = "BindKeyboardPanelSession";
872     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
873     ASSERT_NE(keyboardSession, nullptr);
874     sptr<SceneSession> panelSession = nullptr;
875     keyboardSession->BindKeyboardPanelSession(panelSession);
876     sptr<SceneSession> getPanelSession = keyboardSession->GetKeyboardPanelSession();
877     ASSERT_EQ(getPanelSession, nullptr);
878     panelSession = sptr<SceneSession>::MakeSptr(info, nullptr);
879     ASSERT_NE(panelSession, nullptr);
880     keyboardSession->BindKeyboardPanelSession(panelSession);
881     getPanelSession = keyboardSession->GetKeyboardPanelSession();
882     EXPECT_EQ(getPanelSession, panelSession);
883 }
884 
885 /**
886  * @tc.name: GetKeyboardGravity01
887  * @tc.desc: GetKeyboardGravity01
888  * @tc.type: FUNC
889  */
HWTEST_F(KeyboardSessionTest, GetKeyboardGravity01, Function | SmallTest | Level1)890 HWTEST_F(KeyboardSessionTest, GetKeyboardGravity01, Function | SmallTest | Level1)
891 {
892     SessionInfo info;
893     info.abilityName_ = "GetKeyboardGravity";
894     info.bundleName_ = "GetKeyboardGravity";
895     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
896     ASSERT_NE(keyboardSession, nullptr);
897     keyboardSession->property_ = nullptr;
898     auto ret = keyboardSession->GetKeyboardGravity();
899     EXPECT_EQ(SessionGravity::SESSION_GRAVITY_DEFAULT, ret);
900     sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
901     ASSERT_NE(windowSessionProperty, nullptr);
902     keyboardSession->property_ = windowSessionProperty;
903     ASSERT_NE(keyboardSession->property_, nullptr);
904     keyboardSession->property_->sessionGravity_ = SessionGravity::SESSION_GRAVITY_BOTTOM;
905     ASSERT_NE(keyboardSession, nullptr);
906     ret = keyboardSession->GetKeyboardGravity();
907     EXPECT_EQ(SessionGravity::SESSION_GRAVITY_BOTTOM, ret);
908 }
909 
910 /**
911  * @tc.name: GetCallingSessionId01
912  * @tc.desc: GetCallingSessionId01
913  * @tc.type: FUNC
914  */
HWTEST_F(KeyboardSessionTest, GetCallingSessionId01, Function | SmallTest | Level1)915 HWTEST_F(KeyboardSessionTest, GetCallingSessionId01, Function | SmallTest | Level1)
916 {
917     SessionInfo info;
918     info.abilityName_ = "GetCallingSessionId";
919     info.bundleName_ = "GetCallingSessionId";
920     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
921     ASSERT_NE(keyboardSession, nullptr);
922     keyboardSession->property_ = nullptr;
923     auto ret = keyboardSession->GetCallingSessionId();
924     EXPECT_EQ(ret, INVALID_SESSION_ID);
925     sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
926     ASSERT_NE(windowSessionProperty, nullptr);
927     keyboardSession->property_ = windowSessionProperty;
928     ASSERT_NE(keyboardSession->property_, nullptr);
929     keyboardSession->property_->SetCallingSessionId(1);
930     ret = keyboardSession->GetCallingSessionId();
931     EXPECT_EQ(ret, 1);
932 }
933 
934 /**
935  * @tc.name: NotifyKeyboardPanelInfoChange
936  * @tc.desc: NotifyKeyboardPanelInfoChange
937  * @tc.type: FUNC
938  */
HWTEST_F(KeyboardSessionTest, NotifyKeyboardPanelInfoChange, Function | SmallTest | Level1)939 HWTEST_F(KeyboardSessionTest, NotifyKeyboardPanelInfoChange, Function | SmallTest | Level1)
940 {
941     WSRect rect = {800, 800, 1200, 1200};
942     SessionInfo info;
943     info.abilityName_ = "NotifyKeyboardPanelInfoChange";
944     info.bundleName_ = "NotifyKeyboardPanelInfoChange";
945     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
946     ASSERT_NE(keyboardSession, nullptr);
947     keyboardSession->isKeyboardPanelEnabled_ = false;
948     keyboardSession->NotifyKeyboardPanelInfoChange(rect, true);
949     keyboardSession->isKeyboardPanelEnabled_ = true;
950     keyboardSession->sessionStage_ = nullptr;
951     keyboardSession->NotifyKeyboardPanelInfoChange(rect, true);
952     sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
953     ASSERT_NE(mockSessionStage, nullptr);
954     keyboardSession->sessionStage_ = mockSessionStage;
955     ASSERT_NE(keyboardSession->sessionStage_, nullptr);
956     keyboardSession->NotifyKeyboardPanelInfoChange(rect, true);
957 }
958 
959 /**
960  * @tc.name: CheckIfNeedRaiseCallingSession01
961  * @tc.desc: CheckIfNeedRaiseCallingSession01
962  * @tc.type: FUNC
963  */
HWTEST_F(KeyboardSessionTest, CheckIfNeedRaiseCallingSession01, Function | SmallTest | Level1)964 HWTEST_F(KeyboardSessionTest, CheckIfNeedRaiseCallingSession01, Function | SmallTest | Level1)
965 {
966     SessionInfo info;
967     info.abilityName_ = "CheckIfNeedRaiseCallingSession";
968     info.bundleName_ = "CheckIfNeedRaiseCallingSession";
969     sptr<SceneSession::SpecificSessionCallback> specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
970     ASSERT_NE(specificCb, nullptr);
971     sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, specificCb);
972     ASSERT_NE(callingSession, nullptr);
973     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
974     ASSERT_NE(keyboardSession, nullptr);
975     sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
976     ASSERT_NE(windowSessionProperty, nullptr);
977     keyboardSession->property_ = windowSessionProperty;
978     ASSERT_NE(keyboardSession->property_, nullptr);
979     keyboardSession->property_->sessionGravity_ = SessionGravity::SESSION_GRAVITY_BOTTOM;
980     keyboardSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
981     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
982     callingSession->systemConfig_.freeMultiWindowSupport_ = true;
983     callingSession->systemConfig_.freeMultiWindowEnable_ = true;
984     auto ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, true);
985     EXPECT_EQ(ret, false);
986     callingSession->systemConfig_.freeMultiWindowEnable_ = false;
987     ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, true);
988     EXPECT_EQ(ret, false);
989     callingSession->systemConfig_.freeMultiWindowEnable_ = true;
990     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PAD_WINDOW;
991     ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, true);
992     EXPECT_EQ(ret, true);
993     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
994     callingSession->systemConfig_.freeMultiWindowEnable_ = false;
995     ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, true);
996     EXPECT_EQ(ret, true);
997     keyboardSession->property_->SetWindowType(WindowType::APP_MAIN_WINDOW_END);
998     ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, true);
999     EXPECT_EQ(ret, true);
1000     ret = keyboardSession->CheckIfNeedRaiseCallingSession(callingSession, false);
1001     EXPECT_EQ(ret, true);
1002 }
1003 
1004 /**
1005  * @tc.name: UpdateCallingSessionIdAndPosition01
1006  * @tc.desc: UpdateCallingSessionIdAndPosition01
1007  * @tc.type: FUNC
1008  */
HWTEST_F(KeyboardSessionTest, UpdateCallingSessionIdAndPosition01, Function | SmallTest | Level1)1009 HWTEST_F(KeyboardSessionTest, UpdateCallingSessionIdAndPosition01, Function | SmallTest | Level1)
1010 {
1011     SessionInfo info;
1012     info.abilityName_ = "UpdateCallingSessionIdAndPosition";
1013     info.bundleName_ = "UpdateCallingSessionIdAndPosition";
1014     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
1015     ASSERT_NE(keyboardSession, nullptr);
1016     keyboardSession->property_ = nullptr;
1017     keyboardSession->UpdateCallingSessionIdAndPosition(0);
1018     sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
1019     ASSERT_NE(windowSessionProperty, nullptr);
1020     keyboardSession->property_ = windowSessionProperty;
1021     ASSERT_NE(keyboardSession->property_, nullptr);
1022     keyboardSession->property_->SetCallingSessionId(-1);
1023     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1024     keyboardSession->UpdateCallingSessionIdAndPosition(0);
1025     keyboardSession->state_ = SessionState::STATE_CONNECT;
1026     keyboardSession->UpdateCallingSessionIdAndPosition(0);
1027     keyboardSession->UpdateCallingSessionIdAndPosition(-1);
1028     keyboardSession->property_->SetCallingSessionId(0);
1029     keyboardSession->UpdateCallingSessionIdAndPosition(0);
1030 }
1031 
1032 /**
1033  * @tc.name: OpenKeyboardSyncTransaction01
1034  * @tc.desc: OpenKeyboardSyncTransaction
1035  * @tc.type: FUNC
1036  */
HWTEST_F(KeyboardSessionTest, OpenKeyboardSyncTransaction01, Function | SmallTest | Level1)1037 HWTEST_F(KeyboardSessionTest, OpenKeyboardSyncTransaction01, Function | SmallTest | Level1)
1038 {
1039     SessionInfo info;
1040     info.abilityName_ = "UpdateCallingSessionIdAndPosition";
1041     info.bundleName_ = "UpdateCallingSessionIdAndPosition";
1042     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
1043     ASSERT_NE(keyboardSession, nullptr);
1044     keyboardSession->isKeyboardSyncTransactionOpen_ = true;
1045     keyboardSession->OpenKeyboardSyncTransaction();
1046     keyboardSession->isKeyboardSyncTransactionOpen_ = false;
1047     keyboardSession->OpenKeyboardSyncTransaction();
1048     WSRect keyboardPanelRect = {0, 0, 0, 0};
1049     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, true, true);
1050     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, false, false);
1051 }
1052 
1053 /**
1054  * @tc.name: RelayoutKeyBoard01
1055  * @tc.desc: RelayoutKeyBoard01
1056  * @tc.type: FUNC
1057  */
HWTEST_F(KeyboardSessionTest, RelayoutKeyBoard01, Function | SmallTest | Level1)1058 HWTEST_F(KeyboardSessionTest, RelayoutKeyBoard01, Function | SmallTest | Level1)
1059 {
1060     SessionInfo info;
1061     info.abilityName_ = "RelayoutKeyBoard";
1062     info.bundleName_ = "RelayoutKeyBoard";
1063     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
1064     ASSERT_NE(keyboardSession, nullptr);
1065     keyboardSession->property_ = nullptr;
1066     keyboardSession->RelayoutKeyBoard();
1067     sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
1068     ASSERT_NE(windowSessionProperty, nullptr);
1069     keyboardSession->property_ = windowSessionProperty;
1070     ASSERT_NE(keyboardSession->property_, nullptr);
1071     keyboardSession->property_->sessionGravity_ = SessionGravity::SESSION_GRAVITY_BOTTOM;
1072     keyboardSession->property_->sessionGravitySizePercent_ = 0;
1073     keyboardSession->RelayoutKeyBoard();
1074     keyboardSession->property_->sessionGravitySizePercent_ = 100;
1075     keyboardSession->RelayoutKeyBoard();
1076 }
1077 
1078 /**
1079  * @tc.name: Hide01
1080  * @tc.desc: test function : Hide
1081  * @tc.type: FUNC
1082  */
HWTEST_F(KeyboardSessionTest, Hide01, Function | SmallTest | Level1)1083 HWTEST_F(KeyboardSessionTest, Hide01, Function | SmallTest | Level1)
1084 {
1085     SessionInfo info;
1086     info.abilityName_ = "Hide";
1087     info.bundleName_ = "Hide";
1088     sptr<SceneSession::SpecificSessionCallback> specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
1089     ASSERT_NE(specificCb, nullptr);
1090     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
1091         sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
1092     ASSERT_NE(keyboardCb, nullptr);
1093     sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
1094     ASSERT_NE(keyboardSession, nullptr);
1095 
1096     // setActive false return not ok
1097     keyboardSession->state_ = SessionState::STATE_DISCONNECT;
1098     EXPECT_EQ(WSError::WS_OK, keyboardSession->Hide());
1099 
1100     // setActive false return ok and deviceType is phone
1101     keyboardSession->state_ = SessionState::STATE_CONNECT;
1102     keyboardSession->isActive_ = true;
1103     keyboardSession->sessionStage_ = new (std::nothrow) SessionStageMocker();
1104     ASSERT_NE(keyboardSession->sessionStage_, nullptr);
1105     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
1106     EXPECT_EQ(WSError::WS_OK, keyboardSession->Hide());
1107 
1108     // deviceType is pc and property is not nullptr
1109     ASSERT_NE(keyboardSession->property_, nullptr);
1110     keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
1111     EXPECT_EQ(WSError::WS_OK, keyboardSession->Hide());
1112 
1113     // deviceType is pc and property is nullptr
1114     keyboardSession->property_ = nullptr;
1115     EXPECT_EQ(WSError::WS_OK, keyboardSession->Hide());
1116 }
1117 
1118 /**
1119  * @tc.name: RaiseCallingSession01
1120  * @tc.desc: test function : RaiseCallingSession
1121  * @tc.type: FUNC
1122  */
HWTEST_F(KeyboardSessionTest, RaiseCallingSession01, Function | SmallTest | Level1)1123 HWTEST_F(KeyboardSessionTest, RaiseCallingSession01, Function | SmallTest | Level1)
1124 {
1125     auto keyboardSession = GetKeyboardSession("RaiseCallingSession01",
1126         "RaiseCallingSession01");
1127     ASSERT_NE(keyboardSession, nullptr);
1128     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1129     keyboardSession->isVisible_ = true;
1130 
1131     Rosen::WSRect resultRect{ 0, 0, 0, 0 };
1132     sptr<KSSceneSessionMocker> callingSession = GetSceneSessionMocker("callingSession", "callingSession");
1133     ASSERT_NE(callingSession, nullptr);
1134 
1135     callingSession->updateRectCallback_ = [&resultRect](const WSRect& rect, const SizeChangeReason reason) {
1136         resultRect.posX_ = rect.posX_;
1137         resultRect.posY_ = rect.posY_;
1138         resultRect.width_ = rect.width_;
1139         resultRect.height_ = rect.height_;
1140     };
1141     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
1142     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1143 
1144     Rosen::WSRect keyboardPanelRect{ 0, 0, 0, 0 };
1145     Rosen::WSRect emptyRect{ 0, 0, 0, 0 };
1146     std::shared_ptr<RSTransaction> rsTransaction = nullptr;
1147     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1148     keyboardSession->RaiseCallingSession(keyboardPanelRect, rsTransaction);
1149     ASSERT_EQ(resultRect, emptyRect);
1150 
1151     // for cover GetSceneSession
1152     keyboardSession->keyboardCallback_->onGetSceneSession_ =
1153         [callingSession](int32_t persistentId)->sptr<SceneSession> {
1154         return callingSession;
1155     };
1156     keyboardSession->RaiseCallingSession(keyboardPanelRect, rsTransaction);
1157     // for cover CheckIfNeedRaiseCallingSession
1158     keyboardSession->property_->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_BOTTOM, 0);
1159 
1160     // for empty rect check;
1161     keyboardSession->winRect_.posX_ = 1;
1162     keyboardSession->winRect_.posY_ = 1;
1163     keyboardSession->winRect_.posX_ = 1;
1164     keyboardSession->winRect_.posX_ = 1;
1165 
1166     // for cover oriPosYBeforeRaisedBykeyboard == 0
1167     callingSession->SetOriPosYBeforeRaisedByKeyboard(0);
1168     ASSERT_EQ(resultRect, emptyRect);
1169 }
1170 
1171 /**
1172  * @tc.name: RaiseCallingSession02
1173  * @tc.desc: test function : RaiseCallingSession
1174  * @tc.type: FUNC
1175  */
HWTEST_F(KeyboardSessionTest, RaiseCallingSession02, Function | SmallTest | Level1)1176 HWTEST_F(KeyboardSessionTest, RaiseCallingSession02, Function | SmallTest | Level1)
1177 {
1178     Rosen::WSRect keyboardPanelRect{ 1, 1, 1, 1 };
1179     auto keyboardSession = GetKeyboardSession("RaiseCallingSession02",
1180         "RaiseCallingSession02");
1181     ASSERT_NE(keyboardSession, nullptr);
1182     sptr<KSSceneSessionMocker> callingSession = GetSceneSessionMocker("callingSession", "callingSession");
1183     ASSERT_NE(callingSession, nullptr);
1184     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1185     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
1186     callingSession->winRect_ = { 1, 1, 1, 1 };
1187     keyboardSession->keyboardCallback_->onGetSceneSession_ = [callingSession](int32_t persistentId) {
1188         return callingSession;
1189     };
1190     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1191     keyboardSession->isVisible_ = true;
1192     callingSession->oriPosYBeforeRaisedByKeyboard_ = 0;
1193     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1194     ASSERT_EQ(callingSession->winRect_.posY_, 1);
1195 
1196     callingSession->oriPosYBeforeRaisedByKeyboard_ = 10;
1197     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1198     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1199     ASSERT_EQ(callingSession->winRect_.posY_, 1);
1200 
1201     keyboardPanelRect = { 0, 0, 0, 0 };
1202     callingSession->oriPosYBeforeRaisedByKeyboard_ = 10;
1203     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1204     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1205     ASSERT_EQ(callingSession->winRect_.posY_, 1);
1206 }
1207 
1208 /**
1209  * @tc.name: RaiseCallingSession03
1210  * @tc.desc: test function : RaiseCallingSession
1211  * @tc.type: FUNC
1212  */
HWTEST_F(KeyboardSessionTest, RaiseCallingSession03, Function | SmallTest | Level1)1213 HWTEST_F(KeyboardSessionTest, RaiseCallingSession03, Function | SmallTest | Level1)
1214 {
1215     Rosen::WSRect keyboardPanelRect{ 1, 1, 1, 1 };
1216     auto keyboardSession = GetKeyboardSession("RaiseCallingSession03",
1217         "RaiseCallingSession03");
1218     ASSERT_NE(keyboardSession, nullptr);
1219     sptr<KSSceneSessionMocker> callingSession = GetSceneSessionMocker("callingSession", "callingSession");
1220     ASSERT_NE(callingSession, nullptr);
1221     callingSession->winRect_ = { 1, 1, 1, 1 };
1222     callingSession->oriPosYBeforeRaisedByKeyboard_ = 0;
1223     callingSession->updateRectCallback_ = [](const WSRect& rect, const SizeChangeReason reason) {};
1224     keyboardSession->keyboardCallback_->onGetSceneSession_ = [callingSession](int32_t persistentId) {
1225         return callingSession;
1226     };
1227     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1228     keyboardSession->isVisible_ = true;
1229     auto callingOriPosY = 0;
1230     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
1231     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1232     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1233     callingOriPosY = callingSession->oriPosYBeforeRaisedByKeyboard_;
1234     ASSERT_EQ(callingOriPosY, 0);
1235 
1236     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1237     callingSession->winRect_.posY_ = 200;
1238     keyboardPanelRect.posY_ = 200;
1239     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1240     callingOriPosY = callingSession->oriPosYBeforeRaisedByKeyboard_;
1241     ASSERT_EQ(callingOriPosY, 200);
1242 
1243     callingSession->oriPosYBeforeRaisedByKeyboard_ = 10;
1244     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
1245     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1246     callingOriPosY = callingSession->oriPosYBeforeRaisedByKeyboard_;
1247     ASSERT_EQ(callingOriPosY, 10);
1248 }
1249 
1250 /**
1251  * @tc.name: IsCallingSessionSplitMode01
1252  * @tc.desc: test function : IsCallingSessionSplitMode
1253  * @tc.type: FUNC
1254  */
HWTEST_F(KeyboardSessionTest, IsCallingSessionSplitMode01, Function | SmallTest | Level1)1255 HWTEST_F(KeyboardSessionTest, IsCallingSessionSplitMode01, Function | SmallTest | Level1)
1256 {
1257     Rosen::WSRect keyboardPanelRect{ 0, 0, 0, 0 };
1258     auto keyboardSession = GetKeyboardSession("IsCallingSessionSplitMode01",
1259         "IsCallingSessionSplitMode01");
1260     ASSERT_NE(keyboardSession, nullptr);
1261     sptr<KSSceneSessionMocker> callingSession = GetSceneSessionMocker("callingSession", "callingSession");
1262     ASSERT_NE(callingSession, nullptr);
1263     callingSession->oriPosYBeforeRaisedByKeyboard_ = 0;
1264     callingSession->winRect_ = { 0, 0, 0, 0 };
1265     callingSession->updateRectCallback_ = [](const WSRect& rect, const SizeChangeReason reason) {};
1266     keyboardSession->keyboardCallback_->onGetSceneSession_ = [callingSession](int32_t persistentId) {
1267         return callingSession;
1268     };
1269     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1270 
1271     auto callingParentSession = GetSceneSession("callingParentSession", "callingParentSession");
1272     ASSERT_NE(callingSession, nullptr);
1273 
1274     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
1275     callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_SPLIT_SECONDARY);
1276     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1277     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1278 
1279     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
1280     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1281     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1282 
1283     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
1284     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1285     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1286 
1287     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
1288     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1289     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1290 
1291     callingSession->parentSession_ = callingParentSession;
1292     callingSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
1293     callingParentSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_SPLIT_SECONDARY);
1294     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1295     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1296 
1297     callingParentSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
1298     keyboardSession->RaiseCallingSession(keyboardPanelRect, nullptr);
1299     ASSERT_EQ(callingSession->oriPosYBeforeRaisedByKeyboard_, 0);
1300 }
1301 
1302 /**
1303  * @tc.name: CloseKeyBoardSyncTransaction3
1304  * @tc.desc: test function : CloseKeyBoardSyncTransaction
1305  * @tc.type: FUNC
1306  */
HWTEST_F(KeyboardSessionTest, CloseKeyBoardSyncTransaction3, Function | SmallTest | Level1)1307 HWTEST_F(KeyboardSessionTest, CloseKeyBoardSyncTransaction3, Function | SmallTest | Level1)
1308 {
1309     std::string abilityName = "CloseKeyBoardSyncTransaction3";
1310     std::string bundleName = "CloseKeyBoardSyncTransaction3";
1311     sptr<KeyboardSession> keyboardSession = GetKeyboardSession(abilityName, bundleName);
1312     ASSERT_NE(keyboardSession, nullptr);
1313 
1314     WSRect keyboardPanelRect = { 0, 0, 0, 0 };
1315     bool isKeyboardShow = true;
1316     bool isRotating = false;
1317 
1318     keyboardSession->dirtyFlags_ = 0;
1319     keyboardSession->specificCallback_->onUpdateAvoidArea_ = [](uint32_t callingSessionId) {};
1320     keyboardSession->isKeyboardSyncTransactionOpen_ = true;
1321     // isKeyBoardSyncTransactionOpen_ is true
1322     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, isKeyboardShow, isRotating);
1323     usleep(WAIT_ASYNC_US);
1324     ASSERT_EQ(keyboardSession->isKeyboardSyncTransactionOpen_, false);
1325 }
1326 
1327 /**
1328  * @tc.name: CloseKeyboardSyncTransaction4
1329  * @tc.desc: test function : CloseKeyboardSyncTransaction
1330  * @tc.type: FUNC
1331  */
HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction4, Function | SmallTest | Level1)1332 HWTEST_F(KeyboardSessionTest, CloseKeyboardSyncTransaction4, Function | SmallTest | Level1)
1333 {
1334     std::string abilityName = "CloseKeyboardSyncTransaction4";
1335     std::string bundleName = "CloseKeyboardSyncTransaction4";
1336     sptr<KeyboardSession> keyboardSession = GetKeyboardSession(abilityName, bundleName);
1337     ASSERT_NE(keyboardSession, nullptr);
1338     WSRect keyboardPanelRect = { 0, 0, 0, 0 };
1339     keyboardSession->dirtyFlags_ = 0;
1340 
1341     keyboardSession->isKeyboardSyncTransactionOpen_ = false;
1342     keyboardSession->property_ = nullptr;
1343     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, false, false);
1344     usleep(WAIT_ASYNC_US);
1345     ASSERT_EQ(0, keyboardSession->dirtyFlags_);
1346 
1347     sptr<WindowSessionProperty> keyboardProperty = sptr<WindowSessionProperty>::MakeSptr();
1348     ASSERT_NE(keyboardProperty, nullptr);
1349     keyboardSession->property_ = keyboardProperty;
1350     keyboardSession->isKeyboardSyncTransactionOpen_ = true;
1351     keyboardSession->property_->SetCallingSessionId(1);
1352     keyboardSession->CloseKeyboardSyncTransaction(keyboardPanelRect, false, false);
1353     usleep(WAIT_ASYNC_US);
1354     auto callingSessionId = keyboardSession->property_->GetCallingSessionId();
1355     ASSERT_EQ(callingSessionId, INVALID_WINDOW_ID);
1356 }
1357 
1358 /**
1359  * @tc.name: GetRSTransaction01
1360  * @tc.desc: test function : GetRSTransaction
1361  * @tc.type: FUNC
1362  */
HWTEST_F(KeyboardSessionTest, GetRSTransaction01, Function | SmallTest | Level1)1363 HWTEST_F(KeyboardSessionTest, GetRSTransaction01, Function | SmallTest | Level1)
1364 {
1365     auto keyboardSession = GetKeyboardSession("GetRSTransaction01",
1366         "GetRSTransaction01");
1367     ASSERT_NE(keyboardSession, nullptr);
1368 
1369     auto rsTransaction = keyboardSession->GetRSTransaction();
1370     ASSERT_EQ(rsTransaction, nullptr);
1371 }
1372 
1373 /**
1374  * @tc.name: GetSessionScreenName01
1375  * @tc.desc: test function : GetSessionScreenName
1376  * @tc.type: FUNC
1377  */
HWTEST_F(KeyboardSessionTest, GetSessionScreenName01, Function | SmallTest | Level1)1378 HWTEST_F(KeyboardSessionTest, GetSessionScreenName01, Function | SmallTest | Level1)
1379 {
1380     auto keyboardSession = GetKeyboardSession("GetSessionScreenName01",
1381         "GetSessionScreenName01");
1382     ASSERT_NE(keyboardSession, nullptr);
1383     keyboardSession->property_ = nullptr;
1384     auto resultStr = keyboardSession->GetSessionScreenName();
1385     ASSERT_EQ(resultStr, "");
1386 
1387     sptr<WindowSessionProperty> keyboardProperty = sptr<WindowSessionProperty>::MakeSptr();
1388     ASSERT_NE(keyboardProperty, nullptr);
1389     keyboardSession->property_ = keyboardProperty;
1390     keyboardSession->property_->displayId_ = 100;
1391 
1392     sptr<ScreenSession> screenSession = sptr<ScreenSession>::MakeSptr();
1393     ASSERT_NE(screenSession, nullptr);
1394     screenSession->name_ = "testScreenSession";
1395     screenSession->screenId_ = 100;
1396     ScreenSessionManagerClient::GetInstance().screenSessionMap_.emplace(100, screenSession);
1397 
1398     resultStr = keyboardSession->GetSessionScreenName();
1399     ASSERT_EQ(resultStr, screenSession->name_);
1400 }
1401 
1402 /**
1403  * @tc.name: UseFocusIdIfCallingSessionIdInvalid01
1404  * @tc.desc: test function : UseFocusIdIfCallingSessionIdInvalid
1405  * @tc.type: FUNC
1406  */
HWTEST_F(KeyboardSessionTest, UseFocusIdIfCallingSessionIdInvalid01, Function | SmallTest | Level1)1407 HWTEST_F(KeyboardSessionTest, UseFocusIdIfCallingSessionIdInvalid01, Function | SmallTest | Level1)
1408 {
1409     auto keyboardSession = GetKeyboardSession("UseFocusIdIfCallingSessionIdInvalid01",
1410         "UseFocusIdIfCallingSessionIdInvalid01");
1411     ASSERT_NE(keyboardSession, nullptr);
1412     sptr<KeyboardSession::KeyboardSessionCallback> keyboardCallback =
1413         sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
1414     ASSERT_NE(keyboardCallback, nullptr);
1415     keyboardSession->keyboardCallback_ = keyboardCallback;
1416     sptr<SceneSession> sceneSession = GetSceneSession("TestSceneSession", "TestSceneSession");
1417     ASSERT_NE(sceneSession, nullptr);
1418     sceneSession->persistentId_ = 100;
1419     keyboardSession->keyboardCallback_->onGetSceneSession_ =
1420         [sceneSession](uint32_t callingSessionId)->sptr<SceneSession> {
1421             if (sceneSession->persistentId_ != callingSessionId) {
1422                 return nullptr;
1423             }
1424             return sceneSession;
1425         };
1426 
1427     keyboardSession->GetSessionProperty()->SetCallingSessionId(100);
1428     keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
1429     auto resultId = keyboardSession->GetCallingSessionId();
1430     ASSERT_EQ(resultId, 100);
1431 
1432     keyboardSession->GetSessionProperty()->SetCallingSessionId(101);
1433     keyboardSession->keyboardCallback_->onGetFocusedSessionId_ = []()->int32_t {
1434         return 100;
1435     };
1436     keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
1437     resultId = keyboardSession->GetCallingSessionId();
1438     ASSERT_EQ(resultId, 100);
1439 }
1440 
1441 /**
1442  * @tc.name: UpdateKeyboardAvoidArea01
1443  * @tc.desc: test function : UpdateKeyboardAvoidArea
1444  * @tc.type: FUNC
1445  */
HWTEST_F(KeyboardSessionTest, UpdateKeyboardAvoidArea01, Function | SmallTest | Level1)1446 HWTEST_F(KeyboardSessionTest, UpdateKeyboardAvoidArea01, Function | SmallTest | Level1)
1447 {
1448     auto keyboardSession = GetKeyboardSession("UpdateKeyboardAvoidArea01",
1449         "UpdateKeyboardAvoidArea01");
1450     ASSERT_NE(keyboardSession, nullptr);
1451 
1452     // not foreground
1453     keyboardSession->dirtyFlags_ = 0;
1454     keyboardSession->state_ = SessionState::STATE_CONNECT;
1455     keyboardSession->UpdateKeyboardAvoidArea();
1456     ASSERT_EQ(keyboardSession->dirtyFlags_, 0);
1457 
1458     // has callback
1459     auto expectDirtyFlag = 0;
1460     keyboardSession->dirtyFlags_ = 0;
1461     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1462     keyboardSession->isVisible_ = true;
1463     keyboardSession->specificCallback_->onUpdateAvoidArea_ = [&expectDirtyFlag](const uint32_t& persistentId) {
1464         expectDirtyFlag = 1;
1465     };
1466     keyboardSession->UpdateKeyboardAvoidArea();
1467     if (Session::IsScbCoreEnabled()) {
1468         expectDirtyFlag = 0 | static_cast<uint32_t>(SessionUIDirtyFlag::AVOID_AREA);
1469         ASSERT_EQ(keyboardSession->dirtyFlags_, expectDirtyFlag);
1470     } else {
1471         ASSERT_EQ(expectDirtyFlag, 1);
1472     }
1473 
1474     // miss callback
1475     expectDirtyFlag = 0;
1476     keyboardSession->dirtyFlags_ = 1;
1477     keyboardSession->specificCallback_->onUpdateAvoidArea_ = nullptr;
1478     keyboardSession->UpdateKeyboardAvoidArea();
1479     if (Session::IsScbCoreEnabled()) {
1480         ASSERT_EQ(keyboardSession->dirtyFlags_, 1);
1481     } else {
1482         ASSERT_EQ(expectDirtyFlag, 0);
1483     }
1484 
1485     expectDirtyFlag = 0;
1486     keyboardSession->dirtyFlags_ = 2;
1487     keyboardSession->specificCallback_ = nullptr;
1488     keyboardSession->UpdateKeyboardAvoidArea();
1489     if (Session::IsScbCoreEnabled()) {
1490         ASSERT_EQ(keyboardSession->dirtyFlags_, 2);
1491     } else {
1492         ASSERT_EQ(expectDirtyFlag, 0);
1493     }
1494 }
1495 
1496 /**
1497  * @tc.name: MoveAndResizeKeyboard01
1498  * @tc.desc: test function : MoveAndResizeKeyboard
1499  * @tc.type: FUNC
1500  */
HWTEST_F(KeyboardSessionTest, MoveAndResizeKeyboard01, Function | SmallTest | Level1)1501 HWTEST_F(KeyboardSessionTest, MoveAndResizeKeyboard01, Function | SmallTest | Level1)
1502 {
1503     auto keyboardSession = GetKeyboardSession("MoveAndResizeKeyboard01",
1504         "MoveAndResizeKeyboard01");
1505     ASSERT_NE(keyboardSession, nullptr);
1506 
1507     KeyboardLayoutParams param;
1508     param.LandscapeKeyboardRect_ = { 100, 100, 100, 200 };
1509     param.PortraitKeyboardRect_ = { 200, 200, 200, 100 };
1510 
1511     keyboardSession->isScreenAngleMismatch_ = true;
1512     keyboardSession->targetScreenWidth_ = 300;
1513     keyboardSession->targetScreenHeight_ = 400;
1514 
1515     // branch SESSION_GRAVITY_BOTTOM
1516     param.gravity_ = WindowGravity::WINDOW_GRAVITY_BOTTOM;
1517     Rect expectRect = { 0, 300, 300, 100 };
1518     keyboardSession->MoveAndResizeKeyboard(param, nullptr, false);
1519     ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
1520 
1521     //branch SESSION_GRAVITY_DEFAULT
1522     param.gravity_ = WindowGravity::WINDOW_GRAVITY_DEFAULT;
1523     expectRect = { 200, 300, 200, 100 };
1524     keyboardSession->MoveAndResizeKeyboard(param, nullptr, true);
1525     ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
1526 }
1527 
1528 /**
1529  * @tc.name: MoveAndResizeKeyboard02
1530  * @tc.desc: test function : MoveAndResizeKeyboard
1531  * @tc.type: FUNC
1532  */
HWTEST_F(KeyboardSessionTest, MoveAndResizeKeyboard02, Function | SmallTest | Level1)1533 HWTEST_F(KeyboardSessionTest, MoveAndResizeKeyboard02, Function | SmallTest | Level1)
1534 {
1535     auto keyboardSession = GetKeyboardSession("MoveAndResizeKeyboard02",
1536         "MoveAndResizeKeyboard02");
1537     ASSERT_NE(keyboardSession, nullptr);
1538 
1539     KeyboardLayoutParams param;
1540     param.LandscapeKeyboardRect_ = { 100, 100, 100, 200 };
1541     param.PortraitKeyboardRect_ = { 200, 200, 200, 100 };
1542 
1543     keyboardSession->isScreenAngleMismatch_ = true;
1544     keyboardSession->targetScreenWidth_ = 300;
1545     keyboardSession->targetScreenHeight_ = 400;
1546     param.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT;
1547 
1548     // branch else
1549     Rect expectRect = param.PortraitKeyboardRect_;
1550     keyboardSession->MoveAndResizeKeyboard(param, nullptr, true);
1551     ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
1552 
1553     param.PortraitKeyboardRect_ = { 200, 200, 200, 0 };
1554     auto requestRect = keyboardSession->GetSessionRequestRect();
1555     expectRect = { requestRect.posX_, requestRect.posY_, requestRect.width_, requestRect.height_ };
1556     keyboardSession->MoveAndResizeKeyboard(param, nullptr, true);
1557     ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
1558 
1559     param.PortraitKeyboardRect_ = { 200, 200, 0, 0 };
1560     requestRect = keyboardSession->GetSessionRequestRect();
1561     expectRect = { requestRect.posX_, requestRect.posY_, requestRect.width_, requestRect.height_ };
1562     keyboardSession->MoveAndResizeKeyboard(param, nullptr, true);
1563     ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
1564 }
1565 
1566 /**
1567  * @tc.name: OnCallingSessionUpdated01
1568  * @tc.desc: test function : OnCallingSessionUpdated
1569  * @tc.type: FUNC
1570  */
HWTEST_F(KeyboardSessionTest, OnCallingSessionUpdated01, Function | SmallTest | Level1)1571 HWTEST_F(KeyboardSessionTest, OnCallingSessionUpdated01, Function | SmallTest | Level1)
1572 {
1573     auto keyboardSession = GetKeyboardSession("OnCallingSessionUpdated01",
1574         "OnCallingSessionUpdated01");
1575     ASSERT_NE(keyboardSession, nullptr);
1576 
1577     // keyboardSession is not foreground
1578     keyboardSession->OnCallingSessionUpdated();
1579     ASSERT_EQ(keyboardSession->state_, SessionState::STATE_DISCONNECT);
1580 
1581     // keyboardSession's isVisible_ is false
1582     keyboardSession->state_ = SessionState::STATE_FOREGROUND;
1583     keyboardSession->OnCallingSessionUpdated();
1584     ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
1585 
1586     // keyboardSession's isVisible_ is true
1587     keyboardSession->isVisible_ = true;
1588     keyboardSession->OnCallingSessionUpdated();
1589     ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
1590 
1591     // callingsession is not nullptr
1592     sptr<SceneSession::SpecificSessionCallback> specificCb =
1593         new (std::nothrow) SceneSession::SpecificSessionCallback();
1594     EXPECT_NE(specificCb, nullptr);
1595     SessionInfo info;
1596     info.abilityName_ = "OnCallingSessionUpdated01";
1597     info.bundleName_ = "OnCallingSessionUpdated01";
1598     info.windowType_ = 1; // 1 is main_window_type
1599     sptr<SceneSession> callingSession = new (std::nothrow) SceneSession(info, specificCb);
1600     EXPECT_NE(callingSession, nullptr);
1601     ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
1602     keyboardSession->keyboardCallback_->onGetSceneSession_ =
1603         [callingSession](int32_t persistentId)->sptr<SceneSession> {
1604         return callingSession;
1605     };
1606     // callingSession is fullScreen and isCallingSessionFloating is false
1607     // keyboardSession's gravity is SessionGravity::SESSION_GRAVITY_DEFAULT
1608     keyboardSession->OnCallingSessionUpdated();
1609     ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
1610 
1611     // keyboardSession's gravity is SessionGravity::SESSION_GRAVITY_FLOAT
1612     WSError res = keyboardSession->SetKeyboardSessionGravity(SessionGravity::SESSION_GRAVITY_FLOAT,
1613         50); // 50 is percent
1614     ASSERT_EQ(res, WSError::WS_OK);
1615     ASSERT_EQ(keyboardSession->GetKeyboardGravity(), SessionGravity::SESSION_GRAVITY_FLOAT);
1616     keyboardSession->OnCallingSessionUpdated();
1617     ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
1618 }
1619 }  // namespace
1620 }  // namespace Rosen
1621 }  // namespace OHOS
1622