1 /* 2 * Copyright (c) 2023 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 #define private public 17 #define protected public 18 #include "core/common/asset_manager_impl.h" 19 #undef private 20 #undef protected 21 #include "mock_asset.h" 22 #include "test/mock/core/common/mock_container.h" 23 24 #include "adapter/ohos/entrance/file_asset_provider_impl.h" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 namespace OHOS::Ace { 30 namespace { 31 const std::string ASSET_TEST = "test"; 32 const std::string PACK_PATH = "/system/app/com.ohos.photos/Photos.hap"; 33 const std::string ASSET_BASE_PATH = "/resources/base/profile/"; 34 MediaFileInfo MEDIA_FILE_INFO = { .fileName = ASSET_TEST, .length = 1, .lastModTime = 1, .lastModDate = 1 }; 35 } // namespace 36 37 class AssetTest : public testing::Test { 38 public: SetUpTestCase()39 static void SetUpTestCase() {} TearDownTestCase()40 static void TearDownTestCase() {} SetUp()41 void SetUp() {} TearDown()42 void TearDown() {} 43 }; 44 45 /** 46 * @tc.name: AssetTest01 47 * @tc.desc: Test the asset operation. 48 * @tc.type: FUNC 49 */ 50 HWTEST_F(AssetTest, AssetTest01, TestSize.Level1) 51 { 52 /** 53 * @tc.steps: step1. Build a manager. 54 */ 55 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 56 auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>(); 57 58 /** 59 * @tc.steps: step2. call Initialize. 60 * @tc.steps: step3. call PushBack with the asset provider. 61 */ 62 std::vector<std::string> basePaths; 63 basePaths.push_back(ASSET_BASE_PATH); 64 assetProvider->Initialize(PACK_PATH, basePaths); 65 assetManager->PushBack(std::move(assetProvider)); 66 67 /** 68 * @tc.steps: step4. call GetAssetPath with ASSET_TEST. 69 * @tc.expected: step4. Return expected results. 70 */ 71 std::string result = assetManager->GetAssetPath(ASSET_TEST, true); 72 EXPECT_EQ(result, ""); 73 } 74 75 /** 76 * @tc.name: AssetTest02 77 * @tc.desc: Test the asset operation 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(AssetTest, AssetTest02, TestSize.Level1) 81 { 82 /** 83 * @tc.steps: step1. Build a manager. 84 */ 85 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 86 /** 87 * @tc.steps: step2. Call GetAssetPath of asset manager. 88 * @tc.expected: step2. Return expected results. 89 */ 90 std::string asset_path = assetManager->GetAssetPath(ASSET_TEST, true); 91 EXPECT_EQ(asset_path, ""); 92 93 /** 94 * @tc.steps: step3. Call GetAssetPath with the asset provider. 95 * @tc.expected: step3. Return expected results. 96 */ 97 98 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 99 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 100 EXPECT_CALL(*assetProvider, GetAssetPath(ASSET_TEST, true)).Times(1).WillOnce(Return(ASSET_TEST)); 101 assetManager->PushBack(std::move(assetProvider)); 102 103 /** 104 * @tc.steps: step4. Call GetAssetPath with the asset provider. 105 * @tc.expected: step4. Return expected results. 106 */ 107 std::string path_Result = assetManager->GetAssetPath(ASSET_TEST, true); 108 EXPECT_EQ(path_Result, ASSET_TEST); 109 } 110 111 /** 112 * @tc.name: AssetTest03 113 * @tc.desc: Test the asset operation. 114 * @tc.type: FUNC 115 */ 116 HWTEST_F(AssetTest, AssetTest03, TestSize.Level1) 117 { 118 /** 119 * @tc.steps: step1. Build a manager. 120 * @tc.steps: step2. Build a mockAssetProvider. 121 */ 122 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 123 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 124 std::vector<std::string> assetList; 125 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 126 EXPECT_CALL(*assetProvider, GetAssetList(ASSET_TEST, assetList)).WillOnce(Return()); 127 128 /** 129 * @tc.steps: step3. Call PushBack with the asset provider. 130 * @tc.steps: step4. Call GetAssetList. 131 * @tc.expected: step4. Return expected results. 132 */ 133 assetManager->PushBack(std::move(assetProvider)); 134 assetManager->GetAssetList(ASSET_TEST, assetList); 135 EXPECT_TRUE(assetList.empty()); 136 } 137 138 /** 139 * @tc.name: AssetTest04 140 * @tc.desc: Test the asset operation. 141 * @tc.type: FUNC 142 */ 143 HWTEST_F(AssetTest, AssetTest04, TestSize.Level1) 144 { 145 /** 146 * @tc.steps: step1. Build a manager. 147 * @tc.steps: step2. Call GetFileInfo with null assetName. 148 * @tc.expected: step2. Return expected results. 149 */ 150 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 151 auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>(); 152 std::vector<std::string> assetBasePaths; 153 assetBasePaths.push_back(ASSET_BASE_PATH); 154 assetProvider->Initialize(PACK_PATH, assetBasePaths); 155 assetProvider->GetFileInfo(PACK_PATH, MEDIA_FILE_INFO); 156 assetManager->PushBack(std::move(assetProvider)); 157 bool info_Result = assetManager->GetFileInfo(PACK_PATH, MEDIA_FILE_INFO); 158 EXPECT_FALSE(info_Result); 159 } 160 161 /** 162 * @tc.name: AssetTest05 163 * @tc.desc: Test the asset operation. 164 * @tc.type: FUNC 165 */ 166 HWTEST_F(AssetTest, AssetTest05, TestSize.Level1) 167 { 168 /** 169 * @tc.steps: step1. Build a manager. 170 * @tc.steps: step2. Call GetAsset with the asset provider. 171 * @tc.expected: step2. The expected result is null. 172 */ 173 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 174 std::string testFilePath = "core/common/asset/BUILD.gn"; 175 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 176 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 177 EXPECT_CALL(*assetProvider, GetAsset(testFilePath)).Times(1).WillOnce(Return(nullptr)); 178 assetProvider->GetAsset(testFilePath); 179 assetManager->PushBack(std::move(assetProvider)); 180 auto asset_Value = assetManager->GetAsset(testFilePath); 181 EXPECT_EQ(asset_Value, nullptr); 182 } 183 184 /** 185 * @tc.name: AssetTest06 186 * @tc.desc: Test the asset operation. 187 * @tc.type: FUNC 188 */ 189 HWTEST_F(AssetTest, AssetTest06, TestSize.Level1) 190 { 191 /** 192 * @tc.steps: step1. Build a manager. 193 */ 194 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 195 196 /** 197 * @tc.steps: step2. Call GetAsset with null assetName. 198 * @tc.expected: step2. Return expected results. 199 */ 200 auto result = assetManager->GetAsset(""); 201 EXPECT_EQ(result, nullptr); 202 203 /** 204 * @tc.steps: step3. Call GetAsset with assetName. 205 * @tc.expected: step3. Return expected results. 206 */ 207 auto asset_Result = assetManager->GetAsset(ASSET_TEST); 208 EXPECT_EQ(asset_Result, nullptr); 209 } 210 211 /** 212 * @tc.name: AssetTest07 213 * @tc.desc: Test PushFront/PushBack 214 * @tc.type: FUNC 215 */ 216 HWTEST_F(AssetTest, AssetTest07, TestSize.Level1) 217 { 218 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 219 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 220 // assetProvider null 221 assetManager->PushFront(nullptr); 222 assetManager->PushBack(nullptr); 223 EXPECT_EQ(assetManager->assetProviders_.size() == 0, true); 224 // assetProvider is not null,IsValid is true 225 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 226 assetManager->PushFront(assetProvider); 227 EXPECT_EQ(assetManager->assetProviders_.size() == 1, true); 228 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 229 assetManager->PushBack(assetProvider); 230 EXPECT_EQ(assetManager->assetProviders_.size() == 2, true); 231 // assetProvider is not null,IsValid is false 232 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(false)); 233 assetManager->PushFront(assetProvider); 234 EXPECT_EQ(assetManager->assetProviders_.size() == 2, true); 235 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(false)); 236 assetManager->PushBack(assetProvider); 237 EXPECT_EQ(assetManager->assetProviders_.size() == 2, true); 238 } 239 240 /** 241 * @tc.name: AssetTest08 242 * @tc.desc: Test GetAssetFromI18n 243 * @tc.type: FUNC 244 */ 245 HWTEST_F(AssetTest, AssetTest08, TestSize.Level1) 246 { 247 auto assetManager = AceType::MakeRefPtr<AssetManagerImpl>(); 248 auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>(); 249 // assetProvider is not null,IsValid is true 250 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 251 assetManager->PushFront(assetProvider); 252 EXPECT_EQ(assetManager->assetProviders_.size() == 1, true); 253 EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true)); 254 assetManager->PushBack(assetProvider); 255 EXPECT_EQ(assetManager->assetProviders_.size() == 2, true); 256 // assetName is empty 257 std::string assetName = ""; 258 std::vector<RefPtr<Asset>> ret = assetManager->GetAssetFromI18n(assetName); 259 EXPECT_EQ(ret.size() == 0, true); 260 // assetName is not empty 261 assetName = "test"; 262 std::vector<RefPtr<Asset>> ret2 = assetManager->GetAssetFromI18n(assetName); 263 EXPECT_EQ(ret2.size() == 0, true); 264 } 265 } // namespace OHOS::Ace 266