• 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 
27 #define protected public
28 #define private public
29 
30 #include <ui/rs_surface_node.h>
31 #include "nweb_window_adapter.h"
32 #include "nweb.h"
33 #include "nweb_adapter_helper.h"
34 #include "vsync_receiver.h"
35 #include "vsync_distributor.h"
36 #include "vsync_controller.h"
37 #include "surface_buffer.h"
38 #include "surface_type.h"
39 #include "transaction/rs_interfaces.h"
40 #include "pointer_event.h"
41 #include "window.h"
42 
43 using namespace testing;
44 using namespace testing::ext;
45 using namespace OHOS;
46 using namespace OHOS::MMI;
47 using namespace OHOS::Rosen;
48 
49 namespace OHOS {
50 namespace Rosen {
51 
52 class RSInterfacesMock : public RSInterfaces {
53 public:
54     MOCK_METHOD2(CreateVSyncReceiver, std::shared_ptr<VSyncReceiver>(const std::string&,
55                  const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &));
56 };
57 
58 namespace {
59 RSInterfacesMock *g_instance = new RSInterfacesMock();
60 sptr<VSyncConnection> vsyncConnection = nullptr;
61 }
62 
63 class VSyncReceiverMock : public VSyncReceiver {
64 public:
VSyncReceiverMock()65     VSyncReceiverMock() : VSyncReceiver(vsyncConnection) {}
66     MOCK_METHOD1(RequestNextVSync, VsyncError(FrameCallback));
67 };
68 
GetInstance()69 RSInterfaces &RSInterfaces::GetInstance()
70 {
71     return *g_instance;
72 }
73 }
74 
75 namespace NWeb {
76 namespace {
77 sptr<OHOS::Rosen::Window> g_window;
78 NWebCreateInfo g_info;
79 sptr<SurfaceBuffer> g_surfaceBuffer = nullptr;
80 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
81 const std::string TEST_BUFFER = "test render frame buffer";
82 } // namespace
83 
84 class NWebWindowAdapterTest : public testing::Test, public IBufferConsumerListenerClazz {
85 public:
86     static void SetUpTestCase(void);
87     static void TearDownTestCase(void);
88     void SetUp() override;
89     void TearDown() override;
90     void OnBufferAvailable() override;
91 };
92 
OnBufferAvailable()93 void NWebWindowAdapterTest::OnBufferAvailable()
94 {}
95 
SetUpTestCase(void)96 void NWebWindowAdapterTest::SetUpTestCase(void)
97 {}
98 
TearDownTestCase(void)99 void NWebWindowAdapterTest::TearDownTestCase(void)
100 {}
101 
SetUp(void)102 void NWebWindowAdapterTest::SetUp(void)
103 {}
104 
TearDown(void)105 void NWebWindowAdapterTest::TearDown(void)
106 {}
107 
108 /**
109  * @tc.name: NWebInputEvent_GetCreateInfo_001.
110  * @tc.desc: Test the GetCreateInfo.
111  * @tc.type: FUNC
112  * @tc.require:issueI5R6E0
113  */
114 HWTEST_F(NWebWindowAdapterTest, NWebWindowAdapter_GetCreateInfo_001, TestSize.Level1)
115 {
116     NWebWindowAdapter &windowAdapter = NWebWindowAdapter::Instance();
117     NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
118     bool result = NWebAdapterHelper::Instance().Init(false);
119     EXPECT_TRUE(result);
120     g_window = CreateWindow();
121     EXPECT_NE(g_window, nullptr);
122     g_info = windowAdapter.GetCreateInfo(g_window.GetRefPtr(), GetInitArgs());
123     sptr<OHOS::Rosen::Window> window = nullptr;
124     windowAdapter.GetCreateInfo(window.GetRefPtr(), GetInitArgs());
125 }
126 
127 /**
128  * @tc.name: NWebInputEvent_OnInputEvent_002.
129  * @tc.desc: Test the OnInputEvent.
130  * @tc.type: FUNC
131  * @tc.require:issueI5R6E0
132  */
133 HWTEST_F(NWebWindowAdapterTest, NWebWindowAdapter_OnInputEvent_002, TestSize.Level1)
134 {
135     const int DEFAULT_WIDTH = 2560;
136     const int DEFAULT_HEIGHT = 1396;
137     auto windowAdapter = NWebWindowAdapter::Instance();
138     std::shared_ptr<NWeb> mock = std::make_shared<NWebMock>();
139     EXPECT_NE(mock, nullptr);
140     windowAdapter.GetRenderInterface(g_window.GetRefPtr(), g_info);
141     windowAdapter.RegistEventCb(g_window.GetRefPtr(), mock);
142 
143     sptr<Surface> surface = g_window->GetSurfaceNode()->GetSurface();
144     EXPECT_NE(surface, nullptr);
145     int32_t releaseFence = -1;
146     BufferRequestConfig requestConfig = {
147         .width = DEFAULT_WIDTH,
148         .height = DEFAULT_HEIGHT,
149         .strideAlignment = sizeof(void *),
150         .format = PIXEL_FMT_RGBA_8888,
151         .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
152         .timeout = 0,
153     };
154     surface->RequestBuffer(g_surfaceBuffer, releaseFence, requestConfig);
155     EXPECT_NE(g_surfaceBuffer, nullptr);
156 
157     windowAdapter.windowInfoMap_[g_window.GetRefPtr()].cachedSurfaceBuffer = g_surfaceBuffer;
158     bool result = g_info.output_render_frame(TEST_BUFFER.c_str(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
159     EXPECT_FALSE(result);
160     windowAdapter.GetRenderInterface(g_window.GetRefPtr(), g_info);
161     windowAdapter.VsyncCb(g_window.GetRefPtr(), mock);
162 
163     windowAdapter.windowInfoMap_[g_window.GetRefPtr()].cachedSurfaceBuffer = nullptr;
164     result = g_info.output_render_frame(TEST_BUFFER.c_str(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
165     EXPECT_FALSE(result);
166     windowAdapter.VsyncCb(g_window.GetRefPtr(), mock);
167     windowAdapter.GetRenderInterface(g_window.GetRefPtr(), g_info);
168 
169     g_window.GetRefPtr()->GetSurfaceNode()->surface_ = nullptr;
170     result = g_info.output_render_frame(TEST_BUFFER.c_str(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
171     EXPECT_FALSE(result);
172     windowAdapter.GetRenderInterface(g_window.GetRefPtr(), g_info);
173     windowAdapter.RequestVsync(g_window.GetRefPtr(), mock);
174 
175     windowAdapter.windowInfoMap_[g_window.GetRefPtr()].cachedSurfaceBuffer = g_surfaceBuffer;
176     result = g_info.output_render_frame(TEST_BUFFER.c_str(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
177     EXPECT_FALSE(result);
178     windowAdapter.VsyncCb(g_window.GetRefPtr(), mock);
179 }
180 
181 /**
182  * @tc.name: NWebInputEvent_RequestVsync_003.
183  * @tc.desc: Test the RequestVsync.
184  * @tc.type: FUNC
185  * @tc.require:issueI5R6E0
186  */
187 HWTEST_F(NWebWindowAdapterTest, NWebWindowAdapter_RequestVsync_003, TestSize.Level1)
188 {
189     auto windowAdapter = NWebWindowAdapter::Instance();
190     sptr<OHOS::Rosen::Window> window = nullptr;
191     std::shared_ptr<NWeb> mock = std::make_shared<NWebMock>();
192     EXPECT_NE(mock, nullptr);
193     windowAdapter.RequestVsync(window.GetRefPtr(), mock);
194     windowAdapter.VsyncCb(window.GetRefPtr(), mock);
195     windowAdapter.RegistEventCb(window.GetRefPtr(), mock);
196 }
197 
198 /**
199  * @tc.name: NWebInputEvent_RequestVsync_004.
200  * @tc.desc: Test the RequestVsync.
201  * @tc.type: FUNC
202  * @tc.require:issueI5R6E0
203  */
204 HWTEST_F(NWebWindowAdapterTest, NWebWindowAdapter_RequestVsync_004, TestSize.Level1)
205 {
206     auto windowAdapter = NWebWindowAdapter::Instance();
207     std::shared_ptr<NWeb> mock = std::make_shared<NWebMock>();
208     EXPECT_NE(mock, nullptr);
209     VSyncReceiverMock *vsyncMock = new VSyncReceiverMock();
210     EXPECT_NE(vsyncMock, nullptr);
211     windowAdapter.receiver_.reset(vsyncMock);
212     windowAdapter.RequestVsync(g_window.GetRefPtr(), mock);
213 }
214 
215 /**
216  * @tc.name: NWebInputEvent_RequestVsync_005.
217  * @tc.desc: Test the RequestVsync.
218  * @tc.type: FUNC
219  * @tc.require:issueI5R6E0
220  */
221 HWTEST_F(NWebWindowAdapterTest, NWebWindowAdapter_RequestVsync_005, TestSize.Level1)
222 {
223     if (g_instance) {
224         delete g_instance;
225         g_instance = nullptr;
226     }
227     g_instance = new RSInterfacesMock();
228     auto windowAdapter = NWebWindowAdapter::Instance();
229     windowAdapter.receiver_ = nullptr;
230     std::shared_ptr<NWeb> mock = std::make_shared<NWebMock>();
231     EXPECT_NE(mock, nullptr);
232     std::shared_ptr<VSyncReceiverMock> vsmock = std::make_shared<VSyncReceiverMock>();
233     EXPECT_NE(vsmock, nullptr);
234     testing::Mock::AllowLeak(g_instance);
235     EXPECT_CALL(*g_instance, CreateVSyncReceiver(::testing::_, ::testing::_))
236         .Times(1)
237         .WillRepeatedly(::testing::Return(vsmock));
238     VSyncReceiverMock *receiver = vsmock.get();
239     testing::Mock::AllowLeak(receiver);
240     EXPECT_CALL(*receiver, RequestNextVSync(::testing::_))
241         .Times(1)
242         .WillRepeatedly(::testing::Return(GSError::GSERROR_OK));
243     windowAdapter.RequestVsync(g_window.GetRefPtr(), mock);
244 
245     EXPECT_CALL(*g_instance, CreateVSyncReceiver(::testing::_, ::testing::_))
246         .Times(1)
247         .WillRepeatedly(::testing::Return(nullptr));
248     windowAdapter.RequestVsync(g_window.GetRefPtr(), mock);
249 }
250 }
251 }