1 /*
2 * Copyright (c) 2022-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
16 #include <fcntl.h>
17 #include <gtest/gtest.h>
18 #include "common_test_utils.h"
19 #include "iremote_object_mocker.h"
20 #include "mock_IWindow.h"
21 #include "mock_RSIWindowAnimationController.h"
22 #include "window_manager_service.h"
23
24 #include <thread>
25
26 #include <ability_manager_client.h>
27 #include <cinttypes>
28 #include <chrono>
29 #include <hisysevent.h>
30 #include <hitrace_meter.h>
31 #include <ipc_skeleton.h>
32 #include <parameters.h>
33 #include <rs_iwindow_animation_controller.h>
34 #include <system_ability_definition.h>
35 #include <sstream>
36 #include "xcollie/watchdog.h"
37
38 #include "color_parser.h"
39 #include "display_manager_service_inner.h"
40 #include "dm_common.h"
41 #include "drag_controller.h"
42 #include "minimize_app.h"
43 #include "permission.h"
44 #include "remote_animation.h"
45 #include "singleton_container.h"
46 #include "ui/rs_ui_director.h"
47 #include "window_helper.h"
48 #include "window_inner_manager.h"
49 #include "window_manager_agent_controller.h"
50 #include "window_manager_hilog.h"
51 #include "wm_common.h"
52 #include "wm_math.h"
53
54
55 using namespace testing;
56 using namespace testing::ext;
57 namespace OHOS {
58 namespace Rosen {
59 class WindowManagerServiceTest : public testing::Test {
60 public:
61 static void SetUpTestCase();
62 static void TearDownTestCase();
63 void SetUp() override;
64 void TearDown() override;
65
66 void SetAceessTokenPermission(const std::string processName);
67 sptr<WindowManagerService> wms = new WindowManagerService();
68 };
69
SetUpTestCase()70 void WindowManagerServiceTest::SetUpTestCase()
71 {
72 }
73
TearDownTestCase()74 void WindowManagerServiceTest::TearDownTestCase()
75 {
76 }
77
SetUp()78 void WindowManagerServiceTest::SetUp()
79 {
80 CommonTestUtils::SetAceessTokenPermission("WindowManagerServiceTest");
81 }
82
TearDown()83 void WindowManagerServiceTest::TearDown()
84 {
85 }
86
87 namespace {
88 /**
89 * @tc.name: OnAddSystemAbility
90 * @tc.desc: OnAddSystemAbility test
91 * @tc.type: FUNC
92 */
93 HWTEST_F(WindowManagerServiceTest, OnAddSystemAbility01, Function | SmallTest | Level2)
94 {
95 std::string str = "OnAddSystemAbility";
96 wms->OnAddSystemAbility(0, str);
97 ASSERT_EQ(nullptr, wms->windowCommonEvent_->subscriber_);
98 }
99 /**
100 * @tc.name: WindowVisibilityChangeCallback
101 * @tc.desc: WindowVisibilityChangeCallback test
102 * @tc.type: FUNC
103 */
104 HWTEST_F(WindowManagerServiceTest, WindowVisibilityChangeCallback01, Function | SmallTest | Level2)
105 {
106 std::shared_ptr<RSOcclusionData> occlusionData = nullptr;
107 wms->WindowVisibilityChangeCallback(occlusionData);
108 ASSERT_EQ(nullptr, occlusionData);
109 }
110 /**
111 * @tc.name: InitWithAbilityManagerServiceAdded
112 * @tc.desc: Init with ability manager service added.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(WindowManagerServiceTest, InitWithAbilityManagerServiceAdded01, Function | SmallTest | Level2)
116 {
117 wms->wmsHandler_ = new WindowManagerServiceHandler;
118 wms->InitWithAbilityManagerServiceAdded();
119 ASSERT_NE(nullptr, wms->wmsHandler_);
120 }
121 /**
122 * @tc.name: Dump
123 * @tc.desc: Dump info
124 * @tc.type: FUNC
125 */
126 HWTEST_F(WindowManagerServiceTest, Dump01, Function | SmallTest | Level2)
127 {
128 wms->windowDumper_ = nullptr;
129 std::vector<std::u16string> args;
130 const std::string dumpFile = "data/window_dump_test.txt";
131 int fd = open(dumpFile.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
132 if (fd == -1) {
133 return;
134 }
135 ASSERT_EQ(static_cast<int>(WMError::WM_ERROR_INVALID_PARAM), wms->Dump(-1, args));
136 ASSERT_EQ(static_cast<int>(WMError::WM_OK), wms->Dump(fd, args));
137 close(fd);
138 unlink(dumpFile.c_str());
139 }
140 /**
141 * @tc.name: NotifyWindowTransition
142 * @tc.desc: NotifyWindowTransition test
143 * @tc.type: FUNC
144 */
145 HWTEST_F(WindowManagerServiceTest, NotifyWindowTransition01, Function | SmallTest | Level2)
146 {
147 sptr<WindowTransitionInfo> fromInfo = nullptr;
148 sptr<WindowTransitionInfo> toInfo = nullptr;
149 ASSERT_EQ(WMError::WM_OK, wms->NotifyWindowTransition(fromInfo, toInfo, false));
150 ASSERT_EQ(WMError::WM_ERROR_NO_REMOTE_ANIMATION, wms->NotifyWindowTransition(fromInfo, toInfo, true));
151 }
152 /**
153 * @tc.name: StartingWindow
154 * @tc.desc: StartingWindow test
155 * @tc.type: FUNC
156 */
157 HWTEST_F(WindowManagerServiceTest, StartingWindow01, Function | SmallTest | Level2)
158 {
159 wms->startingOpen_ = false;
160 wms->StartingWindow(nullptr, nullptr, false, 0);
161 ASSERT_EQ(false, wms->startingOpen_);
162 wms->CancelStartingWindow(nullptr);
163 wms->startingOpen_ = true;
164 wms->StartingWindow(nullptr, nullptr, false, 0);
165 ASSERT_EQ(true, wms->startingOpen_);
166 wms->CancelStartingWindow(nullptr);
167 }
168
169 /**
170 * @tc.name: MoveMissionsToForeground
171 * @tc.desc: MoveMissionsToForont test
172 * @tc.type: FUNC
173 */
174 HWTEST_F(WindowManagerServiceTest, MoveMissionsToForeground01, Function | SmallTest | Level2)
175 {
176 WMError rs = wms->MoveMissionsToForeground({}, -1);
177 ASSERT_EQ(WMError::WM_OK, rs);
178 }
179
180
181 /**
182 * @tc.name: MoveMissionsToBackground
183 * @tc.desc: MoveMissionsToBackground test
184 * @tc.type: FUNC
185 */
186 HWTEST_F(WindowManagerServiceTest, MoveMissionsToBackground01, Function | SmallTest | Level2)
187 {
188 std::vector<int32_t> moveRs;
189 WMError rs = wms->MoveMissionsToBackground({}, moveRs);
190 ASSERT_EQ(WMError::WM_OK, rs);
191 }
192
193 /**
194 * @tc.name: CreateWindow
195 * @tc.desc: CreateWindow test
196 * @tc.type: FUNC
197 */
198 HWTEST_F(WindowManagerServiceTest, CreateWindow01, Function | SmallTest | Level2)
199 {
200 sptr<IWindow> window = nullptr;
201 uint32_t id = 2;
202 std::shared_ptr<RSSurfaceNode> RS_node = nullptr;
203 sptr<WindowProperty> property = new WindowProperty();
204 property->SetWindowType(WindowType::WINDOW_TYPE_WALLPAPER);
205 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->CreateWindow(window, property, RS_node, id, nullptr));
206 wms->DestroyWindow(id, true);
207 }
208 /**
209 * @tc.name: AddWindow
210 * @tc.desc: AddWindow test
211 * @tc.type: FUNC
212 */
213 HWTEST_F(WindowManagerServiceTest, AddWindow01, Function | SmallTest | Level2)
214 {
215 sptr<WindowProperty> property = nullptr;
216 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->AddWindow(property));
217 }
218 /**
219 * @tc.name: RegisterWindowManagerAgent
220 * @tc.desc: RegisterWindowManagerAgent test
221 * @tc.type: FUNC
222 */
223 HWTEST_F(WindowManagerServiceTest, RegisterWindowManagerAgent01, Function | SmallTest | Level2)
224 {
225 sptr<IWindowManagerAgent> windowManagerAgent = nullptr;
226 WindowManagerAgentType type = WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT;
227 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->RegisterWindowManagerAgent(type, windowManagerAgent));
228 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->UnregisterWindowManagerAgent(type, windowManagerAgent));
229 }
230 /**
231 * @tc.name: SetWindowAnimationController
232 * @tc.desc: SetWindowAnimationController test
233 * @tc.type: FUNC
234 */
235 HWTEST_F(WindowManagerServiceTest, SetWindowAnimationController01, Function | SmallTest | Level2)
236 {
237 sptr<RSIWindowAnimationController> controller = nullptr;
238 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->SetWindowAnimationController(controller));
239 controller = new RSIWindowAnimationControllerMocker;
240 ASSERT_EQ(WMError::WM_OK, wms->SetWindowAnimationController(controller));
241 }
242 /**
243 * @tc.name: OnWindowEvent
244 * @tc.desc: OnWindowEvent test
245 * @tc.type: FUNC
246 */
247 HWTEST_F(WindowManagerServiceTest, OnWindowEvent01, Function | SmallTest | Level2)
248 {
249 sptr<IRemoteObject> remoteObject = new IRemoteObjectMocker;
250 wms->OnWindowEvent(static_cast<Event>(1), remoteObject);
251 wms->OnWindowEvent(Event::REMOTE_DIED, remoteObject);
252 ASSERT_EQ(INVALID_WINDOW_ID, wms->windowRoot_->GetWindowIdByObject(remoteObject));
253 }
254 /**
255 * @tc.name: UpdateProperty
256 * @tc.desc: UpdateProperty test
257 * @tc.type: FUNC
258 */
259 HWTEST_F(WindowManagerServiceTest, UpdateProperty01, Function | SmallTest | Level2)
260 {
261 sptr<WindowProperty> windowProperty = nullptr;
262 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->UpdateProperty(windowProperty,
263 PropertyChangeAction::ACTION_UPDATE_ANIMATION_FLAG, true));
264 }
265 /**
266 * @tc.name: GetModeChangeHotZones
267 * @tc.desc: GetModeChangeHotZones test
268 * @tc.type: FUNC
269 */
270 HWTEST_F(WindowManagerServiceTest, GetModeChangeHotZones01, Function | SmallTest | Level2)
271 {
272 ModeChangeHotZonesConfig config = {false, 0, 0, 0};
273 DisplayId displayId = 0;
274 ModeChangeHotZones hotZone;
275 wms->hotZonesConfig_ = config;
276 ASSERT_EQ(WMError::WM_DO_NOTHING, wms->GetModeChangeHotZones(displayId, hotZone));
277 config.isModeChangeHotZoneConfigured_ = true;
278 wms->hotZonesConfig_ = config;
279 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->GetModeChangeHotZones(displayId, hotZone));
280 }
281 /**
282 * @tc.name: UpdateAvoidAreaListener
283 * @tc.desc: UpdateAvoidAreaListener test
284 * @tc.type: FUNC
285 */
286 HWTEST_F(WindowManagerServiceTest, UpdateAvoidAreaListener01, Function | SmallTest | Level2)
287 {
288 sptr<WindowProperty> property = new WindowProperty();
289 sptr<WindowNode> node = new WindowNode(property);
290 wms->windowRoot_->windowNodeMap_.insert(std::make_pair(0, node));
291 ASSERT_EQ(WMError::WM_DO_NOTHING, wms->UpdateAvoidAreaListener(0, true));
292 }
293 /**
294 * @tc.name: BindDialogTarget
295 * @tc.desc: BindDialogTarget test
296 * @tc.type: FUNC
297 */
298 HWTEST_F(WindowManagerServiceTest, BindDialogTarget01, Function | SmallTest | Level2)
299 {
300 sptr<IRemoteObject> targetToken = new IRemoteObjectMocker();
301 uint32_t id = 0;
302 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->BindDialogTarget(id, targetToken));
303 }
304 /**
305 * @tc.name: DispatchKeyEvent01
306 * @tc.desc: Dispatch KeyEvent for app window bellow the component window.
307 * @tc.require: issueI6RMUY
308 * @tc.type: FUNC
309 */
310 HWTEST_F(WindowManagerServiceTest, DispatchKeyEvent01, Function | SmallTest | Level2)
311 {
312 wms = new WindowManagerService();
313 sptr<WindowNode> compNode = new WindowNode();
314 compNode->property_->type_ = WindowType::WINDOW_TYPE_APP_COMPONENT;
315 compNode->SetWindowToken(new IWindowMocker);
316 compNode->property_->windowId_ = 1U;
317 sptr<WindowNode> appNode = new WindowNode();
318 appNode->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
319 IWindowMocker* token = new IWindowMocker;
320 appNode->SetWindowToken(token);
321 appNode->property_->windowId_ = 2U;
322 auto event = MMI::KeyEvent::Create();
323 wms->windowController_->windowRoot_->windowNodeMap_[appNode->GetWindowId()] = appNode;
324 wms->windowController_->windowRoot_->windowNodeMap_[compNode->GetWindowId()] = compNode;
325 sptr<WindowNodeContainer> container = new WindowNodeContainer(new DisplayInfo, 0);
326 wms->windowController_->windowRoot_->windowNodeContainerMap_[compNode->GetDisplayId()] = container;
327 wms->windowController_->windowRoot_->displayIdMap_[compNode->GetDisplayId()] = { compNode->GetDisplayId() };
328 container->appWindowNode_->children_.push_back(appNode);
329 container->appWindowNode_->children_.push_back(compNode);
330 std::vector<sptr<WindowNode>> windowNodes;
331 container->TraverseContainer(windowNodes);
332 ASSERT_TRUE(windowNodes[0] == compNode);
333 ASSERT_TRUE(windowNodes[1] == appNode);
334 EXPECT_CALL(*token, ConsumeKeyEvent(_));
335 wms->DispatchKeyEvent(compNode->GetWindowId(), event);
336 testing::Mock::AllowLeak(token);
337 }
338 /**
339 * @tc.name: DispatchKeyEvent02
340 * @tc.desc: Dispatch KeyEvent for app window bellow the app window.
341 * @tc.require: issueI6RMUY
342 * @tc.type: FUNC
343 */
344 HWTEST_F(WindowManagerServiceTest, DispatchKeyEvent02, Function | SmallTest | Level2)
345 {
346 wms = new WindowManagerService();
347 sptr<WindowNode> appNode1 = new WindowNode();
348 appNode1->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
349 appNode1->SetWindowToken(new IWindowMocker);
350 appNode1->property_->windowId_ = 1U;
351 sptr<WindowNode> appNode2 = new WindowNode();
352 appNode2->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
353 appNode2->SetWindowToken(new IWindowMocker);
354 appNode2->property_->windowId_ = 2U;
355 auto event = MMI::KeyEvent::Create();
356 wms->windowController_->windowRoot_->windowNodeMap_[appNode2->GetWindowId()] = appNode2;
357 wms->windowController_->windowRoot_->windowNodeMap_[appNode1->GetWindowId()] = appNode1;
358 sptr<WindowNodeContainer> container = new WindowNodeContainer(new DisplayInfo, 0);
359 wms->windowController_->windowRoot_->windowNodeContainerMap_[appNode1->GetDisplayId()] = container;
360 wms->windowController_->windowRoot_->displayIdMap_[appNode1->GetDisplayId()] = { appNode1->GetDisplayId() };
361 container->appWindowNode_->children_.push_back(appNode2);
362 container->appWindowNode_->children_.push_back(appNode1);
363 std::vector<sptr<WindowNode>> windowNodes;
364 container->TraverseContainer(windowNodes);
365 ASSERT_TRUE(windowNodes[0] == appNode1);
366 ASSERT_TRUE(windowNodes[1] == appNode2);
367 wms->DispatchKeyEvent(appNode1->GetWindowId(), event);
368 }
369 /**
370 * @tc.name: DispatchKeyEvent03
371 * @tc.desc: Dispatch KeyEvent for app window bellow two component window.
372 * @tc.require: issueI6RMUY
373 * @tc.type: FUNC
374 */
375 HWTEST_F(WindowManagerServiceTest, DispatchKeyEvent03, Function | SmallTest | Level2)
376 {
377 wms = new WindowManagerService();
378 sptr<WindowNode> compNode1 = new WindowNode();
379 compNode1->property_->type_ = WindowType::WINDOW_TYPE_APP_COMPONENT;
380 compNode1->SetWindowToken(new IWindowMocker);
381 compNode1->property_->windowId_ = 1U;
382 sptr<WindowNode> compNode2 = new WindowNode();
383 compNode2->property_->type_ = WindowType::WINDOW_TYPE_APP_COMPONENT;
384 compNode2->SetWindowToken(new IWindowMocker);
385 compNode2->property_->windowId_ = 2U;
386 sptr<WindowNode> appNode = new WindowNode();
387 appNode->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
388 IWindowMocker* token = new IWindowMocker;
389 appNode->SetWindowToken(token);
390 appNode->property_->windowId_ = 3U;
391 auto event = MMI::KeyEvent::Create();
392 wms->windowController_->windowRoot_->windowNodeMap_[appNode->GetWindowId()] = appNode;
393 wms->windowController_->windowRoot_->windowNodeMap_[compNode1->GetWindowId()] = compNode1;
394 wms->windowController_->windowRoot_->windowNodeMap_[compNode2->GetWindowId()] = compNode2;
395 sptr<WindowNodeContainer> container = new WindowNodeContainer(new DisplayInfo, 0);
396 wms->windowController_->windowRoot_->windowNodeContainerMap_[compNode1->GetDisplayId()] = container;
397 wms->windowController_->windowRoot_->displayIdMap_[compNode1->GetDisplayId()] = { compNode1->GetDisplayId() };
398 container->appWindowNode_->children_.push_back(appNode);
399 container->appWindowNode_->children_.push_back(compNode1);
400 container->appWindowNode_->children_.push_back(compNode2);
401 std::vector<sptr<WindowNode>> windowNodes;
402 container->TraverseContainer(windowNodes);
403 ASSERT_TRUE(windowNodes[0] == compNode2);
404 ASSERT_TRUE(windowNodes[1] == compNode1);
405 ASSERT_TRUE(windowNodes[2] == appNode);
406 EXPECT_CALL(*token, ConsumeKeyEvent(_));
407 wms->DispatchKeyEvent(compNode2->GetWindowId(), event);
408 testing::Mock::AllowLeak(token);
409 }
410 /**
411 * @tc.name: SetWindowGravity
412 * @tc.desc: SetWindowGravity test
413 * @tc.type: FUNC
414 */
415 HWTEST_F(WindowManagerServiceTest, SetWindowGravity01, Function | SmallTest | Level2)
416 {
417 uint32_t id = 0;
418 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, wms->SetWindowGravity(id, WindowGravity::WINDOW_GRAVITY_BOTTOM, 0));
419 }
420 /*
421 * @tc.name: GetWindowAnimationTargets01
422 * @tc.desc: get window animation targets
423 * @tc.type: FUNC
424 */
425 HWTEST_F(WindowManagerServiceTest, GetWindowAnimationTargets01, Function | SmallTest | Level2)
426 {
427 wms = new WindowManagerService();
428 sptr<WindowNode> compNode = new WindowNode();
429 compNode->property_->type_ = WindowType::WINDOW_TYPE_APP_COMPONENT;
430 compNode->SetWindowToken(new IWindowMocker);
431 compNode->property_->windowId_ = 1U;
432 compNode->abilityInfo_.missionId_ = 1;
433 sptr<WindowNode> appNode = new WindowNode();
434 appNode->property_->type_ = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW;
435 IWindowMocker* token = new IWindowMocker;
436 appNode->SetWindowToken(token);
437 appNode->property_->windowId_ = 2U;
438 appNode->abilityInfo_.missionId_ = 2;
439 wms->windowController_->windowRoot_->windowNodeMap_[appNode->GetWindowId()] = appNode;
440 wms->windowController_->windowRoot_->windowNodeMap_[compNode->GetWindowId()] = compNode;
441 sptr<WindowNodeContainer> container = new WindowNodeContainer(new DisplayInfo, 0);
442 wms->windowController_->windowRoot_->windowNodeContainerMap_[compNode->GetDisplayId()] = container;
443 wms->windowController_->windowRoot_->displayIdMap_[compNode->GetDisplayId()] = { compNode->GetDisplayId() };
444 container->appWindowNode_->children_.push_back(appNode);
445 container->appWindowNode_->children_.push_back(compNode);
446 RemoteAnimation::SetWindowControllerAndRoot(wms->windowController_, wms->windowController_->windowRoot_);
447 std::vector<uint32_t> missionIds;
448 missionIds.push_back(1);
449 missionIds.push_back(2);
450 std::vector<sptr<RSWindowAnimationTarget>> targets;
451 ASSERT_EQ(WMError::WM_OK, wms->GetWindowAnimationTargets(missionIds, targets));
452 ASSERT_EQ(0, targets.size());
453 }
454 }
455 }
456 }
457