1 /*
2 * Copyright (c) 2023 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 "interfaces/include/ws_common.h"
18 #include "screen_scene_config.h"
19 #include "session_manager/include/scene_session_manager.h"
20 #include "session_info.h"
21 #include "session/host/include/scene_session.h"
22 #include "window_manager_agent.h"
23 #include "session_manager.h"
24 #include "screen_cutout_controller.h"
25 #include "zidl/window_manager_agent_interface.h"
26 #include "screen_session_manager/include/screen_session_manager.h"
27 #include "display_manager_agent_default.h"
28 #include "screen_scene_config.h"
29 #include "common_test_utils.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Rosen {
36 namespace {
37 constexpr uint32_t SLEEP_TIME_US = 100000;
38 }
39
40 class ScreenCutoutControllerTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46 void SetAceessTokenPermission(const std::string processName);
47 };
48
SetUpTestCase()49 void ScreenCutoutControllerTest::SetUpTestCase()
50 {
51 CommonTestUtils::InjectTokenInfoByHapName(0, "com.ohos.systemui", 0);
52 const char** perms = new const char *[1];
53 perms[0] = "ohos.permission.CAPTURE_SCREEN";
54 CommonTestUtils::SetAceessTokenPermission("foundation", perms, 1);
55 }
56
TearDownTestCase()57 void ScreenCutoutControllerTest::TearDownTestCase()
58 {
59 }
60
SetUp()61 void ScreenCutoutControllerTest::SetUp()
62 {
63 }
64
TearDown()65 void ScreenCutoutControllerTest::TearDown()
66 {
67 usleep(SLEEP_TIME_US);
68 }
69
70 namespace {
71 /**
72 * @tc.name: GetCutoutArea
73 * @tc.desc: GetCutoutArea func
74 * @tc.type: FUNC
75 */
76 HWTEST_F(ScreenCutoutControllerTest, GetCutoutArea, TestSize.Level1)
77 {
78 uint32_t width = 1276;
79 uint32_t height = 2848;
80 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
81
82 std::string svgPath = "M600 44 L676 44 v 76 h -76 Z";
83 ScreenSceneConfig::SetSubCutoutSvgPath(svgPath);
84 std::vector<DMRect> cutoutRects;
85 controller->GetCutoutArea(0, width, height, Rotation::ROTATION_0, cutoutRects);
86 EXPECT_EQ(cutoutRects.size(), 0);
87 controller->GetCutoutArea(0, width, height, Rotation::ROTATION_90, cutoutRects);
88 EXPECT_EQ(cutoutRects.size(), 0);
89 controller->GetCutoutArea(0, width, height, Rotation::ROTATION_180, cutoutRects);
90 EXPECT_EQ(cutoutRects.size(), 0);
91 controller->GetCutoutArea(0, width, height, Rotation::ROTATION_270, cutoutRects);
92 EXPECT_EQ(cutoutRects.size(), 0);
93 }
94
95 /**
96 * @tc.name: CalcCutoutRects
97 * @tc.desc: CalcCutoutRects func
98 * @tc.type: FUNC
99 */
100 HWTEST_F(ScreenCutoutControllerTest, CalcCutoutRects, TestSize.Level1)
101 {
102 DMRect emptyRect = { 0, 0, 0, 0 };
103 uint32_t width = 2224;
104 uint32_t height = 2496;
105 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
106
107 std::vector<DMRect> boundaryRects = {};
108 boundaryRects.emplace_back(DMRect {2109, 28, 69, 69});
109
110 std::vector<DMRect> cutoutRects;
111 controller->CalcCutoutRects(boundaryRects, width, height, Rotation::ROTATION_0, cutoutRects);
112 EXPECT_EQ(cutoutRects.size(), 1);
113 emptyRect = { 2109, 28, 69, 69 };
114 EXPECT_EQ(cutoutRects[0], emptyRect);
115
116 cutoutRects.clear();
117 controller->CalcCutoutRects(boundaryRects, height, width, Rotation::ROTATION_90, cutoutRects);
118 EXPECT_EQ(cutoutRects.size(), 1);
119 emptyRect = { 2399, 2109, 69, 69 };
120 EXPECT_EQ(cutoutRects[0], emptyRect);
121
122 cutoutRects.clear();
123 controller->CalcCutoutRects(boundaryRects, width, height, Rotation::ROTATION_180, cutoutRects);
124 EXPECT_EQ(cutoutRects.size(), 1);
125 emptyRect = { 46, 2399, 69, 69 };
126 EXPECT_EQ(cutoutRects[0], emptyRect);
127
128 cutoutRects.clear();
129 controller->CalcCutoutRects(boundaryRects, height, width, Rotation::ROTATION_270, cutoutRects);
130 EXPECT_EQ(cutoutRects.size(), 1);
131 emptyRect = { 28, 46, 69, 69 };
132 EXPECT_EQ(cutoutRects[0], emptyRect);
133
134 cutoutRects.clear();
135 controller->CalcCutoutRects(boundaryRects, height, width, static_cast<Rotation>(5), cutoutRects);
136 EXPECT_EQ(cutoutRects.size(), 0);
137 }
138
139 /**
140 * @tc.name: GetWaterfallArea
141 * @tc.desc: GetWaterfallArea func
142 * @tc.type: FUNC
143 */
144 HWTEST_F(ScreenCutoutControllerTest, GetWaterfallArea, TestSize.Level1)
145 {
146 DMRect emptyRect = { 0, 0, 0, 0 };
147 uint32_t width = 1276;
148 uint32_t height = 2848;
149 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
150
151 ScreenSceneConfig::isWaterfallDisplay_ = true;
152 WaterfallDisplayAreaRects waterfallArea;
153 controller->GetWaterfallArea(width, height, Rotation::ROTATION_0, waterfallArea);
154 EXPECT_EQ(waterfallArea.left, emptyRect);
155
156 ScreenSceneConfig::intNumbersConfig_["curvedScreenBoundary"] = { 0, 0, 0, 0 };
157 controller->GetWaterfallArea(width, height, Rotation::ROTATION_0, waterfallArea);
158 EXPECT_EQ(waterfallArea.left, emptyRect);
159 }
160
161 /**
162 * @tc.name: CalcCutoutRects
163 * @tc.desc: CalcCutoutRects func
164 * @tc.type: FUNC
165 */
166 HWTEST_F(ScreenCutoutControllerTest, CalcWaterfallRects0, TestSize.Level1)
167 {
168 DMRect emptyRect = { 0, 0, 0, 0 };
169 const std::vector numberVec = { 0, 0, 0, 0 };
170 uint32_t width = 1276;
171 uint32_t height = 2848;
172 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
173 WaterfallDisplayAreaRects waterfallArea;
174
175 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_0, waterfallArea);
176 EXPECT_EQ(waterfallArea.left, emptyRect);
177 EXPECT_EQ(waterfallArea.top, emptyRect);
178 EXPECT_EQ(waterfallArea.right, emptyRect);
179 EXPECT_EQ(waterfallArea.bottom, emptyRect);
180 }
181
182 /**
183 * @tc.name: CalcCutoutRects
184 * @tc.desc: CalcCutoutRects func
185 * @tc.type: FUNC
186 */
187 HWTEST_F(ScreenCutoutControllerTest, CalcWaterfallRects1, TestSize.Level1)
188 {
189 DMRect emptyRect = { 0, 0, 0, 0 };
190 const std::vector numberVec = { 1276, 0, 0, 0 };
191 uint32_t width = 1276;
192 uint32_t height = 2848;
193 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
194 WaterfallDisplayAreaRects waterfallArea;
195
196 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_0, waterfallArea);
197 EXPECT_EQ(waterfallArea.left, emptyRect);
198 EXPECT_EQ(waterfallArea.top, emptyRect);
199 EXPECT_EQ(waterfallArea.right, emptyRect);
200 EXPECT_EQ(waterfallArea.bottom, emptyRect);
201 }
202
203 /**
204 * @tc.name: CalcCutoutRects
205 * @tc.desc: CalcCutoutRects func
206 * @tc.type: FUNC
207 */
208 HWTEST_F(ScreenCutoutControllerTest, CalcWaterfallRects2, TestSize.Level1)
209 {
210 DMRect emptyRect = { 0, 0, 0, 0 };
211 const std::vector numberVec = { 1, 2, 1, 2 };
212 uint32_t width = 1276;
213 uint32_t height = 2848;
214 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
215 WaterfallDisplayAreaRects waterfallArea;
216
217 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_0, waterfallArea);
218 emptyRect = { 0, 0, 1, height };
219 EXPECT_EQ(waterfallArea.left, emptyRect);
220 emptyRect = { 0, 0, width, 2 };
221 EXPECT_EQ(waterfallArea.top, emptyRect);
222 emptyRect = { width - 1, 0, 1, height };
223 EXPECT_EQ(waterfallArea.right, emptyRect);
224 emptyRect = { 0, height - 2, width, 2 };
225 EXPECT_EQ(waterfallArea.bottom, emptyRect);
226
227 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_180, waterfallArea);
228 emptyRect = { 0, 0, 1, height };
229 EXPECT_EQ(waterfallArea.left, emptyRect);
230 emptyRect = { 0, 0, width, 2 };
231 EXPECT_EQ(waterfallArea.top, emptyRect);
232 emptyRect = { width - 1, 0, 1, height };
233 EXPECT_EQ(waterfallArea.right, emptyRect);
234 emptyRect = { 0, height - 2, width, 2 };
235 EXPECT_EQ(waterfallArea.bottom, emptyRect);
236
237 std::swap(width, height);
238 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_90, waterfallArea);
239 emptyRect = { 0, 0, 2, height };
240 EXPECT_EQ(waterfallArea.left, emptyRect);
241 emptyRect = { 0, 0, width, 1 };
242 EXPECT_EQ(waterfallArea.top, emptyRect);
243 emptyRect = { width - 2, 0, 2, height };
244 EXPECT_EQ(waterfallArea.right, emptyRect);
245 emptyRect = { 0, height - 1, width, 1 };
246 EXPECT_EQ(waterfallArea.bottom, emptyRect);
247
248 controller->CalcWaterfallRects(numberVec, width, height, Rotation::ROTATION_270, waterfallArea);
249 emptyRect = { 0, 0, 2, height };
250 EXPECT_EQ(waterfallArea.left, emptyRect);
251 emptyRect = { 0, 0, width, 1 };
252 EXPECT_EQ(waterfallArea.top, emptyRect);
253 emptyRect = { width - 2, 0, 2, height };
254 EXPECT_EQ(waterfallArea.right, emptyRect);
255 emptyRect = { 0, height - 1, width, 1 };
256 EXPECT_EQ(waterfallArea.bottom, emptyRect);
257
258 emptyRect = { 0, 0, 0, 0 };
259 waterfallArea.left = emptyRect;
260 controller->CalcWaterfallRects(numberVec, width, height, static_cast<Rotation>(5), waterfallArea);
261 EXPECT_EQ(waterfallArea.left, emptyRect);
262 }
263
264 /**
265 * @tc.name: InitRect
266 * @tc.desc: InitRect func
267 * @tc.type: FUNC
268 */
269 HWTEST_F(ScreenCutoutControllerTest, InitRect, TestSize.Level1)
270 {
271 DMRect emptyRect = { 0, 0, 0, 0 };
272 sptr<ScreenCutoutController> controller = sptr<ScreenCutoutController>::MakeSptr();
273
274 controller->InitRect(100, 100, 0, 100, emptyRect);
275 EXPECT_EQ(emptyRect, DMRect::NONE());
276 controller->InitRect(100, 100, 100, 0, emptyRect);
277 EXPECT_EQ(emptyRect, DMRect::NONE());
278 controller->InitRect(100, 100, 100, 100, emptyRect);
279 EXPECT_EQ(emptyRect.posX_, 100);
280 EXPECT_EQ(emptyRect.posY_, 100);
281 EXPECT_EQ(emptyRect.width_, 100);
282 EXPECT_EQ(emptyRect.height_, 100);
283 }
284 }
285 } // namespace Rosen
286 } // namespace OHOS
287
288