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.h"
18 #include <event_handler.h>
19 #include <input_manager.h>
20 #include <ui_content.h>
21 #include <viewport_config.h>
22 
23 #include "app_mgr_client.h"
24 #include "singleton.h"
25 #include "singleton_container.h"
26 
27 #include "vsync_station.h"
28 #include "window_manager_hilog.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace Rosen {
35 const uint32_t MOCK_LEM_SUB_WIDTH = 340;
36 const uint32_t MOCK_LEM_SUB_HEIGHT = 340;
37 
38 class RootSceneTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp() override;
43     void TearDown() override;
44 private:
45     static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
46 };
47 
SetUpTestCase()48 void RootSceneTest::SetUpTestCase()
49 {
50 }
51 
TearDownTestCase()52 void RootSceneTest::TearDownTestCase()
53 {
54 }
55 
SetUp()56 void RootSceneTest::SetUp()
57 {
58 }
59 
TearDown()60 void RootSceneTest::TearDown()
61 {
62     usleep(WAIT_SYNC_IN_NS);
63 }
64 
65 namespace {
66 /**
67  * @tc.name: LoadContent01
68  * @tc.desc: context is nullptr
69  * @tc.type: FUNC
70  */
71 HWTEST_F(RootSceneTest, LoadContent01, Function | SmallTest | Level3)
72 {
73     RootScene rootScene;
74     rootScene.LoadContent("a", nullptr, nullptr, nullptr);
75     ASSERT_EQ(1, rootScene.GetWindowId());
76 }
77 
78 /**
79  * @tc.name: UpdateViewportConfig01
80  * @tc.desc: UpdateViewportConfig Test
81  * @tc.type: FUNC
82  */
83 HWTEST_F(RootSceneTest, UpdateViewportConfig01, Function | SmallTest | Level3)
84 {
85     RootScene rootScene;
86     Rect rect;
87 
88     rootScene.uiContent_ = nullptr;
89     rootScene.UpdateViewportConfig(rect, WindowSizeChangeReason::UNDEFINED);
90 
91     auto uiContent = Ace::UIContent::Create(nullptr, nullptr);
92     rootScene.uiContent_ = std::move(uiContent);
93     rootScene.UpdateViewportConfig(rect, WindowSizeChangeReason::UNDEFINED);
94 
95     rect.width_ = MOCK_LEM_SUB_WIDTH;
96     rect.height_ = MOCK_LEM_SUB_HEIGHT;
97     rootScene.UpdateViewportConfig(rect, WindowSizeChangeReason::UNDEFINED);
98     ASSERT_EQ(1, rootScene.GetWindowId());
99 }
100 
101 /**
102  * @tc.name: UpdateConfiguration
103  * @tc.desc: UpdateConfiguration Test
104  * @tc.type: FUNC
105  */
106 HWTEST_F(RootSceneTest, UpdateConfiguration, Function | SmallTest | Level3)
107 {
108     RootScene rootScene;
109     std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
110 
111     rootScene.uiContent_ = nullptr;
112     rootScene.UpdateConfiguration(configuration);
113 
114     auto uiContent = Ace::UIContent::Create(nullptr, nullptr);
115     rootScene.uiContent_ = std::move(uiContent);
116     rootScene.UpdateConfiguration(configuration);
117     ASSERT_EQ(1, rootScene.GetWindowId());
118 }
119 
120 /**
121  * @tc.name: UpdateConfigurationForAll
122  * @tc.desc: UpdateConfigurationForAll Test
123  * @tc.type: FUNC
124  */
125 HWTEST_F(RootSceneTest, UpdateConfigurationForAll, Function | SmallTest | Level3)
126 {
127     RootScene rootScene;
128     std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
129 
130     auto prevStaticRootScene = RootScene::staticRootScene_;
131     rootScene.UpdateConfigurationForAll(configuration);
132 
133     sptr<RootScene> staticRootScene;
134     RootScene::staticRootScene_ = staticRootScene;
135     rootScene.UpdateConfigurationForAll(configuration);
136 
137     RootScene::staticRootScene_ = prevStaticRootScene;
138     ASSERT_EQ(1, rootScene.GetWindowId());
139 }
140 
141 /**
142  * @tc.name: RegisterInputEventListener01
143  * @tc.desc: RegisterInputEventListener Test
144  * @tc.type: FUNC
145  */
146 HWTEST_F(RootSceneTest, RegisterInputEventListener01, Function | SmallTest | Level3)
147 {
148     RootScene rootScene;
149     rootScene.RegisterInputEventListener();
150     ASSERT_EQ(1, rootScene.GetWindowId());
151 }
152 
153 /**
154  * @tc.name: RequestVsyncErr
155  * @tc.desc: RequestVsync Test Err
156  * @tc.type: FUNC
157  */
158 HWTEST_F(RootSceneTest, RequestVsyncErr, Function | SmallTest | Level3)
159 {
160     RootScene rootScene;
161     std::shared_ptr<VsyncCallback> vsyncCallback = std::make_shared<VsyncCallback>();
162     rootScene.RequestVsync(vsyncCallback);
163     ASSERT_EQ(1, rootScene.GetWindowId());
164 }
165 
166 /**
167  * @tc.name: GetVSyncPeriod
168  * @tc.desc: GetVSyncPeriod Test
169  * @tc.type: FUNC
170  */
171 HWTEST_F(RootSceneTest, GetVSyncPeriod, Function | SmallTest | Level3)
172 {
173     RootScene rootScene;
174     rootScene.GetVSyncPeriod();
175     ASSERT_EQ(1, rootScene.GetWindowId());
176 }
177 
178 /**
179  * @tc.name: FlushFrameRate
180  * @tc.desc: FlushFrameRate Test
181  * @tc.type: FUNC
182  */
183 HWTEST_F(RootSceneTest, FlushFrameRate, Function | SmallTest | Level3)
184 {
185     RootScene rootScene;
186     uint32_t rate = 120;
187     int32_t animatorExpectedFrameRate = -1;
188     rootScene.FlushFrameRate(rate, animatorExpectedFrameRate);
189     ASSERT_EQ(1, rootScene.GetWindowId());
190 }
191 
192 /**
193  * @tc.name: SetFrameLayoutFinishCallback
194  * @tc.desc: SetFrameLayoutFinishCallback Test
195  * @tc.type: FUNC
196  */
197 HWTEST_F(RootSceneTest, SetFrameLayoutFinishCallback, Function | SmallTest | Level3)
198 {
199     RootScene rootScene;
200 
201     rootScene.SetFrameLayoutFinishCallback(nullptr);
202 
203     auto uiContent = Ace::UIContent::Create(nullptr, nullptr);
204     rootScene.uiContent_ = std::move(uiContent);
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: UpdateConfigurationSync
237  * @tc.desc: UpdateConfigurationSync Test
238  * @tc.type: FUNC
239  */
240 HWTEST_F(RootSceneTest, UpdateConfigurationSync, Function | SmallTest | Level3)
241 {
242     RootScene rootScene;
243     std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
244 
245     rootScene.uiContent_ = nullptr;
246     rootScene.UpdateConfigurationSync(configuration);
247     ASSERT_EQ(1, rootScene.GetWindowId());
248 }
249 
250 /**
251  * @tc.name: UpdateConfigurationSyncForAll
252  * @tc.desc: UpdateConfigurationSyncForAll Test
253  * @tc.type: FUNC
254  */
255 HWTEST_F(RootSceneTest, UpdateConfigurationSyncForAll, Function | SmallTest | Level3)
256 {
257     RootScene rootScene;
258     std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
259 
260     auto prevStaticRootScene = RootScene::staticRootScene_;
261     rootScene.UpdateConfigurationSyncForAll(configuration);
262 
263     sptr<RootScene> staticRootScene;
264     RootScene::staticRootScene_ = staticRootScene;
265     rootScene.UpdateConfigurationSyncForAll(configuration);
266 
267     RootScene::staticRootScene_ = prevStaticRootScene;
268     ASSERT_EQ(1, rootScene.GetWindowId());
269 }
270 }
271 } // namespace Rosen
272 } // namespace OHOS