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