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
19 #include "interfaces/include/ws_common.h"
20 #include "mock/mock_session_stage.h"
21 #include "mock/mock_keyboard_session.h"
22 #include "session/host/include/session.h"
23 #include "session/host/include/scene_session.h"
24 #include "window_helper.h"
25 #include "window_manager_hilog.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "KeyboardSessionTest"};
34 }
35
36 class KeyboardSessionTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp() override;
41 void TearDown() override;
42
43 private:
44 sptr<SceneSession> GetSceneSession(const std::string& abilityName, const std::string& bundleName);
45 static constexpr uint32_t SPLIT_TEST_SLEEP_S = 1;
46 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
47 };
48
SetUpTestCase()49 void KeyboardSessionTest::SetUpTestCase()
50 {
51 }
52
TearDownTestCase()53 void KeyboardSessionTest::TearDownTestCase()
54 {
55 }
56
SetUp()57 void KeyboardSessionTest::SetUp()
58 {
59 }
60
TearDown()61 void KeyboardSessionTest::TearDown()
62 {
63 }
64
GetSceneSession(const std::string & abilityName,const std::string & bundleName)65 sptr<SceneSession> KeyboardSessionTest::GetSceneSession(const std::string& abilityName,
66 const std::string& bundleName)
67 {
68 SessionInfo info;
69 info.abilityName_ = abilityName;
70 info.bundleName_ = bundleName;
71 sptr<SceneSession::SpecificSessionCallback> specificCb =
72 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
73 EXPECT_NE(specificCb, nullptr);
74 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
75
76 return sceneSession;
77 }
78
79 namespace {
80 /**
81 * @tc.name: Show
82 * @tc.desc: test function : Show
83 * @tc.type: FUNC
84 */
85 HWTEST_F(KeyboardSessionTest, GetKeyboardGravity, Function | SmallTest | Level1)
86 {
87 SessionInfo info;
88 info.abilityName_ = "GetKeyboardGravity";
89 info.bundleName_ = "GetKeyboardGravity";
90 sptr<SceneSession::SpecificSessionCallback> specificCb =
91 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
92 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, nullptr);
93 sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
94 keyboardSession->property_ = windowSessionProperty;
95 ASSERT_EQ(SessionGravity::SESSION_GRAVITY_BOTTOM, keyboardSession->GetKeyboardGravity());
96 }
97
98 /**
99 * @tc.name: Show01
100 * @tc.desc: test function : Show
101 * @tc.type: FUNC
102 */
103 HWTEST_F(KeyboardSessionTest, Show01, Function | SmallTest | Level1)
104 {
105 SessionInfo info;
106 info.abilityName_ = "Show01";
107 info.bundleName_ = "Show01";
108 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
109 auto keyboardCb = sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
110 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
111 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
112
113 keyboardSession->isKeyboardPanelEnabled_ = true;
114 ASSERT_EQ(WSError::WS_OK, keyboardSession->Show(property));
115
116 keyboardSession->isKeyboardPanelEnabled_ = false;
117 ASSERT_EQ(WSError::WS_OK, keyboardSession->Show(property));
118
119 ASSERT_NE(nullptr, keyboardSession->property_);
120 KeyboardLayoutParams params;
121 params.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT;
122 keyboardSession->property_->SetKeyboardLayoutParams(params);
123 ASSERT_NE(SessionGravity::SESSION_GRAVITY_BOTTOM, keyboardSession->GetKeyboardGravity());
124 ASSERT_EQ(WSError::WS_OK, keyboardSession->Show(property));
125 }
126
127 /**
128 * @tc.name: Show02
129 * @tc.desc: test function : Show
130 * @tc.type: FUNC
131 */
132 HWTEST_F(KeyboardSessionTest, Show02, Function | SmallTest | Level1)
133 {
134 SessionInfo info;
135 info.abilityName_ = "Show02";
136 info.bundleName_ = "Show02";
137 sptr<SceneSession::SpecificSessionCallback> specificCb =
138 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
139 EXPECT_NE(specificCb, nullptr);
140 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
141 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
142 EXPECT_NE(keyboardCb, nullptr);
143 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
144 ASSERT_TRUE((keyboardSession != nullptr));
145 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, keyboardSession->Show(nullptr));
146 }
147
148 /**
149 * @tc.name: Hide
150 * @tc.desc: test function : Hide
151 * @tc.type: FUNC
152 */
153 HWTEST_F(KeyboardSessionTest, Hide, Function | SmallTest | Level1)
154 {
155 SessionInfo info;
156 info.abilityName_ = "Hide";
157 info.bundleName_ = "Hide";
158 sptr<SceneSession::SpecificSessionCallback> specificCb =
159 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
160 EXPECT_NE(specificCb, nullptr);
161 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
162 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
163 EXPECT_NE(keyboardCb, nullptr);
164 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
165 ASSERT_TRUE((keyboardSession != nullptr));
166
167 ASSERT_EQ(WSError::WS_OK, keyboardSession->Hide());
168 }
169
170 /**
171 * @tc.name: Disconnect
172 * @tc.desc: normal function
173 * @tc.type: FUNC
174 */
175 HWTEST_F(KeyboardSessionTest, Disconnect, Function | SmallTest | Level2)
176 {
177 SessionInfo info;
178 info.abilityName_ = "Disconnect";
179 info.bundleName_ = "Disconnect";
180 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
181 EXPECT_NE(keyboardSession, nullptr);
182 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
183 property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
184 keyboardSession->SetSessionProperty(property);
185 keyboardSession->isActive_ = true;
186 auto result = keyboardSession->Disconnect();
187 ASSERT_EQ(result, WSError::WS_OK);
188 }
189
190 /**
191 * @tc.name: Disconnect01
192 * @tc.desc: test system keyboard disconnect
193 * @tc.type: FUNC
194 */
195 HWTEST_F(KeyboardSessionTest, DisConnect01, Function | SmallTest | Level2)
196 {
197 SessionInfo info;
198 info.abilityName_ = "DisConnect01";
199 info.bundleName_ = "DisConnect01";
200 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
201 ASSERT_EQ(WSError::WS_OK, keyboardSession->Disconnect());
202
203 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
204 property->SetWindowType(WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT);
205 ASSERT_EQ(WSError::WS_OK, keyboardSession->SetSessionProperty(property));
206 keyboardSession->isActive_ = true;
207 ASSERT_EQ(WSError::WS_OK, keyboardSession->Disconnect());
208
209 SessionInfo panelInfo;
210 panelInfo.abilityName_ = "systemKeyboardPanel";
211 panelInfo.bundleName_ = "systemKeyboardPanel";
212 sptr<SystemSession> panelSession = sptr<SystemSession>::MakeSptr(panelInfo, nullptr);
213 keyboardSession->BindKeyboardPanelSession(panelSession);
214 ASSERT_EQ(WSError::WS_OK, keyboardSession->Disconnect());
215 panelSession->SetIsSystemKeyboard(true);
216 ASSERT_EQ(WSError::WS_OK, keyboardSession->Disconnect());
217 }
218
219 /**
220 * @tc.name: GetSceneSession01
221 * @tc.desc: GetSceneSession
222 * @tc.type: FUNC
223 */
224 HWTEST_F(KeyboardSessionTest, GetSceneSession01, Function | SmallTest | Level1)
225 {
226 SessionInfo info;
227 info.abilityName_ = "GetSceneSession01";
228 info.bundleName_ = "GetSceneSession01";
229 sptr<SceneSession::SpecificSessionCallback> specificCb =
230 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
231 EXPECT_NE(specificCb, nullptr);
232 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
233 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
234 EXPECT_NE(keyboardCb, nullptr);
235 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
236 EXPECT_NE(keyboardSession, nullptr);
237 info.windowType_ = 1;
238 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
239 EXPECT_NE(sceneSession, nullptr);
240 auto id = sceneSession->GetPersistentId();
241 EXPECT_NE(id, 0);
242 auto ret = keyboardSession->GetSceneSession(id);
243
__anonf548f40d0302(uint32_t) 244 keyboardCb->onGetSceneSession = [](uint32_t) {
245 return nullptr;
246 };
247 EXPECT_NE(keyboardCb->onGetSceneSession, nullptr);
248 ret = keyboardSession->GetSceneSession(id);
249 }
250
251 /**
252 * @tc.name: NotifyOccupiedAreaChangeInfo
253 * @tc.desc: NotifyOccupiedAreaChangeInfo
254 * @tc.type: FUNC
255 */
256 HWTEST_F(KeyboardSessionTest, NotifyOccupiedAreaChangeInfo, Function | SmallTest | Level1)
257 {
258 SessionInfo info;
259 info.abilityName_ = "NotifyOccupiedAreaChangeInfo";
260 info.bundleName_ = "NotifyOccupiedAreaChangeInfo";
261 sptr<SceneSession::SpecificSessionCallback> specificCb =
262 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
263 EXPECT_NE(specificCb, nullptr);
264 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
265 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
266 EXPECT_NE(keyboardCb, nullptr);
267 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
268 EXPECT_NE(keyboardSession, nullptr);
269 sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, nullptr);
270 WSRect rect = { 0, 0, 1260, 2720 };
271 WSRect occupiedArea = { 0, 1700, 1260, 1020 };
272 keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
273
274 keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
275
276 callingSession->lastSafeRect = { 0, 0, 0, 0 };
277 callingSession->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_GLOBAL_SEARCH);
278 keyboardSession->NotifyOccupiedAreaChangeInfo(callingSession, rect, occupiedArea);
279 }
280
281 /**
282 * @tc.name: NotifyRootSceneOccupiedAreaChange
283 * @tc.desc: NotifyRootSceneOccupiedAreaChange
284 * @tc.type: FUNC
285 */
286 HWTEST_F(KeyboardSessionTest, NotifyRootSceneOccupiedAreaChange, Function | SmallTest | Level1)
287 {
288 SessionInfo info;
289 info.abilityName_ = "NotifyRootSceneOccupiedAreaChange";
290 info.bundleName_ = "NotifyRootSceneOccupiedAreaChange";
291 sptr<SceneSession::SpecificSessionCallback> specificCb =
292 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
293 EXPECT_NE(specificCb, nullptr);
294 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
295 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
296 EXPECT_NE(keyboardCb, nullptr);
297 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
298 EXPECT_NE(keyboardSession, nullptr);
299 auto occupiedInfo = sptr<OccupiedAreaChangeInfo>::MakeSptr();
300 ASSERT_NE(nullptr, occupiedInfo);
301 keyboardSession->NotifyRootSceneOccupiedAreaChange(occupiedInfo);
302 keyboardSession->GetSessionProperty()->SetDisplayId(2025);
303 keyboardSession->NotifyRootSceneOccupiedAreaChange(occupiedInfo);
304 keyboardSession->GetSessionProperty()->SetDisplayId(0);
305 keyboardSession->keyboardCallback_->onNotifyOccupiedAreaChange = nullptr;
306 keyboardSession->NotifyRootSceneOccupiedAreaChange(occupiedInfo);
307 keyboardSession->keyboardCallback_ = nullptr;
308 keyboardSession->NotifyRootSceneOccupiedAreaChange(occupiedInfo);
309 }
310
311 /**
312 * @tc.name: RestoreCallingSession
313 * @tc.desc: RestoreCallingSession
314 * @tc.type: FUNC
315 */
316 HWTEST_F(KeyboardSessionTest, RestoreCallingSession, Function | SmallTest | Level1)
317 {
318 SessionInfo info;
319 info.abilityName_ = "RestoreCallingSession";
320 info.bundleName_ = "RestoreCallingSession";
321 sptr<SceneSession::SpecificSessionCallback> specificCb =
322 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
323 EXPECT_NE(specificCb, nullptr);
324 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
325 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
326 EXPECT_NE(keyboardCb, nullptr);
327 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
328 EXPECT_NE(keyboardSession, nullptr);
329
330 // callingSession is nullptr
331 keyboardSession->RestoreCallingSession();
332
333 // callingsession is not nullptr
334 info.windowType_ = 1; // 1 is main_window_type
335 sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, specificCb);
336 EXPECT_NE(callingSession, nullptr);
337 ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
338 keyboardSession->keyboardCallback_->onGetSceneSession =
__anonf548f40d0402(int32_t persistentId) 339 [callingSession](int32_t persistentId) -> sptr<SceneSession> {
340 return callingSession;
341 };
342 keyboardSession->RestoreCallingSession();
343 ASSERT_EQ(callingSession->GetOriPosYBeforeRaisedByKeyboard(), 0); // 0: default value
344
345 callingSession->SetOriPosYBeforeRaisedByKeyboard(100); // 100 is not default
346 ASSERT_NE(callingSession->property_, nullptr);
347 callingSession->property_->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
348 keyboardSession->RestoreCallingSession();
349 ASSERT_EQ(callingSession->GetOriPosYBeforeRaisedByKeyboard(), 0); // 0: default value
350 }
351
352 /**
353 * @tc.name: RestoreCallingSession02
354 * @tc.desc: RestoreCallingSession
355 * @tc.type: FUNC
356 */
357 HWTEST_F(KeyboardSessionTest, RestoreCallingSession02, Function | SmallTest | Level1)
358 {
359 SessionInfo info;
360 info.abilityName_ = "RestoreCallingSession02";
361 info.bundleName_ = "RestoreCallingSession02";
362 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
363 auto keyboardCb = sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
364 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
365 info.windowType_ = 1; // 1 is main_window_type
366 sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, specificCb);
367 ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
368 keyboardSession->keyboardCallback_->onGetSceneSession =
__anonf548f40d0502(int32_t persistentId) 369 [callingSession](int32_t persistentId) -> sptr<SceneSession> {
370 return callingSession;
371 };
372
373 keyboardSession->keyboardAvoidAreaActive_ = false;
374 keyboardSession->RestoreCallingSession();
375 ASSERT_EQ(callingSession->GetOriPosYBeforeRaisedByKeyboard(), 0); // 0: default value
376 }
377
378 /**
379 * @tc.name: UseFocusIdIfCallingSessionIdInvalid
380 * @tc.desc: UseFocusIdIfCallingSessionIdInvalid
381 * @tc.type: FUNC
382 */
383 HWTEST_F(KeyboardSessionTest, UseFocusIdIfCallingSessionIdInvalid, Function | SmallTest | Level1)
384 {
385 SessionInfo info;
386 info.abilityName_ = "UseFocusIdIfCallingSessionIdInvalid";
387 info.bundleName_ = "UseFocusIdIfCallingSessionIdInvalid";
388 sptr<SceneSession::SpecificSessionCallback> specificCb =
389 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
390 EXPECT_NE(specificCb, nullptr);
391 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
392 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
393 EXPECT_NE(keyboardCb, nullptr);
394 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
395 EXPECT_NE(keyboardSession, nullptr);
396
397 info.windowType_ = 1;
398 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
399 EXPECT_NE(sceneSession, nullptr);
400 auto id = sceneSession->GetPersistentId();
401 EXPECT_NE(id, 0);
402
403 keyboardSession->GetSessionProperty()->SetCallingSessionId(id);
404 keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
405 }
406
407 /**
408 * @tc.name: UpdateCallingSessionIdAndPosition
409 * @tc.desc: UpdateCallingSessionIdAndPosition
410 * @tc.type: FUNC
411 */
412 HWTEST_F(KeyboardSessionTest, UpdateCallingSessionIdAndPosition, Function | SmallTest | Level1)
413 {
414 SessionInfo info;
415 info.abilityName_ = "UpdateCallingSessionIdAndPosition";
416 info.bundleName_ = "UpdateCallingSessionIdAndPosition";
417 sptr<SceneSession::SpecificSessionCallback> specificCb =
418 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
419 EXPECT_NE(specificCb, nullptr);
420 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
421 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
422 EXPECT_NE(keyboardCb, nullptr);
423 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
424 EXPECT_NE(keyboardSession, nullptr);
425
426 info.windowType_ = 1;
427 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
428 EXPECT_NE(sceneSession, nullptr);
429 auto id = sceneSession->GetPersistentId();
430 EXPECT_NE(id, 0);
431
432 keyboardSession->UpdateCallingSessionIdAndPosition(id);
433 }
434
435 /**
436 * @tc.name: GetFocusedSessionId
437 * @tc.desc: GetFocusedSessionId
438 * @tc.type: FUNC
439 */
440 HWTEST_F(KeyboardSessionTest, GetFocusedSessionId, Function | SmallTest | Level1)
441 {
442 SessionInfo info;
443 info.abilityName_ = "GetFocusedSessionId";
444 info.bundleName_ = "GetFocusedSessionId";
445 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
446 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
447 EXPECT_NE(keyboardCb, nullptr);
__anonf548f40d0602() 448 keyboardCb->onGetFocusedSessionId = []() {
449 return 0;
450 };
451 EXPECT_NE(keyboardCb->onGetFocusedSessionId, nullptr);
452 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, keyboardCb);
453 ASSERT_EQ(INVALID_WINDOW_ID, keyboardSession->GetFocusedSessionId());
454
455 keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
456 ASSERT_EQ(INVALID_WINDOW_ID, keyboardSession->GetFocusedSessionId());
457 }
458
459 /**
460 * @tc.name: OnKeyboardPanelUpdated
461 * @tc.desc: OnKeyboardPanelUpdated
462 * @tc.type: FUNC
463 */
464 HWTEST_F(KeyboardSessionTest, OnKeyboardPanelUpdated, Function | SmallTest | Level1)
465 {
466 WLOGFI("OnKeyboardPanelUpdated begin!");
467 int ret = 0;
468
469 SessionInfo info;
470 info.abilityName_ = "OnKeyboardPanelUpdated";
471 info.bundleName_ = "OnKeyboardPanelUpdated";
472 sptr<SceneSession::SpecificSessionCallback> specificCb =
473 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
474 EXPECT_NE(specificCb, nullptr);
475 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
476 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
477 EXPECT_NE(keyboardCb, nullptr);
478 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
479 EXPECT_NE(keyboardSession, nullptr);
480 keyboardSession->isKeyboardPanelEnabled_ = false;
481 keyboardSession->OnKeyboardPanelUpdated();
482
483 keyboardSession->isKeyboardPanelEnabled_ = true;
484 keyboardSession->specificCallback_ = nullptr;
485 keyboardSession->OnKeyboardPanelUpdated();
486
487 keyboardSession->specificCallback_ = specificCb;
488 auto onUpdateAvoidArea = specificCb->onUpdateAvoidArea_;
489 if (onUpdateAvoidArea == nullptr) {
__anonf548f40d0702(const int32_t& id)490 onUpdateAvoidArea = [](const int32_t& id){};
491 }
492 specificCb->onUpdateAvoidArea_ = nullptr;
493 keyboardSession->OnKeyboardPanelUpdated();
494
495 specificCb->onUpdateAvoidArea_ = onUpdateAvoidArea;
496 keyboardSession->OnKeyboardPanelUpdated();
497
498 ASSERT_EQ(ret, 0);
499 WLOGFI("OnKeyboardPanelUpdated end!");
500 }
501
502 /**
503 * @tc.name: SetCallingSessionId
504 * @tc.desc: SetCallingSessionId
505 * @tc.type: FUNC
506 */
507 HWTEST_F(KeyboardSessionTest, SetCallingSessionId, Function | SmallTest | Level1)
508 {
509 WLOGFI("SetCallingSessionId begin!");
510 SessionInfo info;
511 info.abilityName_ = "SetCallingSessionId";
512 info.bundleName_ = "SetCallingSessionId";
513 sptr<SceneSession::SpecificSessionCallback> specificCb =
514 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
515 EXPECT_NE(specificCb, nullptr);
516 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
517 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
518 EXPECT_NE(keyboardCb, nullptr);
519 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
520 EXPECT_NE(keyboardSession, nullptr);
521
522 // keyboardCallback_->onGetSceneSession is nullptr, getCallingSession is nullptr
523 keyboardSession->SetCallingSessionId(0);
524 ASSERT_EQ(keyboardSession->GetCallingSessionId(), INVALID_SESSION_ID);
525
526 // getCallingSession is not nullptr
527 info.windowType_ = 1; // 1 is main_window_type
528 sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, specificCb);
529 EXPECT_NE(callingSession, nullptr);
530 ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
531 keyboardSession->keyboardCallback_->onGetSceneSession =
__anonf548f40d0802(int32_t persistenId)532 [callingSession](int32_t persistenId)->sptr<SceneSession> {
533 if (persistenId != 100) { // callingSession's persistentId is 100
534 return nullptr;
535 }
536 return callingSession;
537 };
__anonf548f40d0902()538 keyboardSession->keyboardCallback_->onGetFocusedSessionId = []()->int32_t {
539 return 100; // focusSession's persistentId is 100
540 };
541 keyboardSession->SetCallingSessionId(0);
542 ASSERT_EQ(keyboardSession->GetCallingSessionId(), 100); // 100 is callingSessionId
543
544 ASSERT_NE(keyboardSession->property_, nullptr);
545 keyboardSession->property_->SetCallingSessionId(INVALID_SESSION_ID);
__anonf548f40d0a02(int32_t callingSessionid) 546 keyboardSession->keyboardCallback_->onCallingSessionIdChange = [](int32_t callingSessionid) {};
547 keyboardSession->SetCallingSessionId(100);
548 ASSERT_EQ(keyboardSession->GetCallingSessionId(), 100); // 100 is callingSessionId
549 }
550
551 /**
552 * @tc.name: GetCallingSessionId
553 * @tc.desc: GetCallingSessionId
554 * @tc.type: FUNC
555 */
556 HWTEST_F(KeyboardSessionTest, GetCallingSessionId, Function | SmallTest | Level1)
557 {
558 SessionInfo info;
559 info.abilityName_ = "GetCallingSessionId";
560 info.bundleName_ = "GetCallingSessionId";
561 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
562 EXPECT_NE(keyboardSession, nullptr);
563 auto ret = keyboardSession->GetCallingSessionId();
564 ASSERT_EQ(ret, INVALID_WINDOW_ID);
565 }
566
567 /**
568 * @tc.name: GetCallingSessionId01
569 * @tc.desc: GetCallingSessionId01
570 * @tc.type: FUNC
571 */
572 HWTEST_F(KeyboardSessionTest, GetCallingSessionId01, Function | SmallTest | Level1)
573 {
574 SessionInfo info;
575 info.abilityName_ = "GetCallingSessionId";
576 info.bundleName_ = "GetCallingSessionId";
577 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
578 ASSERT_NE(keyboardSession, nullptr);
579 auto ret = keyboardSession->GetCallingSessionId();
580 EXPECT_EQ(ret, INVALID_SESSION_ID);
581 ASSERT_NE(keyboardSession->property_, nullptr);
582 keyboardSession->property_->SetCallingSessionId(1);
583 ret = keyboardSession->GetCallingSessionId();
584 EXPECT_EQ(ret, 1);
585 }
586
587 /**
588 * @tc.name: NotifySystemKeyboardAvoidChange
589 * @tc.desc: test NotifySystemKeyboardAvoidChange
590 * @tc.type: FUNC
591 */
592 HWTEST_F(KeyboardSessionTest, NotifySystemKeyboardAvoidChange, Function | SmallTest | Level1)
593 {
594 SessionInfo info;
595 info.abilityName_ = "NotifySystemKeyboardAvoidChange";
596 info.bundleName_ = "NotifySystemKeyboardAvoidChange";
597 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
598
599 keyboardSession->NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason::KEYBOARD_CREATED);
600 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
601
602 keyboardSession->systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
603 keyboardSession->NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason::KEYBOARD_CREATED);
604 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
605
606 keyboardSession->SetIsSystemKeyboard(true);
607 keyboardSession->NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason::KEYBOARD_CREATED);
608 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
609
610 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCallback =
611 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
612 keyboardSession->keyboardCallback_ = keyboardCallback;
613 keyboardSession->NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason::KEYBOARD_CREATED);
614 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
615
616 ASSERT_NE(nullptr, keyboardSession->keyboardCallback_);
617 keyboardSession->keyboardCallback_->onSystemKeyboardAvoidChange =
__anonf548f40d0b02(DisplayId displayId, SystemKeyboardAvoidChangeReason reason) 618 [](DisplayId displayId, SystemKeyboardAvoidChangeReason reason) {};
619 keyboardSession->NotifySystemKeyboardAvoidChange(SystemKeyboardAvoidChangeReason::KEYBOARD_CREATED);
620 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
621 }
622
623 /**
624 * @tc.name: ChangeKeyboardViewMode
625 * @tc.desc: test ChangeKeyboardViewMode
626 * @tc.type: FUNC
627 */
628 HWTEST_F(KeyboardSessionTest, ChangeKeyboardViewMode, Function | SmallTest | Level1)
629 {
630 SessionInfo info;
631 info.abilityName_ = "ChangeKeyboardViewMode";
632 info.bundleName_ = "ChangeKeyboardViewMode";
633 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, nullptr, nullptr);
634
635 auto result = KeyboardViewMode::NON_IMMERSIVE_MODE;
__anonf548f40d0c02(KeyboardViewMode mode) 636 keyboardSession->changeKeyboardViewModeFunc_ = [&result](KeyboardViewMode mode) {
637 result = mode;
638 };
639 keyboardSession->ChangeKeyboardViewMode(KeyboardViewMode::DARK_IMMERSIVE_MODE);
640 sleep(SPLIT_TEST_SLEEP_S);
641 ASSERT_EQ(result, KeyboardViewMode::DARK_IMMERSIVE_MODE);
642 auto mode = keyboardSession->property_->GetKeyboardViewMode();
643 ASSERT_EQ(mode, KeyboardViewMode::DARK_IMMERSIVE_MODE);
644 }
645 } // namespace
646 } // namespace Rosen
647 } // namespace OHOS
648