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