• 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 "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     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 /**
140  * @tc.name: DecodeImageToPixelMap
141  * @tc.desc: SurfaceDraw::DecodeImageToPixelMap test
142  * @tc.type: FUNC
143  */
144 HWTEST_F(SurfaceDrawTest, DecodeImageToPixelMap01, Function | SmallTest | Level1)
145 {
146     ASSERT_EQ(SurfaceDraw::DecodeImageToPixelMap(""), nullptr);
147     ASSERT_NE(SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH), nullptr);
148 }
149 
150 /**
151  * @tc.name: DrawMasking
152  * @tc.desc: SurfaceDraw::DrawMasking test
153  * @tc.type: FUNC
154  */
155 HWTEST_F(SurfaceDrawTest, DrawMasking01, Function | SmallTest | Level1)
156 {
157     OHOS::Rosen::Rect screenRect = {0, 0, 0, 0};
158     OHOS::Rosen::Rect transRect = {0, 0, 0, 0};
159     ASSERT_FALSE(SurfaceDraw::DrawMasking(nullptr, screenRect, transRect));
160 
161     sptr<Window> window = CreateTestWindow("testDrawMasking");
162     if (window == nullptr) {
163         return;
164     }
165     ASSERT_NE(nullptr, window);
166     window->Show();
167     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
168 
169     auto surfaceNode = window->GetSurfaceNode();
170     ASSERT_NE(surfaceNode, nullptr);
171     ASSERT_FALSE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
172 
173     screenRect.width_ = displayWidth_;
174     screenRect.height_ = displayHeight_;
175     transRect.width_ = displayWidth_;
176     transRect.height_ = displayHeight_;
177     SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect);
178     window->Destroy();
179 }
180 
181 /**
182  * @tc.name: DoDrawImageRect
183  * @tc.desc: SurfaceDraw::DoDrawImageRect test
184  * @tc.type: FUNC
185  */
186 HWTEST_F(SurfaceDrawTest, DoDrawImageRect01, Function | SmallTest | Level1)
187 {
188     sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
189     if (window == nullptr) {
190         return;
191     }
192     ASSERT_NE(window, nullptr);
193     window->Show();
194     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
195 
196     OHOS::Rosen::Rect rect = window->GetRect();
197     uint32_t color = 0x00660000;
198 
199     auto surfaceNode = window->GetSurfaceNode();
200     ASSERT_NE(surfaceNode, nullptr);
201     sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
202     ASSERT_NE(layer, nullptr);
203     sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
204     if (buffer == nullptr) {
205         return;
206     }
207 
208     ASSERT_FALSE(SurfaceDraw::DoDrawImageRect(buffer, rect, nullptr, color, false));
209 
210     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
211     ASSERT_NE(pixelMap, nullptr);
212 
213     ASSERT_TRUE(SurfaceDraw::DoDrawImageRect(buffer, rect, pixelMap, color, false));
214     window->Destroy();
215 }
216 
217 /**
218  * @tc.name: GetSurfaceSnapshot
219  * @tc.desc: SurfaceDraw::GetSurfaceSnapshot test
220  * @tc.type: FUNC
221  */
222 HWTEST_F(SurfaceDrawTest, GetSurfaceSnapshot01, Function | SmallTest | Level1)
223 {
224     sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
225     if (window == nullptr) {
226         return;
227     }
228     ASSERT_NE(window, nullptr);
229     window->Show();
230     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
231 
232     auto surfaceNode = window->GetSurfaceNode();
233     ASSERT_NE(surfaceNode, nullptr);
234 
235     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
236     ASSERT_NE(pixelMap, nullptr);
237 
238     ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(nullptr, pixelMap, 0, 0, 0));
239     ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, 0, 0, 0));
240     window->Destroy();
241 }
242 
243 /**
244  * @tc.name: DrawColor
245  * @tc.desc: SurfaceDraw::DrawColor test
246  * @tc.type: FUNC
247  */
248 HWTEST_F(SurfaceDrawTest, DrawColor01, Function | SmallTest | Level1)
249 {
250     ASSERT_FALSE(SurfaceDraw::DrawColor(nullptr, 0, 0, 0));
251     sptr<Window> window = CreateTestWindow("DrawColor");
252     if (window == nullptr) {
253         return;
254     }
255     ASSERT_NE(nullptr, window);
256     window->Show();
257     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
258     auto surfaceNode = window->GetSurfaceNode();
259     ASSERT_NE(surfaceNode, nullptr);
260     uint32_t width = window->GetRect().width_;
261     uint32_t height = window->GetRect().height_;
262     uint32_t color = 0x00660000;
263     SurfaceDraw::DrawColor(surfaceNode, width, height, color);
264     SurfaceDraw::DrawColor(surfaceNode, -1, -1, color);
265     ASSERT_EQ(WMError::WM_OK, window->Destroy());
266 }
267 
268 /**
269  * @tc.name: DoDraw
270  * @tc.desc: SurfaceDraw::DoDraw test
271  * @tc.type: FUNC
272  */
273 HWTEST_F(SurfaceDrawTest, DoDraw01, Function | SmallTest | Level1)
274 {
275     sptr<Window> window = CreateTestWindow("DoDrawTest01");
276     if (window == nullptr) {
277         return;
278     }
279     ASSERT_NE(nullptr, window);
280     window->Show();
281     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
282     OHOS::Rosen::Rect rect = window->GetRect();
283     auto surfaceNode = window->GetSurfaceNode();
284     ASSERT_NE(surfaceNode, nullptr);
285     sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
286     ASSERT_NE(layer, nullptr);
287     sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
288     ASSERT_NE(buffer, nullptr);
289     ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, ""));
290     window->Destroy();
291 }
292 
293 /**
294  * @tc.name: DoDraw
295  * @tc.desc: SurfaceDraw::DoDraw02 test
296  * @tc.type: FUNC
297  */
298 HWTEST_F(SurfaceDrawTest, DoDraw02, Function | SmallTest | Level1)
299 {
300     sptr<Window> window = CreateTestWindow("DoDraw02");
301     if (window == nullptr) {
302         return;
303     }
304     ASSERT_NE(window, nullptr);
305     window->Show();
306     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
307     OHOS::Rosen::Rect rect = window->GetRect();
308     auto surfaceNode = window->GetSurfaceNode();
309     ASSERT_NE(surfaceNode, nullptr);
310     sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
311     ASSERT_NE(layer, nullptr);
312     sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
313     if (buffer == nullptr) {
314         return;
315     }
316     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
317     ASSERT_NE(pixelMap, nullptr);
318     ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, pixelMap));
319     window->Destroy();
320 }
321 
322 /**
323  * @tc.name: DoDraw03
324  * @tc.desc: SurfaceDraw::DoDraw03 test
325  * @tc.type: FUNC
326  */
327 HWTEST_F(SurfaceDrawTest, DoDraw03, Function | SmallTest | Level1)
328 {
329     sptr<Window> window = CreateTestWindow("DoDrawTest03");
330     if (window == nullptr) {
331         return;
332     }
333     ASSERT_NE(nullptr, window);
334     window->Show();
335     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
336     OHOS::Rosen::Rect rect = window->GetRect();
337     uint32_t color = 0x00660000;
338     auto surfaceNode = window->GetSurfaceNode();
339     ASSERT_NE(surfaceNode, nullptr);
340     sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
341     ASSERT_NE(layer, nullptr);
342     sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
343     if (buffer == nullptr) {
344         return;
345     }
346     ASSERT_FALSE(SurfaceDraw::DoDraw(nullptr, 0, 0, color));
347     window->Destroy();
348 }
349 
350 /**
351  * @tc.name: DrawImageRect
352  * @tc.desc: SurfaceDraw::DoDrawImageRect test
353  * @tc.type: FUNC
354  */
355 HWTEST_F(SurfaceDrawTest, DrawImageRect01, Function | SmallTest | Level1)
356 {
357     sptr<Window> window = CreateTestWindow("DrawImageRect");
358     if (window == nullptr) {
359         return;
360     }
361     ASSERT_NE(window, nullptr);
362     window->Show();
363     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
364     OHOS::Rosen::Rect rect = window->GetRect();
365     uint32_t color = 0x00660000;
366     auto surfaceNode = window->GetSurfaceNode();
367     ASSERT_NE(surfaceNode, nullptr);
368     ASSERT_FALSE(SurfaceDraw::DrawImageRect(surfaceNode, rect, nullptr, color, false));
369     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
370     ASSERT_NE(pixelMap, nullptr);
371     SurfaceDraw::DrawImageRect(surfaceNode, rect, pixelMap, color, false);
372     window->Destroy();
373 }
374 }
375 } // namespace Rosen
376 } // namespace OHOS