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 NWebHelper::Instance().SetCustomSchemeCmdLine("single-process");
339 bool result = NWebHelper::Instance().InitAndRun(false);
340 EXPECT_TRUE(result);
341 NWebHelper::Instance().SetConnectionTimeout(1);
342 NWebHelper::Instance().LoadWebEngine(true, false);
343
344 // To test SetRenderProcessMode and GetRenderProcessMode.
345 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::SINGLE_MODE);
346 RenderProcessMode render_process_mode =
347 NWebHelper::Instance().GetRenderProcessMode();
348 EXPECT_EQ(render_process_mode, RenderProcessMode::SINGLE_MODE);
349 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::MULTIPLE_MODE);
350 render_process_mode = NWebHelper::Instance().GetRenderProcessMode();
351 EXPECT_EQ(render_process_mode, RenderProcessMode::MULTIPLE_MODE);
352
353 NWebHelper::Instance().nwebEngine_ = nullptr;
354 NWebHelper::Instance().SetRenderProcessMode(RenderProcessMode::MULTIPLE_MODE);
355 EXPECT_EQ(NWebHelper::Instance().GetRenderProcessMode(), RenderProcessMode::SINGLE_MODE);
356 }
357
358 /**
359 * @tc.name: NWebHelper_LoadNWebSDK_006
360 * @tc.desc: LoadNWebSDK.
361 * @tc.type: FUNC
362 * @tc.require:
363 */
364 HWTEST_F(NwebHelperTest, NWebHelper_LoadNWebSDK_006, TestSize.Level1)
365 {
366 std::shared_ptr<NWebCreateInfo> create_info = std::make_shared<NWebCreateInfoImpl>();
367 NWebHelper::Instance().SetBundlePath(MOCK_NWEB_INSTALLATION_DIR);
368 bool result = NWebAdapterHelper::Instance().Init(false);
369 EXPECT_EQ(RESULT_OK, result);
370 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
371 EXPECT_EQ(nweb, nullptr);
372 result = NWebHelper::Instance().LoadNWebSDK();
373 EXPECT_TRUE(result);
374 static WebDownloadDelegateCallback *downloadCallback;
375 WebDownloader_CreateDownloadDelegateCallback(&downloadCallback);
376 EXPECT_NE(downloadCallback, nullptr);
__anonff8f5f1c0202(NWebDownloadItem *downloadItem, WebBeforeDownloadCallbackWrapper *wrapper) 377 OnDownloadBeforeStart fun = [] (NWebDownloadItem *downloadItem, WebBeforeDownloadCallbackWrapper *wrapper) {};
378 WebDownloader_SetDownloadBeforeStart(downloadCallback, fun);
379 WebDownloadManager_PutDownloadCallback(downloadCallback);
__anonff8f5f1c0302(NWebDownloadItem *downloadItem, WebDownloadItemCallbackWrapper *wrapper) 380 OnDownloadDidUpdate didUpdate = [] (NWebDownloadItem *downloadItem, WebDownloadItemCallbackWrapper *wrapper) {};
381 WebDownloader_SetDownloadDidUpdate(downloadCallback, didUpdate);
382 NWebDownloadItem *downloadItem = nullptr;
383 WebDownloadItem_CreateWebDownloadItem(&downloadItem);
384 EXPECT_NE(downloadItem, nullptr);
385 WebDownloader_ResumeDownloadStatic(downloadItem);
386 WebDownloader_StartDownload(1, "test_web");
387 WebDownload_Continue(nullptr, "test_web");
388 WebDownload_CancelBeforeDownload(nullptr);
389 WebDownload_PauseBeforeDownload(nullptr);
390 WebDownload_ResumeBeforeDownload(nullptr);
391 WebDownload_Cancel(nullptr);
392 WebDownload_Pause(nullptr);
393 WebDownload_Resume(nullptr);
394 long itemId = WebDownloadItem_GetDownloadItemId(downloadItem);
395 EXPECT_NE(itemId, -1);
396 NWebDownloadItemState state = WebDownloadItem_GetState(downloadItem);
397 EXPECT_NE(state, NWebDownloadItemState::MAX_DOWNLOAD_STATE);
398 NWebDownloadItem *download = nullptr;
399 int speed = WebDownloadItem_CurrentSpeed(download);
400 EXPECT_EQ(speed, 0);
401 int complete = WebDownloadItem_PercentComplete(download);
402 EXPECT_EQ(complete, 0);
403 WebDownloadItem_SetReceivedBytes(downloadItem, 1);
404 WebDownloadItem_TotalBytes(downloadItem);
405 int64_t receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
406 EXPECT_NE(receivedBytes, 0);
407 char* originalUrl = WebDownloadItem_OriginalUrl(downloadItem);
408 EXPECT_EQ(originalUrl, nullptr);
409 char* fileName = WebDownloadItem_SuggestedFileName(downloadItem);
410 EXPECT_EQ(fileName, nullptr);
411 char* disposition = WebDownloadItem_ContentDisposition(downloadItem);
412 EXPECT_EQ(disposition, nullptr);
413 }
414
415 /**
416 * @tc.name: NWebHelper_WebDownloadItem_IsPaused_007
417 * @tc.desc: WebDownloadItem_IsPaused.
418 * @tc.type: FUNC
419 * @tc.require:
420 */
421 HWTEST_F(NwebHelperTest, NWebHelper_WebDownloadItem_IsPaused_007, TestSize.Level1)
422 {
423 bool result = NWebHelper::Instance().LoadNWebSDK();
424 EXPECT_TRUE(result);
425 NWebDownloadItem *downloadItem = nullptr;
426 WebDownloadItem_CreateWebDownloadItem(&downloadItem);
427 EXPECT_NE(downloadItem, nullptr);
428 NWebDownloadItem *download = nullptr;
429 bool isPaused = WebDownloadItem_IsPaused(download);
430 EXPECT_FALSE(isPaused);
431 char* method = WebDownloadItem_Method(downloadItem);
432 EXPECT_EQ(method, nullptr);
433 WebDownloadItem_LastErrorCode(downloadItem);
434 char* receivedSlices = WebDownloadItem_ReceivedSlices(downloadItem);
435 EXPECT_EQ(receivedSlices, nullptr);
436 char* lastModified = WebDownloadItem_LastModified(downloadItem);
437 EXPECT_EQ(lastModified, nullptr);
438 int nWebId = WebDownloadItem_NWebId(download);
439 EXPECT_EQ(nWebId, 0);
440 WebDownloadItem_Destroy(downloadItem);
441 DestroyBeforeDownloadCallbackWrapper(nullptr);
442 DestroyDownloadItemCallbackWrapper(nullptr);
443 WebDownloadItem_SetGuid(downloadItem, "test_web");
444 WebDownloadItem_SetUrl(downloadItem, "test_web");
445 WebDownloadItem_SetFullPath(downloadItem, "test_web");
446 WebDownloadItem_SetETag(downloadItem, "test_web");
447 WebDownloadItem_SetLastModified(downloadItem, "test_web");
448 WebDownloadItem_SetMimeType(downloadItem, "test_web");
449 WebDownloadItem_SetReceivedBytes(downloadItem, 1);
450 WebDownloadItem_SetTotalBytes(downloadItem, 1);
451 WebDownloadItem_SetReceivedSlices(downloadItem, "test_web");
452 char* guid = WebDownloadItem_Guid(downloadItem);
453 EXPECT_NE(guid, nullptr);
454 int64_t totalBytes = WebDownloadItem_TotalBytes(downloadItem);
455 EXPECT_NE(totalBytes, 0);
456 int64_t receivedBytes = WebDownloadItem_ReceivedBytes(downloadItem);
457 EXPECT_NE(receivedBytes, 0);
458 char* fullPath = WebDownloadItem_FullPath(downloadItem);
459 EXPECT_NE(fullPath, nullptr);
460 char* url = WebDownloadItem_Url(downloadItem);
461 EXPECT_NE(url, nullptr);
462 char* eTag = WebDownloadItem_ETag(downloadItem);
463 EXPECT_NE(eTag, nullptr);
464 char* mimeType = WebDownloadItem_MimeType(downloadItem);
465 EXPECT_NE(mimeType, nullptr);
466 long itemId = WebDownloadItem_GetDownloadItemId(downloadItem);
467 auto state1 = WebDownload_GetItemState(nWebId, itemId);
468 auto state2 = WebDownload_GetItemStateByGuid("test_web");
469 EXPECT_EQ(state1, state2);
470 }
471
472 /**
473 * @tc.name: NWebHelper_LoadWebEngine_008
474 * @tc.desc: LoadWebEngine.
475 * @tc.type: FUNC
476 * @tc.require: AR000GGHJ8
477 */
478 HWTEST_F(NwebHelperTest, NWebHelper_LoadWebEngine_008, TestSize.Level1)
479 {
480 NWebHelper::Instance().nwebEngine_ = nullptr;
481 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
482 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
483 EXPECT_EQ(nweb, nullptr);
484 nweb = NWebHelper::Instance().GetNWeb(1);
485 EXPECT_EQ(nweb, nullptr);
486 std::shared_ptr<NWebCookieManager> cook = NWebHelper::Instance().GetCookieManager();
487 EXPECT_NE(cook, nullptr);
488 auto manager = NWebHelper::Instance().GetAdsBlockManager();
489 EXPECT_NE(manager, nullptr);
490 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
491 NWebHelper::Instance().SetHttpDns(config);
492 NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
493 NWebHelper::Instance().WarmupServiceWorker("web_test");
494 NWebHelper::Instance().GetDataBase();
495 std::shared_ptr<NWebWebStorage> storage = NWebHelper::Instance().GetWebStorage();
496 EXPECT_NE(storage, nullptr);
497 NWebHelper::Instance().SetConnectionTimeout(1);
498 std::vector<std::string> hosts;
499 NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts);
500 NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts);
501 NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList();
502 NWebHelper::Instance().GetDefaultUserAgent();
503 NWebHelper::Instance().PauseAllTimers();
504 NWebHelper::Instance().ResumeAllTimers();
505 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
506 NWebHelper::Instance().LoadWebEngine(true, false);
507 bool result = NWebHelper::Instance().GetWebEngine(true);
508 EXPECT_TRUE(result);
509 result = NWebHelper::Instance().GetWebEngine(true);
510 EXPECT_TRUE(result);
511 cook = NWebHelper::Instance().GetCookieManager();
512 EXPECT_NE(cook, nullptr);
513 manager = NWebHelper::Instance().GetAdsBlockManager();
514 EXPECT_NE(manager, nullptr);
515 NWebHelper::Instance().SetWebTag(1, "webtag");
516
517 NWebHelper::Instance().SetWebTag(1, "webtag");
518 NWebHelper::Instance().AddIntelligentTrackingPreventionBypassingList(hosts);
519 NWebHelper::Instance().RemoveIntelligentTrackingPreventionBypassingList(hosts);
520 NWebHelper::Instance().ClearIntelligentTrackingPreventionBypassingList();
521 NWebHelper::Instance().GetDefaultUserAgent();
522 NWebHelper::Instance().PauseAllTimers();
523 NWebHelper::Instance().ResumeAllTimers();
524 }
525
526 /**
527 * @tc.name: NWebHelper_GetPerfConfig_001
528 * @tc.desc: GetPerfConfig.
529 * @tc.type: FUNC
530 * @tc.require:
531 */
532 HWTEST_F(NwebHelperTest, NWebHelper_GetPerfConfig_001, TestSize.Level1)
533 {
534 EXPECT_TRUE(NWebAdapterHelper::Instance().GetPerfConfig("test").empty());
535 NWebConfigHelper::Instance().ltpoConfig_["test"] = {OHOS::NWeb::FrameRateSetting{0, 0, 0}};
536 EXPECT_FALSE(NWebAdapterHelper::Instance().GetPerfConfig("test").empty());
537 NWebConfigHelper::Instance().ltpoConfig_.clear();
538 }
539
540 /**
541 * @tc.name: NWebHelper_ParseNWebLTPOConfig_001
542 * @tc.desc: ParseNWebLTPOConfig.
543 * @tc.type: FUNC
544 * @tc.require:
545 */
546 HWTEST_F(NwebHelperTest, NWebHelper_ParseNWebLTPOConfig_001, TestSize.Level1)
547 {
548 EXPECT_TRUE(NWebConfigHelper::Instance().ltpoConfig_.empty());
549 std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
550 NWebAdapterHelper::Instance().ParseConfig(initArgs);
551 EXPECT_TRUE(NWebConfigHelper::Instance().ltpoConfig_.empty());
552 }
553
554 /**
555 * @tc.name: NWebHelper_SetHostIP_001
556 * @tc.desc: SetHostIP.
557 * @tc.type: FUNC
558 * @tc.require:
559 */
560 HWTEST_F(NwebHelperTest, NWebHelper_SetHostIP_001, TestSize.Level1)
561 {
562 std::string hostName = "hello";
563 std::string address = "world";
564 int32_t aliveTime = 0;
565
566 NWebHelper::Instance().nwebEngine_ = nullptr;
567 NWebHelper::Instance().SetHostIP(hostName, address, aliveTime);
568 EXPECT_EQ(NWebHelper::Instance().nwebEngine_, nullptr);
569
570 auto nwebEngineMock = std::make_shared<MockNWebEngine>();
571 NWebHelper::Instance().nwebEngine_ = nwebEngineMock;
572 NWebHelper::Instance().SetHostIP(hostName, address, aliveTime);
573 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
574
575 NWebHelper::Instance().nwebEngine_ = nullptr;
576 }
577
578 /**
579 * @tc.name: NWebHelper_ClearHostIP_001
580 * @tc.desc: ClearHostIP.
581 * @tc.type: FUNC
582 * @tc.require:
583 */
584 HWTEST_F(NwebHelperTest, NWebHelper_ClearHostIP_001, TestSize.Level1)
585 {
586 int32_t nweb_id = 1;
587 auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
588 EXPECT_EQ(nwebHelper, nullptr);
589
590 std::string hostName = "name";
591 NWebHelper::Instance().nwebEngine_ = nullptr;
592 NWebHelper::Instance().ClearHostIP(hostName);
593 EXPECT_EQ(NWebHelper::Instance().nwebEngine_, nullptr);
594
595 auto nwebengineMock = std::make_shared<MockNWebEngine>();
596 NWebHelper::Instance().nwebEngine_ = nwebengineMock;
597 NWebHelper::Instance().ClearHostIP(hostName);
598 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
599
600 NWebHelper::Instance().nwebEngine_ = nullptr;
601 }
602
603 /**
604 * @tc.name: NWebHelper_EnableWholeWebPageDrawing_001
605 * @tc.desc: EnableWholeWebPageDrawing.
606 * @tc.type: FUNC
607 * @tc.require:
608 */
609 HWTEST_F(NwebHelperTest, NWebHelper_EnableWholeWebPageDrawing_001, TestSize.Level1)
610 {
611 int32_t nweb_id = 1;
612 auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
613 EXPECT_EQ(nwebHelper, nullptr);
614
615 auto nwebengineMock = std::make_shared<MockNWebEngine>();
616 NWebHelper::Instance().nwebEngine_ = nwebengineMock;
617 NWebHelper::Instance().SetWholeWebDrawing();
618 EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr);
619
620 NWebHelper::Instance().nwebEngine_ = nullptr;
621 }
622
623 /**
624 * @tc.name: NWebHelper_GetAdsBlockManager_001
625 * @tc.desc: GetAdsBlockManager.
626 * @tc.type: FUNC
627 * @tc.require:
628 */
629 HWTEST_F(NwebHelperTest, NWebHelper_GetAdsBlockManager_001, TestSize.Level1)
630 {
631 NWebHelper::Instance().nwebEngine_ = nullptr;
632 std::shared_ptr<NWebCreateInfoImpl> create_info = std::make_shared<NWebCreateInfoImpl>();
633 std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
634 EXPECT_EQ(nweb, nullptr);
635 nweb = NWebHelper::Instance().GetNWeb(1);
636 EXPECT_EQ(nweb, nullptr);
637 auto manager = NWebHelper::Instance().GetAdsBlockManager();
638 EXPECT_NE(manager, nullptr);
639 }
640
641 /**
642 * @tc.name: NWebHelper_GetWebEngine_001
643 * @tc.desc: GetWebEngine.
644 * @tc.type: FUNC
645 * @tc.require:
646 */
647 HWTEST_F(NwebHelperTest, NWebHelper_GetWebEngine_001, TestSize.Level1)
648 {
649 NWebHelper::Instance().nwebEngine_ = nullptr;
650 NWebHelper::Instance().bundlePath_ = "";
651 g_applicationContext.reset();
652 bool result = NWebHelper::Instance().GetWebEngine(true);
653 EXPECT_FALSE(result);
654 ApplicationContextMock *contextMock = new ApplicationContextMock();
655 ASSERT_NE(contextMock, nullptr);
656 g_applicationContext.reset(contextMock);
657 result = NWebHelper::Instance().GetWebEngine(true);
658 EXPECT_FALSE(result);
659 }
660
661 } // namespace OHOS::NWeb
662 }
663