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