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