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 ASSERT_NE(nullptr, window);
123 window->Show();
124 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
125
126 auto surfaceNode = window->GetSurfaceNode();
127 ASSERT_NE(surfaceNode, nullptr);
128 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
129 uint32_t width = window->GetRect().width_;
130 uint32_t height = window->GetRect().height_;
131 ASSERT_TRUE(SurfaceDraw::DrawImage(surfaceNode, width, height, IMAGE_PLACE_HOLDER_PNG_PATH));
132 ASSERT_FALSE(SurfaceDraw::DrawImage(surfaceNode, -1, -1, IMAGE_PLACE_HOLDER_PNG_PATH));
133 window->Destroy();
134 }
135 /**
136 * @tc.name: DecodeImageToPixelMap
137 * @tc.desc: SurfaceDraw::DecodeImageToPixelMap test
138 * @tc.type: FUNC
139 */
140 HWTEST_F(SurfaceDrawTest, DecodeImageToPixelMap01, Function | SmallTest | Level1)
141 {
142 ASSERT_EQ(SurfaceDraw::DecodeImageToPixelMap(""), nullptr);
143 ASSERT_NE(SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH), nullptr);
144 }
145 /**
146 * @tc.name: DrawMasking
147 * @tc.desc: SurfaceDraw::DrawMasking test
148 * @tc.type: FUNC
149 */
150 HWTEST_F(SurfaceDrawTest, DrawMasking01, Function | SmallTest | Level1)
151 {
152 OHOS::Rosen::Rect screenRect = {0, 0, 0, 0};
153 OHOS::Rosen::Rect transRect = {0, 0, 0, 0};
154 ASSERT_FALSE(SurfaceDraw::DrawMasking(nullptr, screenRect, transRect));
155
156 sptr<Window> window = CreateTestWindow("testDrawMasking");
157 ASSERT_NE(nullptr, window);
158 window->Show();
159 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
160
161 auto surfaceNode = window->GetSurfaceNode();
162 ASSERT_NE(surfaceNode, nullptr);
163 ASSERT_FALSE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
164
165 screenRect.width_ = displayWidth_;
166 screenRect.height_ = displayHeight_;
167 transRect.width_ = displayWidth_;
168 transRect.height_ = displayHeight_;
169 ASSERT_TRUE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
170 window->Destroy();
171 }
172 /**
173 * @tc.name: DoDrawImageRect
174 * @tc.desc: SurfaceDraw::DoDrawImageRect test
175 * @tc.type: FUNC
176 */
177 HWTEST_F(SurfaceDrawTest, DoDrawImageRect01, Function | SmallTest | Level1)
178 {
179 sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
180 ASSERT_NE(window, nullptr);
181 window->Show();
182 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
183
184 OHOS::Rosen::Rect rect = window->GetRect();
185 uint32_t color = 0x00660000;
186
187 auto surfaceNode = window->GetSurfaceNode();
188 ASSERT_NE(surfaceNode, nullptr);
189 sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
190 ASSERT_NE(layer, nullptr);
191 sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
192 ASSERT_NE(buffer, nullptr);
193
194 ASSERT_FALSE(SurfaceDraw::DoDrawImageRect(buffer, rect, nullptr, color, false));
195
196 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
197 ASSERT_NE(pixelMap, nullptr);
198
199 ASSERT_TRUE(SurfaceDraw::DoDrawImageRect(buffer, rect, pixelMap, color, false));
200 window->Destroy();
201 }
202 /**
203 * @tc.name: GetSurfaceSnapshot
204 * @tc.desc: SurfaceDraw::GetSurfaceSnapshot test
205 * @tc.type: FUNC
206 */
207 HWTEST_F(SurfaceDrawTest, GetSurfaceSnapshot01, Function | SmallTest | Level1)
208 {
209 sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
210 ASSERT_NE(window, nullptr);
211 window->Show();
212 usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
213
214 auto surfaceNode = window->GetSurfaceNode();
215 ASSERT_NE(surfaceNode, nullptr);
216
217 std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
218 ASSERT_NE(pixelMap, nullptr);
219
220 ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(nullptr, pixelMap, 0, 0, 0));
221 ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, 0, 0, 0));
222 window->Destroy();
223 }
224 }
225 } // namespace Rosen
226 } // namespace OHOS