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 <event_handler.h>
17 #include <gtest/gtest.h>
18 #include <input_manager.h>
19 #include <ui_content.h>
20 #include <viewport_config.h>
21 #include "root_scene.h"
22
23 #include "app_mgr_client.h"
24 #include "mock_uicontent.h"
25 #include "singleton.h"
26 #include "singleton_container.h"
27
28 #include "vsync_station.h"
29 #include "window_manager_hilog.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Rosen {
36 const uint32_t MOCK_LEM_SUB_WIDTH = 340;
37 const uint32_t MOCK_LEM_SUB_HEIGHT = 340;
38
39 class RootSceneTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45
46 private:
47 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
48 };
49
SetUpTestCase()50 void RootSceneTest::SetUpTestCase() {}
51
TearDownTestCase()52 void RootSceneTest::TearDownTestCase() {}
53
SetUp()54 void RootSceneTest::SetUp() {}
55
TearDown()56 void RootSceneTest::TearDown()
57 {
58 usleep(WAIT_SYNC_IN_NS);
59 }
60
61 namespace {
62 /**
63 * @tc.name: LoadContent01
64 * @tc.desc: context is nullptr
65 * @tc.type: FUNC
66 */
67 HWTEST_F(RootSceneTest, LoadContent01, Function | SmallTest | Level3)
68 {
69 RootScene rootScene;
70 rootScene.LoadContent("a", nullptr, nullptr, nullptr);
71 ASSERT_EQ(1, rootScene.GetWindowId());
72 }
73
74 /**
75 * @tc.name: UpdateViewportConfig01
76 * @tc.desc: UpdateViewportConfig Test
77 * @tc.type: FUNC
78 */
79 HWTEST_F(RootSceneTest, UpdateViewportConfig01, Function | SmallTest | Level3)
80 {
81 RootScene rootScene;
82 Rect rect;
83
84 rootScene.uiContent_ = nullptr;
85 rootScene.UpdateViewportConfig(rect, WindowSizeChangeReason::UNDEFINED);
86
87 rect.width_ = MOCK_LEM_SUB_WIDTH;
88 rect.height_ = MOCK_LEM_SUB_HEIGHT;
89 rootScene.UpdateViewportConfig(rect, WindowSizeChangeReason::UNDEFINED);
90 ASSERT_EQ(1, rootScene.GetWindowId());
91 }
92
93 /**
94 * @tc.name: UpdateConfiguration
95 * @tc.desc: UpdateConfiguration Test
96 * @tc.type: FUNC
97 */
98 HWTEST_F(RootSceneTest, UpdateConfiguration, Function | SmallTest | Level3)
99 {
100 RootScene rootScene;
101 std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
102
103 rootScene.uiContent_ = nullptr;
104 rootScene.UpdateConfiguration(configuration);
105 ASSERT_EQ(1, rootScene.GetWindowId());
106 }
107
108 /**
109 * @tc.name: UpdateConfigurationForSpecified
110 * @tc.desc: UpdateConfigurationForSpecified Test
111 * @tc.type: FUNC
112 */
113 HWTEST_F(RootSceneTest, UpdateConfigurationForSpecified, Function | SmallTest | Level3)
114 {
115 RootScene rootScene;
116 std::shared_ptr<AppExecFwk::Configuration> configuration;
117 std::shared_ptr<Global::Resource::ResourceManager> resourceManager;
118
119 rootScene.uiContent_ = std::make_unique<Ace::UIContentMocker>();
120 rootScene.UpdateConfigurationForSpecified(configuration, resourceManager);
121 ASSERT_EQ(1, rootScene.GetWindowId());
122 }
123
124 /**
125 * @tc.name: UpdateConfigurationForAll
126 * @tc.desc: UpdateConfigurationForAll Test
127 * @tc.type: FUNC
128 */
129 HWTEST_F(RootSceneTest, UpdateConfigurationForAll, Function | SmallTest | Level3)
130 {
131 RootScene rootScene;
132 std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
133
134 auto prevStaticRootScene = RootScene::staticRootScene_;
135 rootScene.UpdateConfigurationForAll(configuration);
136
137 sptr<RootScene> staticRootScene;
138 RootScene::staticRootScene_ = staticRootScene;
139 rootScene.UpdateConfigurationForAll(configuration);
140
141 RootScene::staticRootScene_ = prevStaticRootScene;
142 ASSERT_EQ(1, rootScene.GetWindowId());
143 }
144
145 /**
146 * @tc.name: RegisterInputEventListener01
147 * @tc.desc: RegisterInputEventListener Test
148 * @tc.type: FUNC
149 */
150 HWTEST_F(RootSceneTest, RegisterInputEventListener01, Function | SmallTest | Level3)
151 {
152 RootScene rootScene;
153 rootScene.RegisterInputEventListener();
154 ASSERT_EQ(1, rootScene.GetWindowId());
155 }
156
157 /**
158 * @tc.name: RequestVsyncErr
159 * @tc.desc: RequestVsync Test Err
160 * @tc.type: FUNC
161 */
162 HWTEST_F(RootSceneTest, RequestVsyncErr, Function | SmallTest | Level3)
163 {
164 RootScene rootScene;
165 std::shared_ptr<VsyncCallback> vsyncCallback = std::make_shared<VsyncCallback>();
166 rootScene.RequestVsync(vsyncCallback);
167 ASSERT_EQ(1, rootScene.GetWindowId());
168 }
169
170 /**
171 * @tc.name: GetVSyncPeriod
172 * @tc.desc: GetVSyncPeriod Test
173 * @tc.type: FUNC
174 */
175 HWTEST_F(RootSceneTest, GetVSyncPeriod, Function | SmallTest | Level3)
176 {
177 RootScene rootScene;
178 rootScene.GetVSyncPeriod();
179 ASSERT_EQ(1, rootScene.GetWindowId());
180 }
181
182 /**
183 * @tc.name: FlushFrameRate
184 * @tc.desc: FlushFrameRate Test
185 * @tc.type: FUNC
186 */
187 HWTEST_F(RootSceneTest, FlushFrameRate, Function | SmallTest | Level3)
188 {
189 RootScene rootScene;
190 uint32_t rate = 120;
191 int32_t animatorExpectedFrameRate = -1;
192 rootScene.FlushFrameRate(rate, animatorExpectedFrameRate);
193 ASSERT_EQ(1, rootScene.GetWindowId());
194 }
195
196 /**
197 * @tc.name: SetFrameLayoutFinishCallback
198 * @tc.desc: SetFrameLayoutFinishCallback Test
199 * @tc.type: FUNC
200 */
201 HWTEST_F(RootSceneTest, SetFrameLayoutFinishCallback, Function | SmallTest | Level3)
202 {
203 RootScene rootScene;
204
205 rootScene.SetFrameLayoutFinishCallback(nullptr);
206 ASSERT_EQ(1, rootScene.GetWindowId());
207 }
208
209 /**
210 * @tc.name: SetUiDvsyncSwitch
211 * @tc.desc: SetUiDvsyncSwitch Test
212 * @tc.type: FUNC
213 */
214 HWTEST_F(RootSceneTest, SetUiDvsyncSwitchSucc, Function | SmallTest | Level3)
215 {
216 RootScene rootScene;
217 rootScene.SetUiDvsyncSwitch(true);
218 rootScene.SetUiDvsyncSwitch(false);
219 ASSERT_EQ(1, rootScene.GetWindowId());
220 }
221
222 /**
223 * @tc.name: SetUiDvsyncSwitch
224 * @tc.desc: SetUiDvsyncSwitch Test
225 * @tc.type: FUNC
226 */
227 HWTEST_F(RootSceneTest, SetUiDvsyncSwitchErr, Function | SmallTest | Level3)
228 {
229 RootScene rootScene;
230 rootScene.SetUiDvsyncSwitch(true);
231 rootScene.SetUiDvsyncSwitch(false);
232 ASSERT_EQ(1, rootScene.GetWindowId());
233 }
234
235 /**
236 * @tc.name: IsLastFrameLayoutFinished
237 * @tc.desc: IsLastFrameLayoutFinished Test
238 * @tc.type: FUNC
239 */
240 HWTEST_F(RootSceneTest, IsLastFrameLayoutFinished, Function | SmallTest | Level3)
241 {
242 RootScene rootScene;
243 auto ret = rootScene.IsLastFrameLayoutFinished();
244 ASSERT_EQ(ret, true);
245 ASSERT_EQ(1, rootScene.GetWindowId());
246 }
247
248 /**
249 * @tc.name: OnFlushUIParams
250 * @tc.desc: OnFlushUIParams Test
251 * @tc.type: FUNC
252 */
253 HWTEST_F(RootSceneTest, OnFlushUIParams, Function | SmallTest | Level3)
254 {
255 RootScene rootScene;
256 rootScene.OnFlushUIParams();
257 ASSERT_EQ(1, rootScene.GetWindowId());
258 }
259
260 /**
261 * @tc.name: OnBundleUpdated
262 * @tc.desc: OnBundleUpdated Test
263 * @tc.type: FUNC
264 */
265 HWTEST_F(RootSceneTest, OnBundleUpdated, Function | SmallTest | Level3)
266 {
267 RootScene rootScene;
268 std::string bundleName = "test";
269 rootScene.OnBundleUpdated(bundleName);
270 ASSERT_EQ(1, rootScene.GetWindowId());
271 }
272
273 /**
274 * @tc.name: SetDisplayOrientation
275 * @tc.desc: SetDisplayOrientation Test01
276 * @tc.type: FUNC
277 */
278 HWTEST_F(RootSceneTest, SetDisplayOrientationTest01, Function | SmallTest | Level3)
279 {
280 RootScene rootScene;
281 int32_t orientation = 0;
282 rootScene.SetDisplayOrientation(orientation);
283 ASSERT_EQ(1, rootScene.GetWindowId());
284 }
285
286 /**
287 * @tc.name: SetDisplayOrientation
288 * @tc.desc: SetDisplayOrientation Test02
289 * @tc.type: FUNC
290 */
291 HWTEST_F(RootSceneTest, SetDisplayOrientationTest02, Function | SmallTest | Level3)
292 {
293 RootScene rootScene;
294 int32_t orientation = 100;
295 rootScene.SetDisplayOrientation(orientation);
296 ASSERT_EQ(1, rootScene.GetWindowId());
297 }
298
299 /**
300 * @tc.name: RegisterAvoidAreaChangeListener
301 * @tc.desc: RegisterAvoidAreaChangeListener Test
302 * @tc.type: FUNC
303 */
304 HWTEST_F(RootSceneTest, RegisterAvoidAreaChangeListener, Function | SmallTest | Level3)
305 {
306 RootScene rootScene;
__anon11b50b290202null307 rootScene.updateRootSceneAvoidAreaCallback_ = [] {};
308 sptr<IAvoidAreaChangedListener> listener = sptr<IAvoidAreaChangedListener>::MakeSptr();
309 auto ret = rootScene.RegisterAvoidAreaChangeListener(listener);
310 ASSERT_EQ(WMError::WM_OK, ret);
311 listener = nullptr;
312 ret = rootScene.RegisterAvoidAreaChangeListener(listener);
313 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
314 }
315
316 /**
317 * @tc.name: UnregisterAvoidAreaChangeListener
318 * @tc.desc: UnregisterAvoidAreaChangeListener Test
319 * @tc.type: FUNC
320 */
321 HWTEST_F(RootSceneTest, UnregisterAvoidAreaChangeListener, Function | SmallTest | Level3)
322 {
323 RootScene rootScene;
324 sptr<IAvoidAreaChangedListener> listener = sptr<IAvoidAreaChangedListener>::MakeSptr();
325 auto ret = rootScene.UnregisterAvoidAreaChangeListener(listener);
326 ASSERT_EQ(WMError::WM_OK, ret);
327 listener = nullptr;
328 ret = rootScene.UnregisterAvoidAreaChangeListener(listener);
329 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
330 }
331
332 /**
333 * @tc.name: NotifyAvoidAreaChangeForRoot
334 * @tc.desc: NotifyAvoidAreaChangeForRoot Test
335 * @tc.type: FUNC
336 */
337 HWTEST_F(RootSceneTest, NotifyAvoidAreaChangeForRoot, Function | SmallTest | Level3)
338 {
339 RootScene rootScene;
340 sptr<IAvoidAreaChangedListener> listener = sptr<IAvoidAreaChangedListener>::MakeSptr();
341 ASSERT_NE(nullptr, listener);
342 rootScene.avoidAreaChangeListeners_.insert(listener);
343 AvoidAreaType type = AvoidAreaType::TYPE_SYSTEM_GESTURE;
344 AvoidArea avoidArea;
345 rootScene.NotifyAvoidAreaChangeForRoot(new AvoidArea(avoidArea), type);
346 }
347
348 /**
349 * @tc.name: GetAvoidAreaByType
350 * @tc.desc: GetAvoidAreaByType Test err
351 * @tc.type: FUNC
352 */
353 HWTEST_F(RootSceneTest, GetAvoidAreaByType, Function | SmallTest | Level3)
354 {
355 RootScene rootScene;
356 AvoidAreaType type = AvoidAreaType::TYPE_SYSTEM_GESTURE;
357 AvoidArea avoidArea;
358
359 auto ret = rootScene.GetAvoidAreaByType(type, avoidArea);
360 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
361 }
362
363 /**
364 * @tc.name: UpdateConfigurationSync
365 * @tc.desc: UpdateConfigurationSync Test
366 * @tc.type: FUNC
367 */
368 HWTEST_F(RootSceneTest, UpdateConfigurationSync, Function | SmallTest | Level3)
369 {
370 RootScene rootScene;
371 std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
372
373 rootScene.uiContent_ = nullptr;
374 rootScene.UpdateConfigurationSync(configuration);
375 ASSERT_EQ(1, rootScene.GetWindowId());
376 }
377
378 /**
379 * @tc.name: UpdateConfigurationSyncForAll
380 * @tc.desc: UpdateConfigurationSyncForAll Test
381 * @tc.type: FUNC
382 */
383 HWTEST_F(RootSceneTest, UpdateConfigurationSyncForAll, Function | SmallTest | Level3)
384 {
385 RootScene rootScene;
386 std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
387
388 auto prevStaticRootScene = RootScene::staticRootScene_;
389 rootScene.UpdateConfigurationSyncForAll(configuration);
390
391 sptr<RootScene> staticRootScene;
392 RootScene::staticRootScene_ = staticRootScene;
393 rootScene.UpdateConfigurationSyncForAll(configuration);
394
395 RootScene::staticRootScene_ = prevStaticRootScene;
396 ASSERT_EQ(1, rootScene.GetWindowId());
397 }
398
399 /**
400 * @tc.name: IsSystemWindow
401 * @tc.desc: IsSystemWindow Test
402 * @tc.type: FUNC
403 */
404 HWTEST_F(RootSceneTest, IsSystemWindow, Function | SmallTest | Level3)
405 {
406 RootScene rootScene;
407 bool res = rootScene.IsSystemWindow();
408 ASSERT_EQ(true, res);
409 }
410
411 /**
412 * @tc.name: IsAppWindow
413 * @tc.desc: IsAppWindow Test
414 * @tc.type: FUNC
415 */
416 HWTEST_F(RootSceneTest, IsAppWindow, Function | SmallTest | Level3)
417 {
418 RootScene rootScene;
419 bool res = rootScene.IsAppWindow();
420 ASSERT_EQ(false, res);
421 }
422
423 /**
424 * @tc.name: RegisterOccupiedAreaChangeListener
425 * @tc.desc: RegisterOccupiedAreaChangeListener Test
426 * @tc.type: FUNC
427 */
428 HWTEST_F(RootSceneTest, RegisterOccupiedAreaChangeListener, Function | SmallTest | Level3)
429 {
430 RootScene rootScene;
431 sptr<IOccupiedAreaChangeListener> listener = sptr<IOccupiedAreaChangeListener>::MakeSptr();
432 auto ret = rootScene.RegisterOccupiedAreaChangeListener(listener);
433 ASSERT_EQ(WMError::WM_OK, ret);
434 listener = nullptr;
435 ret = rootScene.RegisterOccupiedAreaChangeListener(listener);
436 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
437 }
438
439 /**
440 * @tc.name: UnregisterOccupiedAreaChangeListener
441 * @tc.desc: UnregisterOccupiedAreaChangeListener Test
442 * @tc.type: FUNC
443 */
444 HWTEST_F(RootSceneTest, UnregisterOccupiedAreaChangeListener, Function | SmallTest | Level3)
445 {
446 RootScene rootScene;
447 sptr<IOccupiedAreaChangeListener> listener = sptr<IOccupiedAreaChangeListener>::MakeSptr();
448 auto ret = rootScene.UnregisterOccupiedAreaChangeListener(listener);
449 ASSERT_EQ(WMError::WM_OK, ret);
450 listener = nullptr;
451 ret = rootScene.UnregisterOccupiedAreaChangeListener(listener);
452 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, ret);
453 }
454
455 /**
456 * @tc.name: NotifyOccupiedAreaChangeForRoot
457 * @tc.desc: NotifyOccupiedAreaChangeForRoot Test
458 * @tc.type: FUNC
459 */
460 HWTEST_F(RootSceneTest, NotifyOccupiedAreaChangeForRoot, Function | SmallTest | Level3)
461 {
462 auto rootScene = sptr<RootScene>::MakeSptr();
463 sptr<IOccupiedAreaChangeListener> listener = sptr<IOccupiedAreaChangeListener>::MakeSptr();
464 ASSERT_NE(nullptr, listener);
465 auto ret = rootScene->RegisterOccupiedAreaChangeListener(listener);
466 ASSERT_EQ(WMError::WM_OK, ret);
467 sptr<OccupiedAreaChangeInfo> info = nullptr;
468 rootScene->NotifyOccupiedAreaChangeForRoot(info);
469 info = sptr<OccupiedAreaChangeInfo>::MakeSptr();
470 ASSERT_NE(nullptr, info);
471 rootScene->NotifyOccupiedAreaChangeForRoot(info);
472 }
473 } // namespace
474 } // namespace Rosen
475 } // namespace OHOS