1 /* 2 * Copyright (c) 2024 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 <unordered_map> 20 21 #include "nweb_config_helper.h" 22 #include "nweb_init_params.h" 23 24 using namespace testing; 25 26 namespace OHOS { 27 namespace NWebConfig { 28 29 class NWebConfigHelperTest : public ::testing::Test { 30 public: 31 static void SetUpTestCase(void); 32 static void TearDownTestCase(void); 33 void TearDown(); 34 35 protected: 36 std::shared_ptr<NWebEngineInitArgsImpl> initArgs; 37 NWebConfigHelper nWebConfigHelper; 38 xmlNodePtr root_element; SetUp()39 void SetUp() override 40 { 41 initArgs = std::make_shared<NWebEngineInitArgsImpl>(); 42 root_element = xmlNewNode(NULL, BAD_CAST "root"); 43 } 44 }; 45 46 /** 47 * @tc.name: ParseWebConfigXml_FileNotFound 48 * @tc.desc: ParseWebConfigXml. 49 * @tc.type: FUNC 50 * @tc.require: 51 */ 52 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_FileNotFound, TestSize.Level0) 53 { 54 std::string configFilePath = "nonexistent.xml"; 55 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 56 } 57 58 /** 59 * @tc.name: ParseWebConfigXml_InvalidRootElement 60 * @tc.desc: ParseWebConfigXml. 61 * @tc.type: FUNC 62 * @tc.require: 63 */ 64 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_InvalidRootElement, TestSize.Level0) 65 { 66 std::string configFilePath = "invalid_root.xml"; 67 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 68 } 69 70 /** 71 * @tc.name: ParseWebConfigXml_ValidInitConfig 72 * @tc.desc: ParseWebConfigXml. 73 * @tc.type: FUNC 74 * @tc.require: 75 */ 76 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_ValidInitConfig, TestSize.Level0) 77 { 78 std::string configFilePath = "valid_init.xml"; 79 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 80 } 81 82 /** 83 * @tc.name: ParseWebConfigXml_ValidDeleteConfig 84 * @tc.desc: ParseWebConfigXml. 85 * @tc.type: FUNC 86 * @tc.require: 87 */ 88 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_ValidDeleteConfig, TestSize.Level0) 89 { 90 std::string configFilePath = "valid_delete.xml"; 91 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 92 } 93 94 /** 95 * @tc.name: ParseWebConfigXml_ValidPerformanceConfig 96 * @tc.desc:ParseWebConfigXml. 97 * @tc.type: FUNC 98 * @tc.require: 99 */ 100 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_ValidPerformanceConfig, TestSize.Level0) 101 { 102 std::string configFilePath = "valid_performance.xml"; 103 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 104 } 105 106 /** 107 * @tc.name: ParseWebConfigXml_ValidLTPOConfig 108 * @tc.desc: ParseWebConfigXml. 109 * @tc.type: FUNC 110 * @tc.require: 111 */ 112 HWTEST_F(NWebConfigHelperTest, ParseWebConfigXml_ValidLTPOConfig, TestSize.Level0) 113 { 114 std::string configFilePath = "valid_ltpo.xml"; 115 EXPECT_NO_THROW(NWebConfigHelper::Instance().ParseWebConfigXml(configFilePath, initArgs)); 116 } 117 118 /** 119 * @tc.name: GetPerfConfig_ShouldReturnEmptyVector_WhenSettingNameNotExist 120 * @tc.desc: GetPerfConfig. 121 * @tc.type: FUNC 122 * @tc.require: 123 */ 124 HWTEST_F(NWebConfigHelperTest, GetPerfConfig_ShouldReturnEmptyVector_WhenSettingNameNotExist, TestSize.Level0) 125 { 126 std::string settingName = "NonExistentSetting"; 127 std::vector<FrameRateSetting> result = NWebConfigHelper::Instance().GetPerfConfig(settingName); 128 EXPECT_TRUE(result.empty()); 129 } 130 131 /** 132 * @tc.name: GetPerfConfig_ShouldReturnNonEmptyVector_WhenSettingNameExist 133 * @tc.desc: GetPerfConfig. 134 * @tc.type: FUNC 135 * @tc.require: 136 */ 137 HWTEST_F(NWebConfigHelperTest, GetPerfConfig_ShouldReturnNonEmptyVector_WhenSettingNameExist, TestSize.Level0) 138 { 139 std::string settingName = "ExistentSetting"; 140 NWebConfigHelper::Instance().ltpoConfig_[settingName] = {FrameRateSetting(30)}; 141 std::vector<FrameRateSetting> result = NWebConfigHelper::Instance().GetPerfConfig(settingName); 142 EXPECT_FALSE(result.empty()); 143 EXPECT_EQ(result[0].frameRate, 30); 144 } 145 146 /** 147 * @tc.name: ParsePerfConfig_ShouldReturnEmptyString_WhenConfigNotFound 148 * @tc.desc: ParsePerfConfig. 149 * @tc.type: FUNC 150 * @tc.require: 151 */ 152 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_ShouldReturnEmptyString_WhenConfigNotFound, TestSize.Level0) 153 { 154 std::string configNodeName = "non_existent_config"; 155 std::string argsNodeName = "non_existent_args"; 156 std::string result = NWebConfigHelper::Instance().ParsePerfConfig(configNodeName, argsNodeName); 157 EXPECT_EQ(result, ""); 158 } 159 160 /** 161 * @tc.name: ParsePerfConfig_ShouldReturnValue_WhenConfigFound 162 * @tc.desc: ParsePerfConfig. 163 * @tc.type: FUNC 164 * @tc.require: 165 */ 166 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_ShouldReturnValue_WhenConfigFound, TestSize.Level0) 167 { 168 std::string configNodeName = "existent_config"; 169 std::string argsNodeName = "existent_args"; 170 std::string expectedValue = "expected_value"; 171 NWebConfigHelper::Instance().perfConfig_[configNodeName + "/" + argsNodeName] = expectedValue; 172 std::string result = NWebConfigHelper::Instance().ParsePerfConfig(configNodeName, argsNodeName); 173 EXPECT_EQ(result, expectedValue); 174 } 175 176 /** 177 * @tc.name: ParsePerfConfig_NullNode 178 * @tc.desc: ParsePerfConfig. 179 * @tc.type: FUNC 180 * @tc.require: 181 */ 182 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_NullNode, TestSize.Level0) 183 { 184 xmlNodePtr node = nullptr; 185 NWebConfigHelper::Instance().ParsePerfConfig(node); 186 EXPECT_TRUE(NWebConfigHelper::Instance().perfConfig_.empty()); 187 } 188 189 /** 190 * @tc.name: ParsePerfConfig_CommentNode 191 * @tc.desc: ParsePerfConfig. 192 * @tc.type: FUNC 193 * @tc.require: 194 */ 195 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_CommentNode, TestSize.Level0) 196 { 197 xmlNodePtr node = xmlNewNode(nullptr, "comment"); 198 NWebConfigHelper::Instance().ParsePerfConfig(node); 199 EXPECT_TRUE(NWebConfigHelper::Instance().perfConfig_.empty()); 200 xmlFreeNode(node); 201 } 202 203 /** 204 * @tc.name: ParsePerfConfig_ValidNode 205 * @tc.desc: ParsePerfConfig. 206 * @tc.type: FUNC 207 * @tc.require: 208 */ 209 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_ValidNode, TestSize.Level0) 210 { 211 xmlNodePtr node = xmlNewNode(nullptr, "node"); 212 xmlNodePtr childNode = xmlNewNode(nullptr, "childNode"); 213 EXPECT_NE(node, nullptr); 214 EXPECT_NE(childNode, nullptr); 215 xmlNodeAddContent(childNode, "value"); 216 xmlAddChild(node, childNode); 217 NWebConfigHelper::Instance().ParsePerfConfig(node); 218 EXPECT_EQ(hNWebConfigHelper::Instance().perfConfig_["node/childNode"], "value"); 219 xmlFreeNode(childNode); 220 xmlFreeNode(node); 221 } 222 223 /** 224 * @tc.name: ParsePerfConfig_InvalidChildNode 225 * @tc.desc: ParsePerfConfig. 226 * @tc.type: FUNC 227 * @tc.require: 228 */ 229 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_InvalidChildNode, TestSize.Level0) 230 { 231 xmlNodePtr node = xmlNewNode(nullptr, "node"); 232 xmlNodePtr childNode = xmlNewNode(nullptr, "comment"); 233 EXPECT_NE(node, nullptr); 234 EXPECT_NE(childNode, nullptr); 235 xmlAddChild(node, childNode); 236 NWebConfigHelper::Instance().ParsePerfConfig(node); 237 EXPECT_TRUE(NWebConfigHelper::Instance().perfConfig_.empty()); 238 xmlFreeNode(childNode); 239 xmlFreeNode(node); 240 } 241 242 /** 243 * @tc.name: ParsePerfConfig_NullContent 244 * @tc.desc: ParsePerfConfig. 245 * @tc.type: FUNC 246 * @tc.require: 247 */ 248 HWTEST_F(NWebConfigHelperTest, ParsePerfConfig_NullContent, TestSize.Level0) 249 { 250 xmlNodePtr node = xmlNewNode(nullptr, "node"); 251 xmlNodePtr childNode = xmlNewNode(nullptr, "childNode"); 252 EXPECT_NE(node, nullptr); 253 EXPECT_NE(childNode, nullptr); 254 xmlAddChild(node, childNode); 255 NWebConfigHelper::Instance().ParsePerfConfig(node); 256 EXPECT_TRUE(NWebConfigHelper::Instance().perfConfig_.empty()); 257 xmlFreeNode(childNode); 258 xmlFreeNode(node); 259 } 260 261 /** 262 * @tc.name: ParseDeleteConfig_NullRootPtr 263 * @tc.desc: ParseDeleteConfig. 264 * @tc.type: FUNC 265 * @tc.require: 266 */ 267 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_NullRootPtr, TestSize.Level0) 268 { 269 xmlNodePtr rootPtr = nullptr; 270 std::string result = NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 271 EXPECT_EQ(result, ""); 272 } 273 274 /** 275 * @tc.name: ParseDeleteConfig_ValidNode 276 * @tc.desc: ParseDeleteConfig. 277 * @tc.type: FUNC 278 * @tc.require: 279 */ 280 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_ValidNode, TestSize.Level0) 281 { 282 xmlNodePtr rootPtr = xmlNewNode(nullptr, "valid"); 283 xmlNodePtr childNodePtr = xmlNewNode(nullptr, "child"); 284 EXPECT_NE(rootPtr, nullptr); 285 EXPECT_NE(childNodePtr, nullptr); 286 xmlAddChild(rootPtr, childNodePtr); 287 xmlChar *content = xmlNodeGetContent(childNodePtr); 288 xmlNodeSetContent(childNodePtr, content); 289 xmlFree(content); 290 NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 291 } 292 293 /** 294 * @tc.name: ParseDeleteConfig_InvalidChildNode 295 * @tc.desc: ParseDeleteConfig. 296 * @tc.type: FUNC 297 * @tc.require: 298 */ 299 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_InvalidChildNode, TestSize.Level0) 300 { 301 xmlNodePtr rootPtr = xmlNewNode(nullptr, "valid"); 302 xmlNodePtr childNodePtr = xmlNewNode(nullptr, "invalid"); 303 EXPECT_NE(rootPtr, nullptr); 304 EXPECT_NE(childNodePtr, nullptr); 305 xmlAddChild(rootPtr, childNodePtr); 306 NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 307 } 308 309 /** 310 * @tc.name: ParseDeleteConfig_NullContent 311 * @tc.desc: ParseDeleteConfig. 312 * @tc.type: FUNC 313 * @tc.require: 314 */ 315 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_NullContent, TestSize.Level0) 316 { 317 xmlNodePtr rootPtr = xmlNewNode(nullptr, "valid"); 318 xmlNodePtr childNodePtr = xmlNewNode(nullptr, "child"); 319 EXPECT_NE(rootPtr, nullptr); 320 EXPECT_NE(childNodePtr, nullptr); 321 xmlAddChild(rootPtr, childNodePtr); 322 NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 323 } 324 325 /** 326 * @tc.name: ParseDeleteConfig_NotFoundConfig 327 * @tc.desc: ParseDeleteConfig. 328 * @tc.type: FUNC 329 * @tc.require: 330 */ 331 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_NotFoundConfig, TestSize.Level0) 332 { 333 xmlNodePtr rootPtr = xmlNewNode(nullptr, "valid"); 334 xmlNodePtr childNodePtr = xmlNewNode(nullptr, "child"); 335 EXPECT_NE(rootPtr, nullptr); 336 EXPECT_NE(childNodePtr, nullptr); 337 xmlAddChild(rootPtr, childNodePtr); 338 xmlChar *content = xmlNodeGetContent(childNodePtr); 339 xmlNodeSetContent(childNodePtr, content); 340 xmlFree(content); 341 NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 342 } 343 344 /** 345 * @tc.name: ParseDeleteConfig_EmptyParam 346 * @tc.desc: ParseDeleteConfig. 347 * @tc.type: FUNC 348 * @tc.require: 349 */ 350 HWTEST_F(NWebConfigHelperTest, ParseDeleteConfig_EmptyParam, TestSize.Level0) 351 { 352 xmlNodePtr rootPtr = xmlNewNode(nullptr, "valid"); 353 xmlNodePtr childNodePtr = xmlNewNode(nullptr, "child"); 354 EXPECT_NE(rootPtr, nullptr); 355 EXPECT_NE(childNodePtr, nullptr); 356 xmlAddChild(rootPtr, childNodePtr); 357 xmlChar *content = xmlNodeGetContent(childNodePtr); 358 xmlNodeSetContent(childNodePtr, content); 359 xmlFree(content); 360 NWebConfigHelper::Instance().ParseDeleteConfig(rootPtr, initArgs); 361 } 362 363 /** 364 * @tc.name : safeGetPropAsInt_ShouldReturnDefaultValue_WhenPropNotExist 365 * @tc.number: OHOS_NWEB_001 366 * @tc.desc : Test safeGetPropAsInt function when the property does not exist. 367 */ 368 HWTEST_F(NWebConfigHelperTest, safeGetPropAsInt_ShouldReturnDefaultValue_WhenPropNotExist, 369 testing::ext::TestSize.Level0) 370 { 371 int defaultValue = 10; 372 int result = NWebConfigHelper::Instance().safeGetPropAsInt(root_element, 373 BAD_CAST "non_existent_prop", defaultValue); 374 EXPECT_EQ(result, defaultValue); 375 } 376 377 /** 378 * @tc.name : safeGetPropAsInt_ShouldReturnPropValue_WhenPropExist 379 * @tc.number: OHOS_NWEB_002 380 * @tc.desc : Test safeGetPropAsInt function when the property exists. 381 */ 382 HWTEST_F(NWebConfigHelperTest, safeGetPropAsInt_ShouldReturnPropValue_WhenPropExist, testing::ext::TestSize.Level0) 383 { 384 xmlNewProp(root_element, BAD_CAST "test_prop", BAD_CAST "20"); 385 int result = NWebConfigHelper::Instance().safeGetPropAsInt(root_element, BAD_CAST "test_prop", 10); 386 EXPECT_EQ(result, 20); 387 } 388 389 /** 390 * @tc.name : safeGetPropAsInt_ShouldReturnDefaultValue_WhenPropValueNotInt 391 * @tc.number: OHOS_NWEB_003 392 * @tc.desc : Test safeGetPropAsInt function when the property value is not an integer. 393 */ 394 HWTEST_F(NWebConfigHelperTest, safeGetPropAsInt_ShouldReturnDefaultValue_WhenPropValueNotInt, 395 testing::ext::TestSize.Level0) 396 { 397 xmlNewProp(root_element, BAD_CAST "test_prop", BAD_CAST "not_an_integer"); 398 int defaultValue = 10; 399 int result = NWebConfigHelper::Instance().safeGetPropAsInt(root_element, BAD_CAST "test_prop", defaultValue); 400 EXPECT_EQ(result, defaultValue); 401 } 402 403 } // NWebConfig 404 } // OHOS```