• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <securec.h>
19 #include <ui/rs_surface_node.h>
20 #include <unordered_map>
21 
22 #define private public
23 #include "nweb.h"
24 #include "nweb_helper.h"
25 #include "nweb_adapter_helper.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::Rosen;
31 
32 namespace OHOS::NWeb {
33 namespace {
34 sptr<Surface> g_surface = nullptr;
35 const bool RESULT_OK = true;
36 const int DEFAULT_WIDTH = 2560;
37 const int DEFAULT_HEIGHT = 1396;
38 const int32_t NWEB_MAX_WIDTH = 7681;
39 const std::string ARG_URL = "--url";
40 const std::string ARG_DUMP = "--dump-path";
41 const std::string ARG_FRAME_INFO = "--frame-info";
42 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
43 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
44 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
45 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
46 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
47 const std::string ARG_WIDTH = "--width";
48 const std::string ARG_HEIGHT = "--height";
49 } // namespace
50 
51 class NwebHelperTest : public testing::Test {
52 public:
53     static void SetUpTestCase(void);
54     static void TearDownTestCase(void);
55     void SetUp();
56     void TearDown();
57 };
58 
SetUpTestCase(void)59 void NwebHelperTest::SetUpTestCase(void)
60 {
61     RSSurfaceNodeConfig config;
62     config.SurfaceNodeName = "webTestSurfaceName";
63     auto surfaceNode = RSSurfaceNode::Create(config, false);
64     EXPECT_NE(surfaceNode, nullptr);
65     g_surface = surfaceNode->GetSurface();
66     EXPECT_NE(g_surface, nullptr);
67 }
68 
TearDownTestCase(void)69 void NwebHelperTest::TearDownTestCase(void)
70 {}
71 
SetUp(void)72 void NwebHelperTest::SetUp(void)
73 {}
74 
TearDown(void)75 void NwebHelperTest::TearDown(void)
76 {}
77 
78 std::unordered_map<std::string, std::string> g_argsMap;
79 
HasArg(const std::string & arg)80 bool HasArg(const std::string& arg)
81 {
82     return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
83 }
84 
GetArgValue(const std::string & arg)85 std::string GetArgValue(const std::string& arg)
86 {
87     if (!HasArg(arg)) {
88         return "";
89     }
90     return g_argsMap.at(arg);
91 }
92 
GetWebEngineArgs(const std::string & arg)93 std::list<std::string> GetWebEngineArgs(const std::string& arg)
94 {
95     std::string webEngineArgValue = GetArgValue(arg);
96     std::list<std::string> webEngineArgList;
97     if (webEngineArgValue.empty()) {
98         return webEngineArgList;
99     }
100     uint32_t start = 0;
101     uint32_t pos = 0;
102     while (pos < webEngineArgValue.size()) {
103         if (webEngineArgValue[pos] == ',') {
104             webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
105             pos++;
106             start = pos;
107         } else {
108             pos++;
109         }
110     }
111     webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
112     webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
113     return webEngineArgList;
114 }
115 
GetInitArgs()116 NWebInitArgs GetInitArgs()
117 {
118     NWebInitArgs initArgs = {
119         .dump_path = GetArgValue(ARG_DUMP),
120         .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
121         .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
122         .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
123         .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
124     };
125     return initArgs;
126 }
127 
128 /**
129  * @tc.name: NWebHelper_SetBundlePath_001
130  * @tc.desc: SetBundlePath.
131  * @tc.type: FUNC
132  * @tc.require: AR000GGHJ8
133  */
134 HWTEST_F(NwebHelperTest, NWebHelper_SetBundlePath_001, TestSize.Level1)
135 {
136     int32_t nweb_id = 1;
137     NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
138     bool result = NWebAdapterHelper::Instance().Init(false);
139     EXPECT_EQ(RESULT_OK, result);
140     NWebCreateInfo create_info;
141     std::shared_ptr<NWeb> nweb = NWebHelper::Instance().CreateNWeb(create_info);
142     EXPECT_NE(nweb, nullptr);
143     NWebDOHConfig config;
144     NWebHelper::Instance().SetHttpDns(config);
145     auto nwebHelper = NWebHelper::Instance().GetNWeb(nweb_id);
146     EXPECT_EQ(nwebHelper.lock(), nullptr);
147     NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
148     result = NWebHelper::Instance().InitAndRun(false);
149     EXPECT_FALSE(result);
150 }
151 
152 /**
153  * @tc.name: NWebHelper_GetWebStorage_002
154  * @tc.desc: GetWebStorage.
155  * @tc.type: FUNC
156  * @tc.require: AR000GGHJ8
157  */
158 HWTEST_F(NwebHelperTest, NWebHelper_GetWebStorage_002, TestSize.Level1)
159 {
160     auto web_storage = NWebHelper::Instance().GetWebStorage();
161     bool result = false;
162     if (web_storage != nullptr) {
163         result = true;
164     }
165     EXPECT_EQ(RESULT_OK, result);
166 }
167 
168 /**
169  * @tc.name: NWebHelper_GetDataBase_003
170  * @tc.desc: GetDataBase.
171  * @tc.type: FUNC
172  * @tc.require:issueI5OESN
173  */
174 HWTEST_F(NwebHelperTest, NWebHelper_GetDataBase_003, TestSize.Level1)
175 {
176     auto dataBase = NWebHelper::Instance().GetDataBase();
177     bool result = false;
178     if (dataBase != nullptr) {
179         result = true;
180     }
181     EXPECT_EQ(RESULT_OK, result);
182 
183     NWebHelper::Instance().libHandleWebEngine_ = nullptr;
184 
185     void *enhanceSurfaceInfo = nullptr;
186     int32_t temp = 1;
187     std::shared_ptr<NWeb> nweb =
188         NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
189         DEFAULT_WIDTH, DEFAULT_HEIGHT);
190     EXPECT_EQ(nweb, nullptr);
191     enhanceSurfaceInfo = static_cast<void *>(&temp);
192     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
193                                                     DEFAULT_WIDTH, DEFAULT_HEIGHT);
194     EXPECT_EQ(nweb, nullptr);
195     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
196                                                     DEFAULT_WIDTH, NWEB_MAX_WIDTH);
197     EXPECT_EQ(nweb, nullptr);
198     nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
199                                                     NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
200     EXPECT_EQ(nweb, nullptr);
201 }
202 
203 /**
204  * @tc.name: NWebHelper_TryPreReadLib_004
205  * @tc.desc: TryPreReadLib.
206  * @tc.type: FUNC
207  * @tc.require: AR000GGHJ8
208  */
209 HWTEST_F(NwebHelperTest, NWebHelper_TryPreReadLib_004, TestSize.Level1)
210 {
211     NWebHelper::Instance().TryPreReadLib(false, MOCK_INSTALLATION_DIR);
212     NWebHelper::Instance().TryPreReadLib(true, MOCK_INSTALLATION_DIR);
213     bool result = NWebHelper::Instance().Init(false);
214     EXPECT_TRUE(result);
215     sptr<Surface> surface = nullptr;
216     std::shared_ptr<NWeb> nweb =
217         NWebAdapterHelper::Instance().CreateNWeb(surface, GetInitArgs(),
218         DEFAULT_WIDTH, DEFAULT_HEIGHT);
219     EXPECT_EQ(nweb, nullptr);
220     nweb = NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
221                                                     DEFAULT_WIDTH, NWEB_MAX_WIDTH);
222     EXPECT_EQ(nweb, nullptr);
223     nweb = NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs(),
224                                                     NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
225     EXPECT_EQ(nweb, nullptr);
226 }
227 
228 /**
229  * @tc.name: NWebHelper_GetConfigPath_005
230  * @tc.desc: GetConfigPath.
231  * @tc.type: FUNC
232  * @tc.require: AR000GGHJ8
233  */
234 HWTEST_F(NwebHelperTest, NWebHelper_GetConfigPath_005, TestSize.Level1)
235 {
236     std::string configFileName = "test";
237     std::string figPath = NWebAdapterHelper::Instance().GetConfigPath(configFileName);
238     EXPECT_FALSE(figPath.empty());
239     NWebInitArgs initArgs;
240     NWebAdapterHelper::Instance().ParseConfig(initArgs);
241     NWebHelper::Instance().libHandleWebEngine_ = nullptr;
242     NWebHelper::Instance().PrepareForPageLoad("web_test", true, 0);
243     bool result = NWebHelper::Instance().InitAndRun(false);
244     EXPECT_FALSE(result);
245 }
246 } // namespace OHOS::NWeb
247