• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define protected public
17 #define private public
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <unistd.h>
21 #include "core/image/image_file_cache.h"
22 #include "core/image/image_loader.h"
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Ace {
27 namespace {
28     const std::string BUNDLE_NAME = "com.example.testImageLoader";
29     const int32_t TEST_SIZE = 100;
30     const std::string CACHE_FILE_PATH = "/data";
31 }
32 
33 class ImageLoaderTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
SetUp()37     void SetUp() {}
TearDown()38     void TearDown() {}
39 };
40 
SetUpTestCase()41 void ImageLoaderTest::SetUpTestCase()
42 {
43     Testing::g_imageDataSize = 0;
44 }
45 
TearDownTestCase()46 void ImageLoaderTest::TearDownTestCase()
47 {
48     Testing::g_imageDataSize = 0;
49 }
50 
51 /**
52  * @tc.name: LoadDataFromCachedFile001
53  * @tc.desc: write data into cacheFilePath success.
54  * @tc.type: FUNC
55  */
56 HWTEST_F(ImageLoaderTest, LoadDataFromCachedFile001, TestSize.Level1)
57 {
58     std::vector<uint8_t> imageData = { 1, 2, 3, 4, 5, 6 };
59     std::string url = "http://testfilecache002/image";
60 
61     ImageFileCache::GetInstance().SetImageCacheFilePath(CACHE_FILE_PATH);
62     auto imageFileCache = ImageLoader::LoadDataFromCachedFile(url);
63     EXPECT_NE(imageFileCache, nullptr);
64 
65     std::string cacheFilePath(PATH_MAX + 1, 'a');
66     ImageFileCache::GetInstance().SetImageCacheFilePath(cacheFilePath);
67 
68     imageFileCache = ImageLoader::LoadDataFromCachedFile(url);
69     EXPECT_EQ(imageFileCache, nullptr);
70 
71     cacheFilePath = "";
72     ImageFileCache::GetInstance().SetImageCacheFilePath(cacheFilePath);
73     imageFileCache = ImageLoader::LoadDataFromCachedFile(url);
74     EXPECT_EQ(imageFileCache, nullptr);
75 }
76 
77 /**
78  * @tc.name: LoadImageData001
79  * @tc.desc: Test for LoadImageData of SrcType::FILE
80  * @tc.type: FUNC
81  */
82 HWTEST_F(ImageLoaderTest, LoadImageData001, TestSize.Level1)
83 {
84     std::string uri = "file:///data/storage/" + BUNDLE_NAME + "/test_file.png";
85     ImageSourceInfo imageSourceInfo;
86     imageSourceInfo.SetSrc(uri);
87     imageSourceInfo.srcType_ = SrcType::FILE;
88     NG::ImageDfxConfig imageDfxConfig;
89     imageSourceInfo.SetImageDfxConfig(imageDfxConfig);
90 
91     Testing::g_imageDataSize = 0;
92     FileImageLoader loader;
93     NG::ImageLoadResultInfo errorInfo;
94     auto result = loader.LoadImageData(imageSourceInfo, errorInfo);
95     EXPECT_EQ(result, nullptr);
96 
97     Testing::g_imageDataSize = TEST_SIZE;
98     result = loader.LoadImageData(imageSourceInfo, errorInfo);
99     EXPECT_NE(result, nullptr);
100 }
101 
102 /**
103  * @tc.name: BuildImageData
104  * @tc.desc: Test for BuildImageData
105  * @tc.type: FUNC
106  */
107 HWTEST_F(ImageLoaderTest, BuildImageData, TestSize.Level1)
108 {
109     auto rsData = std::make_shared<RSData>();
110     Testing::g_imageDataSize = 0;
111     FileImageLoader loader;
112     auto result = loader.BuildImageData(rsData);
113     EXPECT_EQ(result, nullptr);
114 
115     Testing::g_imageDataSize = TEST_SIZE;
116     result = loader.BuildImageData(rsData);
117     EXPECT_NE(result, nullptr);
118 }
119 } // namespace OHOS::Ace