1 /* 2 * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 "gmock/gmock.h" 17 #include "gtest/gtest.h" 18 #define private public 19 #define protected public 20 #include "core/common/rosen/rosen_asset_manager.h" 21 #include "core/common/rosen/rosen_convert_helper.h" 22 #include "core/common/rosen/rosen_enums.h" 23 #include "core/common/rosen/rosen_window.h" 24 #undef private 25 #undef protected 26 #include "mock_rosen.h" 27 #include "test/mock/core/common/mock_container.h" 28 29 #include "adapter/ohos/entrance/file_asset_provider_impl.h" 30 #include "core/pipeline/base/render_node.h" 31 32 using namespace testing; 33 using namespace testing::ext; 34 35 namespace OHOS::Ace { 36 namespace { 37 const std::string RET_TEST = "error"; 38 const std::string HAPPATTH = "/system/app/com.ohos.photos/Photos.hap"; 39 const std::string ASSETBASEPATHS = "/resources/base/profile/"; 40 uint64_t nanoTimestamp_ = 0; 41 uint32_t frameCount_ = 0; 42 AceVsyncCallback callback_; 43 } // namespace 44 45 class RosenTest : public testing::Test { 46 public: SetUpTestCase()47 static void SetUpTestCase() {} TearDownTestCase()48 static void TearDownTestCase() {} SetUp()49 void SetUp() {} TearDown()50 void TearDown() {} 51 }; 52 53 /** 54 * @tc.name: CastToRosenTest001 55 * @tc.desc: Test cast to rosen. 56 * @tc.type: FUNC 57 */ 58 HWTEST_F(RosenTest, CastToRosenTest001, TestSize.Level1) 59 { 60 /** 61 * @tc.steps: step1. Build a object RSWindow. 62 */ 63 Platform::RSWindow object; 64 65 /** 66 * @tc.steps: step2. callback Create. 67 * @tc.expected: step2. Return expected results.. 68 */ 69 auto result1 = PlatformWindow::Create(nullptr); 70 EXPECT_EQ(result1, nullptr); 71 72 /** 73 * @tc.steps: step3. callback SetRootRenderNode. 74 * @tc.expected: step3. SetRootRenderNode success. 75 */ 76 object.SetRootRenderNode(nullptr); 77 } 78 79 /** 80 * @tc.name: CastToRosenTest002 81 * @tc.desc: Test cast to rosen. 82 * @tc.type: FUNC 83 */ 84 HWTEST_F(RosenTest, CastToRosenTest002, TestSize.Level1) 85 { 86 /** 87 * @tc.steps: step1. Build a object RSWindow. 88 */ 89 Platform::RSWindow object; 90 91 /** 92 * @tc.steps: step2. callback RegisterVsyncCallback. 93 * @tc.expected: step2. Return expected results. 94 */ __anonea5ee7c40202(uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) 95 auto callback = [](uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) { 96 nanoTimestamp_ += deltaNanoTimestamp; 97 frameCount_ += deltaFrameCount; 98 if (callback_) { 99 callback_(nanoTimestamp_, frameCount_); 100 } 101 }; 102 object.RegisterVsyncCallback(callback); 103 EXPECT_EQ(nanoTimestamp_, 0); 104 } 105 106 /** 107 * @tc.name: CastToRosenTest005 108 * @tc.desc: Test cast to rosen. 109 * @tc.type: FUNC 110 */ 111 HWTEST_F(RosenTest, CastToRosenTest005, TestSize.Level1) 112 { 113 /** 114 * @tc.steps: step1. callback GetRosenBlurStyleValue.The blurStyleOption parameter is the default value. 115 * @tc.expected: step1. Return expected results. 116 */ 117 auto result = GetRosenBlurStyleValue(BlurStyleOption()); 118 EXPECT_EQ(result, MATERIAL_BLUR_STYLE::NO_MATERIAL); 119 120 /** 121 * @tc.steps: step2. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 122 * @tc.expected: step2. Return expected results. 123 */ 124 BlurStyleOption blurStyleOption; 125 blurStyleOption.blurStyle = BlurStyle::THIN; 126 blurStyleOption.colorMode = ThemeColorMode::DARK; 127 auto retDark = GetRosenBlurStyleValue(blurStyleOption); 128 EXPECT_EQ(retDark, MATERIAL_BLUR_STYLE::STYLE_CARD_THIN_DARK); 129 130 /** 131 * @tc.steps: step3. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 132 * @tc.expected: step3. Return expected results. 133 */ 134 blurStyleOption.blurStyle = BlurStyle::REGULAR; 135 blurStyleOption.colorMode = ThemeColorMode::DARK; 136 auto retDark1 = GetRosenBlurStyleValue(blurStyleOption); 137 EXPECT_EQ(retDark1, MATERIAL_BLUR_STYLE::STYLE_CARD_DARK); 138 139 /** 140 * @tc.steps: step4. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 141 * @tc.expected: step4. Return expected results. 142 */ 143 blurStyleOption.blurStyle = BlurStyle::THICK; 144 blurStyleOption.colorMode = ThemeColorMode::DARK; 145 auto retDark2 = GetRosenBlurStyleValue(blurStyleOption); 146 EXPECT_EQ(retDark2, MATERIAL_BLUR_STYLE::STYLE_CARD_THICK_DARK); 147 148 /** 149 * @tc.steps: step5. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 150 * @tc.expected: step5. Return expected results. 151 */ 152 blurStyleOption.blurStyle = BlurStyle::BACKGROUND_THIN; 153 blurStyleOption.colorMode = ThemeColorMode::DARK; 154 auto retDark3 = GetRosenBlurStyleValue(blurStyleOption); 155 EXPECT_EQ(retDark3, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_SMALL_DARK); 156 157 /** 158 * @tc.steps: step6. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 159 * @tc.expected: step6. Return expected results. 160 */ 161 blurStyleOption.blurStyle = BlurStyle::BACKGROUND_REGULAR; 162 blurStyleOption.colorMode = ThemeColorMode::DARK; 163 auto retDark4 = GetRosenBlurStyleValue(blurStyleOption); 164 EXPECT_EQ(retDark4, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_MEDIUM_DARK); 165 166 /** 167 * @tc.steps: step7. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 168 * @tc.expected: step7. Return expected results. 169 */ 170 blurStyleOption.blurStyle = BlurStyle::BACKGROUND_THICK; 171 blurStyleOption.colorMode = ThemeColorMode::DARK; 172 auto retDark5 = GetRosenBlurStyleValue(blurStyleOption); 173 EXPECT_EQ(retDark5, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_LARGE_DARK); 174 175 /** 176 * @tc.steps: step8. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 177 * @tc.expected: step8. Return expected results. 178 */ 179 blurStyleOption.blurStyle = BlurStyle::BACKGROUND_ULTRA_THICK; 180 blurStyleOption.colorMode = ThemeColorMode::DARK; 181 auto retDark6 = GetRosenBlurStyleValue(blurStyleOption); 182 EXPECT_EQ(retDark6, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_XLARGE_DARK); 183 } 184 185 /** 186 * @tc.name: CastToRosenTest006 187 * @tc.desc: Test cast to rosen. 188 * @tc.type: FUNC 189 */ 190 HWTEST_F(RosenTest, CastToRosenTest006, TestSize.Level1) 191 { 192 /** 193 * @tc.steps: step1. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 194 * @tc.expected: step1. Return expected results. 195 */ 196 BlurStyleOption blurStyleOption; 197 blurStyleOption.blurStyle = BlurStyle::NO_MATERIAL; 198 blurStyleOption.colorMode = ThemeColorMode::LIGHT; 199 auto retLight = GetRosenBlurStyleValue(blurStyleOption); 200 EXPECT_EQ(retLight, MATERIAL_BLUR_STYLE::NO_MATERIAL); 201 202 /** 203 * @tc.steps: step2. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 204 * @tc.expected: step2. Return expected results. 205 */ 206 blurStyleOption.blurStyle = BlurStyle::THIN; 207 blurStyleOption.colorMode = ThemeColorMode::LIGHT; 208 auto retLight1 = GetRosenBlurStyleValue(blurStyleOption); 209 EXPECT_EQ(retLight1, MATERIAL_BLUR_STYLE::STYLE_CARD_THIN_LIGHT); 210 211 /** 212 * @tc.steps: step3. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 213 * @tc.expected: step3. Return expected results. 214 */ 215 blurStyleOption.blurStyle = BlurStyle::REGULAR; 216 blurStyleOption.colorMode = ThemeColorMode::LIGHT; 217 auto retLight2 = GetRosenBlurStyleValue(blurStyleOption); 218 EXPECT_EQ(retLight2, MATERIAL_BLUR_STYLE::STYLE_CARD_LIGHT); 219 220 /** 221 * @tc.steps: step4. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 222 * @tc.expected: step4. Return expected results. 223 */ 224 blurStyleOption.blurStyle = BlurStyle::THICK; 225 blurStyleOption.colorMode = ThemeColorMode::LIGHT; 226 auto retLight3 = GetRosenBlurStyleValue(blurStyleOption); 227 EXPECT_EQ(retLight3, MATERIAL_BLUR_STYLE::STYLE_CARD_THICK_LIGHT); 228 229 /** 230 * @tc.steps: step5. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value. 231 * @tc.expected: step5. Return expected results. 232 */ 233 blurStyleOption.blurStyle = BlurStyle::BACKGROUND_ULTRA_THICK; 234 blurStyleOption.colorMode = ThemeColorMode::LIGHT; 235 auto retLight4 = GetRosenBlurStyleValue(blurStyleOption); 236 EXPECT_EQ(retLight4, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_XLARGE_LIGHT); 237 } 238 239 /** 240 * @tc.name: CastToRosenTest007 241 * @tc.desc: Test cast to rosen. 242 * @tc.type: FUNC 243 */ 244 HWTEST_F(RosenTest, CastToRosenTest007, TestSize.Level1) 245 { 246 /** 247 * @tc.steps: step1. Build a object. 248 * @tc.steps: step2. callback GetAssetPath input provider is null. 249 * @tc.expected: step2. Return expected results. 250 */ 251 RSAssetManager object; 252 auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>(); 253 std::string hapPath = "/system/app/com.ohos.photos/Photos.hap"; 254 std::vector<std::string> assetBasePaths; 255 assetBasePaths.push_back("resources/base/profile/"); 256 assetProvider->Initialize(hapPath, assetBasePaths); 257 object.PushBack(std::move(assetProvider)); 258 std::string assetName = RET_TEST; 259 std::string result = object.GetAssetPath(assetName, true); 260 EXPECT_EQ(result, ""); 261 } 262 263 /** 264 * @tc.name: CastToRosenTest008 265 * @tc.desc: Test cast to rosen. 266 * @tc.type: FUNC 267 */ 268 HWTEST_F(RosenTest, CastToRosenTest008, TestSize.Level1) 269 { 270 /** 271 * @tc.steps: step1. Build a object. 272 * @tc.steps: step2. callback GetAssetPath input provider is null. 273 * @tc.expected: step2. Return expected results.. 274 */ 275 RSAssetManager object; 276 std::string assetName = RET_TEST; 277 std::string ret = object.GetAssetPath(assetName, true); 278 EXPECT_EQ(ret, ""); 279 280 /** 281 * @tc.steps: step3. callback GetAssetPath input provider is not null. 282 * @tc.expected: step3. Return expected results. 283 */ 284 std::string hapPath = "test"; 285 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 286 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 287 EXPECT_CALL(*assetProvider, GetAssetPath(hapPath, true)).Times(1).WillOnce(Return("test")); 288 object.PushBack(std::move(assetProvider)); 289 std::string result = object.GetAssetPath(hapPath, true); 290 EXPECT_EQ(result, "test"); 291 } 292 293 /** 294 * @tc.name: CastToRosenTest009 295 * @tc.desc: Test cast to rosen. 296 * @tc.type: FUNC 297 */ 298 HWTEST_F(RosenTest, CastToRosenTest009, TestSize.Level1) 299 { 300 /** 301 * @tc.steps: step1. Build a object. 302 * @tc.steps: step2. callback GetAssetList input provider is not null. 303 * @tc.expected: step2. Return expected results. 304 */ 305 RSAssetManager object; 306 std::string path = RET_TEST; 307 std::vector<std::string> assetList; 308 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 309 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 310 EXPECT_CALL(*assetProvider, GetAssetList(path, assetList)).WillOnce(Return()); 311 object.PushBack(std::move(assetProvider)); 312 object.GetAssetList(path, assetList); 313 EXPECT_TRUE(assetList.empty()); 314 } 315 316 /** 317 * @tc.name: CastToRosenTest0010 318 * @tc.desc: Test cast to rosen. 319 * @tc.type: FUNC 320 */ 321 HWTEST_F(RosenTest, CastToRosenTest0010, TestSize.Level1) 322 { 323 /** 324 * @tc.steps: step1. Build a object. 325 * @tc.steps: step2. callback GetFileInfo input assetName is null. 326 * @tc.expected: step2. Return results is false. 327 */ 328 RSAssetManager object; 329 std::string hapPath = "test"; 330 MediaFileInfo fileInfo = { .fileName = hapPath, .offset = 1, .length = 1, .lastModTime = 1, .lastModDate = 1 }; 331 auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>(); 332 std::vector<std::string> assetBasePaths; 333 assetBasePaths.push_back("resources/base/profile/"); 334 assetProvider->Initialize(hapPath, assetBasePaths); 335 assetProvider->GetFileInfo(hapPath, fileInfo); 336 object.PushBack(std::move(assetProvider)); 337 bool result = object.GetFileInfo(hapPath, fileInfo); 338 EXPECT_FALSE(result); 339 } 340 341 /** 342 * @tc.name: CastToRosenTest0011 343 * @tc.desc: Test cast to rosen. 344 * @tc.type: FUNC 345 */ 346 HWTEST_F(RosenTest, CastToRosenTest0011, TestSize.Level1) 347 { 348 /** 349 * @tc.steps: step1. Build a object. 350 * @tc.steps: step2. callback GetAsset input assetName is null. 351 * @tc.expected: step2. The expected result is null. 352 */ 353 RSAssetManager object; 354 auto result = object.GetAsset(""); 355 EXPECT_EQ(result, nullptr); 356 357 /** 358 * @tc.steps: step3. callback GetAsset input assetName is not null. 359 * @tc.expected: step3. The expected result is null. 360 */ 361 std::string assetName = RET_TEST; 362 auto testAsset = object.GetAsset(assetName); 363 EXPECT_EQ(testAsset, nullptr); 364 } 365 366 /** 367 * @tc.name: CastToRosenTest0012 368 * @tc.desc: Test cast to rosen. 369 * @tc.type: FUNC 370 */ 371 HWTEST_F(RosenTest, CastToRosenTest0012, TestSize.Level1) 372 { 373 /** 374 * @tc.steps: step1. Build a object. 375 * @tc.steps: step2. callback GetAsset input provider is not null. 376 * @tc.expected: step2. The expected result is null. 377 */ 378 RSAssetManager object; 379 std::string hapPath = "core/common/test/unittest/rosen/BUILD.gn"; 380 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 381 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 382 EXPECT_CALL(*assetProvider, GetAsset(hapPath)).Times(1).WillOnce(Return(nullptr)); 383 assetProvider->GetAsset(hapPath); 384 object.PushBack(std::move(assetProvider)); 385 auto ret = object.GetAsset(hapPath); 386 EXPECT_EQ(ret, nullptr); 387 } 388 } // namespace OHOS::Ace 389