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