• 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 <cstring>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <unordered_map>
21 
22 #include "graphic_common.h"
23 #include "graphic_common_c.h"
24 #include "key_event.h"
25 #include "nweb_create_window.h"
26 #include "surface_buffer_impl.h"
27 
28 #define protected public
29 #define private public
30 
31 #include "nweb_surface_adapter.h"
32 #include <ui/rs_surface_node.h>
33 #include "nweb_window_adapter.h"
34 #include "nweb.h"
35 #include "nweb_adapter_helper.h"
36 #include "surface_buffer.h"
37 #include "surface_type.h"
38 #include "pointer_event.h"
39 #include "window.h"
40 
41 using namespace testing;
42 using namespace testing::ext;
43 using namespace OHOS;
44 using namespace OHOS::MMI;
45 
46 namespace OHOS::NWeb {
47 namespace {
48 sptr<OHOS::Rosen::Window> g_window;
49 NWebCreateInfo g_info;
50 sptr<SurfaceBuffer> g_surfaceBuffer = nullptr;
51 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
52 const uint32_t DEFAULT_WIDTH = 2560;
53 const uint32_t DEFAULT_HEIGHT = 1396;
54 constexpr int BITS_PER_PIXEL = 4;
55 } // namespace
56 
57 class NWebSurfaceAdapterTest : public testing::Test, public IBufferConsumerListenerClazz {
58 public:
59     static void SetUpTestCase(void);
60     static void TearDownTestCase(void);
61     void SetUp() override;
62     void TearDown() override;
63     void OnBufferAvailable() override;
64 };
65 
OnBufferAvailable()66 void NWebSurfaceAdapterTest::OnBufferAvailable()
67 {}
68 
SetUpTestCase(void)69 void NWebSurfaceAdapterTest::SetUpTestCase(void)
70 {}
71 
TearDownTestCase(void)72 void NWebSurfaceAdapterTest::TearDownTestCase(void)
73 {}
74 
SetUp(void)75 void NWebSurfaceAdapterTest::SetUp(void)
76 {}
77 
TearDown(void)78 void NWebSurfaceAdapterTest::TearDown(void)
79 {}
80 
81 class SurfaceBufferImplMock : public SurfaceBufferImpl {
82 public:
83     MOCK_METHOD0(GetVirAddr, void *());
84     MOCK_CONST_METHOD0(GetSize, uint32_t());
85 };
86 
87 /**
88  * @tc.name: NWebSurfaceAdapterTest_GetCreateInfo_001.
89  * @tc.desc: Test the GetCreateInfo.
90  * @tc.type: FUNC
91  * @tc.require:
92  */
93 HWTEST_F(NWebSurfaceAdapterTest, NWebSurfaceAdapterTest_GetCreateInfo_001, TestSize.Level1)
94 {
95     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
96     NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
97     bool result = NWebAdapterHelper::Instance().Init(false);
98     EXPECT_TRUE(result);
99     g_window = CreateWindow();
100     EXPECT_NE(g_window, nullptr);
101     g_info = surfaceAdapter.GetCreateInfo(g_window->GetSurfaceNode()->GetSurface(), GetInitArgs(),
102         DEFAULT_WIDTH, DEFAULT_HEIGHT);
103     sptr<Surface> surface = nullptr;
104     surfaceAdapter.GetCreateInfo(surface, GetInitArgs(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
105 
106     NWebCreateInfo createInfo;
107     surfaceAdapter.GetSize(g_window->GetSurfaceNode()->GetSurface(), createInfo,
108         DEFAULT_WIDTH, DEFAULT_HEIGHT);
109     surfaceAdapter.GetSize(surface, createInfo, DEFAULT_WIDTH, DEFAULT_HEIGHT);
110 }
111 
112 /**
113  * @tc.name: NWebSurfaceAdapterTest_GetRenderInterface_002.
114  * @tc.desc: Test the GetRenderInterface.
115  * @tc.type: FUNC
116  * @tc.require:
117  */
118 HWTEST_F(NWebSurfaceAdapterTest, NWebSurfaceAdapterTest_GetRenderInterface_002, TestSize.Level1)
119 {
120     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
121     surfaceAdapter.GetRenderInterface(g_window->GetSurfaceNode()->GetSurface(), g_info);
__anon7a985e8e0202null122     char *src = new char[DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL] {0};
123     g_info.output_render_frame(src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
124     sptr<Surface> surface = nullptr;
125     surfaceAdapter.GetRenderInterface(surface, g_info);
126     (void)memset_s(src, DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL, 0,
127         DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL);
128     g_info.output_render_frame(src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
129     (void)memset_s(src, DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL, 0,
130         DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL);
131     g_info.output_render_frame(src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
132 }
133 
134 /**
135  * @tc.name: NWebSurfaceAdapterTest_RequestBuffer_003.
136  * @tc.desc: Test the RequestBuffer.
137  * @tc.type: FUNC
138  * @tc.require:
139  */
140 HWTEST_F(NWebSurfaceAdapterTest, NWebSurfaceAdapterTest_RequestBuffer_003, TestSize.Level1)
141 {
142     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
143     sptr<SurfaceBuffer> surfaceBuffer = surfaceAdapter.RequestBuffer(g_window->GetSurfaceNode()->GetSurface(),
144         DEFAULT_WIDTH, DEFAULT_HEIGHT);
145     EXPECT_NE(surfaceBuffer, nullptr);
146     sptr<Surface> surface = nullptr;
147     surfaceBuffer = surfaceAdapter.RequestBuffer(surface, DEFAULT_WIDTH, DEFAULT_HEIGHT);
148     EXPECT_EQ(surfaceBuffer, nullptr);
149 }
150 
151 /**
152  * @tc.name: NWebSurfaceAdapterTest_CopyFrame_004.
153  * @tc.desc: Test the CopyFrame.
154  * @tc.type: FUNC
155  * @tc.require:
156  */
157 HWTEST_F(NWebSurfaceAdapterTest, NWebSurfaceAdapterTest_CopyFrame_004, TestSize.Level1)
158 {
159     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
__anon7a985e8e0302null160     char *src = new char[DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL] {0};
161     bool result = surfaceAdapter.CopyFrame(g_surfaceBuffer, src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
162     EXPECT_FALSE(result);
163     sptr<Surface> surface = g_window->GetSurfaceNode()->GetSurface();
164     EXPECT_NE(surface, nullptr);
165     int32_t releaseFence = -1;
166     BufferRequestConfig requestConfig = {
167         .width = DEFAULT_WIDTH,
168         .height = DEFAULT_HEIGHT,
169         .strideAlignment = sizeof(void *),
170         .format = PIXEL_FMT_RGBA_8888,
171         .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
172         .timeout = 0,
173     };
174     surface->RequestBuffer(g_surfaceBuffer, releaseFence, requestConfig);
175     EXPECT_NE(g_surfaceBuffer, nullptr);
176     result = surfaceAdapter.CopyFrame(g_surfaceBuffer, src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
177     EXPECT_TRUE(result);
178     (void)memset_s(src, DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL, 0,
179         DEFAULT_WIDTH * DEFAULT_HEIGHT * BITS_PER_PIXEL);
180     SurfaceBufferImplMock *mock = new SurfaceBufferImplMock();
181     EXPECT_CALL(*mock, GetVirAddr())
182         .Times(1)
183         .WillRepeatedly(::testing::Return(nullptr));
184     result = surfaceAdapter.CopyFrame((SurfaceBuffer *)mock, src, DEFAULT_WIDTH, DEFAULT_HEIGHT);
185     EXPECT_FALSE(result);
186     delete[] src;
187 }
188 
189 /**
190  * @tc.name: NWebSurfaceAdapterTest_FlushBuffer_005.
191  * @tc.desc: Test the FlushBuffer.
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(NWebSurfaceAdapterTest, NWebSurfaceAdapterTest_FlushBuffer_005, TestSize.Level1)
196 {
197     auto surfaceAdapter = NWebSurfaceAdapter::Instance();
198     bool result = surfaceAdapter.FlushBuffer(g_window->GetSurfaceNode()->GetSurface(), g_surfaceBuffer,
199         DEFAULT_WIDTH, DEFAULT_WIDTH);
200     EXPECT_TRUE(result);
201     sptr<Surface> surface = nullptr;
202     result = surfaceAdapter.FlushBuffer(surface, g_surfaceBuffer, DEFAULT_WIDTH, DEFAULT_WIDTH);
203     EXPECT_FALSE(result);
204 }
205 }