1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "root_scene_session.h"
18 #include "scene_session_manager.h"
19 #include "session_info.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 class RootSceneSessionTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 static sptr<SceneSessionManager> ssm_;
33 };
34
35 sptr<SceneSessionManager> RootSceneSessionTest::ssm_ = nullptr;
36
LoadContentFuncTest(const std::string &,napi_env,napi_value,AbilityRuntime::Context *)37 void LoadContentFuncTest(const std::string&, napi_env, napi_value, AbilityRuntime::Context*) {}
38
SetUpTestCase()39 void RootSceneSessionTest::SetUpTestCase()
40 {
41 ssm_ = &SceneSessionManager::GetInstance();
42 }
43
TearDownTestCase()44 void RootSceneSessionTest::TearDownTestCase()
45 {
46 ssm_ = nullptr;
47 }
48
SetUp()49 void RootSceneSessionTest::SetUp() {}
50
TearDown()51 void RootSceneSessionTest::TearDown() {}
52
53 namespace {
54 /**
55 * @tc.name: SetLoadContentFunc
56 * @tc.desc: test function : SetLoadContentFunc
57 * @tc.type: FUNC
58 */
59 HWTEST_F(RootSceneSessionTest, SetLoadContentFunc, TestSize.Level1)
60 {
61 RootSceneSession rootSceneSession;
62 RootSceneSession::LoadContentFunc loadContentFunc_ = LoadContentFuncTest;
63 rootSceneSession.SetLoadContentFunc(loadContentFunc_);
64 ASSERT_FALSE(rootSceneSession.IsVisible());
65 }
66
67 /**
68 * @tc.name: LoadContentFunc
69 * @tc.desc: test function : LoadContentFunc
70 * @tc.type: FUNC
71 */
72 HWTEST_F(RootSceneSessionTest, LoadContentFunc, TestSize.Level1)
73 {
74 RootSceneSession rootSceneSession;
75 std::string strTest("LoadContentFuncTest");
76 napi_env nativeEnv_ = nullptr;
77 napi_value nativeValue_ = nullptr;
78 AbilityRuntime::Context* conText_ = nullptr;
79 rootSceneSession.LoadContent(strTest, nativeEnv_, nativeValue_, conText_);
80 ASSERT_FALSE(rootSceneSession.IsVisible());
81 }
82
83 /**
84 * @tc.name: LoadContent
85 * @tc.desc: test function : LoadContent
86 * @tc.type: FUNC
87 */
88 HWTEST_F(RootSceneSessionTest, LoadContent, TestSize.Level1)
89 {
90 RootSceneSession rootSceneSession;
91 std::string strTest("LoadContentFuncTest");
92 napi_env nativeEnv_ = nullptr;
93 napi_value nativeValue_ = nullptr;
94 AbilityRuntime::Context* conText_ = nullptr;
95 RootSceneSession::LoadContentFunc loadContentFunc_ = LoadContentFuncTest;
96 rootSceneSession.SetLoadContentFunc(loadContentFunc_);
97 rootSceneSession.LoadContent(strTest, nativeEnv_, nativeValue_, conText_);
98 ASSERT_FALSE(rootSceneSession.IsVisible());
99 }
100
101 /**
102 * @tc.name: GetSystemAvoidAreaForRoot
103 * @tc.desc: test function : GetSystemAvoidAreaForRoot
104 * @tc.type: FUNC
105 */
106 HWTEST_F(RootSceneSessionTest, GetSystemAvoidAreaForRoot, TestSize.Level1)
107 {
108 RootSceneSession rootSceneSession;
109 AvoidArea avoidArea;
110 WSRect rect;
111 rootSceneSession.GetSystemAvoidAreaForRoot(rect, avoidArea);
112 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
113 }
114
115 /**
116 * @tc.name: GetKeyboardAvoidAreaForRoot
117 * @tc.desc: test function : GetKeyboardAvoidAreaForRoot
118 * @tc.type: FUNC
119 */
120 HWTEST_F(RootSceneSessionTest, GetKeyboardAvoidAreaForRoot, TestSize.Level1)
121 {
122 RootSceneSession rootSceneSession;
123 AvoidArea avoidArea;
124 WSRect rect;
125 rootSceneSession.GetKeyboardAvoidAreaForRoot(rect, avoidArea);
126 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
127 }
128
129 /**
130 * @tc.name: GetAINavigationBarAreaForRoot
131 * @tc.desc: test function : GetAINavigationBarAreaForRoot
132 * @tc.type: FUNC
133 */
134 HWTEST_F(RootSceneSessionTest, GetAINavigationBarAreaForRoot, TestSize.Level1)
135 {
136 RootSceneSession rootSceneSession;
137 AvoidArea avoidArea;
138 WSRect rect;
139 rootSceneSession.GetAINavigationBarAreaForRoot(rect, avoidArea);
140 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
141 }
142
143 /**
144 * @tc.name: GetCutoutAvoidAreaForRoot
145 * @tc.desc: test function : GetCutoutAvoidAreaForRoot
146 * @tc.type: FUNC
147 */
148 HWTEST_F(RootSceneSessionTest, GetCutoutAvoidAreaForRoot, TestSize.Level1)
149 {
150 RootSceneSession rootSceneSession;
151 AvoidArea avoidArea;
152 WSRect rect;
153 rootSceneSession.GetCutoutAvoidAreaForRoot(rect, avoidArea);
154 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
155 }
156
157 /**
158 * @tc.name: SetRootSessionRect
159 * @tc.desc: test function : SetRootSessionRect
160 * @tc.type: FUNC
161 */
162 HWTEST_F(RootSceneSessionTest, SetRootSessionRect, TestSize.Level1)
163 {
164 RootSceneSession rootSceneSession;
165 AvoidArea avoidArea;
166 WSRect rect;
167 rootSceneSession.SetRootSessionRect(rect);
168 ASSERT_EQ(rootSceneSession.GetSessionRect(), rect);
169 }
170
171 /**
172 * @tc.name: UpdateAvoidArea
173 * @tc.desc: test function : UpdateAvoidArea
174 * @tc.type: FUNC
175 */
176 HWTEST_F(RootSceneSessionTest, UpdateAvoidArea, TestSize.Level1)
177 {
178 RootSceneSession rootSceneSession;
179 AvoidArea avoidArea;
180 auto ret = rootSceneSession.UpdateAvoidArea(sptr<AvoidArea>::MakeSptr(avoidArea), AvoidAreaType::TYPE_SYSTEM);
181 ASSERT_EQ(WSError::WS_ERROR_NULLPTR, ret);
182 }
183
184 /**
185 * @tc.name: GetAvoidAreaByType
186 * @tc.desc: test function : GetAvoidAreaByType
187 * @tc.type: FUNC
188 */
189 HWTEST_F(RootSceneSessionTest, GetAvoidAreaByType, TestSize.Level1)
190 {
191 ASSERT_NE(nullptr, ssm_);
192 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr();
193 auto avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_SYSTEM, { 0, 0, 0, 0 }, 1);
194 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
195 avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_CUTOUT, { 0, 0, 0, 0 }, 1);
196 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
197 avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_SYSTEM_GESTURE, { 0, 0, 0, 0 }, 1);
198 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
199 avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_KEYBOARD, { 0, 0, 0, 0 }, 1);
200 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
201 avoidArea =
202 ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_NAVIGATION_INDICATOR, { 0, 0, 0, 0 }, 1);
203 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
204 avoidArea = ssm_->rootSceneSession_->GetAvoidAreaByType(AvoidAreaType::TYPE_END, { 0, 0, 0, 0 }, 1);
205 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
206 }
207
208 /**
209 * @tc.name: GetSystemAvoidAreaForRoot_01
210 * @tc.desc: test function : GetSystemAvoidAreaForRoot_01
211 * @tc.type: FUNC
212 */
213 HWTEST_F(RootSceneSessionTest, GetSystemAvoidAreaForRoot_01, TestSize.Level1)
214 {
215 ASSERT_NE(nullptr, ssm_);
216 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
__anone78ad00f0202(WindowType type, uint64_t displayId) 217 specificCb->onGetSceneSessionVectorByTypeAndDisplayId_ = [](WindowType type, uint64_t displayId) {
218 return ssm_->GetSceneSessionVectorByTypeAndDisplayId(type, displayId);
219 };
220 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr(specificCb);
221 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
__anone78ad00f0302(DisplayId displayId, WSRect& barArea) 222 ssm_->rootSceneSession_->onGetStatusBarAvoidHeightFunc_ = [](DisplayId displayId, WSRect& barArea) {};
223 SessionInfo statusBarSessionInfo;
224 statusBarSessionInfo.abilityName_ = "statusBar";
225 statusBarSessionInfo.bundleName_ = "statusBar";
226 statusBarSessionInfo.screenId_ = 0;
227 sptr<SceneSession> statusBarSession = sptr<SceneSession>::MakeSptr(statusBarSessionInfo, nullptr);
228 statusBarSession->property_->SetPersistentId(2);
229 statusBarSession->property_->type_ = WindowType::WINDOW_TYPE_STATUS_BAR;
230 statusBarSession->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 123 });
231 statusBarSession->isVisible_ = true;
232 ssm_->sceneSessionMap_.insert({ statusBarSession->GetPersistentId(), statusBarSession });
233 AvoidArea avoidArea;
234 ssm_->rootSceneSession_->GetSystemAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
235 Rect rect = { 0, 0, 1260, 123 };
236 ASSERT_EQ(avoidArea.topRect_, rect);
237 statusBarSession->isVisible_ = false;
238 avoidArea.topRect_ = { 0, 0, 0, 0 };
239 ssm_->rootSceneSession_->GetSystemAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
240 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
241 }
242
243 /**
244 * @tc.name: GetKeyboardAvoidAreaForRoot_01
245 * @tc.desc: test function : GetKeyboardAvoidAreaForRoot_01
246 * @tc.type: FUNC
247 */
248 HWTEST_F(RootSceneSessionTest, GetKeyboardAvoidAreaForRoot_01, TestSize.Level1)
249 {
250 ASSERT_NE(nullptr, ssm_);
251 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
__anone78ad00f0402(WindowType type) 252 specificCb->onGetSceneSessionVectorByType_ = [](WindowType type) {
253 return ssm_->GetSceneSessionVectorByType(type);
254 };
255 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr(specificCb);
256 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
257 ssm_->rootSceneSession_->isKeyboardPanelEnabled_ = false;
258 SessionInfo keyboardSessionInfo;
259 keyboardSessionInfo.abilityName_ = "keyboard";
260 keyboardSessionInfo.bundleName_ = "keyboard";
261 keyboardSessionInfo.screenId_ = 0;
262 sptr<SceneSession> keyboardSession = sptr<SceneSession>::MakeSptr(keyboardSessionInfo, nullptr);
263 ssm_->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
264 keyboardSession->state_ = SessionState::STATE_FOREGROUND;
265 keyboardSession->keyboardAvoidAreaActive_ = true;
266 keyboardSession->property_->type_ = WindowType::WINDOW_TYPE_INPUT_METHOD_FLOAT;
267 keyboardSession->GetLayoutController()->SetSessionRect({ 0, 1700, 1260, 1020 });
268 keyboardSession->property_->SetPersistentId(2);
269 ssm_->sceneSessionMap_.insert({ keyboardSession->GetPersistentId(), keyboardSession });
270 AvoidArea avoidArea;
271 ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
272 Rect rect = { 0, 1700, 1260, 1020 };
273 EXPECT_EQ(avoidArea.bottomRect_, rect);
274 avoidArea.bottomRect_ = { 0, 0, 0, 0 };
275 ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
276 EXPECT_TRUE(!avoidArea.isEmptyAvoidArea());
277 ssm_->rootSceneSession_->isKeyboardPanelEnabled_ = true;
278 keyboardSession->state_ = SessionState::STATE_BACKGROUND;
279 avoidArea.bottomRect_ = { 0, 0, 0, 0 };
280 ssm_->rootSceneSession_->GetKeyboardAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
281 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
282 }
283
284 /**
285 * @tc.name: GetCutoutAvoidAreaForRoot_01
286 * @tc.desc: test function : GetCutoutAvoidAreaForRoot_01
287 * @tc.type: FUNC
288 */
289 HWTEST_F(RootSceneSessionTest, GetCutoutAvoidAreaForRoot_01, TestSize.Level1)
290 {
291 ASSERT_NE(nullptr, ssm_);
292 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr();
293 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
294 ssm_->rootSceneSession_->property_->displayId_ = -1;
295 AvoidArea avoidArea;
296 ssm_->rootSceneSession_->GetCutoutAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
297 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
298 ssm_->rootSceneSession_->property_->displayId_ = 0;
299 ssm_->rootSceneSession_->GetCutoutAvoidAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
300 }
301
302 /**
303 * @tc.name: GetAINavigationBarAreaForRoot_01
304 * @tc.desc: test function : GetAINavigationBarAreaForRoot_01
305 * @tc.type: FUNC
306 */
307 HWTEST_F(RootSceneSessionTest, GetAINavigationBarAreaForRoot_01, TestSize.Level1)
308 {
309 ASSERT_NE(nullptr, ssm_);
310 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
__anone78ad00f0502(uint64_t displayId) 311 specificCb->onGetAINavigationBarArea_ = [](uint64_t displayId) { return ssm_->GetAINavigationBarArea(displayId); };
312 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr(specificCb);
313 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
314 AvoidArea avoidArea;
315 ssm_->rootSceneSession_->GetAINavigationBarAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
316 EXPECT_TRUE(avoidArea.isEmptyAvoidArea());
317 ssm_->currAINavigationBarAreaMap_[0] = { 409, 2629, 442, 91 };
318 ssm_->rootSceneSession_->GetAINavigationBarAreaForRoot(ssm_->rootSceneSession_->GetSessionRect(), avoidArea);
319 Rect rect = { 409, 2629, 442, 91 };
320 ASSERT_EQ(avoidArea.bottomRect_, rect);
321 }
322
323 /**
324 * @tc.name: SetRootSessionRect_01
325 * @tc.desc: test function : SetRootSessionRect_01
326 * @tc.type: FUNC
327 */
328 HWTEST_F(RootSceneSessionTest, SetRootSessionRect_01, TestSize.Level1)
329 {
330 ASSERT_NE(nullptr, ssm_);
331 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr();
332 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
333 ssm_->rootSceneSession_->SetRootSessionRect({ 0, 0, 1260, 2720 });
334 WSRect rect = { 0, 0, 1260, 2720 };
335 ASSERT_EQ(ssm_->rootSceneSession_->GetSessionRect(), rect);
336 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 2720, 1260 });
337 ssm_->rootSceneSession_->SetRootSessionRect({ 0, 0, 1260, 2720 });
338 rect = { 0, 0, 1260, 2720 };
339 ASSERT_EQ(ssm_->rootSceneSession_->GetSessionRect(), rect);
340 }
341
342 /**
343 * @tc.name: UpdateAvoidArea_01
344 * @tc.desc: test function : UpdateAvoidArea_01
345 * @tc.type: FUNC
346 */
347 HWTEST_F(RootSceneSessionTest, UpdateAvoidArea_01, TestSize.Level1)
348 {
349 ASSERT_NE(nullptr, ssm_);
350 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
__anone78ad00f0602(const sptr<AvoidArea>& avoidArea, AvoidAreaType type) 351 specificCb->onNotifyAvoidAreaChange_ = [](const sptr<AvoidArea>& avoidArea, AvoidAreaType type) {};
352 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr(specificCb);
353 AvoidArea avoidArea;
354 auto ret = ssm_->rootSceneSession_->UpdateAvoidArea(new AvoidArea(avoidArea), AvoidAreaType::TYPE_SYSTEM);
355 ASSERT_EQ(ret, WSError::WS_OK);
356 }
357
358 /**
359 * @tc.name: GetStatusBarHeight
360 * @tc.desc: normal function
361 * @tc.type: FUNC
362 */
363 HWTEST_F(RootSceneSessionTest, GetStatusBarHeight, TestSize.Level1)
364 {
365 ASSERT_NE(nullptr, ssm_);
366 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr();
367 auto height = ssm_->rootSceneSession_->GetStatusBarHeight();
368 EXPECT_EQ(0, height);
369 auto specificCb = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
370 ASSERT_NE(specificCb, nullptr);
__anone78ad00f0702(WindowType type, uint64_t displayId) 371 specificCb->onGetSceneSessionVectorByTypeAndDisplayId_ = [](WindowType type, uint64_t displayId) {
372 return ssm_->GetSceneSessionVectorByTypeAndDisplayId(type, displayId);
373 };
374 ssm_->rootSceneSession_ = sptr<RootSceneSession>::MakeSptr(specificCb);
375 ssm_->rootSceneSession_->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 2720 });
376
377 SessionInfo statusBarSessionInfo;
378 statusBarSessionInfo.abilityName_ = "statusBar";
379 statusBarSessionInfo.bundleName_ = "statusBar";
380 statusBarSessionInfo.screenId_ = 0;
381 sptr<SceneSession> statusBarSession = sptr<SceneSession>::MakeSptr(statusBarSessionInfo, nullptr);
382 statusBarSession->property_->SetPersistentId(2);
383 statusBarSession->property_->type_ = WindowType::WINDOW_TYPE_STATUS_BAR;
384 statusBarSession->GetLayoutController()->SetSessionRect({ 0, 0, 1260, 123 });
385 statusBarSession->isVisible_ = true;
386 ssm_->sceneSessionMap_.insert({ statusBarSession->GetPersistentId(), statusBarSession });
387 height = ssm_->rootSceneSession_->GetStatusBarHeight();
388 EXPECT_EQ(123, height);
389 ssm_->rootSceneSession_->onGetStatusBarAvoidHeightFunc_ = [](DisplayId displayId, WSRect& barArea)
__anone78ad00f0802(DisplayId displayId, WSRect& barArea) 390 { barArea.height_ = 100; };
391 height = ssm_->rootSceneSession_->GetStatusBarHeight();
392 EXPECT_EQ(100, height);
393 }
394 } // namespace
395 } // namespace Rosen
396 } // namespace OHOS
397