1 /*
2 * Copyright (c) 2025 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 "native_window.h"
19 #include "ui/rs_surface_node.h"
20 #include "utils/canvas_utils.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28
29 constexpr static int32_t WIDTH = 720;
30 constexpr static int32_t HEIGHT = 1280;
31
32 class CanvasUtilsTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 protected:
39 OHNativeWindow *window_ = nullptr;
40 std::shared_ptr<RSSurfaceNode> surfaceNode = nullptr;
41 };
42
SetUpTestCase()43 void CanvasUtilsTest::SetUpTestCase() {}
TearDownTestCase()44 void CanvasUtilsTest::TearDownTestCase() {}
45
SetUp()46 void CanvasUtilsTest::SetUp()
47 {
48 RSSurfaceNodeConfig config;
49 surfaceNode = RSSurfaceNode::Create(config);
50 EXPECT_NE(surfaceNode, nullptr);
51 sptr<OHOS::Surface> surf = surfaceNode->GetSurface();
52 window_ = CreateNativeWindowFromSurface(&surf);
53 EXPECT_NE(window_, nullptr);
54 NativeWindowHandleOpt(window_, SET_BUFFER_GEOMETRY, WIDTH, HEIGHT);
55 }
56
TearDown()57 void CanvasUtilsTest::TearDown() {}
58
59 /*
60 * @tc.name: LockAndUnlockCanvas001
61 * @tc.desc: test for Lock and Unlock canvas.
62 * @tc.type: FUNC
63 * @tc.require: I766AZ
64 */
65 HWTEST_F(CanvasUtilsTest, LockAndUnlockCanvas, TestSize.Level1)
66 {
67 Canvas* canvas = CanvasUtils::CreateLockCanvas(nullptr);
68 EXPECT_EQ(canvas, nullptr);
69 bool ret = CanvasUtils::UnlockCanvas(nullptr, window_);
70 EXPECT_EQ(ret, false);
71 ret = CanvasUtils::UnlockCanvas(nullptr, nullptr);
72 EXPECT_EQ(ret, false);
73 Canvas* canvas1 = CanvasUtils::CreateLockCanvas(window_);
74 EXPECT_NE(canvas1, nullptr);
75 ret = CanvasUtils::UnlockCanvas(canvas1, window_);
76 EXPECT_EQ(ret, true);
77 }
78 } // namespace Drawing
79 } // namespace Rosen
80 } // namespace OHOS