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 <gtest/gtest.h>
18 #include <surface.h>
19
20 #define private public
21 #include "nweb_adapter_helper.h"
22 #include "nweb_create_window.h"
23 #include "nweb_helper.h"
24 #undef private
25
26 #include "ohos_adapter_helper.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::Rosen;
32 namespace OHOS::NWeb {
33 namespace {
34 sptr<Surface> g_surface = nullptr;
35 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
36 #if defined(NWEB_PRINT_ENABLE)
37 const std::string PRINT_FILE_DIR = "/data/storage/el2/base/print.png";
38 const std::string PRINT_JOB_NAME = "webPrintTestJob";
39 #endif
40 } // namespace
41
42 #if defined(NWEB_PRINT_ENABLE)
43 class PrintDocumentAdapterImpl : public OHOS::NWeb::PrintDocumentAdapterAdapter {
44 public:
PrintDocumentAdapterImpl()45 PrintDocumentAdapterImpl() {}
46 ~PrintDocumentAdapterImpl() = default;
47
OnStartLayoutWrite(const std::string & jobId,const OHOS::NWeb::PrintAttributesAdapter & oldAttrs,const OHOS::NWeb::PrintAttributesAdapter & newAttrs,uint32_t fd,std::function<void (std::string,uint32_t)> writeResultCallback)48 void OnStartLayoutWrite(const std::string& jobId, const OHOS::NWeb::PrintAttributesAdapter& oldAttrs,
49 const OHOS::NWeb::PrintAttributesAdapter& newAttrs, uint32_t fd,
50 std::function<void(std::string, uint32_t)> writeResultCallback) override
51 {}
52
OnJobStateChanged(const std::string & jobId,uint32_t state)53 void OnJobStateChanged(const std::string& jobId, uint32_t state) override {}
54 };
55 #endif
56
57 class OhosAdapterHelperTest : public testing::Test {
58 public:
59 static void SetUpTestCase(void);
60 static void TearDownTestCase(void);
61 void SetUp();
62 void TearDown();
63 };
64
SetUpTestCase(void)65 void OhosAdapterHelperTest::SetUpTestCase(void)
66 {
67 RSSurfaceNodeConfig config;
68 config.SurfaceNodeName = "webTestSurfaceName";
69 auto surfaceNode = RSSurfaceNode::Create(config, false);
70 EXPECT_NE(surfaceNode, nullptr);
71 g_surface = surfaceNode->GetSurface();
72 EXPECT_NE(g_surface, nullptr);
73 }
74
TearDownTestCase(void)75 void OhosAdapterHelperTest::TearDownTestCase(void) {}
76
SetUp(void)77 void OhosAdapterHelperTest::SetUp(void) {}
78
TearDown(void)79 void OhosAdapterHelperTest::TearDown(void) {}
80
81 /**
82 * @tc.name: OhosAdapterHelper_GetCookieManager_001.
83 * @tc.desc: Test the GetCookieManager.
84 * @tc.type: FUNC
85 * @tc.require:
86 */
87 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetCookieManager_001, TestSize.Level1)
88 {
89 int32_t nweb_id = 1;
90 NWebHelper& helper = NWebHelper::Instance();
91 helper.SetBundlePath(MOCK_INSTALLATION_DIR);
92 helper.Init(false);
93 auto cook = helper.GetCookieManager();
94 EXPECT_NE(cook, nullptr);
95 auto base = helper.GetDataBase();
96 EXPECT_NE(base, nullptr);
97 auto storage = helper.GetWebStorage();
98 EXPECT_NE(storage, nullptr);
99 auto nweb = helper.GetNWeb(nweb_id);
100 EXPECT_EQ(nweb.lock(), nullptr);
101 NWebDOHConfig config;
102 helper.SetHttpDns(config);
103 }
104
105 /**
106 * @tc.name: OhosAdapterHelper_GetInstance_002.
107 * @tc.desc: Test the GetInstance.
108 * @tc.type: FUNC
109 * @tc.require:
110 */
111 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetInstance_002, TestSize.Level1)
112 {
113 OhosAdapterHelper& helper = OhosAdapterHelper::GetInstance();
114 std::unique_ptr<AafwkAppMgrClientAdapter> client = helper.CreateAafwkAdapter();
115 EXPECT_NE(client, nullptr);
116 std::unique_ptr<PowerMgrClientAdapter> powerMgr = helper.CreatePowerMgrClientAdapter();
117 EXPECT_NE(powerMgr, nullptr);
118 std::unique_ptr<DisplayManagerAdapter> display = helper.CreateDisplayMgrAdapter();
119 EXPECT_NE(display, nullptr);
120 std::unique_ptr<BatteryMgrClientAdapter> battery = helper.CreateBatteryClientAdapter();
121 EXPECT_NE(battery, nullptr);
122 helper.GetOhosWebDataBaseAdapterInstance();
123 std::unique_ptr<NetConnectAdapter> connect = helper.CreateNetConnectAdapter();
124 EXPECT_NE(connect, nullptr);
125 helper.GetPasteBoard();
126 std::unique_ptr<AudioRendererAdapter> audioRender = helper.CreateAudioRendererAdapter();
127 EXPECT_NE(audioRender, nullptr);
128 std::unique_ptr<AudioCapturerAdapter> audioCapter = helper.CreateAudioCapturerAdapter();
129 EXPECT_NE(audioCapter, nullptr);
130 helper.GetAudioSystemManager();
131 helper.GetWebPermissionDataBaseInstance();
132 std::unique_ptr<MMIAdapter> mmiAdapter = helper.CreateMMIAdapter();
133 EXPECT_NE(mmiAdapter, nullptr);
134 auto socPerfClientAdapterImpl = helper.CreateSocPerfClientAdapter();
135 EXPECT_NE(socPerfClientAdapterImpl, nullptr);
136 auto ohosResourceAdapterImpl = helper.GetResourceAdapter("testPath");
137 EXPECT_NE(ohosResourceAdapterImpl, nullptr);
138 #if defined(NWEB_PRINT_ENABLE)
139 PrintManagerAdapter& printAdapter = helper.GetPrintManagerInstance();
140 std::vector<std::string> fileList = { PRINT_FILE_DIR };
141 std::vector<uint32_t> fdList = { 1 };
142 std::string taskId;
143 int32_t ret = printAdapter.StartPrint(fileList, fdList, taskId);
144 EXPECT_EQ(ret, -1);
145 std::shared_ptr<PrintDocumentAdapterAdapter> printDocumentAdapterImpl;
146 PrintAttributesAdapter printAttributesAdapter;
147 EXPECT_EQ(printAdapter.Print(PRINT_JOB_NAME, printDocumentAdapterImpl, printAttributesAdapter), -1);
148 void* token = nullptr;
149 EXPECT_EQ(printAdapter.Print(PRINT_JOB_NAME, printDocumentAdapterImpl, printAttributesAdapter, token), -1);
150 #endif
151 sptr<Surface> surface = nullptr;
152 NWebInitArgs initArgs;
153 uint32_t width = 1;
154 uint32_t height = 1;
155 auto nweb = NWebAdapterHelper::Instance().CreateNWeb(surface, initArgs, width, height);
156 EXPECT_EQ(nweb, nullptr);
157 }
158
159 /**
160 * @tc.name: OhosAdapterHelper_GetDataBase_003.
161 * @tc.desc: Test the GetDataBase.
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetDataBase_003, TestSize.Level1)
166 {
167 int32_t nweb_id = 1;
168 NWebHelper& helper = NWebHelper::Instance();
169 NWebCreateInfo create_info;
170 helper.LoadLib(true);
171 helper.libHandleWebEngine_ = nullptr;
172 helper.LoadLib(true);
173 helper.bundlePath_ = "";
174 helper.LoadLib(true);
175 helper.libHandleWebEngine_ = nullptr;
176 helper.LoadLib(true);
177 helper.LoadLib(true);
178 helper.libHandleWebEngine_ = nullptr;
179 NWebDOHConfig config;
180 NWebHelper::Instance().SetHttpDns(config);
181 auto webview = helper.CreateNWeb(create_info);
182 EXPECT_EQ(webview, nullptr);
183 auto cook = helper.GetCookieManager();
184 EXPECT_EQ(cook, nullptr);
185 auto base = helper.GetDataBase();
186 EXPECT_EQ(base, nullptr);
187 auto storage = helper.GetWebStorage();
188 EXPECT_EQ(storage, nullptr);
189 auto nweb = helper.GetNWeb(nweb_id);
190 EXPECT_EQ(nweb.lock(), nullptr);
191 helper.UnloadLib();
192 }
193
194 /**
195 * @tc.name: OhosAdapterHelper_GetSystemPropertiesInstance_004.
196 * @tc.desc: Test the GetSystemPropertiesInstance.
197 * @tc.type: FUNC
198 * @tc.require:
199 */
200 HWTEST_F(OhosAdapterHelperTest, OhosAdapterHelper_GetSystemPropertiesInstance_004, TestSize.Level1)
201 {
202 OhosAdapterHelper& helper = OhosAdapterHelper::GetInstance();
203 helper.GetSystemPropertiesInstance();
204 helper.GetVSyncAdapter();
205 auto initWebAdapter = helper.GetInitWebAdapter();
206 EXPECT_NE(initWebAdapter, nullptr);
207 helper.GetKeystoreAdapterInstance();
208 helper.GetEnterpriseDeviceManagementInstance();
209 helper.GetDatashareInstance();
210 auto imfAdapter = helper.CreateIMFAdapter();
211 EXPECT_NE(imfAdapter, nullptr);
212 auto managerAdapter = helper.GetRootCertDataAdapter();
213 EXPECT_NE(managerAdapter, nullptr);
214 helper.GetAccessTokenAdapterInstance();
215 auto eventHandler = helper.GetEventHandlerAdapter();
216 EXPECT_NE(eventHandler, nullptr);
217 auto playerAdapter = helper.CreatePlayerAdapter();
218 EXPECT_NE(playerAdapter, nullptr);
219 helper.GetWindowAdapterInstance();
220 helper.GetHiSysEventAdapterInstance();
221 helper.GetHiTraceAdapterInstance();
222 helper.GetNetProxyInstance();
223 helper.GetCameraManagerAdapter();
224 auto screenCapture = helper.CreateScreenCaptureAdapter();
225 EXPECT_NE(screenCapture, nullptr);
226 auto dateTimeFormat = helper.CreateDateTimeFormatAdapter();
227 EXPECT_NE(dateTimeFormat, nullptr);
228 auto nativeImage = helper.CreateNativeImageAdapter();
229 EXPECT_NE(nativeImage, nullptr);
230 }
231 } // namespace OHOS::NWeb