• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <gtest/gtest.h>
17 
18 #include "display_manager_config.h"
19 #include "display_manager_service.h"
20 #include "display_manager_agent_default.h"
21 #include "common_test_utils.h"
22 #include "mock_rs_display_node.h"
23 
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Rosen {
30 class DisplayManagerServiceTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 
37     void SetAceessTokenPermission(const std::string processName);
38     static sptr<DisplayManagerService> dms_;
39     static constexpr DisplayId DEFAULT_DISPLAY = 0ULL;
40     static constexpr DisplayId DEFAULT_SCREEN = 0ULL;
41 };
42 
43 sptr<DisplayManagerService> DisplayManagerServiceTest::dms_ = nullptr;
44 
SetUpTestCase()45 void DisplayManagerServiceTest::SetUpTestCase()
46 {
47     dms_ = new DisplayManagerService();
48 
49     dms_->abstractScreenController_->defaultRsScreenId_ = 0;
50     dms_->abstractScreenController_->screenIdManager_.rs2DmsScreenIdMap_.clear();
51     dms_->abstractScreenController_->screenIdManager_.rs2DmsScreenIdMap_ = {
52         {0, 0}
53     };
54     dms_->abstractScreenController_->screenIdManager_.dms2RsScreenIdMap_.clear();
55     dms_->abstractScreenController_->screenIdManager_.dms2RsScreenIdMap_ = {
56         {0, 0}
57     };
58     const char** perms = new const char *[1];
59     perms[0] = "ohos.permission.CAPTURE_SCREEN";
60     CommonTestUtils::SetAceessTokenPermission("DisplayManagerServiceTest", perms, 1);
61 }
62 
TearDownTestCase()63 void DisplayManagerServiceTest::TearDownTestCase()
64 {
65     dms_ = nullptr;
66 }
67 
SetUp()68 void DisplayManagerServiceTest::SetUp()
69 {
70 }
71 
TearDown()72 void DisplayManagerServiceTest::TearDown()
73 {
74 }
75 
76 class DisplayChangeListenerTest : public IDisplayChangeListener {
77 public:
OnDisplayStateChange(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)78     void OnDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
79         const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type) override {};
OnScreenshot(DisplayId displayId)80     void OnScreenshot(DisplayId displayId) override {};
81 };
82 
83 class WindowInfoQueriedListenerTest : public IWindowInfoQueriedListener {
84 public:
HasPrivateWindow(DisplayId id,bool & hasPrivateWindow)85     void HasPrivateWindow(DisplayId id, bool& hasPrivateWindow) override {};
86 };
87 
88 namespace {
89 /**
90  * @tc.name: Dump
91  * @tc.desc: DMS dump
92  * @tc.type: FUNC
93  */
94 HWTEST_F(DisplayManagerServiceTest, Dump, Function | SmallTest | Level3)
95 {
96     std::vector<std::u16string> args;
97     ASSERT_EQ(static_cast<int>(DMError::DM_ERROR_INVALID_PARAM), dms_->Dump(-1, args));
98 }
99 
100 /**
101  * @tc.name: Config
102  * @tc.desc: DMS config
103  * @tc.type: FUNC
104  */
105 HWTEST_F(DisplayManagerServiceTest, Config, Function | SmallTest | Level3)
106 {
107     DisplayManagerConfig::intNumbersConfig_.clear();
108     DisplayManagerConfig::enableConfig_.clear();
109     DisplayManagerConfig::stringConfig_.clear();
110     dms_->ConfigureDisplayManagerService();
111 
112     DisplayManagerConfig::intNumbersConfig_ = {
113         {"dpi", {320}},
114         {"defaultDeviceRotationOffset", {90}},
115         {"curvedScreenBoundary", {20, 30, 40, 50}},
116         {"buildInDefaultOrientation", {90}},
117         {"waterfallAreaCompressionSizeWhenHorzontal", {90}}
118     };
119     DisplayManagerConfig::enableConfig_ = {
120         {"isWaterfallDisplay", false},
121         {"isWaterfallAreaCompressionEnableWhenHorizontal", false}
122     };
123     DisplayManagerConfig::stringConfig_ = {
124         {"defaultDisplayCutoutPath", "/path"}
125     };
126 
127     dms_->ConfigureDisplayManagerService();
128 
129     ASSERT_NE(dms_->displayCutoutController_, nullptr);
130     ASSERT_FALSE(dms_->displayCutoutController_->isWaterfallDisplay_);
131     ASSERT_EQ(dms_->displayCutoutController_->curvedScreenBoundary_[0],
132         DisplayManagerConfig::intNumbersConfig_["curvedScreenBoundary"][0]);
133 }
134 
135 /**
136  * @tc.name: DisplayChange
137  * @tc.desc: DMS display change
138  * @tc.type: FUNC
139  */
140 HWTEST_F(DisplayManagerServiceTest, DisplayChange, Function | SmallTest | Level3)
141 {
142     std::map<DisplayId, sptr<DisplayInfo>> displayInfoMap;
143     sptr<DisplayInfo> displayInfo = new DisplayInfo();
144 
145     dms_->RegisterDisplayChangeListener(nullptr);
146     dms_->NotifyDisplayStateChange(0, nullptr, displayInfoMap, DisplayStateChangeType::SIZE_CHANGE);
147     dms_->NotifyScreenshot(0);
148 
149     sptr<DisplayChangeListenerTest> displayChangeListener = new DisplayChangeListenerTest();
150     ASSERT_NE(nullptr, displayChangeListener);
151     dms_->RegisterDisplayChangeListener(displayChangeListener);
152     dms_->NotifyDisplayStateChange(0, displayInfo, displayInfoMap, DisplayStateChangeType::SIZE_CHANGE);
153     dms_->NotifyScreenshot(0);
154 
155     dms_->NotifyDisplayEvent(DisplayEvent::UNLOCK);
156 
157     std::vector<DisplayId> displayIds;
158     dms_->SetFreeze(displayIds, false);
159 }
160 
161 /**
162  * @tc.name: HasPrivateWindow
163  * @tc.desc: DMS has private window
164  * @tc.type: FUNC
165  */
166 HWTEST_F(DisplayManagerServiceTest, HasPrivateWindow, Function | SmallTest | Level3)
167 {
168     bool hasPrivateWindow = false;
169     dms_->abstractDisplayController_->abstractDisplayMap_.clear();
170     dms_->abstractDisplayController_->abstractDisplayMap_ = {
171         {1, nullptr}
172     };
173     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->HasPrivateWindow(0, hasPrivateWindow));
174 
175     dms_->RegisterWindowInfoQueriedListener(nullptr);
176     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->HasPrivateWindow(1, hasPrivateWindow));
177 
178     sptr<WindowInfoQueriedListenerTest> windowInfoQueriedListener = new WindowInfoQueriedListenerTest();
179     ASSERT_NE(nullptr, windowInfoQueriedListener);
180     dms_->RegisterWindowInfoQueriedListener(windowInfoQueriedListener);
181     ASSERT_EQ(DMError::DM_OK, dms_->HasPrivateWindow(1, hasPrivateWindow));
182 }
183 
184 /**
185  * @tc.name: GetDisplayInfo
186  * @tc.desc: DMS get display info
187  * @tc.type: FUNC
188  */
189 HWTEST_F(DisplayManagerServiceTest, GetDisplayInfo, Function | SmallTest | Level2)
190 {
191     // build abstractDisplayController_ env
192     std::string name = "testDisplay";
193     sptr<SupportedScreenModes> info = new SupportedScreenModes();
194     sptr<AbstractScreen> absScreen = new AbstractScreen(dms_->abstractScreenController_, name, 0, 0);
195     sptr<AbstractDisplay> absDisplay = new AbstractDisplay(0, info, absScreen);
196 
197     dms_->abstractDisplayController_->abstractDisplayMap_.clear();
198     ASSERT_EQ(nullptr, dms_->GetDefaultDisplayInfo());
199 
200     dms_->abstractDisplayController_->abstractDisplayMap_ = {
201         {0, absDisplay}
202     };
203     ASSERT_EQ(absDisplay->name_, dms_->GetDefaultDisplayInfo()->name_);
204 
205     ASSERT_EQ(nullptr, dms_->GetDisplayInfoById(1));
206     ASSERT_EQ(absDisplay->name_, dms_->GetDisplayInfoById(0)->name_);
207 
208     ASSERT_EQ(nullptr, dms_->GetDisplayInfoByScreen(1));
209     ASSERT_EQ(absDisplay->name_, dms_->GetDisplayInfoByScreen(0)->name_);
210 
211     absDisplay->screenId_ = 0;
212 
213     ASSERT_EQ(SCREEN_ID_INVALID, dms_->GetScreenIdByDisplayId(1));
214     ASSERT_EQ(0, dms_->GetScreenIdByDisplayId(0));
215 
216     ASSERT_EQ(nullptr, dms_->GetScreenInfoById(1));
217     ASSERT_EQ(nullptr, dms_->GetScreenInfoById(0));
218 
219     ASSERT_EQ(nullptr, dms_->GetScreenGroupInfoById(1));
220     ASSERT_EQ(nullptr, dms_->GetScreenGroupInfoById(0));
221 
222     ASSERT_EQ(SCREEN_ID_INVALID, dms_->GetScreenGroupIdByScreenId(1));
223     ASSERT_EQ(SCREEN_ID_INVALID, dms_->GetScreenGroupIdByScreenId(0));
224 
225     dms_->GetAllDisplayIds();
226     std::vector<sptr<ScreenInfo>> screenInfos;
227     dms_->GetAllScreenInfos(screenInfos);
228 
229     dms_->abstractDisplayController_->abstractDisplayMap_.clear();
230 }
231 
232 /**
233  * @tc.name: VirtualScreen
234  * @tc.desc: DMS virtual screen
235  * @tc.type: FUNC
236  */
237 HWTEST_F(DisplayManagerServiceTest, VirtualScreen, Function | SmallTest | Level3)
238 {
239     VirtualScreenOption option;
240     ASSERT_EQ(-1, dms_->CreateVirtualScreen(option, nullptr));
241 
242     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetVirtualScreenSurface(-1, nullptr));
243     ASSERT_EQ(DMError::DM_ERROR_RENDER_SERVICE_FAILED, dms_->SetVirtualScreenSurface(0, nullptr));
244 
245     std::vector<ScreenId> screens;
246     dms_->RemoveVirtualScreenFromGroup(screens);
247 }
248 
249 /**
250  * @tc.name: OrientationAndRotation
251  * @tc.desc: DMS set oritation and rotation
252  * @tc.type: FUNC
253  */
254 HWTEST_F(DisplayManagerServiceTest, OrientationAndRotation, Function | SmallTest | Level3)
255 {
256     Orientation orientation = Orientation::UNSPECIFIED;
257     ASSERT_TRUE(DMError::DM_OK != dms_->SetOrientation(0, orientation));
258     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->SetOrientationFromWindow(0, orientation, true));
259     Rotation rotation = Rotation::ROTATION_0;
260     ASSERT_EQ(false, dms_->SetRotationFromWindow(0, rotation, true));
261 }
262 
263 /**
264  * @tc.name: ScreenColor
265  * @tc.desc: DMS screen color
266  * @tc.type: FUNC
267  */
268 HWTEST_F(DisplayManagerServiceTest, ScreenColor, Function | SmallTest | Level3)
269 {
270     std::vector<ScreenColorGamut> colorGamuts;
271     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenSupportedColorGamuts(SCREEN_ID_INVALID, colorGamuts));
272     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenSupportedColorGamuts(0, colorGamuts));
273 
274     ScreenColorGamut colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB;
275     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenColorGamut(SCREEN_ID_INVALID, colorGamut));
276     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenColorGamut(0, colorGamut));
277 
278     colorGamut = ScreenColorGamut::COLOR_GAMUT_SRGB;
279     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorGamut(SCREEN_ID_INVALID, colorGamut));
280     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorGamut(0, colorGamut));
281 
282     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorGamut(SCREEN_ID_INVALID, 0));
283     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorGamut(0, 0));
284 
285     ScreenGamutMap gamutMap;
286     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenGamutMap(SCREEN_ID_INVALID, gamutMap));
287     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->GetScreenGamutMap(0, gamutMap));
288 
289     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenGamutMap(SCREEN_ID_INVALID, gamutMap));
290     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenGamutMap(0, gamutMap));
291 
292     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorTransform(SCREEN_ID_INVALID));
293     ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, dms_->SetScreenColorTransform(0));
294 }
295 
296 /**
297  * @tc.name: RegisterDisplayManagerAgent
298  * @tc.desc: DMS rigister display manager agent
299  * @tc.type: FUNC
300  */
301 HWTEST_F(DisplayManagerServiceTest, RegisterDisplayManagerAgent, Function | SmallTest | Level3)
302 {
303     sptr<IDisplayManagerAgent> displayManagerAgent = new DisplayManagerAgentDefault();
304     DisplayManagerAgentType type = DisplayManagerAgentType::DISPLAY_STATE_LISTENER;
305 
306     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->RegisterDisplayManagerAgent(nullptr, type));
307     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->UnregisterDisplayManagerAgent(nullptr, type));
308 
309     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->UnregisterDisplayManagerAgent(displayManagerAgent, type));
310 
311     ASSERT_EQ(DMError::DM_OK, dms_->RegisterDisplayManagerAgent(displayManagerAgent, type));
312     ASSERT_EQ(DMError::DM_OK, dms_->UnregisterDisplayManagerAgent(displayManagerAgent, type));
313 }
314 
315 /**
316  * @tc.name: ScreenPower
317  * @tc.desc: DMS screen power
318  * @tc.type: FUNC
319  */
320 HWTEST_F(DisplayManagerServiceTest, ScreenPower, Function | SmallTest | Level3)
321 {
322     PowerStateChangeReason reason = PowerStateChangeReason::POWER_BUTTON;
323     ScreenPowerState state = ScreenPowerState::POWER_ON;
324     DisplayState displayState = DisplayState::ON;
325 
326     ASSERT_EQ(false, dms_->WakeUpBegin(reason));
327     ASSERT_EQ(false, dms_->WakeUpEnd());
328 
329     ASSERT_EQ(false, dms_->SuspendBegin(reason));
330     ASSERT_EQ(false, dms_->SuspendEnd());
331 
332     ASSERT_EQ(false, dms_->SetScreenPowerForAll(state, reason));
333 
334     ASSERT_EQ(true, dms_->SetDisplayState(displayState));
335     ASSERT_EQ(DisplayState::ON, dms_->GetDisplayState(0));
336 }
337 
338 /**
339  * @tc.name: RsDisplayNode
340  * @tc.desc: DMS rs display node
341  * @tc.type: FUNC
342  */
343 HWTEST_F(DisplayManagerServiceTest, RsDisplayNode, Function | SmallTest | Level3)
344 {
345     struct RSSurfaceNodeConfig config;
346     std::shared_ptr<RSSurfaceNode> surfaceNode = RSSurfaceNode::Create(config);
347     dms_->UpdateRSTree(DISPLAY_ID_INVALID, DISPLAY_ID_INVALID, surfaceNode, true, false);
348     dms_->UpdateRSTree(0, 0, surfaceNode, true, false);
349 }
350 
351 /**
352  * @tc.name: MirrorAndExpand
353  * @tc.desc: DMS mirror
354  * @tc.type: FUNC
355  */
356 HWTEST_F(DisplayManagerServiceTest, MirrorAndExpand, Function | SmallTest | Level3)
357 {
358     std::vector<ScreenId> mirrorScreenIds;
359     ScreenId screenGroupId1 = DISPLAY_ID_INVALID;
360     dms_->MakeMirror(DISPLAY_ID_INVALID, mirrorScreenIds, screenGroupId1);
361     ASSERT_EQ(SCREEN_ID_INVALID, screenGroupId1);
362     ASSERT_EQ(DMError::DM_OK, dms_->StopMirror(mirrorScreenIds));
363 
364     std::vector<ScreenId> expandScreenIds;
365     std::vector<Point> startPoints;
366     ScreenId screenGroupId2 = DISPLAY_ID_INVALID;
367     dms_->MakeExpand(expandScreenIds, startPoints, screenGroupId2);
368     ASSERT_EQ(SCREEN_ID_INVALID, screenGroupId2);
369     ASSERT_EQ(DMError::DM_OK, dms_->StopExpand(expandScreenIds));
370 }
371 
372 /**
373  * @tc.name: ScreenActiveMode
374  * @tc.desc: DMS mirror
375  * @tc.type: FUNC
376  */
377 HWTEST_F(DisplayManagerServiceTest, ScreenActiveMode, Function | SmallTest | Level3)
378 {
379     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms_->SetScreenActiveMode(SCREEN_ID_INVALID, 0));
380 }
381 
382 /**
383  * @tc.name: VirtualPixelRatio
384  * @tc.desc: DMS mirror
385  * @tc.type: FUNC
386  */
387 HWTEST_F(DisplayManagerServiceTest, VirtualPixelRatio, Function | SmallTest | Level3)
388 {
389     ASSERT_TRUE(DMError::DM_OK != dms_->SetVirtualPixelRatio(SCREEN_ID_INVALID, 0.f));
390 }
391 
392 /**
393  * @tc.name: ScreenRotationLock
394  * @tc.desc: DMS mirror
395  * @tc.type: FUNC
396  */
397 HWTEST_F(DisplayManagerServiceTest, ScreenRotationLock, Function | SmallTest | Level3)
398 {
399     dms_->SetScreenRotationLocked(false);
400 
401     bool isLocked = false;
402     dms_->IsScreenRotationLocked(isLocked);
403     ASSERT_EQ(false, isLocked);
404 
405     ASSERT_NE(nullptr, dms_->GetCutoutInfo(10));
406 }
407 
408 /**
409  * @tc.name: AddSurfaceNodeToDisplay | RemoveSurfaceNodeFromDisplay
410  * @tc.desc: add/remove surfaceNode to/from display
411  * @tc.type: FUNC
412  */
413 HWTEST_F(DisplayManagerServiceTest, AddAndRemoveSurfaceNode, Function | SmallTest | Level3)
414 {
415     sptr<DisplayManagerService> dms = new DisplayManagerService();
416     std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
417     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms->AddSurfaceNodeToDisplay(DEFAULT_DISPLAY, surfaceNode, true));
418     surfaceNode = std::make_shared<RSSurfaceNode>(RSSurfaceNodeConfig{}, true);
419     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms->AddSurfaceNodeToDisplay(DEFAULT_DISPLAY, surfaceNode, true));
420     std::shared_ptr<RSDisplayNode> displayNode = std::make_shared<MockRSDisplayNode>(RSDisplayNodeConfig{});
421     sptr<SupportedScreenModes> info = new SupportedScreenModes;
422     sptr<AbstractScreen> absScreen =
423         new AbstractScreen(nullptr, "", INVALID_SCREEN_ID, INVALID_SCREEN_ID);
424     dms->abstractDisplayController_->abstractDisplayMap_[DEFAULT_DISPLAY] =
425         new AbstractDisplay(DEFAULT_DISPLAY, info, absScreen);
426     dms->abstractDisplayController_->abstractDisplayMap_[DEFAULT_DISPLAY]->screenId_ = DEFAULT_SCREEN;
427 
428     dms->abstractScreenController_->dmsScreenMap_[DEFAULT_SCREEN] =
429         new AbstractScreen(dms->abstractScreenController_, "", INVALID_SCREEN_ID, INVALID_SCREEN_ID);
430     ASSERT_EQ(DMError::DM_ERROR_NULLPTR, dms->AddSurfaceNodeToDisplay(DEFAULT_DISPLAY, surfaceNode, true));
431     dms->abstractScreenController_->dmsScreenMap_[DEFAULT_SCREEN]->rsDisplayNode_ = displayNode;
432 
433     EXPECT_CALL(*reinterpret_cast<MockRSDisplayNode*>(displayNode.get()), AddChild(_, _));
434     ASSERT_EQ(DMError::DM_OK, dms->AddSurfaceNodeToDisplay(DEFAULT_DISPLAY, surfaceNode, false));
435     EXPECT_CALL(*reinterpret_cast<MockRSDisplayNode*>(displayNode.get()), RemoveChild(_));
436     ASSERT_EQ(DMError::DM_OK, dms->RemoveSurfaceNodeFromDisplay(DEFAULT_DISPLAY, surfaceNode));
437 
438     testing::Mock::AllowLeak(displayNode.get());
439 }
440 }
441 } // namespace Rosen
442 } // namespace OHOS
443