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