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 "screen_session_manager_client/include/screen_session_manager_client.h"
24 #include "session/host/include/scene_session.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31
32 class KeyboardSessionTest3 : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 private:
39 sptr<KeyboardSession> GetKeyboardSession(const std::string& abilityName, const std::string& bundleName);
40 sptr<SceneSession> GetSceneSession(const std::string& abilityName, const std::string& bundleName);
41 };
42
SetUpTestCase()43 void KeyboardSessionTest3::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void KeyboardSessionTest3::TearDownTestCase()
48 {
49 }
50
SetUp()51 void KeyboardSessionTest3::SetUp()
52 {
53 }
54
TearDown()55 void KeyboardSessionTest3::TearDown()
56 {
57 }
58
GetKeyboardSession(const std::string & abilityName,const std::string & bundleName)59 sptr<KeyboardSession> KeyboardSessionTest3::GetKeyboardSession(const std::string& abilityName,
60 const std::string& bundleName)
61 {
62 SessionInfo info;
63 info.abilityName_ = abilityName;
64 info.bundleName_ = bundleName;
65 sptr<SceneSession::SpecificSessionCallback> specificCb =
66 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
67 EXPECT_NE(specificCb, nullptr);
68 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCb =
69 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
70 EXPECT_NE(keyboardCb, nullptr);
71 sptr<KeyboardSession> keyboardSession = sptr<KeyboardSession>::MakeSptr(info, specificCb, keyboardCb);
72 EXPECT_NE(keyboardSession, nullptr);
73
74 sptr<WindowSessionProperty> keyboardProperty = sptr<WindowSessionProperty>::MakeSptr();
75 EXPECT_NE(keyboardProperty, nullptr);
76 keyboardProperty->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
77 keyboardSession->SetSessionProperty(keyboardProperty);
78
79 return keyboardSession;
80 }
81
GetSceneSession(const std::string & abilityName,const std::string & bundleName)82 sptr<SceneSession> KeyboardSessionTest3::GetSceneSession(const std::string& abilityName,
83 const std::string& bundleName)
84 {
85 SessionInfo info;
86 info.abilityName_ = abilityName;
87 info.bundleName_ = bundleName;
88 sptr<SceneSession::SpecificSessionCallback> specificCb =
89 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
90 EXPECT_NE(specificCb, nullptr);
91 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, specificCb);
92
93 return sceneSession;
94 }
95
96 namespace {
97 /**
98 * @tc.name: GetRSTransaction01
99 * @tc.desc: test function : GetRSTransaction
100 * @tc.type: FUNC
101 */
102 HWTEST_F(KeyboardSessionTest3, GetRSTransaction01, Function | SmallTest | Level1)
103 {
104 auto keyboardSession = GetKeyboardSession("GetRSTransaction01",
105 "GetRSTransaction01");
106 ASSERT_NE(keyboardSession, nullptr);
107
108 auto rsTransaction = keyboardSession->GetRSTransaction();
109 ASSERT_EQ(rsTransaction, nullptr);
110 }
111
112 /**
113 * @tc.name: GetSessionScreenName01
114 * @tc.desc: test function : GetSessionScreenName
115 * @tc.type: FUNC
116 */
117 HWTEST_F(KeyboardSessionTest3, GetSessionScreenName01, Function | SmallTest | Level1)
118 {
119 auto keyboardSession = GetKeyboardSession("GetSessionScreenName01",
120 "GetSessionScreenName01");
121 ASSERT_NE(keyboardSession, nullptr);
122 keyboardSession->property_ = nullptr;
123 auto resultStr = keyboardSession->GetSessionScreenName();
124 ASSERT_EQ(resultStr, "");
125
126 sptr<WindowSessionProperty> keyboardProperty = sptr<WindowSessionProperty>::MakeSptr();
127 ASSERT_NE(keyboardProperty, nullptr);
128 keyboardSession->property_ = keyboardProperty;
129 keyboardSession->property_->displayId_ = 100;
130
131 sptr<ScreenSession> screenSession = sptr<ScreenSession>::MakeSptr();
132 ASSERT_NE(screenSession, nullptr);
133 screenSession->name_ = "testScreenSession";
134 screenSession->screenId_ = 100;
135 ScreenSessionManagerClient::GetInstance().screenSessionMap_.emplace(100, screenSession);
136
137 resultStr = keyboardSession->GetSessionScreenName();
138 ASSERT_EQ(resultStr, screenSession->name_);
139 }
140
141 /**
142 * @tc.name: UseFocusIdIfCallingSessionIdInvalid01
143 * @tc.desc: test function : UseFocusIdIfCallingSessionIdInvalid
144 * @tc.type: FUNC
145 */
146 HWTEST_F(KeyboardSessionTest3, UseFocusIdIfCallingSessionIdInvalid01, Function | SmallTest | Level1)
147 {
148 auto keyboardSession = GetKeyboardSession("UseFocusIdIfCallingSessionIdInvalid01",
149 "UseFocusIdIfCallingSessionIdInvalid01");
150 ASSERT_NE(keyboardSession, nullptr);
151 sptr<KeyboardSession::KeyboardSessionCallback> keyboardCallback =
152 sptr<KeyboardSession::KeyboardSessionCallback>::MakeSptr();
153 ASSERT_NE(keyboardCallback, nullptr);
154 keyboardSession->keyboardCallback_ = keyboardCallback;
155 sptr<SceneSession> sceneSession = GetSceneSession("TestSceneSession", "TestSceneSession");
156 ASSERT_NE(sceneSession, nullptr);
157 sceneSession->persistentId_ = 100;
158 keyboardSession->keyboardCallback_->onGetSceneSession =
__anon9ea4a9400202(uint32_t callingSessionId)159 [sceneSession](uint32_t callingSessionId)->sptr<SceneSession> {
160 if (sceneSession->persistentId_ != callingSessionId) {
161 return nullptr;
162 }
163 return sceneSession;
164 };
165
166 keyboardSession->GetSessionProperty()->SetCallingSessionId(100);
167 keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
168 auto resultId = keyboardSession->GetCallingSessionId();
169 ASSERT_EQ(resultId, 100);
170
171 keyboardSession->GetSessionProperty()->SetCallingSessionId(101);
__anon9ea4a9400302()172 keyboardSession->keyboardCallback_->onGetFocusedSessionId = []()->int32_t {
173 return 100;
174 };
175 keyboardSession->UseFocusIdIfCallingSessionIdInvalid();
176 resultId = keyboardSession->GetCallingSessionId();
177 ASSERT_EQ(resultId, 100);
178 }
179
180 /**
181 * @tc.name: UpdateKeyboardAvoidArea01
182 * @tc.desc: test function : UpdateKeyboardAvoidArea
183 * @tc.type: FUNC
184 */
185 HWTEST_F(KeyboardSessionTest3, UpdateKeyboardAvoidArea01, Function | SmallTest | Level1)
186 {
187 auto keyboardSession = GetKeyboardSession("UpdateKeyboardAvoidArea01",
188 "UpdateKeyboardAvoidArea01");
189 ASSERT_NE(keyboardSession, nullptr);
190
191 // not foreground
192 keyboardSession->dirtyFlags_ = 0;
193 keyboardSession->state_ = SessionState::STATE_CONNECT;
194 keyboardSession->UpdateKeyboardAvoidArea();
195 ASSERT_EQ(keyboardSession->dirtyFlags_, 0);
196
197 // has callback
198 auto expectDirtyFlag = 0;
199 keyboardSession->dirtyFlags_ = 0;
200 keyboardSession->state_ = SessionState::STATE_FOREGROUND;
201 keyboardSession->isVisible_ = true;
__anon9ea4a9400402(const uint32_t& persistentId) 202 keyboardSession->specificCallback_->onUpdateAvoidArea_ = [&expectDirtyFlag](const uint32_t& persistentId) {
203 expectDirtyFlag = 1;
204 };
205 keyboardSession->UpdateKeyboardAvoidArea();
206 if (Session::IsScbCoreEnabled()) {
207 expectDirtyFlag = 0 | static_cast<uint32_t>(SessionUIDirtyFlag::AVOID_AREA);
208 ASSERT_EQ(keyboardSession->dirtyFlags_, expectDirtyFlag);
209 } else {
210 ASSERT_EQ(expectDirtyFlag, 1);
211 }
212
213 // miss callback
214 expectDirtyFlag = 0;
215 keyboardSession->dirtyFlags_ = 1;
216 keyboardSession->specificCallback_->onUpdateAvoidArea_ = nullptr;
217 keyboardSession->UpdateKeyboardAvoidArea();
218 if (Session::IsScbCoreEnabled()) {
219 ASSERT_EQ(keyboardSession->dirtyFlags_, 1);
220 } else {
221 ASSERT_EQ(expectDirtyFlag, 0);
222 }
223
224 expectDirtyFlag = 0;
225 keyboardSession->dirtyFlags_ = 2;
226 keyboardSession->specificCallback_ = nullptr;
227 keyboardSession->UpdateKeyboardAvoidArea();
228 if (Session::IsScbCoreEnabled()) {
229 ASSERT_EQ(keyboardSession->dirtyFlags_, 2);
230 } else {
231 ASSERT_EQ(expectDirtyFlag, 0);
232 }
233 }
234
235 /**
236 * @tc.name: MoveAndResizeKeyboard01
237 * @tc.desc: test function : MoveAndResizeKeyboard
238 * @tc.type: FUNC
239 */
240 HWTEST_F(KeyboardSessionTest3, MoveAndResizeKeyboard01, Function | SmallTest | Level1)
241 {
242 auto keyboardSession = GetKeyboardSession("MoveAndResizeKeyboard01",
243 "MoveAndResizeKeyboard01");
244 ASSERT_NE(keyboardSession, nullptr);
245
246 KeyboardLayoutParams param;
247 param.LandscapeKeyboardRect_ = { 100, 100, 100, 200 };
248 param.PortraitKeyboardRect_ = { 200, 200, 200, 100 };
249
250 // branch SESSION_GRAVITY_BOTTOM
251 param.gravity_ = WindowGravity::WINDOW_GRAVITY_BOTTOM;
252 Rect expectRect = param.PortraitKeyboardRect_;
253 keyboardSession->MoveAndResizeKeyboard(param, nullptr, false);
254 ASSERT_EQ(keyboardSession->property_->requestRect_, expectRect);
255 }
256
257 /**
258 * @tc.name: OnCallingSessionUpdated01
259 * @tc.desc: test function : OnCallingSessionUpdated
260 * @tc.type: FUNC
261 */
262 HWTEST_F(KeyboardSessionTest3, OnCallingSessionUpdated01, Function | SmallTest | Level1)
263 {
264 auto keyboardSession = GetKeyboardSession("OnCallingSessionUpdated01",
265 "OnCallingSessionUpdated01");
266 ASSERT_NE(keyboardSession, nullptr);
267
268 // keyboardSession is not foreground
269 keyboardSession->OnCallingSessionUpdated();
270 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_DISCONNECT);
271
272 // keyboardSession's isVisible_ is false
273 keyboardSession->state_ = SessionState::STATE_FOREGROUND;
274 keyboardSession->OnCallingSessionUpdated();
275 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
276
277 // keyboardSession's isVisible_ is true
278 keyboardSession->isVisible_ = true;
279 keyboardSession->OnCallingSessionUpdated();
280 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
281
282 // callingsession is not nullptr
283 sptr<SceneSession::SpecificSessionCallback> specificCb =
284 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
285 ASSERT_NE(specificCb, nullptr);
286 SessionInfo info;
287 info.abilityName_ = "OnCallingSessionUpdated01";
288 info.bundleName_ = "OnCallingSessionUpdated01";
289 info.windowType_ = 1; // 1 is main_window_type
290 sptr<SceneSession> callingSession = sptr<SceneSession>::MakeSptr(info, specificCb);
291 EXPECT_NE(callingSession, nullptr);
292 ASSERT_NE(keyboardSession->keyboardCallback_, nullptr);
293 keyboardSession->keyboardCallback_->onGetSceneSession =
__anon9ea4a9400502(int32_t persistentId)294 [callingSession](int32_t persistentId)->sptr<SceneSession> {
295 return callingSession;
296 };
297 // callingSession is fullScreen and isCallingSessionFloating is false
298 // keyboardSession's gravity is SessionGravity::SESSION_GRAVITY_DEFAULT
299 keyboardSession->OnCallingSessionUpdated();
300 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
301
302 // keyboardSession's gravity is WindowGravity::Window_GRAVITY_FLOAT
303 ASSERT_NE(keyboardSession->property_, nullptr);
304 keyboardSession->property_->keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT;
305
306 ASSERT_EQ(keyboardSession->GetKeyboardGravity(), SessionGravity::SESSION_GRAVITY_FLOAT);
307 keyboardSession->OnCallingSessionUpdated();
308 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_FOREGROUND);
309 }
310
311 /**
312 * @tc.name: OnCallingSessionUpdated02
313 * @tc.desc: test function : OnCallingSessionUpdated
314 * @tc.type: FUNC
315 */
316 HWTEST_F(KeyboardSessionTest3, OnCallingSessionUpdated02, Function | SmallTest | Level1)
317 {
318 auto keyboardSession = GetKeyboardSession("OnCallingSessionUpdated02",
319 "OnCallingSessionUpdated02");
320 ASSERT_EQ(true, keyboardSession->keyboardAvoidAreaActive_);
321 keyboardSession->OnCallingSessionUpdated();
322 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_DISCONNECT);
323
324 keyboardSession->ActivateKeyboardAvoidArea(false, false);
325 ASSERT_EQ(false, keyboardSession->keyboardAvoidAreaActive_);
326 keyboardSession->OnCallingSessionUpdated();
327 ASSERT_EQ(keyboardSession->state_, SessionState::STATE_DISCONNECT);
328 }
329 } // namespace
330 } // namespace Rosen
331 } // namespace OHOS
332