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 "surface_draw.h"
19 #include "display.h"
20 #include "display_info.h"
21 #include "display_manager.h"
22 #include "display_manager_proxy.h"
23 #include "window_impl.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 const std::string IMAGE_PLACE_HOLDER_PNG_PATH = "/etc/window/resources/bg_place_holder.png";
32 const int WAIT_FOR_SYNC_US = 1000 * 500; // 500ms
33 }
34 class SurfaceDrawTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40
41 public:
42 struct WindowTestInfo {
43 std::string name;
44 Rect rect;
45 WindowType type;
46 WindowMode mode;
47 bool needAvoid;
48 bool parentLimit;
49 bool forbidSplitMove {false};
50 bool showWhenLocked;
51 uint32_t parentId;
52 };
53 sptr<Window> CreateTestWindow(const std::string& name);
54
55 static inline DisplayId displayId_;
56 static inline int32_t displayWidth_;
57 static inline int32_t displayHeight_;
58 WindowTestInfo windowInfo_;
59 };
60
SetUpTestCase()61 void SurfaceDrawTest::SetUpTestCase()
62 {
63 displayId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
64 sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplay();
65 if (display == nullptr) {
66 return;
67 }
68 displayWidth_ = display->GetWidth();
69 displayHeight_ = display->GetHeight();
70 }
71
TearDownTestCase()72 void SurfaceDrawTest::TearDownTestCase()
73 {
74 }
75
SetUp()76 void SurfaceDrawTest::SetUp()
77 {
78 windowInfo_ = {
79 .name = "main",
80 .rect = {100, 100, 250, 300},
81 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
82 .mode = WindowMode::WINDOW_MODE_FLOATING,
83 .needAvoid = true,
84 .parentLimit = false,
85 .parentId = INVALID_WINDOW_ID,
86 };
87 }
88
TearDown()89 void SurfaceDrawTest::TearDown()
90 {
91 }
92
CreateTestWindow(const std::string & name)93 sptr<Window> SurfaceDrawTest::CreateTestWindow(const std::string& name)
94 {
95 sptr<WindowOption> option = new (std::nothrow)WindowOption();
96 if (option == nullptr) {
97 return nullptr;
98 }
99 option->SetDisplayId(displayId_);
100 option->SetWindowType(windowInfo_.type);
101 option->SetWindowRect(windowInfo_.rect);
102 option->SetWindowMode(windowInfo_.mode);
103 option->SetWindowName(name);
104 sptr<Window> window = Window::Create(option->GetWindowName(), option);
105 if (window == nullptr) {
106 return nullptr;
107 }
108 window->AddWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
109 return window;
110 }
111
112 namespace {
113 /**
114 * @tc.name: DrawImage
115 * @tc.desc: SurfaceDraw::DrawImage test
116 * @tc.type: FUNC
117 */
118 HWTEST_F(SurfaceDrawTest, DrawImage01, Function | SmallTest | Level1)
119 {
120 ASSERT_FALSE(SurfaceDraw::DrawImage(nullptr, 0, 0, ""));
121 sptr<Window> window = CreateTestWindow("testDrawImage");
122 if (window == nullptr) {
123 return;
124 }
125 ASSERT_NE(nullptr, window);
126 window->Show();
127 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
128
129 auto surfaceNode = window->GetSurfaceNode();
130 ASSERT_NE(surfaceNode, nullptr);
131 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
132 uint32_t width = window->GetRect().width_;
133 uint32_t height = window->GetRect().height_;
134 ASSERT_TRUE(SurfaceDraw::DrawImage(surfaceNode, width, height, IMAGE_PLACE_HOLDER_PNG_PATH));
135 ASSERT_FALSE(SurfaceDraw::DrawImage(surfaceNode, -1, -1, IMAGE_PLACE_HOLDER_PNG_PATH));
136 window->Destroy();
137 }
138 /**
139 * @tc.name: DecodeImageToPixelMap
140 * @tc.desc: SurfaceDraw::DecodeImageToPixelMap test
141 * @tc.type: FUNC
142 */
143 HWTEST_F(SurfaceDrawTest, DecodeImageToPixelMap01, Function | SmallTest | Level1)
144 {
145 ASSERT_EQ(SurfaceDraw::DecodeImageToPixelMap(""), nullptr);
146 ASSERT_NE(SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH), nullptr);
147 }
148 /**
149 * @tc.name: DrawMasking
150 * @tc.desc: SurfaceDraw::DrawMasking test
151 * @tc.type: FUNC
152 */
153 HWTEST_F(SurfaceDrawTest, DrawMasking01, Function | SmallTest | Level1)
154 {
155 OHOS::Rosen::Rect screenRect = {0, 0, 0, 0};
156 OHOS::Rosen::Rect transRect = {0, 0, 0, 0};
157 ASSERT_FALSE(SurfaceDraw::DrawMasking(nullptr, screenRect, transRect));
158
159 sptr<Window> window = CreateTestWindow("testDrawMasking");
160 if (window == nullptr) {
161 return;
162 }
163 ASSERT_NE(nullptr, window);
164 window->Show();
165 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
166
167 auto surfaceNode = window->GetSurfaceNode();
168 ASSERT_NE(surfaceNode, nullptr);
169 ASSERT_FALSE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
170
171 screenRect.width_ = displayWidth_;
172 screenRect.height_ = displayHeight_;
173 transRect.width_ = displayWidth_;
174 transRect.height_ = displayHeight_;
175 ASSERT_TRUE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
176 window->Destroy();
177 }
178 /**
179 * @tc.name: DoDrawImageRect
180 * @tc.desc: SurfaceDraw::DoDrawImageRect test
181 * @tc.type: FUNC
182 */
183 HWTEST_F(SurfaceDrawTest, DoDrawImageRect01, Function | SmallTest | Level1)
184 {
185 sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
186 if (window == nullptr) {
187 return;
188 }
189 ASSERT_NE(window, nullptr);
190 window->Show();
191 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
192
193 OHOS::Rosen::Rect rect = window->GetRect();
194 uint32_t color = 0x00660000;
195
196 auto surfaceNode = window->GetSurfaceNode();
197 ASSERT_NE(surfaceNode, nullptr);
198 sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
199 ASSERT_NE(layer, nullptr);
200 sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
201 ASSERT_NE(buffer, nullptr);
202
203 ASSERT_FALSE(SurfaceDraw::DoDrawImageRect(buffer, rect, nullptr, color, false));
204
205 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
206 ASSERT_NE(pixelMap, nullptr);
207
208 ASSERT_TRUE(SurfaceDraw::DoDrawImageRect(buffer, rect, pixelMap, color, false));
209 window->Destroy();
210 }
211 /**
212 * @tc.name: GetSurfaceSnapshot
213 * @tc.desc: SurfaceDraw::GetSurfaceSnapshot test
214 * @tc.type: FUNC
215 */
216 HWTEST_F(SurfaceDrawTest, GetSurfaceSnapshot01, Function | SmallTest | Level1)
217 {
218 sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
219 if (window == nullptr) {
220 return;
221 }
222 ASSERT_NE(window, nullptr);
223 window->Show();
224 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
225
226 auto surfaceNode = window->GetSurfaceNode();
227 ASSERT_NE(surfaceNode, nullptr);
228
229 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
230 ASSERT_NE(pixelMap, nullptr);
231
232 ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(nullptr, pixelMap, 0, 0, 0));
233 ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, 0, 0, 0));
234 window->Destroy();
235 }
236
237 /**
238 * @tc.name: DrawColor
239 * @tc.desc: SurfaceDraw::DrawColor test
240 * @tc.type: FUNC
241 */
242 HWTEST_F(SurfaceDrawTest, DrawColor01, Function | SmallTest | Level1)
243 {
244 ASSERT_FALSE(SurfaceDraw::DrawColor(nullptr, 0, 0, 0));
245 sptr<Window> window = CreateTestWindow("DrawColor");
246 if (window == nullptr) {
247 return;
248 }
249 ASSERT_NE(nullptr, window);
250 window->Show();
251 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
252 auto surfaceNode = window->GetSurfaceNode();
253 ASSERT_NE(surfaceNode, nullptr);
254 uint32_t width = window->GetRect().width_;
255 uint32_t height = window->GetRect().height_;
256 uint32_t color = 0x00660000;
257 ASSERT_TRUE(SurfaceDraw::DrawColor(surfaceNode, width, height, color));
258 ASSERT_FALSE(SurfaceDraw::DrawColor(surfaceNode, -1, -1, color));
259 window->Destroy();
260 }
261
262 /**
263 * @tc.name: DoDraw
264 * @tc.desc: SurfaceDraw::DoDraw test
265 * @tc.type: FUNC
266 */
267 HWTEST_F(SurfaceDrawTest, DoDraw01, Function | SmallTest | Level1)
268 {
269 sptr<Window> window = CreateTestWindow("DoDrawTest01");
270 if (window == nullptr) {
271 return;
272 }
273 ASSERT_NE(nullptr, window);
274 window->Show();
275 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
276 OHOS::Rosen::Rect rect = window->GetRect();
277 auto surfaceNode = window->GetSurfaceNode();
278 ASSERT_NE(surfaceNode, nullptr);
279 sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
280 ASSERT_NE(layer, nullptr);
281 sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
282 ASSERT_NE(buffer, nullptr);
283 ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, ""));
284 window->Destroy();
285 }
286
287 /**
288 * @tc.name: DoDraw
289 * @tc.desc: SurfaceDraw::DoDraw02 test
290 * @tc.type: FUNC
291 */
292 HWTEST_F(SurfaceDrawTest, DoDraw02, Function | SmallTest | Level1)
293 {
294 sptr<Window> window = CreateTestWindow("DoDraw02");
295 if (window == nullptr) {
296 return;
297 }
298 ASSERT_NE(window, nullptr);
299 window->Show();
300 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
301 OHOS::Rosen::Rect rect = window->GetRect();
302 auto surfaceNode = window->GetSurfaceNode();
303 ASSERT_NE(surfaceNode, nullptr);
304 sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
305 ASSERT_NE(layer, nullptr);
306 sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
307 ASSERT_NE(buffer, nullptr);
308 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
309 ASSERT_NE(pixelMap, nullptr);
310 ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, pixelMap));
311 window->Destroy();
312 }
313
314 /**
315 * @tc.name: DoDraw03
316 * @tc.desc: SurfaceDraw::DoDraw03 test
317 * @tc.type: FUNC
318 */
319 HWTEST_F(SurfaceDrawTest, DoDraw03, Function | SmallTest | Level1)
320 {
321 sptr<Window> window = CreateTestWindow("DoDrawTest03");
322 if (window == nullptr) {
323 return;
324 }
325 ASSERT_NE(nullptr, window);
326 window->Show();
327 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
328 OHOS::Rosen::Rect rect = window->GetRect();
329 uint32_t color = 0x00660000;
330 auto surfaceNode = window->GetSurfaceNode();
331 ASSERT_NE(surfaceNode, nullptr);
332 sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
333 ASSERT_NE(layer, nullptr);
334 sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
335 ASSERT_NE(buffer, nullptr);
336 ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, color));
337 window->Destroy();
338 }
339
340 /**
341 * @tc.name: DrawImageRect
342 * @tc.desc: SurfaceDraw::DoDrawImageRect test
343 * @tc.type: FUNC
344 */
345 HWTEST_F(SurfaceDrawTest, DrawImageRect01, Function | SmallTest | Level1)
346 {
347 sptr<Window> window = CreateTestWindow("DrawImageRect");
348 if (window == nullptr) {
349 return;
350 }
351 ASSERT_NE(window, nullptr);
352 window->Show();
353 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
354 OHOS::Rosen::Rect rect = window->GetRect();
355 uint32_t color = 0x00660000;
356 auto surfaceNode = window->GetSurfaceNode();
357 ASSERT_NE(surfaceNode, nullptr);
358 ASSERT_FALSE(SurfaceDraw::DrawImageRect(surfaceNode, rect, nullptr, color, false));
359 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
360 ASSERT_NE(pixelMap, nullptr);
361 ASSERT_TRUE(SurfaceDraw::DrawImageRect(surfaceNode, rect, pixelMap, color, false));
362 window->Destroy();
363 }
364 }
365 } // namespace Rosen
366 } // namespace OHOS