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 #include "window.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS;
31
32 namespace OHOS::NWeb {
33 namespace {
34 const bool RESULT_OK = true;
35 const int DEFAULT_WIDTH = 2560;
36 const int DEFAULT_HEIGHT = 1396;
37 const int32_t NWEB_MAX_WIDTH = 7681;
38 const std::string ARG_URL = "--url";
39 const std::string ARG_DUMP = "--dump-path";
40 const std::string ARG_FRAME_INFO = "--frame-info";
41 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
42 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
43 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
44 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
45 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
46 const std::string ARG_WIDTH = "--width";
47 const std::string ARG_HEIGHT = "--height";
48 } // namespace
49
50 class NwebHelperTest : public testing::Test {
51 public:
52 static void SetUpTestCase(void);
53 static void TearDownTestCase(void);
54 void SetUp();
55 void TearDown();
56 };
57
SetUpTestCase(void)58 void NwebHelperTest::SetUpTestCase(void)
59 {}
60
TearDownTestCase(void)61 void NwebHelperTest::TearDownTestCase(void)
62 {}
63
SetUp(void)64 void NwebHelperTest::SetUp(void)
65 {}
66
TearDown(void)67 void NwebHelperTest::TearDown(void)
68 {}
69
70 std::unordered_map<std::string, std::string> g_argsMap;
71
HasArg(const std::string & arg)72 bool HasArg(const std::string& arg)
73 {
74 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
75 }
76
GetArgValue(const std::string & arg)77 std::string GetArgValue(const std::string& arg)
78 {
79 if (!HasArg(arg)) {
80 return "";
81 }
82 return g_argsMap.at(arg);
83 }
84
GetNumFromArgs(const std::string & arg)85 uint32_t GetNumFromArgs(const std::string& arg)
86 {
87 if (!HasArg(arg)) {
88 return 0;
89 }
90 return std::stoi(GetArgValue(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
CreateWindow()128 sptr<OHOS::Rosen::Window> CreateWindow()
129 {
130 sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
131 int width = HasArg(ARG_WIDTH) ? GetNumFromArgs(ARG_WIDTH) : DEFAULT_WIDTH;
132 int height = HasArg(ARG_HEIGHT) ? GetNumFromArgs(ARG_HEIGHT) : DEFAULT_HEIGHT;
133 OHOS::Rosen::Rect windowRect = { 0, 0, width, height };
134 option->SetWindowRect(windowRect);
135 auto window = OHOS::Rosen::Window::Create("nweb_test_window", option);
136 return window;
137 }
138
139 /**
140 * @tc.name: NWebHelper_SetBundlePath_001
141 * @tc.desc: SetBundlePath.
142 * @tc.type: FUNC
143 * @tc.require: AR000GGHJ8
144 */
145 HWTEST_F(NwebHelperTest, NWebHelper_SetBundlePath_001, TestSize.Level1)
146 {
147 NWebHelper::Instance().SetBundlePath(MOCK_INSTALLATION_DIR);
148 bool result = NWebAdapterHelper::Instance().Init(false);
149 EXPECT_EQ(RESULT_OK, 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 sptr<OHOS::Rosen::Window> window = CreateWindow();
184 NWebHelper::Instance().libHandleWebEngine_ = nullptr;
185 std::shared_ptr<NWeb> nweb =
186 NWebAdapterHelper::Instance().CreateNWeb(window.GetRefPtr(), GetInitArgs());
187 EXPECT_EQ(nweb, nullptr);
188
189 void *enhanceSurfaceInfo = nullptr;
190 int32_t temp = 1;
191 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
192 DEFAULT_WIDTH, DEFAULT_HEIGHT);
193 EXPECT_EQ(nweb, nullptr);
194 enhanceSurfaceInfo = static_cast<void *>(&temp);
195 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
196 DEFAULT_WIDTH, DEFAULT_HEIGHT);
197 EXPECT_EQ(nweb, nullptr);
198 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
199 DEFAULT_WIDTH, NWEB_MAX_WIDTH);
200 EXPECT_EQ(nweb, nullptr);
201 nweb = NWebAdapterHelper::Instance().CreateNWeb(enhanceSurfaceInfo, GetInitArgs(),
202 NWEB_MAX_WIDTH, DEFAULT_HEIGHT);
203 EXPECT_EQ(nweb, nullptr);
204 }
205
206 /**
207 * @tc.name: NWebHelper_TryPreReadLib_004
208 * @tc.desc: TryPreReadLib.
209 * @tc.type: FUNC
210 * @tc.require: AR000GGHJ8
211 */
212 HWTEST_F(NwebHelperTest, NWebHelper_TryPreReadLib_004, TestSize.Level1)
213 {
214 NWebHelper::Instance().TryPreReadLib(false, MOCK_INSTALLATION_DIR);
215 NWebHelper::Instance().TryPreReadLib(true, MOCK_INSTALLATION_DIR);
216 bool result = NWebHelper::Instance().Init(false);
217 EXPECT_TRUE(result);
218 }
219 } // namespace OHOS::NWeb
220