1 /* 2 * Copyright (c) 2025 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 "gtest/gtest.h" 17 18 #define private public 19 #define protected public 20 #include "test/mock/core/common/mock_container.h" 21 #include "test/mock/core/pipeline/mock_pipeline_context.h" 22 #include "ui/resource/resource_parser.h" 23 24 #include "core/common/resource/resource_manager.h" 25 #include "core/common/resource/resource_wrapper.h" 26 27 using namespace testing; 28 using namespace testing::ext; 29 using namespace OHOS::Ace::Kit; 30 namespace OHOS::Ace { 31 class ResourceParserTest : public testing::Test { 32 public: SetUpTestSuite()33 static void SetUpTestSuite() 34 { 35 NG::MockPipelineContext::SetUp(); 36 MockContainer::SetUp(); 37 MockContainer::Current()->pipelineContext_ = PipelineBase::GetCurrentContext(); 38 } TearDownTestSuite()39 static void TearDownTestSuite() 40 { 41 MockContainer::Current()->pipelineContext_ = nullptr; 42 NG::MockPipelineContext::TearDown(); 43 } SetUp()44 void SetUp() {}; TearDown()45 void TearDown() {}; 46 }; 47 48 /** 49 * @tc.name: ResourceParserTestTest001 50 * @tc.desc: Test GetDimension when option is empty 51 * @tc.type: Func 52 */ 53 HWTEST_F(ResourceParserTest, ResourceParserTestTest001, TestSize.Level1) 54 { 55 Kit::ResourceInfo info; 56 info.bundleName = ""; 57 info.moduleName = ""; 58 Ace::CalcDimension dimension; 59 auto result = ResourceParser::GetDimension(info, dimension); 60 EXPECT_FALSE(result); 61 } 62 63 /** 64 * @tc.name: ResourceParserTestTest002 65 * @tc.desc: Test GetDimension when resId is unknown 66 * @tc.type: Func 67 */ 68 HWTEST_F(ResourceParserTest, ResourceParserTestTest002, TestSize.Level1) 69 { 70 Kit::ResourceInfo info; 71 info.resId = UNKNOWN_RESOURCE_ID; 72 info.params.push_back("param"); 73 Ace::CalcDimension dimension; 74 auto result = ResourceParser::GetDimension(info, dimension); 75 EXPECT_FALSE(result); 76 } 77 78 /** 79 * @tc.name: ResourceParserTestTest003 80 * @tc.desc: Test GetDimension when resId is known 81 * @tc.type: Func 82 */ 83 HWTEST_F(ResourceParserTest, ResourceParserTestTest003, TestSize.Level1) 84 { 85 Kit::ResourceInfo info; 86 info.resId = 123; 87 Ace::CalcDimension dimension; 88 auto result = ResourceParser::GetDimension(info, dimension); 89 EXPECT_FALSE(result); 90 } 91 92 /** 93 * @tc.name: ResourceParserTestTest004 94 * @tc.desc: Test GetColor when option is empty 95 * @tc.type: Func 96 */ 97 HWTEST_F(ResourceParserTest, ResourceParserTestTest004, TestSize.Level1) 98 { 99 Kit::ResourceInfo info; 100 info.bundleName = ""; 101 info.moduleName = "module"; 102 Ace::Color color; 103 auto result = ResourceParser::GetColor(info, color); 104 EXPECT_TRUE(result); 105 } 106 107 /** 108 * @tc.name: ResourceParserTestTest005 109 * @tc.desc: Test GetColor when resource ID is unknown 110 * @tc.type: Func 111 */ 112 HWTEST_F(ResourceParserTest, ResourceParserTestTest005, TestSize.Level1) 113 { 114 Kit::ResourceInfo info; 115 info.resId = UNKNOWN_RESOURCE_ID; 116 info.params.push_back("param"); 117 Ace::Color color; 118 auto result = ResourceParser::GetColor(info, color); 119 EXPECT_FALSE(result); 120 } 121 122 /** 123 * @tc.name: ResourceParserTestTest006 124 * @tc.desc: Test GetColor when resource ID is invalid 125 * @tc.type: Func 126 */ 127 HWTEST_F(ResourceParserTest, ResourceParserTestTest006, TestSize.Level1) 128 { 129 Kit::ResourceInfo info; 130 info.resId = 123; 131 Ace::Color color; 132 auto result = ResourceParser::GetColor(info, color); 133 EXPECT_FALSE(result); 134 } 135 136 /** 137 * @tc.name: ResourceParserTestTest007 138 * @tc.desc: Test GetString when option is empty 139 * @tc.type: Func 140 */ 141 HWTEST_F(ResourceParserTest, ResourceParserTestTest007, TestSize.Level1) 142 { 143 Kit::ResourceInfo info; 144 info.resId = UNKNOWN_RESOURCE_ID; 145 std::string str; 146 auto result = ResourceParser::GetString(info, str); 147 EXPECT_FALSE(result); 148 } 149 150 /** 151 * @tc.name: ResourceParserTestTest008 152 * @tc.desc: Test GetString when resource ID is unknown 153 * @tc.type: Func 154 */ 155 HWTEST_F(ResourceParserTest, ResourceParserTestTest008, TestSize.Level1) 156 { 157 Kit::ResourceInfo info; 158 info.resId = UNKNOWN_RESOURCE_ID; 159 info.params.push_back("param"); 160 std::string str; 161 auto result = ResourceParser::GetString(info, str); 162 EXPECT_FALSE(result); 163 } 164 165 /** 166 * @tc.name: ResourceParserTestTest009 167 * @tc.desc: Test GetString when resource ID is known 168 * @tc.type: Func 169 */ 170 HWTEST_F(ResourceParserTest, ResourceParserTestTest009, TestSize.Level1) 171 { 172 Kit::ResourceInfo info; 173 info.resId = 123; 174 std::string str; 175 auto result = ResourceParser::GetString(info, str); 176 EXPECT_FALSE(result); 177 } 178 179 /** 180 * @tc.name: ResourceParserTestTest010 181 * @tc.desc: Test GetMediaPath when different states of resource 182 * @tc.type: Func 183 */ 184 HWTEST_F(ResourceParserTest, ResourceParserTestTest010, TestSize.Level1) 185 { 186 Kit::ResourceInfo info; 187 std::string mediaPath; 188 auto result = ResourceParser::GetMediaPath(info, mediaPath); 189 EXPECT_FALSE(result); 190 info.bundleName = "bundle"; 191 info.moduleName = "module"; 192 result = ResourceParser::GetMediaPath(info, mediaPath); 193 EXPECT_TRUE(result); 194 info.resId = UNKNOWN_RESOURCE_ID; 195 info.params.push_back("param"); 196 result = ResourceParser::GetMediaPath(info, mediaPath); 197 EXPECT_TRUE(result); 198 } 199 200 /** 201 * @tc.name: ResourceParserTestTest011 202 * @tc.desc: Test GetInt when different states of resource 203 * @tc.type: Func 204 */ 205 HWTEST_F(ResourceParserTest, ResourceParserTestTest011, TestSize.Level1) 206 { 207 Kit::ResourceInfo info; 208 int32_t value; 209 auto result = ResourceParser::GetInt(info, value); 210 EXPECT_FALSE(result); 211 info.bundleName = "bundle"; 212 info.moduleName = "module"; 213 result = ResourceParser::GetInt(info, value); 214 EXPECT_TRUE(result); 215 info.resId = UNKNOWN_RESOURCE_ID; 216 info.params.push_back("param"); 217 result = ResourceParser::GetInt(info, value); 218 EXPECT_TRUE(result); 219 } 220 221 /** 222 * @tc.name: ResourceParserTestTest012 223 * @tc.desc: Test GetDouble when different states of resource 224 * @tc.type: Func 225 */ 226 HWTEST_F(ResourceParserTest, ResourceParserTestTest012, TestSize.Level1) 227 { 228 Kit::ResourceInfo info; 229 double value; 230 auto result = ResourceParser::GetDouble(info, value); 231 EXPECT_FALSE(result); 232 info.bundleName = "bundle"; 233 info.moduleName = "module"; 234 result = ResourceParser::GetDouble(info, value); 235 EXPECT_TRUE(result); 236 info.resId = UNKNOWN_RESOURCE_ID; 237 info.params.push_back("param"); 238 result = ResourceParser::GetDouble(info, value); 239 EXPECT_TRUE(result); 240 } 241 242 /** 243 * @tc.name: ResourceParserTestTest013 244 * @tc.desc: Test GetPluralString when different states of resource 245 * @tc.type: Func 246 */ 247 HWTEST_F(ResourceParserTest, ResourceParserTestTest013, TestSize.Level1) 248 { 249 Kit::ResourceInfo info; 250 int value = 0; 251 std::string plural; 252 auto result = ResourceParser::GetPluralString(info, value, plural); 253 EXPECT_FALSE(result); 254 info.bundleName = "bundle"; 255 info.moduleName = "module"; 256 result = ResourceParser::GetPluralString(info, value, plural); 257 EXPECT_TRUE(result); 258 info.resId = UNKNOWN_RESOURCE_ID; 259 info.params.push_back("param"); 260 result = ResourceParser::GetPluralString(info, value, plural); 261 EXPECT_TRUE(result); 262 } 263 264 /** 265 * @tc.name: ResourceParserTestTest014 266 * @tc.desc: Test GetBoolean when different states of resource 267 * @tc.type: Func 268 */ 269 HWTEST_F(ResourceParserTest, ResourceParserTestTest014, TestSize.Level1) 270 { 271 Kit::ResourceInfo info; 272 bool value; 273 auto result = ResourceParser::GetBoolean(info, value); 274 EXPECT_FALSE(result); 275 info.bundleName = "bundle"; 276 info.moduleName = "module"; 277 result = ResourceParser::GetBoolean(info, value); 278 EXPECT_TRUE(result); 279 info.resId = UNKNOWN_RESOURCE_ID; 280 info.params.push_back("param"); 281 result = ResourceParser::GetBoolean(info, value); 282 EXPECT_TRUE(result); 283 } 284 285 /** 286 * @tc.name: ResourceParserTestTest015 287 * @tc.desc: Test GetIntArray when different states of resource 288 * @tc.type: Func 289 */ 290 HWTEST_F(ResourceParserTest, ResourceParserTestTest015, TestSize.Level1) 291 { 292 Kit::ResourceInfo info; 293 std::vector<uint32_t> value; 294 auto result = ResourceParser::GetIntArray(info, value); 295 EXPECT_FALSE(result); 296 info.bundleName = "bundle"; 297 info.moduleName = "module"; 298 info.resId = 1; 299 result = ResourceParser::GetIntArray(info, value); 300 EXPECT_TRUE(result); 301 EXPECT_EQ(value.size(), 0); 302 info.resId = UNKNOWN_RESOURCE_ID; 303 result = ResourceParser::GetIntArray(info, value); 304 EXPECT_TRUE(result); 305 EXPECT_EQ(value.size(), 0); 306 } 307 308 /** 309 * @tc.name: ResourceParserTestTest016 310 * @tc.desc: Test GetStringArray when different states of resource 311 * @tc.type: Func 312 */ 313 HWTEST_F(ResourceParserTest, ResourceParserTestTest016, TestSize.Level1) 314 { 315 Kit::ResourceInfo info; 316 std::vector<std::string> value; 317 auto result = ResourceParser::GetStringArray(info, value); 318 EXPECT_FALSE(result); 319 info.bundleName = "bundle"; 320 info.moduleName = "module"; 321 info.resId = 1; 322 result = ResourceParser::GetStringArray(info, value); 323 EXPECT_TRUE(result); 324 EXPECT_EQ(value.size(), 0); 325 info.resId = UNKNOWN_RESOURCE_ID; 326 result = ResourceParser::GetStringArray(info, value); 327 EXPECT_TRUE(result); 328 EXPECT_EQ(value.size(), 0); 329 } 330 331 /** 332 * @tc.name: ResourceParserTestTest017 333 * @tc.desc: Test GetMediaPath when ResourceAdapter is null 334 * @tc.type: Func 335 */ 336 HWTEST_F(ResourceParserTest, ResourceParserTestTest017, TestSize.Level1) 337 { 338 Kit::ResourceInfo info; 339 info.resId = UNKNOWN_RESOURCE_ID; 340 341 std::string mediaPath; 342 bool result = ResourceParser::GetMediaPath(info, mediaPath); 343 344 EXPECT_FALSE(result); 345 } 346 347 /** 348 * @tc.name: ResourceParserTestTest018 349 * @tc.desc: Test GetMediaPath when resId is unknown 350 * @tc.type: Func 351 */ 352 HWTEST_F(ResourceParserTest, ResourceParserTestTest018, TestSize.Level1) 353 { 354 Kit::ResourceInfo info; 355 info.resId = UNKNOWN_RESOURCE_ID; 356 info.params = {"param"}; 357 358 std::string mediaPath; 359 bool result = ResourceParser::GetMediaPath(info, mediaPath); 360 361 EXPECT_FALSE(result); 362 EXPECT_TRUE(mediaPath.empty()); 363 } 364 365 /** 366 * @tc.name: ResourceParserTestTest019 367 * @tc.desc: Test GetMediaPath when resId is known 368 * @tc.type: Func 369 */ 370 HWTEST_F(ResourceParserTest, ResourceParserTestTest019, TestSize.Level1) 371 { 372 Kit::ResourceInfo info; 373 info.resId = 123; 374 375 std::string mediaPath; 376 bool result = ResourceParser::GetMediaPath(info, mediaPath); 377 378 EXPECT_FALSE(result); 379 EXPECT_TRUE(mediaPath.empty()); 380 } 381 382 /** 383 * @tc.name: ResourceParserTestTest020 384 * @tc.desc: Test if GetInt gets an integer by name when resource ID is unknown 385 * @tc.type: Func 386 */ 387 HWTEST_F(ResourceParserTest, ResourceParserTestTest020, TestSize.Level1) 388 { 389 Kit::ResourceInfo info; 390 info.resId = UNKNOWN_RESOURCE_ID; 391 info.params = {"paramName"}; 392 int32_t intRes; 393 bool result = ResourceParser::GetInt(info, intRes); 394 EXPECT_FALSE(result); 395 } 396 397 /** 398 * @tc.name: ResourceParserTestTest021 399 * @tc.desc: Test if GetInt gets an integer by name when resource ID is known 400 * @tc.type: Func 401 */ 402 HWTEST_F(ResourceParserTest, ResourceParserTestTest021, TestSize.Level1) 403 { 404 Kit::ResourceInfo info; 405 info.resId = 123; 406 int32_t intRes; 407 bool result = ResourceParser::GetInt(info, intRes); 408 EXPECT_FALSE(result); 409 } 410 411 /** 412 * @tc.name: ResourceParserTestTest022 413 * @tc.desc: Test GetDouble when resourceWrapper is null 414 * @tc.type: Func 415 */ 416 HWTEST_F(ResourceParserTest, ResourceParserTestTest022, TestSize.Level1) 417 { 418 Kit::ResourceInfo info; 419 info.bundleName = ""; 420 info.moduleName = ""; 421 info.resId = 1; 422 423 double doubleRes; 424 EXPECT_TRUE(ResourceParser::GetDouble(info, doubleRes)); 425 } 426 427 /** 428 * @tc.name: ResourceParserTestTest023 429 * @tc.desc: Test GetDouble when resourceId is unknown 430 * @tc.type: Func 431 */ 432 HWTEST_F(ResourceParserTest, ResourceParserTestTest023, TestSize.Level1) 433 { 434 Kit::ResourceInfo info; 435 info.bundleName = "bundle"; 436 info.moduleName = "module"; 437 info.resId = UNKNOWN_RESOURCE_ID; 438 info.params = {"paramName"}; 439 440 double doubleRes; 441 EXPECT_TRUE(ResourceParser::GetDouble(info, doubleRes)); 442 } 443 444 /** 445 * @tc.name: ResourceParserTestTest024 446 * @tc.desc: Test GetDouble when resourceId is known 447 * @tc.type: Func 448 */ 449 HWTEST_F(ResourceParserTest, ResourceParserTestTest024, TestSize.Level1) 450 { 451 Kit::ResourceInfo info; 452 info.bundleName = "bundle"; 453 info.moduleName = "module"; 454 info.resId = 1; 455 456 double doubleRes; 457 EXPECT_TRUE(ResourceParser::GetDouble(info, doubleRes)); 458 } 459 460 /** 461 * @tc.name: ResourceParserTestTest025 462 * @tc.desc: Test GetPluralString when ResourceAdapter is null 463 * @tc.type: Func 464 */ 465 HWTEST_F(ResourceParserTest, ResourceParserTestTest025, TestSize.Level1) 466 { 467 Kit::ResourceInfo info; 468 info.resId = UNKNOWN_RESOURCE_ID; 469 470 std::string str; 471 bool result = ResourceParser::GetPluralString(info, 2, str); 472 473 EXPECT_FALSE(result); 474 } 475 476 /** 477 * @tc.name: ResourceParserTestTest026 478 * @tc.desc: Test GetPluralString when resId is UNKNOWN_RESOURCE_ID 479 * @tc.type: Func 480 */ 481 HWTEST_F(ResourceParserTest, ResourceParserTestTest026, TestSize.Level1) 482 { 483 Kit::ResourceInfo info; 484 info.resId = UNKNOWN_RESOURCE_ID; 485 info.params = {"param"}; 486 487 std::string str; 488 bool result = ResourceParser::GetPluralString(info, 2, str); 489 490 EXPECT_FALSE(result); 491 } 492 493 /** 494 * @tc.name: ResourceParserTestTest027 495 * @tc.desc: Test GetPluralString when resId is known 496 * @tc.type: Func 497 */ 498 HWTEST_F(ResourceParserTest, ResourceParserTestTest027, TestSize.Level1) 499 { 500 Kit::ResourceInfo info; 501 info.resId = 123; 502 503 std::string str; 504 bool result = ResourceParser::GetPluralString(info, 2, str); 505 506 EXPECT_FALSE(result); 507 } 508 509 /** 510 * @tc.name: ResourceParserTestTest028 511 * @tc.desc: Test GetDimension when resId is unknown, check GetDimension rst by different type 512 * @tc.type: Func 513 */ 514 HWTEST_F(ResourceParserTest, ResourceParserTestTest028, TestSize.Level1) 515 { 516 Kit::ResourceInfo info; 517 info.bundleName = ""; 518 info.moduleName = ""; 519 info.resId = UNKNOWN_RESOURCE_ID; 520 Ace::CalcDimension dimension; 521 522 // param is empty, check return false 523 auto result = ResourceParser::GetDimension(info, dimension); 524 EXPECT_FALSE(result); 525 526 info.params.push_back("param"); 527 info.type = static_cast<int32_t>(ResourceType::STRING); 528 result = ResourceParser::GetDimension(info, dimension); 529 EXPECT_FALSE(result); 530 531 info.type = static_cast<int32_t>(ResourceType::INTEGER); 532 result = ResourceParser::GetDimension(info, dimension); 533 EXPECT_TRUE(result); 534 535 info.type = static_cast<int32_t>(ResourceType::FLOAT); 536 result = ResourceParser::GetDimension(info, dimension); 537 EXPECT_TRUE(result); 538 } 539 540 /** 541 * @tc.name: ResourceParserTestTest029 542 * @tc.desc: Test GetDimension when resId is known, check GetDimension rst by different type 543 * @tc.type: Func 544 */ 545 HWTEST_F(ResourceParserTest, ResourceParserTestTest029, TestSize.Level1) 546 { 547 Kit::ResourceInfo info; 548 info.bundleName = ""; 549 info.moduleName = ""; 550 info.resId = 123; 551 Ace::CalcDimension dimension; 552 553 info.type = static_cast<int32_t>(ResourceType::STRING); 554 auto result = ResourceParser::GetDimension(info, dimension); 555 EXPECT_FALSE(result); 556 557 info.type = static_cast<int32_t>(ResourceType::INTEGER); 558 result = ResourceParser::GetDimension(info, dimension); 559 EXPECT_TRUE(result); 560 561 info.type = static_cast<int32_t>(ResourceType::FLOAT); 562 result = ResourceParser::GetDimension(info, dimension); 563 EXPECT_TRUE(result); 564 } 565 } // namespace OHOS::Ace 566