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 #include "display_cutout_controller.h"
18 #include "display_manager_service_inner.h"
19 #include "display_manager_service.h"
20 #include "dm_common.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 class DisplayCutoutControllerTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp() override;
32     void TearDown() override;
33 };
34 
SetUpTestCase()35 void DisplayCutoutControllerTest::SetUpTestCase()
36 {
37 }
38 
TearDownTestCase()39 void DisplayCutoutControllerTest::TearDownTestCase()
40 {
41 }
42 
SetUp()43 void DisplayCutoutControllerTest::SetUp()
44 {
45 }
46 
TearDown()47 void DisplayCutoutControllerTest::TearDown()
48 {
49 }
50 
51 namespace {
52 /**
53  * @tc.name: SetCurvedScreenBoundary
54  * @tc.desc: SetCurvedScreenBoundary size < 4
55  * @tc.type: FUNC
56  */
HWTEST_F(DisplayCutoutControllerTest, SetCurvedScreenBoundary, Function | SmallTest | Level1)57 HWTEST_F(DisplayCutoutControllerTest, SetCurvedScreenBoundary, Function | SmallTest | Level1)
58 {
59     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
60     std::vector<int> curvedScreenBoundary;
61     curvedScreenBoundary.emplace_back(2);
62     controller->SetCurvedScreenBoundary(curvedScreenBoundary);
63     ASSERT_EQ(controller->curvedScreenBoundary_.size(), 4);
64 }
65 
66 /**
67  * @tc.name: SetCutoutSvgPath
68  * @tc.desc: SetCutoutSvgPath
69  * @tc.type: FUNC
70  */
HWTEST_F(DisplayCutoutControllerTest, SetCutoutSvgPath, Function | SmallTest | Level1)71 HWTEST_F(DisplayCutoutControllerTest, SetCutoutSvgPath, Function | SmallTest | Level1)
72 {
73     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
74     DisplayId displayId = 1;
75     std::string svgPath = "m10";
76     controller->SetCutoutSvgPath(displayId, svgPath);
77     controller->SetCutoutSvgPath(displayId, svgPath);
78     ASSERT_EQ(controller->svgPaths_.size(), 1);
79     ASSERT_EQ(controller->svgPaths_[displayId].size(), 2);
80 }
81 
82 /**
83  * @tc.name: GetCutoutInfo
84  * @tc.desc: GetCutoutInfo
85  * @tc.type: FUNC
86  */
HWTEST_F(DisplayCutoutControllerTest, GetCutoutInfo, Function | SmallTest | Level1)87 HWTEST_F(DisplayCutoutControllerTest, GetCutoutInfo, Function | SmallTest | Level1)
88 {
89     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
90     DisplayId displayId = DisplayManagerServiceInner::GetInstance().GetDefaultDisplayId();
91     std::string svgPath = "m10";
92     controller->SetCutoutSvgPath(displayId, svgPath);
93     controller->SetIsWaterfallDisplay(true);
94     sptr<CutoutInfo> cutoutInfo = controller->GetCutoutInfo(displayId);
95     ASSERT_NE(cutoutInfo, nullptr);
96 }
97 
98 
99 /**
100  * @tc.name: CalcBuiltInDisplayWaterfallRects
101  * @tc.desc: CalcBuiltInDisplayWaterfallRects
102  * @tc.type: FUNC
103  */
HWTEST_F(DisplayCutoutControllerTest, CalcBuiltInDisplayWaterfallRects, Function | SmallTest | Level1)104 HWTEST_F(DisplayCutoutControllerTest, CalcBuiltInDisplayWaterfallRects, Function | SmallTest | Level1)
105 {
106     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
107     controller->SetIsWaterfallDisplay(true);
108     std::vector<int> curvedScreenBoundary;
109     controller->SetCurvedScreenBoundary(curvedScreenBoundary);
110     controller->CalcBuiltInDisplayWaterfallRects();
111     ASSERT_TRUE(controller->waterfallDisplayAreaRects_.isUninitialized());
112     curvedScreenBoundary.emplace_back(1);
113     curvedScreenBoundary.emplace_back(2);
114     curvedScreenBoundary.emplace_back(3);
115     curvedScreenBoundary.emplace_back(4);
116     controller->SetCurvedScreenBoundary(curvedScreenBoundary);
117     controller->CalcBuiltInDisplayWaterfallRects();
118     ASSERT_TRUE(controller->waterfallDisplayAreaRects_.isUninitialized());
119 }
120 
121 
122 /**
123  * @tc.name: CalcBuiltInDisplayWaterfallRectsByRotation
124  * @tc.desc: CalcBuiltInDisplayWaterfallRectsByRotation
125  * @tc.type: FUNC
126  */
HWTEST_F(DisplayCutoutControllerTest, CalcBuiltInDisplayWaterfallRectsByRotation, Function | SmallTest | Level1)127 HWTEST_F(DisplayCutoutControllerTest, CalcBuiltInDisplayWaterfallRectsByRotation, Function | SmallTest | Level1)
128 {
129     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
130     uint32_t displayHeight = 300;
131     uint32_t displayWidth = 300;
132     std::vector<int> curvedScreenBoundary;
133     curvedScreenBoundary.emplace_back(0);
134     curvedScreenBoundary.emplace_back(0);
135     curvedScreenBoundary.emplace_back(500);
136     curvedScreenBoundary.emplace_back(500);
137     controller->SetCurvedScreenBoundary(curvedScreenBoundary);
138     controller->CalcBuiltInDisplayWaterfallRectsByRotation(Rotation::ROTATION_270, displayHeight, displayWidth);
139     controller->CalcBuiltInDisplayWaterfallRectsByRotation(Rotation::ROTATION_180, displayHeight, displayWidth);
140     controller->CalcBuiltInDisplayWaterfallRectsByRotation(Rotation::ROTATION_90, displayHeight, displayWidth);
141     controller->CalcBuiltInDisplayWaterfallRectsByRotation(Rotation::ROTATION_0, displayHeight, displayWidth);
142     controller->CalcBuiltInDisplayWaterfallRectsByRotation(static_cast<Rotation>(10), displayHeight, displayWidth);
143     ASSERT_FALSE(controller->waterfallDisplayAreaRects_.isUninitialized());
144 }
145 
146 /**
147  * @tc.name: CheckBoundingRectsBoundary01
148  * @tc.desc: CheckBoundingRectsBoundary
149  * @tc.type: FUNC
150  */
HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary01, Function | SmallTest | Level1)151 HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary01, Function | SmallTest | Level1)
152 {
153     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
154     DisplayId displayId = DisplayManagerServiceInner::GetInstance().GetDefaultDisplayId();
155     std::vector<DMRect> boundingRects;
156     controller->CheckBoundingRectsBoundary(displayId, boundingRects);
157     ASSERT_TRUE(boundingRects.empty());
158 }
159 
160 /**
161  * @tc.name: CheckBoundingRectsBoundary02
162  * @tc.desc: CheckBoundingRectsBoundary
163  * @tc.type: FUNC
164  */
HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary02, Function | SmallTest | Level1)165 HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary02, Function | SmallTest | Level1)
166 {
167     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
168     DisplayId displayId = DisplayManagerServiceInner::GetInstance().GetDefaultDisplayId();
169     std::vector<DMRect> boundingRects;
170     DMRect rect1 = {0, 0, 100, 100};
171     DMRect rect2 = {50, 50, 50, 50};
172     boundingRects.push_back(rect1);
173     boundingRects.push_back(rect2);
174     controller->CheckBoundingRectsBoundary(displayId, boundingRects);
175     ASSERT_FALSE(boundingRects.empty());
176 }
177 
178 /**
179  * @tc.name: CheckBoundingRectsBoundary03
180  * @tc.desc: CheckBoundingRectsBoundary
181  * @tc.type: FUNC
182  */
HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary03, Function | SmallTest | Level1)183 HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary03, Function | SmallTest | Level1)
184 {
185     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
186     DisplayId displayId = 0;
187     std::vector<DMRect> boundingRects;
188     DMRect rect = {-1, -1, 100, 100};
189     boundingRects.push_back(rect);
190     controller->CheckBoundingRectsBoundary(displayId, boundingRects);
191     ASSERT_FALSE(boundingRects.empty());
192 }
193 
194 /**
195  * @tc.name: CheckBoundingRectsBoundary04
196  * @tc.desc: CheckBoundingRectsBoundary
197  * @tc.type: FUNC
198  */
HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary04, Function | SmallTest | Level1)199 HWTEST_F(DisplayCutoutControllerTest, CheckBoundingRectsBoundary04, Function | SmallTest | Level1)
200 {
201     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
202     DisplayId displayId = DisplayManagerServiceInner::GetInstance().GetDefaultDisplayId();
203     std::vector<DMRect> boundingRects;
204     DMRect rect1 = {-1, -1, 100, 100};
205     DMRect rect2 = {50, 50, 50, 50};
206     boundingRects.push_back(rect1);
207     boundingRects.push_back(rect2);
208     controller->CheckBoundingRectsBoundary(displayId, boundingRects);
209     ASSERT_FALSE(boundingRects.empty());
210 }
211 
212 /**
213  * @tc.name: CalcCutoutBoundingRect
214  * @tc.desc: CalcCutoutBoundingRect success
215  * @tc.type: FUNC
216  */
HWTEST_F(DisplayCutoutControllerTest, CalcCutoutBoundingRect, Function | SmallTest | Level1)217 HWTEST_F(DisplayCutoutControllerTest, CalcCutoutBoundingRect, Function | SmallTest | Level1)
218 {
219     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
220     std::string svgPath = "M 100,100 m -75,0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0 z";
221     DMRect rect = controller->CalcCutoutBoundingRect(svgPath);
222     DMRect emptyRect = {0, 0, 0, 0};
223     ASSERT_NE(rect, emptyRect);
224 }
225 
226 /**
227  * @tc.name: TransferBoundingRectsByRotation01
228  * @tc.desc: TransferBoundingRectsByRotation empty
229  * @tc.type: FUNC
230  */
HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation01, Function | SmallTest | Level1)231 HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation01, Function | SmallTest | Level1)
232 {
233     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
234     DisplayId id = 10;
235     std::vector<DMRect> boundingRects;
236     controller->TransferBoundingRectsByRotation(id, boundingRects);
237     ASSERT_TRUE(boundingRects.empty());
238 }
239 
240 /**
241  * @tc.name: TransferBoundingRectsByRotation02
242  * @tc.desc: TransferBoundingRectsByRotation empty
243  * @tc.type: FUNC
244  */
HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation02, Function | SmallTest | Level1)245 HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation02, Function | SmallTest | Level1)
246 {
247     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
248     DisplayId id = 10;
249     std::vector<DMRect> emptyRects;
250     controller->boundingRects_[id] = emptyRects;
251     std::vector<DMRect> boundingRects;
252     controller->TransferBoundingRectsByRotation(id, boundingRects);
253     ASSERT_TRUE(boundingRects.empty());
254 }
255 
256 /**
257  * @tc.name: TransferBoundingRectsByRotation03
258  * @tc.desc: TransferBoundingRectsByRotation success
259  * @tc.type: FUNC
260  */
HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation03, Function | SmallTest | Level1)261 HWTEST_F(DisplayCutoutControllerTest, TransferBoundingRectsByRotation03, Function | SmallTest | Level1)
262 {
263     DisplayId id = 11;
264     ScreenId sid = 12;
265     std::string name = "abstract_display_test";
266     SupportedScreenModes modesInfo;
267     modesInfo.width_ = 2160;
268     modesInfo.height_ = 1600;
269     modesInfo.refreshRate_ = 60;
270     sptr<SupportedScreenModes> info = new SupportedScreenModes(modesInfo);
271     sptr<AbstractScreenController> absScreenController;
272     sptr<AbstractScreen> absScreen;
273     absScreenController = nullptr;
274     absScreen = new AbstractScreen(absScreenController, name, sid, 1);
275     auto displayController = DisplayManagerService::GetInstance().abstractDisplayController_;
276     ASSERT_NE(displayController, nullptr);
277 
278     sptr<AbstractDisplay> absDisplay = new AbstractDisplay(id, info, absScreen);
279     absDisplay->RequestRotation(Rotation::ROTATION_0);
280     displayController->abstractDisplayMap_.insert((std::make_pair(id, absDisplay)));
281 
282     sptr<DisplayCutoutController> controller = new DisplayCutoutController();
283     std::vector<DMRect> dmRects;
284     DMRect rect = {1, 1, 100, 100};
285     dmRects.emplace_back(rect);
286     controller->boundingRects_[id] = dmRects;
287     ASSERT_FALSE(controller->boundingRects_.count(id) == 0);
288     ASSERT_FALSE(controller->boundingRects_[id].empty());
289     ASSERT_NE(DisplayManagerServiceInner::GetInstance().GetDisplayById(id), nullptr);
290     std::vector<DMRect> boundingRects;
291     controller->TransferBoundingRectsByRotation(id, boundingRects);
292 
293     absScreenController = DisplayManagerService::GetInstance().abstractScreenController_;
294     ASSERT_NE(absScreenController, nullptr);
295     absScreenController->dmsScreenMap_.insert(std::make_pair(sid, absScreen));
296     absScreen->modes_.emplace_back(info);
297     absDisplay->RequestRotation(Rotation::ROTATION_180);
298     controller->TransferBoundingRectsByRotation(id, boundingRects);
299 
300     absDisplay->RequestRotation(Rotation::ROTATION_90);
301     controller->TransferBoundingRectsByRotation(id, boundingRects);
302 
303     absDisplay->RequestRotation(Rotation::ROTATION_270);
304     controller->TransferBoundingRectsByRotation(id, boundingRects);
305     ASSERT_FALSE(boundingRects.empty());
306     displayController->abstractDisplayMap_.clear();
307     absScreenController->dmsScreenMap_.clear();
308 }
309 }
310 } // Rosen
311 } // OHOS
312