• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #define private public
18 #define protected public
19 #include "include/core/SkImage.h"
20 
21 #include "core/image/image_cache.h"
22 #include "core/image/image_compressor.h"
23 #include "core/image/image_loader.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS::Ace {
29 const std::string FILE_JPG = "file:///data/test/resource/imageprovider/images/unbroken.jpg";
30 
31 class ImageCompressorTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase() {}
TearDownTestCase()34     static void TearDownTestCase() {}
SetUp()35     void SetUp()
36     {
37         instance->refCount_ = 0;
38     }
TearDown()39     void TearDown() {}
40 
41     std::shared_ptr<ImageCompressor> instance = ImageCompressor::GetInstance();
42 };
43 
LoadImage()44 static sk_sp<SkImage> LoadImage()
45 {
46     auto imageLoader = FileImageLoader();
47     auto data = imageLoader.LoadImageData(ImageSourceInfo(FILE_JPG));
48     auto image = SkImage::MakeFromEncoded(data);
49     GTEST_LOG_(INFO) << "ImageCompressorTest LoadImage " << image;
50     if (image) {
51         return image->makeRasterImage();
52     }
53     return nullptr;
54 }
55 
56 /**
57  * @tc.name: ImageCompressor001
58  * @tc.desc: Test partition info init
59  * @tc.type: FUNC
60  * @tc.require: issueI5PFV6
61  */
62 HWTEST_F(ImageCompressorTest, ImageCompressor001, TestSize.Level1)
63 {
64     /**
65      * @tc.steps: step1. init instance
66      * @tc.expected: parts inited
67      */
68     instance->Init();
69     ImageCompressor::PartInfo p = {};
70     for (int i = 0; i < 1024; i++) {
71         instance->InitPartitionInfo(&p, instance->partitions_[1], 1);
72         instance->InitPartitionInfo(&p, instance->partitions_[1], 2);
73         instance->InitPartitionInfo(&p, instance->partitions_[1], 3);
74     }
75     ASSERT_TRUE(instance->parts_.size() > 0);
76 }
77 
78 /**
79  * @tc.name: ImageCompressor002
80  * @tc.desc: Test no shader file
81  * @tc.type: FUNC
82  * @tc.require: issueI5PFV6
83  */
84 HWTEST_F(ImageCompressorTest, ImageCompressor002, TestSize.Level1)
85 {
86     /**
87      * @tc.steps: step1. cache images one by one.
88      */
89     std::string& path = const_cast<std::string&>(instance->shader_path_);
90     path = "";
91     instance->Init();
92     auto image = LoadImage();
93     if (image && instance->CanCompress()) {
94         SkPixmap pixmap;
95         image->peekPixels(&pixmap);
96         std::string key = "key2";
97         int32_t width = static_cast<int32_t>(pixmap.width());
98         int32_t height = static_cast<int32_t>(pixmap.height());
99         auto compressData = instance->GpuCompress(key, pixmap, width, height);
100         ASSERT_TRUE(compressData == nullptr);
101         instance->WriteToFile(key, compressData, { width, height });
102     }
103     path = "/system/bin/astc.bin";
104 }
105 
106 /**
107  * @tc.name: ImageCompressor003
108  * @tc.desc: Test compress success
109  * @tc.type: FUNC
110  * @tc.require: issueI5PFV6
111  */
112 HWTEST_F(ImageCompressorTest, ImageCompressor003, TestSize.Level1)
113 {
114     instance->Init();
115     auto image = LoadImage();
116     if (image && instance->CanCompress()) {
117         SkPixmap pixmap;
118         image->peekPixels(&pixmap);
119         std::string key = "key3";
120         int32_t width = static_cast<int32_t>(pixmap.width());
121         int32_t height = static_cast<int32_t>(pixmap.height());
122         auto compressData = instance->GpuCompress(key, pixmap, width, height);
123         ASSERT_TRUE(compressData != nullptr);
124         instance->WriteToFile(key, compressData, { width, height });
125     }
126 }
127 
128 /**
129  * @tc.name: ImageCompressor004
130  * @tc.desc: Test compress failed with quality failed
131  * @tc.type: FUNC
132  * @tc.require: issueI5PFV6
133  */
134 HWTEST_F(ImageCompressorTest, ImageCompressor004, TestSize.Level1)
135 {
136     instance->Init();
137     instance->maxErr_ = 0;
138     auto image = LoadImage();
139     if (image && instance->CanCompress()) {
140         SkPixmap pixmap;
141         image->peekPixels(&pixmap);
142         std::string key = "key4";
143         int32_t width = static_cast<int32_t>(pixmap.width());
144         int32_t height = static_cast<int32_t>(pixmap.height());
145         auto compressData = instance->GpuCompress(key, pixmap, width, height);
146         ASSERT_TRUE(compressData == nullptr);
147         instance->WriteToFile(key, compressData, { width, height });
148     }
149 }
150 
151 /**
152  * @tc.name: ImageCompressor005
153  * @tc.desc: Test compress sucess and release context
154  * @tc.type: FUNC
155  * @tc.require: issueI5PFV6
156  */
157 HWTEST_F(ImageCompressorTest, ImageCompressor005, TestSize.Level1)
158 {
159     instance->Init();
160     auto image = LoadImage();
161     if (image && instance->CanCompress()) {
162         SkPixmap pixmap;
163         image->peekPixels(&pixmap);
164         std::string key = "key5";
165         int32_t width = static_cast<int32_t>(pixmap.width());
166         int32_t height = static_cast<int32_t>(pixmap.height());
167         auto compressData = instance->GpuCompress(key, pixmap, width, height);
168         ASSERT_TRUE(compressData != nullptr);
169         instance->WriteToFile(key, compressData, { width, height });
170         auto task = instance->ScheduleReleaseTask();
171         task();
172         ASSERT_TRUE(instance->context_ == nullptr);
173         instance->InitRecords();
174     }
175 }
176 
177 /**
178  * @tc.name: ImageCompressor006
179  * @tc.desc: Test compress sucess and release context
180  * @tc.type: FUNC
181  * @tc.require: issueI5PFV6
182  */
183 HWTEST_F(ImageCompressorTest, ImageCompressor006, TestSize.Level1)
184 {
185     instance->Init();
186     auto image = LoadImage();
187     if (image && instance->CanCompress()) {
188         SkPixmap pixmap;
189         image->peekPixels(&pixmap);
190         std::string key = "key6";
191         int32_t width = 0;
192         int32_t height = 0;
193         auto compressData = instance->GpuCompress(key, pixmap, width, height);
194         ASSERT_TRUE(compressData == nullptr);
195     }
196 }
197 
198 /**
199  * @tc.name: ImageCompressor007
200  * @tc.desc: Test compress sucess and release context
201  * @tc.type: FUNC
202  * @tc.require: issueI5PFV6
203  */
204 HWTEST_F(ImageCompressorTest, ImageCompressor007, TestSize.Level1)
205 {
206     instance->Init();
207     auto image = LoadImage();
208     if (image && instance->CanCompress()) {
209         SkPixmap pixmap;
210         image->peekPixels(&pixmap);
211         std::string key = "key7";
212         int32_t width = static_cast<int32_t>(pixmap.width());
213         int32_t height = static_cast<int32_t>(pixmap.height());
214         auto compressData = instance->GpuCompress(key, pixmap, width, height);
215         auto strip = instance->StripFileHeader(compressData);
216         ASSERT_TRUE(strip != nullptr);
217     }
218 }
219 
220 /**
221  * @tc.name: TestSwitch001
222  * @tc.desc: Test switch
223  * @tc.type: FUNC
224  * @tc.require: issueI5PFV6
225  */
226 HWTEST_F(ImageCompressorTest, TestSwitch001, TestSize.Level1)
227 {
228     instance->Init();
229     instance->switch_ = false;
230     ASSERT_TRUE(!instance->CanCompress());
231     instance->switch_ = true;
232     instance->clOk_ = true;
233     ASSERT_TRUE(instance->CanCompress());
234 }
235 
236 } // namespace OHOS::Ace
237