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 <gmock/gmock.h>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21 #include <unordered_map>
22
23 #define private public
24 #include "nweb.h"
25 #include "nweb_helper.h"
26 #include "nweb_adapter_helper.h"
27 #include "nweb_create_window.h"
28 #include "nweb_c_api.h"
29 #include "nweb_init_params.h"
30 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/application_context.h"
31
32 using namespace testing;
33 using namespace testing::ext;
34 using namespace OHOS;
35 using namespace OHOS::Rosen;
36 using namespace OHOS::AbilityRuntime;
37
38 namespace OHOS {
39 namespace {
40 sptr<Surface> g_surface = nullptr;
41 const bool RESULT_OK = true;
42 const int DEFAULT_WIDTH = 2560;
43 const int DEFAULT_HEIGHT = 1396;
44 const int32_t NWEB_MAX_WIDTH = 7681;
45 const std::string MOCK_NWEB_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.arkwebcore";
46 std::shared_ptr<AbilityRuntime::ApplicationContext> g_applicationContext = nullptr;
47 } // namespace
48
49 namespace AbilityRuntime {
GetApplicationContext()50 std::shared_ptr<ApplicationContext> Context::GetApplicationContext()
51 {
52 return g_applicationContext;
53 }
54 } // namespace AbilityRuntime
55
56 namespace NWeb {
57
58 class NwebHelperTest : public testing::Test {
59 public:
60 static void SetUpTestCase(void);
61 static void TearDownTestCase(void);
62 void SetUp();
63 void TearDown();
64 };
65
66 class ApplicationContextMock : public ApplicationContext {
67 public:
68 MOCK_CONST_METHOD0(GetBaseDir, std::string());
69 };
70
71 class MockNWebEngine : public OHOS::NWeb::NWebEngine {
72 public:
CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)73 std::shared_ptr<NWeb> CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info)
74 {
75 return nullptr;
76 }
77
GetNWeb(int32_t nweb_id)78 std::shared_ptr<NWeb> GetNWeb(int32_t nweb_id)
79 {
80 return nullptr;
81 }
82
GetDataBase()83 std::shared_ptr<NWebDataBase> GetDataBase()
84 {
85 return nullptr;
86 }
87
GetWebStorage()88 std::shared_ptr<NWebWebStorage> GetWebStorage()
89 {
90 return nullptr;
91 }
92
GetCookieManager()93 std::shared_ptr<NWebCookieManager> GetCookieManager()
94 {
95 return nullptr;
96 }
97
GetDownloadManager()98 std::shared_ptr<NWebDownloadManager> GetDownloadManager()
99 {
100 return nullptr;
101 }
102
SetWebTag(int32_t nweb_id,const char * web_tag)103 void SetWebTag(int32_t nweb_id, const char* web_tag) {}
104
InitializeWebEngine(std::shared_ptr<NWebEngineInitArgs> init_args)105 void InitializeWebEngine(std::shared_ptr<NWebEngineInitArgs> init_args) {}
106
PrepareForPageLoad(const std::string & url,bool preconnectable,int32_t num_sockets)107 void PrepareForPageLoad(const std::string& url, bool preconnectable, int32_t num_sockets) {}
108
SetWebDebuggingAccess(bool isEnableDebug)109 void SetWebDebuggingAccess(bool isEnableDebug) {}
110
AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)111 void AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts) {}
112
RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string> & hosts)113 void RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts) {}
ClearIntelligentTrackingPreventionBypassingList()114 void ClearIntelligentTrackingPreventionBypassingList() {}
115
GetDefaultUserAgent()116 std::string GetDefaultUserAgent()
117 {
118 return "";
119 }
120
PauseAllTimers()121 void PauseAllTimers() {}
122
ResumeAllTimers()123 void ResumeAllTimers() {}
124
PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs> & pre_args,const std::map<std::string,std::string> & additional_http_headers,const std::string & cache_key,const uint32_t & cache_valid_time)125 void PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args,
126 const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key,
127 const uint32_t& cache_valid_time)
128 {}
129
SetRenderProcessMode(RenderProcessMode mode)130 void SetRenderProcessMode(RenderProcessMode mode) {}
131
GetRenderProcessMode()132 RenderProcessMode GetRenderProcessMode()
133 {
134 return RenderProcessMode::SINGLE_MODE;
135 }
136
ClearPrefetchedResource(const std::vector<std::string> & cache_key_list)137 void ClearPrefetchedResource(const std::vector<std::string>& cache_key_list) {}
138
WarmupServiceWorker(const std::string & url)139 void WarmupServiceWorker(const std::string& url) {}
140
SetHostIP(const std::string & hostName,const std::string & address,int32_t aliveTime)141 void SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime) {}
142
ClearHostIP(const std::string & hostName)143 void ClearHostIP(const std::string& hostName) {}
144
SetWholeWebDrawing()145 void SetWholeWebDrawing() {}
146
GetAdsBlockManager()147 std::shared_ptr<NWebAdsBlockManager> GetAdsBlockManager()
148 {
149 return nullptr;
150 }
151
EnableBackForwardCache(bool nativeEmbed,bool mediaTakeOver)152 void EnableBackForwardCache(bool nativeEmbed, bool mediaTakeOver) {}
153 };
154
SetUpTestCase(void)155 void NwebHelperTest::SetUpTestCase(void)
156 {
157 RSSurfaceNodeConfig config;
158 config.SurfaceNodeName = "webTestSurfaceName";
159 auto surfaceNode = RSSurfaceNode::Create(config, false);
160 EXPECT_NE(surfaceNode, nullptr);
161 g_surface = surfaceNode->GetSurface();
162 EXPECT_NE(g_surface, nullptr);
163 }
164
TearDownTestCase(void)165 void NwebHelperTest::TearDownTestCase(void)
166 {}
167
SetUp(void)168 void NwebHelperTest::SetUp(void)
169 {}
170
TearDown(void)171 void NwebHelperTest::TearDown(void)
172 {}
173
174 std::unordered_map<std::string, std::string> g_argsMap;
175
176 /**
177 * @tc.name: NWebHelper_SetBundlePath_001
178 * @tc.desc: SetBundlePath.
179 * @tc.type: FUNC
180 * @tc.require: AR000GGHJ8
181 */
182 HWTEST_F(NwebHelperTest, NWebHelper_SetBundlePath_001, TestSize.Level1)
183 {
184 int32_t nweb_id = 1;
185 bool result = NWebHelper::Instance().LoadNWebSDK();
186 EXPECT_FALSE(result);
187 NWebHelper::Instance().SetBundlePath(MOCK_NWEB_INSTALLATION_DIR);
188 result = NWebAdapterHelper::Instance().Init(false);
189 EXPECT_EQ(RESULT_OK, result);
190 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
191 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
192 EXPECT_EQ(nweb, nullptr);
193 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
194 NWebHelper::Instance().SetHttpDns(config);
195 auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
196 EXPECT_EQ(nwebHelper, nullptr);
197 NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
198 NWebHelper::Instance().WarmupServiceWorker("web_test");
199 NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0);
200 NWebHelper::Instance().ClearPrefetchedResource({"web_test"});
201 NWebHelper::Instance().EnableBackForwardCache(true, true);
202 result = NWebHelper::Instance().InitAndRun(false);
203 EXPECT_FALSE(result);
204 ApplicationContextMock *contextMock = new ApplicationContextMock();
205 ASSERT_NE(contextMock, nullptr);
206 g_applicationContext.reset(contextMock);
207 EXPECT_CALL(*contextMock, GetBaseDir())
208 .Times(1)
209 .WillRepeatedly(::testing::Return(""));
210 result = NWebHelper::Instance().InitAndRun(false);
211 EXPECT_FALSE(result);
212 NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
213 DEFAULT_WIDTH, DEFAULT_HEIGHT);
214
215 EXPECT_CALL(*contextMock, GetBaseDir())
216 .Times(2)
217 .WillRepeatedly(::testing::Return("test_web"));
218 result = NWebHelper::Instance().InitAndRun(false);
219 EXPECT_TRUE(result);
220 NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
221 DEFAULT_WIDTH, DEFAULT_HEIGHT);
222 result = NWebHelper::Instance().LoadNWebSDK();
223 EXPECT_TRUE(result);
224 result = NWebHelper::Instance().LoadNWebSDK();
225 EXPECT_TRUE(result);
226 WebDownloadManager_PutDownloadCallback(nullptr);
227 g_applicationContext.reset();
228 }
229
230 /**
231 * @tc.name: NWebHelper_GetWebStorage_002
232 * @tc.desc: GetWebStorage.
233 * @tc.type: FUNC
234 * @tc.require: AR000GGHJ8
235 */
236 HWTEST_F(NwebHelperTest, NWebHelper_GetWebStorage_002, TestSize.Level1)
237 {
238 auto web_storage = NWebHelper::Instance().GetWebStorage();
239 bool result = false;
240 if (web_storage != nullptr) {
241 result = true;
242 }
243 EXPECT_EQ(RESULT_OK, result);
244 std::string config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
245 EXPECT_TRUE(config.empty());
246 NWebConfigHelper::Instance().perfConfig_.emplace("web/test", "web_test");
247 config = NWebAdapterHelper::Instance().ParsePerfConfig("web", "test");
248 EXPECT_FALSE(config.empty());
249 }
250
251 /**
252 * @tc.name: NWebHelper_GetDataBase_003
253 * @tc.desc: GetDataBase.
254 * @tc.type: FUNC
255 * @tc.require:issueI5OESN
256 */
257 HWTEST_F(NwebHelperTest, NWebHelper_GetDataBase_003, TestSize.Level1)
258 {
259 auto dataBase = NWebHelper::Instance().GetDataBase();
260 bool result = false;
261 if (dataBase != nullptr) {
262 result = true;
263 }
264 EXPECT_EQ(RESULT_OK, result);
265
266 std::shared_ptr<NWebCookieManager> cook = NWebHelper::Instance().GetCookieManager();
267 EXPECT_NE(cook, nullptr);
268
269 void *enhanceSurfaceInfo = nullptr;
270 int32_t temp = 1;
271 std::shared_ptr<NWeb> nweb =
272 NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
273 DEFAULT_WIDTH, DEFAULT_HEIGHT);
274 EXPECT_EQ(nweb, nullptr);
275 enhanceSurfaceInfo = static_cast<void *>(&temp);
276 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
277 DEFAULT_WIDTH, DEFAULT_HEIGHT);
278 EXPECT_EQ(nweb, nullptr);
279 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
280 DEFAULT_WIDTH, NWEB_MAX_WIDTH);
281 EXPECT_EQ(nweb, nullptr);
282 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
283 NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
284 EXPECT_EQ(nweb, nullptr);
285 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
286 nweb = NWebHelper::Instance().CreateNWeb(create_info);
287 EXPECT_EQ(nweb, nullptr);
288 }
289
290 /**
291 * @tc.name: NWebHelper_TryPreReadLib_004
292 * @tc.desc: TryPreReadLib.
293 * @tc.type: FUNC
294 * @tc.require: AR000GGHJ8
295 */
296 HWTEST_F(NwebHelperTest, NWebHelper_TryPreReadLib_004, TestSize.Level1)
297 {
298 std::string hapPath = "";
299 if (access(MOCK_NWEB_INSTALLATION_DIR.c_str(), F_OK) == 0) {
300 hapPath = MOCK_NWEB_INSTALLATION_DIR;
301 }
302 NWebHelper::Instance().TryPreReadLib(false, hapPath);
303 NWebHelper::Instance().TryPreReadLib(true, hapPath);
304 bool result = NWebHelper::Instance().Init(false);
305 EXPECT_TRUE(result);
306 sptr<Surface> surface = nullptr;
307 std::shared_ptr<NWeb> nweb =
308 NWebAdapterHelper::Instance().CreateNWeb(surface, GetInitArgs(),
309 DEFAULT_WIDTH, DEFAULT_HEIGHT);
310 EXPECT_EQ(nweb, nullptr);
311 nweb = NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
312 DEFAULT_WIDTH, NWEB_MAX_WIDTH);
313 EXPECT_EQ(nweb, nullptr);
314 nweb = NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
315 NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
316 EXPECT_EQ(nweb, nullptr);
317 }
318
319 /**
320 * @tc.name: NWebHelper_GetConfigPath_005
321 * @tc.desc: GetConfigPath.
322 * @tc.type: FUNC
323 * @tc.require: AR000GGHJ8
324 */
325 HWTEST_F(NwebHelperTest, NWebHelper_GetConfigPath_005, TestSize.Level1)
326 {
327 std::string configFileName = "test";
328 std::string figPath = NWebConfigHelper::Instance().GetConfigPath(configFileName);
329 EXPECT_FALSE(figPath.empty());
330 std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
331 NWebAdapterHelper::Instance().ParseConfig(initArgs);
332 NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
333 NWebHelper::Instance().WarmupServiceWorker("web_test");
334 NWebHelper::Instance().PrefetchResource(nullptr, {}, "web_test", 0);
335 NWebHelper::Instance().ClearPrefetchedResource({"web_test"});
336 NWebHelper::Instance().bundlePath_.clear();
337 NWebHelper::Instance().EnableBackForwardCache(true, true);
338 bool result = NWebHelper::Instance().InitAndRun(false);
339 EXPECT_TRUE(result);
340 NWebHelper::Instance().SetConnectionTimeout(1);
341 NWebHelper::Instance().LoadWebEngine(true, false);
342
343 // To test SetRenderProcessMode and GetRenderProcessMode.
344 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::SINGLE_MODE);
345 RenderProcessMode render_process_mode =
346 NWebHelper::Instance().GetRenderProcessMode();
347 EXPECT_EQ(render_process_mode, RenderProcessMode::SINGLE_MODE);
348 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::MULTIPLE_MODE);
349 render_process_mode = NWebHelper::Instance().GetRenderProcessMode();
350 EXPECT_EQ(render_process_mode, RenderProcessMode::MULTIPLE_MODE);
351
352 NWebHelper::Instance().nwebEngine_ = nullptr;
353 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::MULTIPLE_MODE);
354 EXPECT_EQ(NWebHelper::Instance().GetRenderProcessMode(), RenderProcessMode::SINGLE_MODE);
355 }
356
357 /**
358 * @tc.name: NWebHelper_LoadNWebSDK_006
359 * @tc.desc: LoadNWebSDK.
360 * @tc.type: FUNC
361 * @tc.require:
362 */
363 HWTEST_F(NwebHelperTest, NWebHelper_LoadNWebSDK_006, TestSize.Level1)
364 {
365 std::shared_ptr<NWebCreateInfo> create_info = std::make_shared<NWebCreateInfoImpl>();
366 NWebHelper::Instance().SetBundlePath(MOCK_NWEB_INSTALLATION_DIR);
367 bool result = NWebAdapterHelper::Instance().Init(false);
368 EXPECT_EQ(RESULT_OK, result);
369 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
370 EXPECT_EQ(nweb, nullptr);
371 result = NWebHelper::Instance().LoadNWebSDK();
372 EXPECT_TRUE(result);
373 static WebDownloadDelegateCallback *downloadCallback;
374 WebDownloader_CreateDownloadDelegateCallback(&downloadCallback);
375 EXPECT_NE(downloadCallback, nullptr);
__anonef6278120202(NWebDownloadItem *downloadItem, WebBeforeDownloadCallbackWrapper *wrapper) 376 OnDownloadBeforeStart fun = [] (NWebDownloadItem *downloadItem, WebBeforeDownloadCallbackWrapper *wrapper) {};
377 WebDownloader_SetDownloadBeforeStart(downloadCallback, fun);
378 WebDownloadManager_PutDownloadCallback(downloadCallback);
__anonef6278120302(NWebDownloadItem *downloadItem, WebDownloadItemCallbackWrapper *wrapper) 379 OnDownloadDidUpdate didUpdate = [] (NWebDownloadItem *downloadItem, WebDownloadItemCallbackWrapper *wrapper) {};
380 WebDownloader_SetDownloadDidUpdate(downloadCallback, didUpdate);
381 NWebDownloadItem *downloadItem = nullptr;
382 WebDownloadItem_CreateWebDownloadItem(&downloadItem);
383 EXPECT_NE(downloadItem, nullptr);
384 WebDownloader_ResumeDownloadStatic(downloadItem);
385 WebDownloader_StartDownload(1, "test_web");
386 WebDownload_Continue(nullptr, "test_web");
387 WebDownload_CancelBeforeDownload(nullptr);
388 WebDownload_PauseBeforeDownload(nullptr);
389 WebDownload_ResumeBeforeDownload(nullptr);
390 WebDownload_Cancel(nullptr);
391 WebDownload_Pause(nullptr);
392 WebDownload_Resume(nullptr);
393 long itemId = WebDownloadItem_GetDownloadItemId(downloadItem);
394 EXPECT_NE(itemId, -1);
395 NWebDownloadItemState state = WebDownloadItem_GetState(downloadItem);
396 EXPECT_NE(state, NWebDownloadItemState::MAX_DOWNLOAD_STATE);
397 NWebDownloadItem *download = nullptr;
398 int speed = WebDownloadItem_CurrentSpeed(download);
399 EXPECT_EQ(speed, 0);
400 int complete = WebDownloadItem_PercentComplete(download);
401 EXPECT_EQ(complete, 0);
402 WebDownloadItem_SetReceivedBytes(downloadItem, 1);
403 WebDownloadItem_TotalBytes(downloadItem);
404 int64_t receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
405 EXPECT_NE(receivedBytes, 0);
406 char* originalUrl = WebDownloadItem_OriginalUrl(downloadItem);
407 EXPECT_EQ(originalUrl, nullptr);
408 char* fileName = WebDownloadItem_SuggestedFileName(downloadItem);
409 EXPECT_EQ(fileName, nullptr);
410 char* disposition = WebDownloadItem_ContentDisposition(downloadItem);
411 EXPECT_EQ(disposition, nullptr);
412 }
413
414 /**
415 * @tc.name: NWebHelper_WebDownloadItem_IsPaused_007
416 * @tc.desc: WebDownloadItem_IsPaused.
417 * @tc.type: FUNC
418 * @tc.require:
419 */
420 HWTEST_F(NwebHelperTest, NWebHelper_WebDownloadItem_IsPaused_007, TestSize.Level1)
421 {
422 bool result = NWebHelper::Instance().LoadNWebSDK();
423 EXPECT_TRUE(result);
424 NWebDownloadItem *downloadItem = nullptr;
425 WebDownloadItem_CreateWebDownloadItem(&downloadItem);
426 EXPECT_NE(downloadItem, nullptr);
427 NWebDownloadItem *download = nullptr;
428 bool isPaused = WebDownloadItem_IsPaused(download);
429 EXPECT_FALSE(isPaused);
430 char* method = WebDownloadItem_Method(downloadItem);
431 EXPECT_EQ(method, nullptr);
432 WebDownloadItem_LastErrorCode(downloadItem);
433 char* receivedSlices = WebDownloadItem_ReceivedSlices(downloadItem);
434 EXPECT_EQ(receivedSlices, nullptr);
435 char* lastModified = WebDownloadItem_LastModified(downloadItem);
436 EXPECT_EQ(lastModified, nullptr);
437 int nWebId = WebDownloadItem_NWebId(download);
438 EXPECT_EQ(nWebId, 0);
439 WebDownloadItem_Destroy(downloadItem);
440 DestroyBeforeDownloadCallbackWrapper(nullptr);
441 DestroyDownloadItemCallbackWrapper(nullptr);
442 WebDownloadItem_SetGuid(downloadItem, "test_web");
443 WebDownloadItem_SetUrl(downloadItem, "test_web");
444 WebDownloadItem_SetFullPath(downloadItem, "test_web");
445 WebDownloadItem_SetETag(downloadItem, "test_web");
446 WebDownloadItem_SetLastModified(downloadItem, "test_web");
447 WebDownloadItem_SetMimeType(downloadItem, "test_web");
448 WebDownloadItem_SetReceivedBytes(downloadItem, 1);
449 WebDownloadItem_SetTotalBytes(downloadItem, 1);
450 WebDownloadItem_SetReceivedSlices(downloadItem, "test_web");
451 char* guid = WebDownloadItem_Guid(downloadItem);
452 EXPECT_NE(guid, nullptr);
453 int64_t totalBytes = WebDownloadItem_TotalBytes(downloadItem);
454 EXPECT_NE(totalBytes, 0);
455 int64_t receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
456 EXPECT_NE(receivedBytes, 0);
457 char* fullPath = WebDownloadItem_FullPath(downloadItem);
458 EXPECT_NE(fullPath, nullptr);
459 char* url = WebDownloadItem_Url(downloadItem);
460 EXPECT_NE(url, nullptr);
461 char* eTag = WebDownloadItem_ETag(downloadItem);
462 EXPECT_NE(eTag, nullptr);
463 char* mimeType = WebDownloadItem_MimeType(downloadItem);
464 EXPECT_NE(mimeType, nullptr);
465 long itemId = WebDownloadItem_GetDownloadItemId(downloadItem);
466 auto state1 = WebDownload_GetItemState(nWebId, itemId);
467 auto state2 = WebDownload_GetItemStateByGuid("test_web");
468 EXPECT_EQ(state1, state2);
469 }
470
471 /**
472 * @tc.name: NWebHelper_LoadWebEngine_008
473 * @tc.desc: LoadWebEngine.
474 * @tc.type: FUNC
475 * @tc.require: AR000GGHJ8
476 */
477 HWTEST_F(NwebHelperTest, NWebHelper_LoadWebEngine_008, TestSize.Level1)
478 {
479 NWebHelper::Instance().nwebEngine_ = nullptr;
480 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
481 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
482 EXPECT_EQ(nweb, nullptr);
483 nweb = NWebHelper::Instance().GetNWeb(1);
484 EXPECT_EQ(nweb, nullptr);
485 std::shared_ptr<NWebCookieManager> cook = NWebHelper::Instance().GetCookieManager();
486 EXPECT_NE(cook, nullptr);
487 auto manager = NWebHelper::Instance().GetAdsBlockManager();
488 EXPECT_NE(manager, nullptr);
489 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
490 NWebHelper::Instance().SetHttpDns(config);
491 NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
492 NWebHelper::Instance().WarmupServiceWorker("web_test");
493 NWebHelper::Instance().GetDataBase();
494 std::shared_ptr<NWebWebStorage> storage = NWebHelper::Instance().GetWebStorage();
495 EXPECT_NE(storage, nullptr);
496 NWebHelper::Instance().SetConnectionTimeout(1);
497 std::vector<std::string> hosts;
498 NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts);
499 NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts);
500 NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList();
501 NWebHelper::Instance().GetDefaultUserAgent();
502 NWebHelper::Instance().PauseAllTimers();
503 NWebHelper::Instance().ResumeAllTimers();
504 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
505 NWebHelper::Instance().LoadWebEngine(true, false);
506 bool result = NWebHelper::Instance().GetWebEngine(true);
507 EXPECT_TRUE(result);
508 result = NWebHelper::Instance().GetWebEngine(true);
509 EXPECT_TRUE(result);
510 cook = NWebHelper::Instance().GetCookieManager();
511 EXPECT_NE(cook, nullptr);
512 manager = NWebHelper::Instance().GetAdsBlockManager();
513 EXPECT_NE(manager, nullptr);
514 NWebHelper::Instance().SetWebTag(1, "webtag");
515
516 NWebHelper::Instance().SetWebTag(1, "webtag");
517 NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts);
518 NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts);
519 NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList();
520 NWebHelper::Instance().GetDefaultUserAgent();
521 NWebHelper::Instance().PauseAllTimers();
522 NWebHelper::Instance().ResumeAllTimers();
523 }
524
525 /**
526 * @tc.name: NWebHelper_GetPerfConfig_001
527 * @tc.desc: GetPerfConfig.
528 * @tc.type: FUNC
529 * @tc.require:
530 */
531 HWTEST_F(NwebHelperTest, NWebHelper_GetPerfConfig_001, TestSize.Level1)
532 {
533 EXPECT_TRUE(NWebAdapterHelper::Instance().GetPerfConfig("test").empty());
534 NWebConfigHelper::Instance().ltpoConfig_["test"] = {OHOS::NWeb::FrameRateSetting{0, 0, 0}};
535 EXPECT_FALSE(NWebAdapterHelper::Instance().GetPerfConfig("test").empty());
536 NWebConfigHelper::Instance().ltpoConfig_.clear();
537 }
538
539 /**
540 * @tc.name: NWebHelper_ParseNWebLTPOConfig_001
541 * @tc.desc: ParseNWebLTPOConfig.
542 * @tc.type: FUNC
543 * @tc.require:
544 */
545 HWTEST_F(NwebHelperTest, NWebHelper_ParseNWebLTPOConfig_001, TestSize.Level1)
546 {
547 EXPECT_TRUE(NWebConfigHelper::Instance().ltpoConfig_.empty());
548 std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
549 NWebAdapterHelper::Instance().ParseConfig(initArgs);
550 EXPECT_TRUE(NWebConfigHelper::Instance().ltpoConfig_.empty());
551 }
552
553 /**
554 * @tc.name: NWebHelper_SetHostIP_001
555 * @tc.desc: SetHostIP.
556 * @tc.type: FUNC
557 * @tc.require:
558 */
559 HWTEST_F(NwebHelperTest, NWebHelper_SetHostIP_001, TestSize.Level1)
560 {
561 std::string hostName = "hello";
562 std::string address = "world";
563 int32_t aliveTime = 0;
564
565 NWebHelper::Instance().nwebEngine_ = nullptr;
566 NWebHelper::Instance().SetHostIP(hostName, address, aliveTime);
567 EXPECT_EQ(NWebHelper::Instance().nwebEngine_, nullptr);
568
569 auto nwebEngineMock = std::make_shared<MockNWebEngine>();
570 NWebHelper::Instance().nwebEngine_ = nwebEngineMock;
571 NWebHelper::Instance().SetHostIP(hostName, address, aliveTime);
572 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
573
574 NWebHelper::Instance().nwebEngine_ = nullptr;
575 }
576
577 /**
578 * @tc.name: NWebHelper_ClearHostIP_001
579 * @tc.desc: ClearHostIP.
580 * @tc.type: FUNC
581 * @tc.require:
582 */
583 HWTEST_F(NwebHelperTest, NWebHelper_ClearHostIP_001, TestSize.Level1)
584 {
585 int32_t nweb_id = 1;
586 auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
587 EXPECT_EQ(nwebHelper, nullptr);
588
589 std::string hostName = "name";
590 NWebHelper::Instance().nwebEngine_ = nullptr;
591 NWebHelper::Instance().ClearHostIP(hostName);
592 EXPECT_EQ(NWebHelper::Instance().nwebEngine_, nullptr);
593
594 auto nwebengineMock = std::make_shared<MockNWebEngine>();
595 NWebHelper::Instance().nwebEngine_ = nwebengineMock;
596 NWebHelper::Instance().ClearHostIP(hostName);
597 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
598
599 NWebHelper::Instance().nwebEngine_ = nullptr;
600 }
601
602 /**
603 * @tc.name: NWebHelper_EnableWholeWebPageDrawing_001
604 * @tc.desc: EnableWholeWebPageDrawing.
605 * @tc.type: FUNC
606 * @tc.require:
607 */
608 HWTEST_F(NwebHelperTest, NWebHelper_EnableWholeWebPageDrawing_001, TestSize.Level1)
609 {
610 int32_t nweb_id = 1;
611 auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
612 EXPECT_EQ(nwebHelper, nullptr);
613
614 auto nwebengineMock = std::make_shared<MockNWebEngine>();
615 NWebHelper::Instance().nwebEngine_ = nwebengineMock;
616 NWebHelper::Instance().SetWholeWebDrawing();
617 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
618
619 NWebHelper::Instance().nwebEngine_ = nullptr;
620 }
621
622 /**
623 * @tc.name: NWebHelper_GetAdsBlockManager_001
624 * @tc.desc: GetAdsBlockManager.
625 * @tc.type: FUNC
626 * @tc.require:
627 */
628 HWTEST_F(NwebHelperTest, NWebHelper_GetAdsBlockManager_001, TestSize.Level1)
629 {
630 NWebHelper::Instance().nwebEngine_ = nullptr;
631 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
632 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
633 EXPECT_EQ(nweb, nullptr);
634 nweb = NWebHelper::Instance().GetNWeb(1);
635 EXPECT_EQ(nweb, nullptr);
636 auto manager = NWebHelper::Instance().GetAdsBlockManager();
637 EXPECT_NE(manager, nullptr);
638 }
639 } // namespace OHOS::NWeb
640 }
641