• 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 "window_impl.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30     const std::string IMAGE_PLACE_HOLDER_PNG_PATH = "/etc/window/resources/bg_place_holder.png";
31     const int WAIT_FOR_SYNC_US = 1000 * 500;  // 500ms
32 }
33 class SurfaceDrawTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp() override;
38     void TearDown() override;
39 
40 public:
41     struct WindowTestInfo {
42         std::string name;
43         Rect rect;
44         WindowType type;
45         WindowMode mode;
46         bool needAvoid;
47         bool parentLimit;
48         bool forbidSplitMove {false};
49         bool showWhenLocked;
50         uint32_t parentId;
51     };
52     sptr<Window> CreateTestWindow(const std::string& name);
53 
54     static inline DisplayId displayId_;
55     static inline int32_t displayWidth_;
56     static inline int32_t displayHeight_;
57     WindowTestInfo windowInfo_;
58 };
59 
SetUpTestCase()60 void SurfaceDrawTest::SetUpTestCase()
61 {
62     displayId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
63     sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplay();
64     if (display == nullptr) {
65         return;
66     }
67     displayWidth_ = display->GetWidth();
68     displayHeight_ = display->GetHeight();
69 }
70 
TearDownTestCase()71 void SurfaceDrawTest::TearDownTestCase()
72 {
73 }
74 
SetUp()75 void SurfaceDrawTest::SetUp()
76 {
77     windowInfo_ = {
78         .name = "main",
79         .rect = {100, 100, 250, 300},
80         .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
81         .mode = WindowMode::WINDOW_MODE_FLOATING,
82         .needAvoid = true,
83         .parentLimit = false,
84         .parentId = INVALID_WINDOW_ID,
85     };
86 }
87 
TearDown()88 void SurfaceDrawTest::TearDown()
89 {
90 }
91 
CreateTestWindow(const std::string & name)92 sptr<Window> SurfaceDrawTest::CreateTestWindow(const std::string& name)
93 {
94     sptr<WindowOption> option = new WindowOption();
95     option->SetDisplayId(displayId_);
96     option->SetWindowType(windowInfo_.type);
97     option->SetWindowRect(windowInfo_.rect);
98     option->SetWindowMode(windowInfo_.mode);
99     option->SetWindowName(name);
100     sptr<Window> window = Window::Create(option->GetWindowName(), option);
101     window->AddWindowFlag(WindowFlag::WINDOW_FLAG_SHOW_WHEN_LOCKED);
102     return window;
103 }
104 
105 namespace {
106 /**
107  * @tc.name: DrawImage
108  * @tc.desc: SurfaceDraw::DrawImage test
109  * @tc.type: FUNC
110  */
111 HWTEST_F(SurfaceDrawTest, DrawImage01, Function | SmallTest | Level1)
112 {
113     ASSERT_FALSE(SurfaceDraw::DrawImage(nullptr, 0, 0, ""));
114     sptr<Window> window = CreateTestWindow("testDrawImage");
115     window->Show();
116     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
117 
118     auto surfaceNode = window->GetSurfaceNode();
119     ASSERT_NE(surfaceNode, nullptr);
120     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
121     uint32_t width = window->GetRect().width_;
122     uint32_t height = window->GetRect().height_;
123     ASSERT_TRUE(SurfaceDraw::DrawImage(surfaceNode, width, height, IMAGE_PLACE_HOLDER_PNG_PATH));
124     ASSERT_FALSE(SurfaceDraw::DrawImage(surfaceNode, -1, -1, IMAGE_PLACE_HOLDER_PNG_PATH));
125     window->Destroy();
126 }
127 /**
128  * @tc.name: DecodeImageToPixelMap
129  * @tc.desc: SurfaceDraw::DecodeImageToPixelMap test
130  * @tc.type: FUNC
131  */
132 HWTEST_F(SurfaceDrawTest, DecodeImageToPixelMap01, Function | SmallTest | Level1)
133 {
134     ASSERT_EQ(SurfaceDraw::DecodeImageToPixelMap(""), nullptr);
135     ASSERT_NE(SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH), nullptr);
136 }
137 /**
138  * @tc.name: DrawMasking
139  * @tc.desc: SurfaceDraw::DrawMasking test
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SurfaceDrawTest, DrawMasking01, Function | SmallTest | Level1)
143 {
144     OHOS::Rosen::Rect screenRect = {0, 0, 0, 0};
145     OHOS::Rosen::Rect transRect = {0, 0, 0, 0};
146     ASSERT_FALSE(SurfaceDraw::DrawMasking(nullptr, screenRect, transRect));
147 
148     sptr<Window> window = CreateTestWindow("testDrawMasking");
149     window->Show();
150     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
151 
152     auto surfaceNode = window->GetSurfaceNode();
153     ASSERT_NE(surfaceNode, nullptr);
154     ASSERT_FALSE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
155 
156     screenRect.width_ = displayWidth_;
157     screenRect.height_ = displayHeight_;
158     transRect.width_ = displayWidth_;
159     transRect.height_ = displayHeight_;
160     ASSERT_TRUE(SurfaceDraw::DrawMasking(surfaceNode, screenRect, transRect));
161     window->Destroy();
162 }
163 /**
164  * @tc.name: DoDrawImageRect
165  * @tc.desc: SurfaceDraw::DoDrawImageRect test
166  * @tc.type: FUNC
167  */
168 HWTEST_F(SurfaceDrawTest, DoDrawImageRect01, Function | SmallTest | Level1)
169 {
170     sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
171     ASSERT_NE(window, nullptr);
172     window->Show();
173     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
174 
175     OHOS::Rosen::Rect rect = window->GetRect();
176     uint32_t color = 0x00660000;
177 
178     auto surfaceNode = window->GetSurfaceNode();
179     ASSERT_NE(surfaceNode, nullptr);
180     sptr<OHOS::Surface> layer = SurfaceDraw::GetLayer(surfaceNode);
181     ASSERT_NE(layer, nullptr);
182     sptr<OHOS::SurfaceBuffer> buffer = SurfaceDraw::GetSurfaceBuffer(layer, rect.width_, rect.height_);
183     ASSERT_NE(buffer, nullptr);
184 
185     ASSERT_FALSE(SurfaceDraw::DoDrawImageRect(buffer, rect, nullptr, color, false));
186 
187     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
188     ASSERT_NE(pixelMap, nullptr);
189 
190     ASSERT_TRUE(SurfaceDraw::DoDrawImageRect(buffer, rect, pixelMap, color, false));
191     window->Destroy();
192 }
193 /**
194  * @tc.name: GetSurfaceSnapshot
195  * @tc.desc: SurfaceDraw::GetSurfaceSnapshot test
196  * @tc.type: FUNC
197  */
198 HWTEST_F(SurfaceDrawTest, GetSurfaceSnapshot01, Function | SmallTest | Level1)
199 {
200     sptr<Window> window = CreateTestWindow("testDoDrawImageRect");
201     ASSERT_NE(window, nullptr);
202     window->Show();
203     usleep(WAIT_FOR_SYNC_US / 20); // wait for rect updated
204 
205     auto surfaceNode = window->GetSurfaceNode();
206     ASSERT_NE(surfaceNode, nullptr);
207 
208     std::shared_ptr<Media::PixelMap> pixelMap = SurfaceDraw::DecodeImageToPixelMap(IMAGE_PLACE_HOLDER_PNG_PATH);
209     ASSERT_NE(pixelMap, nullptr);
210 
211     ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(nullptr, pixelMap, 0, 0, 0));
212     ASSERT_FALSE(SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, 0, 0, 0));
213     window->Destroy();
214 }
215 }
216 } // namespace Rosen
217 } // namespace OHOS