1 /*
2 * Copyright (c) 2022 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 #include <gtest/gtest.h>
16 #include "window_scene.h"
17 #include "ability_context_impl.h"
18 #include "mock_static_call.h"
19 #include "singleton_mocker.h"
20 #include "window_impl.h"
21 #include <configuration.h>
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 using Mocker = SingletonMocker<StaticCall, MockStaticCall>;
29 class WindowSceneTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 virtual void SetUp() override;
34 virtual void TearDown() override;
35
36 sptr<WindowScene> scene_ = nullptr;
37 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
38 };
SetUpTestCase()39 void WindowSceneTest::SetUpTestCase()
40 {
41 }
42
TearDownTestCase()43 void WindowSceneTest::TearDownTestCase()
44 {
45 }
46
SetUp()47 void WindowSceneTest::SetUp()
48 {
49 DisplayId displayId = 0;
50 sptr<IWindowLifeCycle> listener = nullptr;
51 scene_ = new WindowScene();
52 abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
53 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
54 sptr<WindowOption> option = new WindowOption();
55 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(option)));
56 ASSERT_EQ(WMError::WM_OK, scene_->Init(displayId, abilityContext_, listener));
57 }
58
TearDown()59 void WindowSceneTest::TearDown()
60 {
61 scene_->GoDestroy();
62 scene_ = nullptr;
63 abilityContext_ = nullptr;
64 }
65
66 namespace {
67 /**
68 * @tc.name: Init01
69 * @tc.desc: Init Scene with null abilityContext, null listener
70 * @tc.type: FUNC
71 */
72 HWTEST_F(WindowSceneTest, Init01, Function | SmallTest | Level2)
73 {
74 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
75 sptr<WindowOption> optionTest = new WindowOption();
76 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
77 DisplayId displayId = 0;
78 sptr<IWindowLifeCycle> listener = nullptr;
79 sptr<WindowScene> scene = new WindowScene();
80 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
81 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
82 }
83
84 /**
85 * @tc.name: Init02
86 * @tc.desc: Mock window Create Static Method return nullptr, init Scene with null abilityContext, null listener
87 * @tc.type: FUNC
88 */
89 HWTEST_F(WindowSceneTest, Init02, Function | SmallTest | Level2)
90 {
91 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
92 sptr<WindowOption> optionTest = new WindowOption();
93 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(nullptr));
94 DisplayId displayId = 0;
95 sptr<IWindowLifeCycle> listener = nullptr;
96 sptr<WindowScene> scene = new WindowScene();
97 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
98 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->Init(displayId, abilityContext, listener));
99 }
100
101 /**
102 * @tc.name: Init03
103 * @tc.desc: Init Scene with abilityContext, null listener
104 * @tc.type: FUNC
105 */
106 HWTEST_F(WindowSceneTest, Init03, Function | SmallTest | Level2)
107 {
108 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
109 sptr<WindowOption> optionTest = new WindowOption();
110 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
111 DisplayId displayId = 0;
112 sptr<IWindowLifeCycle> listener = nullptr;
113 sptr<WindowScene> scene = new WindowScene();
114 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext_, listener));
115 }
116
117 /**
118 * @tc.name: Create01
119 * @tc.desc: CreateWindow without windowName
120 * @tc.type: FUNC
121 */
122 HWTEST_F(WindowSceneTest, Create01, Function | SmallTest | Level2)
123 {
124 sptr<WindowOption> optionTest = new WindowOption();
125 sptr<WindowScene> scene = new WindowScene();
126 ASSERT_EQ(nullptr, scene->CreateWindow("", optionTest));
127 }
128
129 /**
130 * @tc.name: Create02
131 * @tc.desc: CreateWindow with windowName and without mainWindow
132 * @tc.type: FUNC
133 */
134 HWTEST_F(WindowSceneTest, Create02, Function | SmallTest | Level2)
135 {
136 sptr<WindowOption> optionTest = new WindowOption();
137 sptr<WindowScene> scene = new WindowScene();
138 ASSERT_EQ(nullptr, scene->CreateWindow("WindowSceneTest02", optionTest));
139 }
140
141 /**
142 * @tc.name: Create03
143 * @tc.desc: CreateWindow with windowName and mainWindow
144 * @tc.type: FUNC
145 */
146 HWTEST_F(WindowSceneTest, Create03, Function | SmallTest | Level2)
147 {
148 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
149 sptr<WindowOption> optionTest = new WindowOption();
150 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
151 ASSERT_NE(nullptr, scene_->CreateWindow("WindowSceneTest03", optionTest));
152 }
153
154 /**
155 * @tc.name: Create04
156 * @tc.desc: Mock window Create Static Method return nullptr, createWindow with windowName and mainWindow
157 * @tc.type: FUNC
158 */
159 HWTEST_F(WindowSceneTest, Create04, Function | SmallTest | Level2)
160 {
161 sptr<WindowOption> optionTest = new WindowOption();
162 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
163 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(nullptr));
164 ASSERT_EQ(nullptr, scene_->CreateWindow("WindowSceneTest04", optionTest));
165 }
166
167 /**
168 * @tc.name: Create05
169 * @tc.desc: createWindow with windowName and null option
170 * @tc.type: FUNC
171 */
172 HWTEST_F(WindowSceneTest, Create05, Function | SmallTest | Level2)
173 {
174 sptr<WindowOption> optionTest = nullptr;
175 ASSERT_EQ(nullptr, scene_->CreateWindow("WindowSceneTest05", optionTest));
176 }
177
178 /**
179 * @tc.name: GetMainWindow01
180 * @tc.desc: GetMainWindow without scene init
181 * @tc.type: FUNC
182 */
183 HWTEST_F(WindowSceneTest, GetMainWindow01, Function | SmallTest | Level2)
184 {
185 sptr<WindowScene> scene = new WindowScene();
186 ASSERT_EQ(nullptr, scene->GetMainWindow());
187 }
188
189 /**
190 * @tc.name: GetMainWindow02
191 * @tc.desc: GetMainWindow01 with nullptr
192 * @tc.type: FUNC
193 */
194 HWTEST_F(WindowSceneTest, GetMainWindow02, Function | SmallTest | Level2)
195 {
196 ASSERT_NE(nullptr, scene_->GetMainWindow());
197 }
198
199 /**
200 * @tc.name: GetSubWindow01
201 * @tc.desc: GetSubWindow without scene init
202 * @tc.type: FUNC
203 */
204 HWTEST_F(WindowSceneTest, GetSubWindow01, Function | SmallTest | Level2)
205 {
206 sptr<WindowScene> scene = new WindowScene();
207 std::vector<sptr<Window>> subWindows = scene->GetSubWindow();
208 ASSERT_TRUE(subWindows.empty());
209 }
210
211 /**
212 * @tc.name: GetSubWindow02
213 * @tc.desc: GetSubWindow without scene init
214 * @tc.type: FUNC
215 */
216 HWTEST_F(WindowSceneTest, GetSubWindow02, Function | SmallTest | Level2)
217 {
218 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
219 sptr<WindowOption> optionTest = new WindowOption();
220 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
221 DisplayId displayId = 0;
222 sptr<IWindowLifeCycle> listener = nullptr;
223 sptr<WindowScene> scene = new WindowScene();
224 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
225 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
226 std::vector<sptr<Window>> subWindows = scene->GetSubWindow();
227 ASSERT_TRUE(subWindows.empty());
228 }
229
230 /**
231 * @tc.name: OnNewWant01
232 * @tc.desc: OnNewWant nullptr
233 * @tc.type: FUNC
234 */
235 HWTEST_F(WindowSceneTest, OnNewWant01, Function | SmallTest | Level2)
236 {
237 sptr<WindowScene> scene = new WindowScene();
238 AAFwk::Want want;
239 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->OnNewWant(want));
240 }
241
242 /**
243 * @tc.name: OnNewWant02
244 * @tc.desc: OnNewWant without scene init
245 * @tc.type: FUNC
246 */
247 HWTEST_F(WindowSceneTest, OnNewWant02, Function | SmallTest | Level2)
248 {
249 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
250 sptr<WindowOption> optionTest = new WindowOption();
251 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
252 DisplayId displayId = 0;
253 sptr<IWindowLifeCycle> listener = nullptr;
254 sptr<WindowScene> scene = new WindowScene();
255 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
256 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
257 AAFwk::Want want;
258 ASSERT_EQ(WMError::WM_OK, scene->OnNewWant(want));
259 }
260
261 /**
262 * @tc.name: UpdateConfiguration01
263 * @tc.desc: UpdateConfiguration nullptr
264 * @tc.type: FUNC
265 */
266 HWTEST_F(WindowSceneTest, UpdateConfiguration01, Function | SmallTest | Level2)
267 {
268 sptr<WindowScene> scene = new WindowScene();
269 std::shared_ptr<AppExecFwk::Configuration> configuration = nullptr;
270 scene->UpdateConfiguration(configuration);
271 }
272
273 /**
274 * @tc.name: UpdateConfiguration02
275 * @tc.desc: UpdateConfiguration without scene init
276 * @tc.type: FUNC
277 */
278 HWTEST_F(WindowSceneTest, UpdateConfiguration02, Function | SmallTest | Level2)
279 {
280 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
281 sptr<WindowOption> optionTest = new WindowOption();
282 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
283 DisplayId displayId = 0;
284 sptr<IWindowLifeCycle> listener = nullptr;
285 sptr<WindowScene> scene = new WindowScene();
286 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
287 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
288 std::shared_ptr<AppExecFwk::Configuration> configuration = std::make_shared<AppExecFwk::Configuration>();
289 scene->UpdateConfiguration(configuration);
290 }
291
292 /**
293 * @tc.name: GetContentInfo01
294 * @tc.desc: GetContentInfo nullptr
295 * @tc.type: FUNC
296 */
297 HWTEST_F(WindowSceneTest, GetContentInfo01, Function | SmallTest | Level2)
298 {
299 sptr<WindowScene> scene = new WindowScene();
300 ASSERT_EQ("", scene->GetContentInfo());
301 }
302
303 /**
304 * @tc.name: GetContentInfo02
305 * @tc.desc: GetContentInfo without scene init
306 * @tc.type: FUNC
307 */
308 HWTEST_F(WindowSceneTest, GetContentInfo02, Function | SmallTest | Level2)
309 {
310 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
311 sptr<WindowOption> optionTest = new WindowOption();
312 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
313 DisplayId displayId = 0;
314 sptr<IWindowLifeCycle> listener = nullptr;
315 sptr<WindowScene> scene = new WindowScene();
316 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
317 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
318 ASSERT_EQ("", scene->GetContentInfo());
319 }
320
321 /**
322 * @tc.name: SetSystemBarProperty01
323 * @tc.desc: SetSystemBarProperty nullptr
324 * @tc.type: FUNC
325 */
326 HWTEST_F(WindowSceneTest, SetSystemBarProperty01, Function | SmallTest | Level2)
327 {
328 sptr<WindowScene> scene = new WindowScene();
329 WindowType type = WindowType::WINDOW_TYPE_DIALOG;
330 SystemBarProperty property;
331 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->SetSystemBarProperty(type, property));
332 }
333
334 /**
335 * @tc.name: SetSystemBarProperty02
336 * @tc.desc: SetSystemBarProperty without scene init
337 * @tc.type: FUNC
338 */
339 HWTEST_F(WindowSceneTest, SetSystemBarProperty02, Function | SmallTest | Level2)
340 {
341 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
342 sptr<WindowOption> optionTest = new WindowOption();
343 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(optionTest)));
344 DisplayId displayId = 0;
345 sptr<IWindowLifeCycle> listener = nullptr;
346 sptr<WindowScene> scene = new WindowScene();
347 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext = nullptr;
348 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext, listener));
349 WindowType type = WindowType::WINDOW_TYPE_DIALOG;
350 SystemBarProperty property;
351 ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, scene->SetSystemBarProperty(type, property));
352 }
353
354 /**
355 * @tc.name: GoForeground01
356 * @tc.desc: GoForeground01 without mainWindow
357 * @tc.type: FUNC
358 */
359 HWTEST_F(WindowSceneTest, GoForeground01, Function | SmallTest | Level2)
360 {
361 sptr<WindowScene> scene = new WindowScene();
362 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoForeground());
363 }
364
365 /**
366 * @tc.name: GoBackground01
367 * @tc.desc: GoBackground01 without mainWindow
368 * @tc.type: FUNC
369 */
370 HWTEST_F(WindowSceneTest, GoBackground01, Function | SmallTest | Level2)
371 {
372 sptr<WindowScene> scene = new WindowScene();
373 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->GoBackground());
374 }
375
376 /**
377 * @tc.name: RequestFocus01
378 * @tc.desc: RequestFocus01 without mainWindow
379 * @tc.type: FUNC
380 */
381 HWTEST_F(WindowSceneTest, RequestFocus01, Function | SmallTest | Level2)
382 {
383 sptr<WindowScene> scene = new WindowScene();
384 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->RequestFocus());
385 }
386
387 /**
388 * @tc.name: NotifyMemoryLevel01
389 * @tc.desc: NotifyMemoryLevel without mainWindow
390 * @tc.type: FUNC
391 * @tc.require: issueI5JQ04
392 */
393 HWTEST_F(WindowSceneTest, NotifyMemoryLevel01, Function | SmallTest | Level2)
394 {
395 sptr<WindowScene> scene = new WindowScene();
396 int32_t level = 0;
397 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->NotifyMemoryLevel(level));
398 }
399
400 /**
401 * @tc.name: NotifyMemoryLevel02
402 * @tc.desc: NotifyMemoryLevel with level
403 * @tc.type: FUNC
404 * @tc.require: issueI5JQ04
405 */
406 HWTEST_F(WindowSceneTest, NotifyMemoryLevel02, Function | SmallTest | Level2)
407 {
408 DisplayId displayId = 0;
409 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
410 sptr<IWindowLifeCycle> listener = nullptr;
411 sptr<WindowScene> scene = new WindowScene();
412 sptr<WindowOption> option = new WindowOption();
413 EXPECT_CALL(m->Mock(), CreateWindow(_, _, _)).Times(1).WillOnce(Return(new WindowImpl(option)));
414 ASSERT_EQ(WMError::WM_OK, scene->Init(displayId, abilityContext_, listener));
415 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, scene->NotifyMemoryLevel(0)); // ui content is null
416 }
417 }
418 } // namespace Rosen
419 } // namespace OHOS