• 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 #include <cstddef>
16 #include <optional>
17 #include <vector>
18 
19 #include "gtest/gtest.h"
20 
21 #include "base/geometry/ng/size_t.h"
22 #include "base/geometry/size.h"
23 
24 #define protected public
25 #define private public
26 
27 #include "test/mock/base/mock_pixel_map.h"
28 #include "test/mock/core/image_provider/mock_image_file_cache.cpp"
29 #include "test/mock/core/image_provider/mock_image_loader.h"
30 #include "test/mock/core/pipeline/mock_pipeline_context.h"
31 
32 #include "base/utils/system_properties.h"
33 #include "core/components/common/layout/constants.h"
34 #include "core/components_ng/image_provider/adapter/rosen/drawing_image_data.h"
35 #include "core/components_ng/image_provider/animated_image_object.h"
36 #include "core/components_ng/image_provider/image_data.h"
37 #include "core/components_ng/image_provider/image_loading_context.h"
38 #include "core/components_ng/image_provider/image_provider.h"
39 #include "core/components_ng/image_provider/image_state_manager.h"
40 #include "core/components_ng/image_provider/pixel_map_image_object.h"
41 #include "core/components_ng/image_provider/static_image_object.h"
42 #include "core/components_ng/render/canvas_image.h"
43 #include "core/components_ng/render/image_painter.h"
44 #include "core/image/image_source_info.h"
45 #include "core/image/sk_image_cache.h"
46 
47 using namespace testing;
48 using namespace testing::ext;
49 
50 namespace {
51 const char* SRC_JPG = "file://data/data/com.example.test/res/exampleAlt.jpg";
52 const char* SRC_THUMBNAIL = "datashare:///media/9/thumbnail/300/300";
53 constexpr int32_t LENGTH_100 = 100;
54 constexpr int32_t LENGTH_65 = 65;
55 constexpr int32_t LENGTH_64 = 64;
56 constexpr int32_t LENGTH_63 = 63;
57 constexpr int32_t LENGTH_128 = 128;
58 constexpr uint64_t MAX_WAITING_TIME_FOR_TASKS = 1000; // 1000ms
59 int32_t callbackFlag = 0;
60 } // namespace
61 
62 namespace OHOS::Ace::NG {
63 class ImageProviderTestNg : public testing::Test {
64 public:
65     static void SetUpTestSuite();
66     static void TearDownTestSuite();
67     static void WaitForAsyncTasks();
68 
69     void SetUp() override;
70     void TearDown() override;
71 };
72 
SetUpTestSuite()73 void ImageProviderTestNg::SetUpTestSuite()
74 {
75     MockPipelineContext::SetUp();
76     g_threads = std::vector<std::thread>();
77 }
78 
SetUp()79 void ImageProviderTestNg::SetUp()
80 {
81     g_loader = AceType::MakeRefPtr<MockImageLoader>();
82 }
83 
TearDown()84 void ImageProviderTestNg::TearDown()
85 {
86     g_loader = nullptr;
87 }
88 
TearDownTestSuite()89 void ImageProviderTestNg::TearDownTestSuite()
90 {
91     MockPipelineContext::TearDown();
92     g_loader = nullptr;
93 }
94 
95 // wait for load task to finish
WaitForAsyncTasks()96 void ImageProviderTestNg::WaitForAsyncTasks()
97 {
98     for (auto& thread : g_threads) {
99         thread.join();
100     }
101     g_threads.clear();
102 }
103 
104 /**
105  * @tc.name: CreateFun001
106  * @tc.desc: Test CreateFun.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(ImageProviderTestNg, CreateFunc001, TestSize.Level1)
110 {
111     auto src = ImageSourceInfo(SRC_JPG);
112     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
113     EXPECT_NE(ctx, nullptr);
114 
115     auto src1 = ImageSourceInfo(SRC_JPG);
116     src1.srcType_ = SrcType::PIXMAP;
117     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
118     EXPECT_NE(ctx1, nullptr);
119 }
120 
121 /**
122  * @tc.name: Destruction001
123  * @tc.desc: Test Destruction.
124  * @tc.type: FUNC
125  */
126 HWTEST_F(ImageProviderTestNg, Destruction001, TestSize.Level1)
127 {
128     auto src = ImageSourceInfo(SRC_JPG);
129     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
130     EXPECT_NE(ctx, nullptr);
131     ctx->syncLoad_ = false;
132 
133     auto src1 = ImageSourceInfo(SRC_JPG);
134     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
135     EXPECT_NE(ctx1, nullptr);
136 
137     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx1);
138     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
139     EXPECT_NE(ctx, nullptr);
140 }
141 
142 /**
143  * @tc.name: Destruction002
144  * @tc.desc: Test Destruction.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(ImageProviderTestNg, Destruction002, TestSize.Level1)
148 {
149     auto src = ImageSourceInfo(SRC_JPG);
150     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
151     EXPECT_NE(ctx, nullptr);
152     ctx->syncLoad_ = false;
153 
154     auto src1 = ImageSourceInfo(SRC_JPG);
155     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
156     EXPECT_NE(ctx1, nullptr);
157 
158     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx1);
159     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
160     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(1000, 1000), nullptr);
161     EXPECT_NE(ctx, nullptr);
162 }
163 
164 /**
165  * @tc.name: HandleCommand001
166  * @tc.desc: Test HandleCommand.
167  * @tc.type: FUNC
168  */
169 HWTEST_F(ImageProviderTestNg, HandleCommand001, TestSize.Level1)
170 {
171     auto src = ImageSourceInfo(SRC_JPG);
172     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
173     EXPECT_NE(ctx, nullptr);
174     ctx->syncLoad_ = false;
175 
176     auto src1 = ImageSourceInfo(SRC_JPG);
177     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
178     EXPECT_NE(ctx1, nullptr);
179 
180     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx1);
181     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
182     EXPECT_NE(ctx, nullptr);
183 
184     ctx->stateManager_->state_ = ImageLoadingState::LOAD_FAIL;
185     ctx->stateManager_->HandleCommand(ImageLoadingCommand::LOAD_FAIL);
186     EXPECT_NE(ctx, nullptr);
187 
188     ctx->stateManager_->state_ = ImageLoadingState(-1);
189     ctx->stateManager_->HandleCommand(ImageLoadingCommand::LOAD_FAIL);
190     EXPECT_NE(ctx, nullptr);
191 }
192 
193 /**
194  * @tc.name: NotifiersTest001
195  * @tc.desc: Test NotifiersTest.
196  * @tc.type: FUNC
197  */
198 HWTEST_F(ImageProviderTestNg, NotifiersTest001, TestSize.Level1)
199 {
200     auto src = ImageSourceInfo(SRC_JPG);
201     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
202     EXPECT_NE(ctx, nullptr);
203 
204     callbackFlag = 0;
__anon2fe2f2800202(const ImageSourceInfo& src) 205     auto callback1 = [](const ImageSourceInfo& src) {
206         callbackFlag = 1;
207         return;
208     };
__anon2fe2f2800302(const ImageSourceInfo& src) 209     auto callback2 = [](const ImageSourceInfo& src) {
210         callbackFlag = 2;
211         return;
212     };
__anon2fe2f2800402(const ImageSourceInfo& src, const std::string& errorMsg) 213     auto callback3 = [](const ImageSourceInfo& src, const std::string& errorMsg) {
214         callbackFlag = 3;
215         return;
216     };
217     ctx->notifiers_ = LoadNotifier(std::move(callback1), std::move(callback2), std::move(callback3));
__anon2fe2f2800502() 218     std::function<void()> func = []() {};
219     ctx->pendingMakeCanvasImageTask_ = func;
220     ctx->OnLoadFail();
221     EXPECT_EQ(callbackFlag, 3);
222     ctx->OnDataReady();
223     EXPECT_EQ(callbackFlag, 1);
224     ctx->OnLoadSuccess();
225     EXPECT_EQ(callbackFlag, 2);
226 }
227 
228 /**
229  * @tc.name: ImageProviderTestNg001
230  * @tc.desc: Test ImageProvider Synchronous CreateImageObj failure
231  * @tc.type: FUNC
232  */
233 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg001, TestSize.Level1)
234 {
235     EXPECT_CALL(*g_loader, LoadImageData).Times(1);
236     auto src = ImageSourceInfo(SRC_JPG);
237     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
238     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
239     ctx->LoadImageData();
240 
241     EXPECT_EQ(ctx->syncLoad_, true);
242     EXPECT_EQ(ctx->imageObj_, nullptr);
243     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::DATA_LOADING);
244 
245     auto src1 = ImageSourceInfo(SRC_JPG);
246     src1.srcType_ = SrcType::PIXMAP;
247     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
248     EXPECT_NE(ctx1, nullptr);
249 }
250 
251 /**
252  * @tc.name: CalculateTargetSize001
253  * @tc.desc: Test CalculateTargetSize
254  * @tc.type: FUNC
255  */
256 HWTEST_F(ImageProviderTestNg, CalculateTargetSize001, TestSize.Level1)
257 {
258     auto src = ImageSourceInfo(SRC_JPG);
259     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
260     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
261 
262     SizeF srcSize(1, 1);
263     SizeF dstSize(2, 2);
264     SizeF rawImageSize(3, 3);
265     auto size = ctx->CalculateTargetSize(srcSize, dstSize, rawImageSize);
266     EXPECT_EQ(size, rawImageSize);
267 
268     auto context = PipelineContext::GetCurrentContext();
269     context.Reset();
270 
271     srcSize.SetSizeT(SizeF(-1, -1));
272     size = ctx->CalculateTargetSize(srcSize, dstSize, rawImageSize);
273     EXPECT_EQ(size, rawImageSize);
274 
275     srcSize.SetSizeT(SizeF(4, 4));
276     size = ctx->CalculateTargetSize(srcSize, dstSize, rawImageSize);
277     EXPECT_EQ(size, SizeF(1.5, 1.5));
278 
279     srcSize.SetSizeT(SizeF(4, 1));
280     size = ctx->CalculateTargetSize(srcSize, dstSize, rawImageSize);
281     EXPECT_EQ(size, rawImageSize);
282 
283     srcSize.SetSizeT(SizeF(1, 4));
284     size = ctx->CalculateTargetSize(srcSize, dstSize, rawImageSize);
285     EXPECT_EQ(size, rawImageSize);
286 }
287 
288 /**
289  * @tc.name: SourceSizeTest001
290  * @tc.desc: Test SourceSizeTest
291  * @tc.type: FUNC
292  */
293 HWTEST_F(ImageProviderTestNg, SourceSizeTest001, TestSize.Level1)
294 {
295     auto src = ImageSourceInfo(SRC_JPG);
296     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
297     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
298 
299     ctx->sourceSizePtr_.reset(new SizeF(1, 1));
300     auto size = ctx->GetSourceSize();
301     EXPECT_EQ(size.value(), *ctx->sourceSizePtr_.get());
302 
303     ctx->sourceSizePtr_.reset(new SizeF(-1, 1));
304     size = ctx->GetSourceSize();
305     EXPECT_EQ(size, std::nullopt);
306 
307     ctx->sourceSizePtr_.reset(new SizeF(1, -1));
308     size = ctx->GetSourceSize();
309     EXPECT_EQ(size, std::nullopt);
310 
311     ctx->sourceSizePtr_.reset(new SizeF(-1, -1));
312     size = ctx->GetSourceSize();
313     EXPECT_EQ(size, std::nullopt);
314 
315     ctx->sourceSizePtr_ = nullptr;
316     ctx->SetSourceSize(std::nullopt);
317     EXPECT_EQ(ctx->sourceSizePtr_, nullptr);
318 
319     ctx->SetSourceSize(SizeF(1, 1));
320     EXPECT_EQ(*ctx->sourceSizePtr_.get(), SizeF(1, 1));
321 }
322 
323 /**
324  * @tc.name: GetImageSize001
325  * @tc.desc: Test GetImageSize
326  * @tc.type: FUNC
327  */
328 HWTEST_F(ImageProviderTestNg, GetImageSize001, TestSize.Level1)
329 {
330     auto src = ImageSourceInfo(SRC_JPG);
331     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
332     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
333     auto size = ctx->GetImageSize();
334     EXPECT_EQ(size, SizeF(-1, -1));
335 
336     ctx->imageObj_ =
337         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
338     size = ctx->GetImageSize();
339     EXPECT_EQ(size, SizeF(LENGTH_128, LENGTH_128));
340 }
341 
342 /**
343  * @tc.name: MakeCanvasImageIfNeed001
344  * @tc.desc: Test MakeCanvasImageIfNeed
345  * @tc.type: FUNC
346  */
347 HWTEST_F(ImageProviderTestNg, MakeCanvasImageIfNeed001, TestSize.Level1)
348 {
349     auto src = ImageSourceInfo(SRC_JPG);
350     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
351     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
352 
353     std::vector<ImageFit> imageFitCases = { ImageFit::COVER, ImageFit::FILL };
354     std::vector<std::optional<SizeF>> sourceSizeCases = { std::make_optional(SizeF(1, 1)),
355         std::make_optional(SizeF(2, 2)) };
356     ctx->SetSourceSize(SizeF(1, 1));
357 
358     for (int i = 0; i < 2; ++i) {
359         for (int j = 0; j < 2; ++j) {
360             for (int k = 0; k < 2; ++k) {
361                 if (i == 0 && j == 0 && k == 0) {
362                     ctx->dstSize_ = SizeF(1, 1);
363                     auto ret = ctx->MakeCanvasImageIfNeed(SizeF(1, 1), i, imageFitCases[j], sourceSizeCases[k]);
364                     EXPECT_TRUE(ret);
365                 } else {
366                     ctx->dstSize_ = SizeF();
367                     auto ret = ctx->MakeCanvasImageIfNeed(SizeF(4, 4), i, imageFitCases[j], sourceSizeCases[k]);
368                     EXPECT_TRUE(ret);
369                 }
370             }
371         }
372     }
373 }
374 
375 /**
376  * @tc.name: ImageProviderTestNg002
377  * @tc.desc: Test ImageProvider merging repeated tasks
378  * @tc.type: FUNC
379  */
380 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg002, TestSize.Level1)
381 {
382     EXPECT_CALL(*g_loader, LoadImageData).Times(1);
383     auto src = ImageSourceInfo(SRC_JPG);
384     // create 20 repeated tasks
385     std::vector<RefPtr<ImageLoadingContext>> contexts(20);
386     for (auto& ctx : contexts) {
387         ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), false);
388         ctx->LoadImageData();
389     }
390     // check task map
391     {
392         if (!ImageProvider::taskMtx_.try_lock_for(std::chrono::milliseconds(MAX_WAITING_TIME_FOR_TASKS))) {
393             return;
394         }
395         // Adopt the already acquired lock
396         std::scoped_lock lock(std::adopt_lock, ImageProvider::taskMtx_);
397         EXPECT_EQ(ImageProvider::tasks_.size(), (size_t)1);
398         EXPECT_EQ(ImageProvider::tasks_[src.GetKey()].ctxs_.size(), (size_t)20);
399     }
400     // wait for load task to finish
401     WaitForAsyncTasks();
402     if (!ImageProvider::taskMtx_.try_lock_for(std::chrono::milliseconds(MAX_WAITING_TIME_FOR_TASKS))) {
403         return;
404     }
405     // Adopt the already acquired lock
406     std::scoped_lock lock(std::adopt_lock, ImageProvider::taskMtx_);
407     EXPECT_EQ(ImageProvider::tasks_.size(), (size_t)0);
408 }
409 
410 /**
411  * @tc.name: ImageProviderTestNg003
412  * @tc.desc: Test ImageProvider load thumbnail
413  * @tc.type: FUNC
414  */
415 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg003, TestSize.Level1)
416 {
417     auto src = ImageSourceInfo(SRC_THUMBNAIL);
418 
419     EXPECT_CALL(*g_loader, LoadDecodedImageData).Times(1);
420     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
421     ctx->LoadImageData();
422 }
423 
424 /**
425  * @tc.name: ImageProviderTestNg004
426  * @tc.desc: Test ImageProvider load success
427  * @tc.type: FUNC
428  */
429 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg004, TestSize.Level1)
430 {
431     auto src = ImageSourceInfo(SRC_THUMBNAIL);
432 
433     EXPECT_CALL(*g_loader, LoadDecodedImageData).Times(1);
434     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr));
435     ctx->LoadImageData();
436     EXPECT_EQ(ctx->stateManager_->state_, ImageLoadingState::DATA_LOADING);
437 
438     WaitForAsyncTasks();
439 
440     // mock data loading succeeded
441     ctx->stateManager_->state_ = ImageLoadingState::DATA_LOADING;
442     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
443     SizeF size(LENGTH_100, LENGTH_100);
444     ctx->DataReadyCallback(AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size));
445     EXPECT_EQ(ctx->stateManager_->state_, ImageLoadingState::DATA_READY);
446     EXPECT_TRUE(ctx->NeedAlt());
447     EXPECT_EQ(ctx->GetImageSize(), size);
448 }
449 
450 /**
451  * @tc.name: ImageProviderTestNg005
452  * @tc.desc: Test ImageProvider resize condition
453  * @tc.type: FUNC
454  */
455 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg005, TestSize.Level1)
456 {
457     auto ctx =
458         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
459     ctx->stateManager_->state_ = ImageLoadingState::DATA_READY;
460     ctx->imageObj_ =
461         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
462     SizeF dstSize(LENGTH_100, LENGTH_100);
__anon2fe2f2800602() 463     std::function<void()> func = []() {};
464     ctx->pendingMakeCanvasImageTask_ = func;
465     auto res = ctx->MakeCanvasImageIfNeed(dstSize, true, ImageFit::COVER);
466     EXPECT_TRUE(res);
467     EXPECT_EQ(ctx->dstSize_, SizeF(LENGTH_100, LENGTH_100));
468     EXPECT_EQ(ctx->stateManager_->state_, ImageLoadingState::LOAD_SUCCESS);
469 
470     // call MakeCanvasSize with dstSize in the same level
471     res = ctx->MakeCanvasImageIfNeed(SizeF(LENGTH_65, LENGTH_65), true, ImageFit::COVER);
472     EXPECT_FALSE(res);
473     EXPECT_EQ(ctx->sizeLevel_, LENGTH_128);
474 
475     res = ctx->MakeCanvasImageIfNeed(SizeF(LENGTH_128, LENGTH_128), true, ImageFit::COVER);
476     EXPECT_FALSE(res);
477 
478     res = ctx->MakeCanvasImageIfNeed(SizeF(LENGTH_64, LENGTH_64), true, ImageFit::COVER);
479     EXPECT_TRUE(res);
480     EXPECT_EQ(ctx->sizeLevel_, LENGTH_64);
481 
482     res = ctx->MakeCanvasImageIfNeed(SizeF(LENGTH_63, LENGTH_63), true, ImageFit::COVER);
483     EXPECT_FALSE(res);
484     EXPECT_EQ(ctx->sizeLevel_, LENGTH_64);
485 }
486 
487 /**
488  * @tc.name: ImageProviderTestNg006
489  * @tc.desc: Test BuildImageObject and frame count check
490  * @tc.type: FUNC
491  */
492 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg006, TestSize.Level1)
493 {
494     auto src = ImageSourceInfo(SRC_JPG);
495     EXPECT_FALSE(ImageProvider::BuildImageObject(src, nullptr));
496 
497     auto data = AceType::MakeRefPtr<DrawingImageData>(nullptr, 0);
498     auto imageObject = ImageProvider::BuildImageObject(src, data);
499     EXPECT_TRUE(AceType::DynamicCast<StaticImageObject>(imageObject));
500 
501     data = AceType::MakeRefPtr<DrawingImageData>(nullptr, 2);
502     imageObject = ImageProvider::BuildImageObject(src, data);
503     EXPECT_TRUE(AceType::DynamicCast<AnimatedImageObject>(imageObject));
504 
505     // thumbnail src with mismatched data
506     src = ImageSourceInfo(SRC_THUMBNAIL);
507     EXPECT_FALSE(ImageProvider::BuildImageObject(src, data));
508 }
509 
510 /**
511  * @tc.name: ImageProviderTestNg007
512  * @tc.desc: Test Cancel task
513  * @tc.type: FUNC
514  */
515 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg007, TestSize.Level1)
516 {
517     EXPECT_CALL(*g_loader, LoadImageData).Times(1);
518     auto src = ImageSourceInfo(SRC_JPG);
519     // create 2 repeated tasks
520     std::vector<RefPtr<ImageLoadingContext>> contexts(2);
521     for (auto& ctx : contexts) {
522         ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr));
523         ctx->LoadImageData();
524     }
525     {
526         if (!ImageProvider::taskMtx_.try_lock_for(std::chrono::milliseconds(MAX_WAITING_TIME_FOR_TASKS))) {
527             return;
528         }
529         // Adopt the already acquired lock
530         std::scoped_lock lock(std::adopt_lock, ImageProvider::taskMtx_);
531         EXPECT_EQ(ImageProvider::tasks_.size(), (size_t)1);
532         auto it = ImageProvider::tasks_.find(src.GetKey());
533         EXPECT_NE(it, ImageProvider::tasks_.end());
534         // set impl_ to false to force successful cancellation
535         it->second.bgTask_.impl_ = nullptr;
536     }
537 
538     for (auto& ctx : contexts) {
539         // nullify ctx to trigger task cancel
540         ctx = nullptr;
541     }
542     // check task is successfully canceled
543     {
544         if (!ImageProvider::taskMtx_.try_lock_for(std::chrono::milliseconds(MAX_WAITING_TIME_FOR_TASKS))) {
545             return;
546         }
547         // Adopt the already acquired lock
548         std::scoped_lock lock(std::adopt_lock, ImageProvider::taskMtx_);
549         EXPECT_EQ(ImageProvider::tasks_.size(), (size_t)0);
550     }
551     WaitForAsyncTasks();
552     {
553         if (!ImageProvider::taskMtx_.try_lock_for(std::chrono::milliseconds(MAX_WAITING_TIME_FOR_TASKS))) {
554             return;
555         }
556         // Adopt the already acquired lock
557         std::scoped_lock lock(std::adopt_lock, ImageProvider::taskMtx_);
558         EXPECT_EQ(ImageProvider::tasks_.size(), (size_t)0);
559     }
560 }
561 
562 /**
563  * @tc.name: RoundUp001
564  * @tc.desc: Test RoundUp with invalid input (infinite loop)
565  * @tc.type: FUNC
566  */
567 HWTEST_F(ImageProviderTestNg, RoundUp001, TestSize.Level1)
568 {
569     auto ctx =
570         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
571     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(-1, -1), nullptr);
572     EXPECT_EQ(ctx->RoundUp(-1), -1);
573 
574     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(0, 0), nullptr);
575     EXPECT_EQ(ctx->RoundUp(0), -1);
576 
577     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(-1, -1), nullptr);
578     EXPECT_EQ(ctx->RoundUp(0), -1);
579 
580     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(0, 0), nullptr);
581     EXPECT_EQ(ctx->RoundUp(-1), -1);
582 
583     ctx->imageObj_ =
584         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
585     EXPECT_EQ(ctx->RoundUp(0), -1);
586 
587     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(0, 0), nullptr);
588     EXPECT_EQ(ctx->RoundUp(LENGTH_128), -1);
589 }
590 
591 /**
592  * @tc.name: TargetSize001
593  * @tc.desc: Test calculating targetSize
594  * @tc.type: FUNC
595  */
596 HWTEST_F(ImageProviderTestNg, TargetSize001, TestSize.Level1)
597 {
598     auto ctx =
599         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
600     ctx->dstSize_ = SizeF(200, 200);
601     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(1000, 1000), nullptr);
602     ctx->imageFit_ = ImageFit::NONE;
603     ctx->sourceSizePtr_ = std::make_unique<SizeF>(50, 50);
604     ctx->OnMakeCanvasImage();
605     // canvasKey contains targetSize info
606     EXPECT_EQ(ctx->dstRect_.GetSize(), SizeF(50, 50));
607 
608     ctx->imageFit_ = ImageFit::SCALE_DOWN;
609     ctx->OnMakeCanvasImage();
610     EXPECT_EQ(ctx->dstRect_.GetSize(), SizeF(50, 50));
611 }
612 
613 /**
614  * @tc.name: NotifiersTest002
615  * @tc.desc: Test NotifiersTest.
616  * @tc.type: FUNC
617  */
618 HWTEST_F(ImageProviderTestNg, NotifiersTest002, TestSize.Level1)
619 {
620     /**
621      * @tc.steps: step1. Construction parameters
622      * @tc.expected: ImageSourceInfo.
623      */
624     auto src = ImageSourceInfo(SRC_JPG);
625     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
626     EXPECT_NE(ctx, nullptr);
627 
628     callbackFlag = 0;
__anon2fe2f2800702(const ImageSourceInfo& src) 629     auto callback1 = [](const ImageSourceInfo& src) {
630         callbackFlag = 1;
631         return;
632     };
__anon2fe2f2800802(const ImageSourceInfo& src) 633     auto callback2 = [](const ImageSourceInfo& src) {
634         callbackFlag = 2;
635         return;
636     };
__anon2fe2f2800902(const ImageSourceInfo& src, const std::string& errorMsg) 637     auto callback3 = [](const ImageSourceInfo& src, const std::string& errorMsg) {
638         callbackFlag = 3;
639         return;
640     };
641     /**
642      * @tc.steps: step2. Manage notification tasks for loading data
643      * @tc.expected: LoadNotifier.
644      */
645     ctx->notifiers_ = LoadNotifier(std::move(callback1), std::move(callback2), std::move(callback3));
__anon2fe2f2800a02() 646     std::function<void()> func = []() {};
647     ctx->pendingMakeCanvasImageTask_ = func;
648     /**
649      * @tc.steps: step3. Cleaning or resetting with image loading.
650      * @tc.expected: OnUnloaded.
651      */
652     ctx->OnUnloaded();
653     ctx->OnLoadFail();
654     EXPECT_EQ(callbackFlag, 3);
655     ctx->OnDataReady();
656     EXPECT_EQ(callbackFlag, 1);
657     ctx->OnLoadSuccess();
658     EXPECT_EQ(callbackFlag, 2);
659 }
660 
661 /**
662  * @tc.name: MakeCanvasImage
663  * @tc.desc: Test MakeCanvasImage
664  * @tc.type: FUNC
665  */
666 HWTEST_F(ImageProviderTestNg, MakeCanvasImage, TestSize.Level1)
667 {
668     auto src = ImageSourceInfo(SRC_THUMBNAIL);
669     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
670     SizeF size(LENGTH_100, LENGTH_100);
671     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
672     auto pixmapObj = AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size);
673     pixmapObj->MakeCanvasImage(ctx, size, true, true);
674     EXPECT_NE(ctx->canvasImage_, nullptr);
675 
676     pixmap = nullptr;
677     pixmapObj = AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size);
678     pixmapObj->MakeCanvasImage(ctx, size, true, true);
679     EXPECT_NE(ctx->canvasImage_, nullptr);
680 }
681 
682 /**
683  * @tc.name: CreatePixmap
684  * @tc.desc: Test CreatePixmap
685  * @tc.type: FUNC
686  */
687 HWTEST_F(ImageProviderTestNg, CreatePixmap, TestSize.Level1)
688 {
689     auto src = ImageSourceInfo(SRC_JPG);
690     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
691     auto data = AceType::MakeRefPtr<PixmapData>(pixmap);
692     EXPECT_NE(PixelMapImageObject::Create(src, data), nullptr);
693 }
694 
695 /**
696  * @tc.name: TargetSize002
697  * @tc.desc: Test calculating targetSize
698  * @tc.type: FUNC
699  */
700 HWTEST_F(ImageProviderTestNg, TargetSize002, TestSize.Level1)
701 {
702     /**
703      * @tc.steps1: Construction parameters
704      */
705     RefPtr<NG::CanvasImage> canvasImage;
706     NG::ImagePainter imagePainter(canvasImage);
707     NG::RectF srcRect_;
708     NG::RectF dstRect_;
709     NG::SizeF rawpicsize { 1, 1 };
710     NG::SizeF dstsize { 1, 1 };
711     auto ctx =
712         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
713     ctx->dstSize_ = SizeF(200, 200);
714     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(-1.0, -1.0), nullptr);
715     ctx->imageFit_ = ImageFit::NONE;
716 
717     /**
718      * @tc.steps2: If userDefinedSize is true.SizeF(1, 1).
719      * @tc.expected:EXPECT_EQ(size.value(), *ctx->sourceSizePtr_.get()).
720      */
721     ctx->sourceSizePtr_.reset(new SizeF(1, 1));
722     auto size = ctx->GetSourceSize();
723     EXPECT_EQ(size.value(), *ctx->sourceSizePtr_.get());
724 
725     /**
726      * @tc.steps3: callback ApplyImageFit when ImageFit::NONE.
727      */
728     imagePainter.ApplyImageFit(ImageFit::NONE, rawpicsize, dstsize, srcRect_, dstRect_);
729     NG::SizeF srcSize(std::min(dstsize.Width(), rawpicsize.Width()), std::min(dstsize.Height(), rawpicsize.Height()));
730     auto testSize = Alignment::GetAlignPosition(dstsize, srcSize, Alignment::CENTER);
731     EXPECT_EQ(testSize.GetX(), 0);
732     EXPECT_EQ(testSize.GetY(), 0);
733     auto imageSize = ctx->GetImageSize();
734     EXPECT_EQ(imageSize, SizeF(-1.0, -1.0));
735     bool autoResize = true;
736     bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == ctx->GetSourceInfo().GetSrcType());
737     ctx->ResizableCalcDstSize();
738 
739     /**
740      * @tc.steps4: callback ApplyImageFit when ImageFit::COVER.
741      */
742     ctx->imageFit_ = ImageFit::COVER;
743     imagePainter.ApplyImageFit(ImageFit::COVER, rawpicsize, dstsize, srcRect_, dstRect_);
744     auto testSize1 = Alignment::GetAlignPosition(rawpicsize, srcRect_.GetSize(), Alignment::CENTER);
745     EXPECT_EQ(testSize1.GetX(), 0);
746     EXPECT_EQ(testSize1.GetY(), 0);
747     ctx->ResizableCalcDstSize();
748     EXPECT_EQ(imageSize, SizeF(-1.0, -1.0));
749     EXPECT_TRUE(autoResize);
750     EXPECT_FALSE(isPixelMapResource);
751 }
752 
753 /**
754  * @tc.name: ImageProviderTestNg008
755  * @tc.desc: Test ImageProvider
756  * @tc.type: FUNC
757  */
758 HWTEST_F(ImageProviderTestNg, ImageProviderTestNg008, TestSize.Level1)
759 {
760     /**
761      * @tc.steps: step1. Construction parameters
762      * @tc.expected: ImageSourceInfo.
763      */
764     auto src = ImageSourceInfo(SRC_JPG);
765     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
766     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
767 
768     /**
769      * @tc.steps: step2. Information related to building and managing image loading
770      */
771     ctx->GetDstRect();
772     ctx->GetSrcRect();
773     ctx->MoveCanvasImage();
774     ctx->GetFrameCount();
775 
776     EXPECT_EQ(ctx->syncLoad_, true);
777     EXPECT_EQ(ctx->imageObj_, nullptr);
778 
779     auto src1 = ImageSourceInfo(SRC_JPG);
780     src1.srcType_ = SrcType::PIXMAP;
781     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
782     EXPECT_NE(ctx1, nullptr);
783 }
784 
785 /**
786  * @tc.name: TargetSize003
787  * @tc.desc: Test calculating targetSize
788  * @tc.type: FUNC
789  */
790 HWTEST_F(ImageProviderTestNg, TargetSize003, TestSize.Level1)
791 {
792     /**
793      * @tc.steps1: Construction parameters
794      */
795     RefPtr<NG::CanvasImage> canvasImage;
796     NG::ImagePainter imagePainter(canvasImage);
797     NG::RectF srcRect_;
798     NG::RectF dstRect_;
799     NG::SizeF rawpicsize { 1, 1 };
800     NG::SizeF dstsize { 1, 1 };
801     auto ctx =
802         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
803     ctx->dstSize_ = SizeF(200, 200);
804     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(-1.0, -1.0), nullptr);
805     ctx->imageFit_ = ImageFit::NONE;
806 
807     /**
808      * @tc.steps2: If userDefinedSize is false.Width() <= 0.0.Height() <= 0.0.
809      * @tc.expected: std::nullopt.
810      */
811     ctx->sourceSizePtr_.reset(new SizeF(-1, 1));
812     auto size = ctx->GetSourceSize();
813     EXPECT_EQ(size, std::nullopt);
814 
815     /**
816      * @tc.steps3: callback ApplyImageFit when ImageFit::NONE.
817      */
818     imagePainter.ApplyImageFit(ImageFit::NONE, rawpicsize, dstsize, srcRect_, dstRect_);
819     NG::SizeF srcSize(std::min(dstsize.Width(), rawpicsize.Width()), std::min(dstsize.Height(), rawpicsize.Height()));
820     auto testSize = Alignment::GetAlignPosition(dstsize, srcSize, Alignment::CENTER);
821     EXPECT_EQ(testSize.GetX(), 0);
822     EXPECT_EQ(testSize.GetY(), 0);
823     auto imageSize = ctx->GetImageSize();
824     EXPECT_EQ(imageSize, SizeF(-1.0, -1.0));
825     bool autoResize = true;
826     bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == ctx->GetSourceInfo().GetSrcType());
827     ctx->ResizableCalcDstSize();
828 
829     /**
830      * @tc.steps4: callback ApplyImageFit when ImageFit::COVER.
831      */
832     ctx->imageFit_ = ImageFit::COVER;
833     imagePainter.ApplyImageFit(ImageFit::COVER, rawpicsize, dstsize, srcRect_, dstRect_);
834     auto testSize1 = Alignment::GetAlignPosition(rawpicsize, srcRect_.GetSize(), Alignment::CENTER);
835     EXPECT_EQ(testSize1.GetX(), 0);
836     EXPECT_EQ(testSize1.GetY(), 0);
837     ctx->ResizableCalcDstSize();
838     EXPECT_EQ(imageSize, SizeF(-1.0, -1.0));
839     EXPECT_TRUE(autoResize);
840     EXPECT_FALSE(isPixelMapResource);
841 }
842 
843 /**
844  * @tc.name: HandleCommand002
845  * @tc.desc: Test HandleCommand.
846  * @tc.type: FUNC
847  */
848 HWTEST_F(ImageProviderTestNg, HandleCommand002, TestSize.Level1)
849 {
850     auto src = ImageSourceInfo(SRC_JPG);
851     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
852     ctx->syncLoad_ = true;
853     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(AceType::WeakClaim(AceType::RawPtr(ctx)));
854     stateManager->HandleCommandByUnloadedState(ImageLoadingCommand::LOAD_DATA);
855     EXPECT_EQ(stateManager->state_, ImageLoadingState::DATA_LOADING);
856 
857     stateManager->HandleCommandByUnloadedState(ImageLoadingCommand::RESET_STATE);
858     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
859 
860     stateManager->HandleCommandByUnloadedState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
861     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
862 }
863 
864 /**
865  * @tc.name: HandleCommand003
866  * @tc.desc: Test HandleCommand.
867  * @tc.type: FUNC
868  */
869 HWTEST_F(ImageProviderTestNg, HandleCommand003, TestSize.Level1)
870 {
871     auto src = ImageSourceInfo(SRC_JPG);
872     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
873     ctx->syncLoad_ = true;
874     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(ctx);
875     stateManager->HandleCommandByDataLoadingState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
876     EXPECT_EQ(stateManager->state_, ImageLoadingState::DATA_READY);
877 
878     stateManager->HandleCommandByDataLoadingState(ImageLoadingCommand::LOAD_FAIL);
879     EXPECT_EQ(stateManager->state_, ImageLoadingState::LOAD_FAIL);
880 
881     stateManager->HandleCommandByDataLoadingState(ImageLoadingCommand::RESET_STATE);
882     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
883 
884     stateManager->HandleCommandByDataLoadingState(ImageLoadingCommand::RETRY_LOADING);
885     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
886 }
887 
888 /**
889  * @tc.name: HandleCommand004
890  * @tc.desc: Test HandleCommand.
891  * @tc.type: FUNC
892  */
893 HWTEST_F(ImageProviderTestNg, HandleCommand004, TestSize.Level1)
894 {
895     auto src = ImageSourceInfo(SRC_JPG);
896     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
897     ctx->syncLoad_ = true;
898     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(ctx);
899     stateManager->HandleCommandByDataReadyState(ImageLoadingCommand::MAKE_CANVAS_IMAGE);
900     EXPECT_EQ(stateManager->state_, ImageLoadingState::MAKE_CANVAS_IMAGE);
901 
902     stateManager->HandleCommandByDataReadyState(ImageLoadingCommand::RESET_STATE);
903     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
904 
905     stateManager->HandleCommandByDataReadyState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
906     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
907 }
908 
909 /**
910  * @tc.name: HandleCommand005
911  * @tc.desc: Test HandleCommand.
912  * @tc.type: FUNC
913  */
914 HWTEST_F(ImageProviderTestNg, HandleCommand005, TestSize.Level1)
915 {
916     auto src = ImageSourceInfo(SRC_JPG);
917     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
918     ctx->syncLoad_ = true;
919     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(ctx);
920 
921     stateManager->HandleCommandByMakeCanvasImageState(ImageLoadingCommand::LOAD_FAIL);
922     EXPECT_EQ(stateManager->state_, ImageLoadingState::LOAD_FAIL);
923 
924     stateManager->HandleCommandByMakeCanvasImageState(ImageLoadingCommand::RESET_STATE);
925     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
926 
927     stateManager->HandleCommandByMakeCanvasImageState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
928     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
929 }
930 
931 /**
932  * @tc.name: HandleCommand006
933  * @tc.desc: Test HandleCommand.
934  * @tc.type: FUNC
935  */
936 HWTEST_F(ImageProviderTestNg, HandleCommand006, TestSize.Level1)
937 {
938     auto src = ImageSourceInfo(SRC_JPG);
939     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
940     ctx->syncLoad_ = true;
941     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(ctx);
942     stateManager->HandleCommandByLoadSuccessState(ImageLoadingCommand::MAKE_CANVAS_IMAGE);
943     EXPECT_EQ(stateManager->state_, ImageLoadingState::MAKE_CANVAS_IMAGE);
944 
945     stateManager->HandleCommandByLoadSuccessState(ImageLoadingCommand::RESET_STATE);
946     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
947 
948     stateManager->HandleCommandByLoadSuccessState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
949     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
950 }
951 
952 /**
953  * @tc.name: HandleCommand007
954  * @tc.desc: Test HandleCommand.
955  * @tc.type: FUNC
956  */
957 HWTEST_F(ImageProviderTestNg, HandleCommand007, TestSize.Level1)
958 {
959     auto src = ImageSourceInfo(SRC_JPG);
960     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
961     ctx->syncLoad_ = true;
962     auto stateManager = AceType::MakeRefPtr<ImageStateManager>(ctx);
963 
964     stateManager->HandleCommandByLoadFailState(ImageLoadingCommand::RESET_STATE);
965     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
966 
967     stateManager->HandleCommandByLoadFailState(ImageLoadingCommand::LOAD_DATA_SUCCESS);
968     EXPECT_EQ(stateManager->state_, ImageLoadingState::UNLOADED);
969 }
970 
971 /**
972  * @tc.name: GetCurrentLoadingState001
973  * @tc.desc: Test GetCurrentLoadingState
974  * @tc.type: FUNC
975  */
976 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState001, TestSize.Level1)
977 {
978     auto src = ImageSourceInfo(SRC_JPG);
979     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
980     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
981     ctx->GetCurrentLoadingState();
982 
983     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
984     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
985     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::MAKE_CANVAS_IMAGE);
986 
987     ctx->stateManager_->state_ = ImageLoadingState::LOAD_FAIL;
988     ctx->stateManager_->HandleCommand(ImageLoadingCommand::LOAD_FAIL);
989     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::LOAD_FAIL);
990 }
991 
992 /**
993  * @tc.name: GetCurrentLoadingState002
994  * @tc.desc: Test GetCurrentLoadingState
995  * @tc.type: FUNC
996  */
997 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState002, TestSize.Level1)
998 {
999     auto src = ImageSourceInfo(SRC_JPG);
1000     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1001     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
1002 
1003     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
1004     ctx->stateManager_->state_ = ImageLoadingState::DATA_LOADING;
1005     ctx->GetCurrentLoadingState();
1006     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::DATA_LOADING);
1007 }
1008 
1009 /**
1010  * @tc.name: GetCurrentLoadingState003
1011  * @tc.desc: Test GetCurrentLoadingState
1012  * @tc.type: FUNC
1013  */
1014 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState003, TestSize.Level1)
1015 {
1016     auto src = ImageSourceInfo(SRC_JPG);
1017     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1018     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
1019 
1020     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
1021     ctx->stateManager_->state_ = ImageLoadingState::DATA_READY;
1022     ctx->GetCurrentLoadingState();
1023     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::DATA_READY);
1024 }
1025 
1026 /**
1027  * @tc.name: GetCurrentLoadingState004
1028  * @tc.desc: Test GetCurrentLoadingState
1029  * @tc.type: FUNC
1030  */
1031 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState004, TestSize.Level1)
1032 {
1033     auto src = ImageSourceInfo(SRC_JPG);
1034     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1035     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
1036 
1037     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
1038     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
1039     ctx->GetCurrentLoadingState();
1040     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::MAKE_CANVAS_IMAGE);
1041 }
1042 
1043 /**
1044  * @tc.name: GetCurrentLoadingState005
1045  * @tc.desc: Test GetCurrentLoadingState
1046  * @tc.type: FUNC
1047  */
1048 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState005, TestSize.Level1)
1049 {
1050     auto src = ImageSourceInfo(SRC_JPG);
1051     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1052     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
1053 
1054     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
1055     ctx->stateManager_->state_ = ImageLoadingState::LOAD_SUCCESS;
1056     ctx->GetCurrentLoadingState();
1057     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::LOAD_SUCCESS);
1058 }
1059 
1060 /**
1061  * @tc.name: GetCurrentLoadingState006
1062  * @tc.desc: Test GetCurrentLoadingState
1063  * @tc.type: FUNC
1064  */
1065 HWTEST_F(ImageProviderTestNg, GetCurrentLoadingState006, TestSize.Level1)
1066 {
1067     auto src = ImageSourceInfo(SRC_JPG);
1068     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1069     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::UNLOADED);
1070 
1071     ctx->stateManager_ = AceType::MakeRefPtr<ImageStateManager>(ctx);
1072     ctx->stateManager_->state_ = ImageLoadingState::LOAD_FAIL;
1073     ctx->GetCurrentLoadingState();
1074     EXPECT_EQ(ctx->stateManager_->GetCurrentState(), ImageLoadingState::LOAD_FAIL);
1075 }
1076 
1077 /**
1078  * @tc.name: GetFrameCount001
1079  * @tc.desc: Test GetFrameCount
1080  * @tc.type: FUNC
1081  */
1082 HWTEST_F(ImageProviderTestNg, GetFrameCount001, TestSize.Level1)
1083 {
1084     auto src = ImageSourceInfo(SRC_JPG);
1085     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1086 
1087     ctx->GetFrameCount();
1088     EXPECT_EQ(ctx->imageObj_, nullptr);
1089 
1090     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(1000, 1000), nullptr);
1091     ctx->GetFrameCount();
1092     EXPECT_NE(ctx->imageObj_, nullptr);
1093 }
1094 
1095 /**
1096  * @tc.name: CallbackAfterMeasureIfNeed001
1097  * @tc.desc: Test CallbackAfterMeasureIfNeed
1098  * @tc.type: FUNC
1099  */
1100 HWTEST_F(ImageProviderTestNg, CallbackAfterMeasureIfNeed001, TestSize.Level1)
1101 {
1102     auto src = ImageSourceInfo(SRC_JPG);
1103     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1104 
1105     ctx->needErrorCallBack_ = false;
1106     ctx->CallbackAfterMeasureIfNeed();
1107 
1108     ctx->needErrorCallBack_ = true;
1109     ctx->CallbackAfterMeasureIfNeed();
1110     EXPECT_EQ(ctx->needErrorCallBack_, false);
1111 }
1112 
1113 /**
1114  * @tc.name: CallbackAfterMeasureIfNeed002
1115  * @tc.desc: Test CallbackAfterMeasureIfNeed
1116  * @tc.type: FUNC
1117  */
1118 HWTEST_F(ImageProviderTestNg, CallbackAfterMeasureIfNeed002, TestSize.Level1)
1119 {
1120     auto src = ImageSourceInfo(SRC_JPG);
1121     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1122 
1123     ctx->needErrorCallBack_ = false;
1124     ctx->needDataReadyCallBack_ = false;
1125     ctx->CallbackAfterMeasureIfNeed();
1126 
1127     ctx->needDataReadyCallBack_ = true;
1128     ctx->CallbackAfterMeasureIfNeed();
1129     EXPECT_EQ(ctx->needDataReadyCallBack_, false);
1130 }
1131 
1132 /**
1133  * @tc.name: FailCallback001
1134  * @tc.desc: Test FailCallback
1135  * @tc.type: FUNC
1136  */
1137 HWTEST_F(ImageProviderTestNg, FailCallback001, TestSize.Level1)
1138 {
1139     auto src = ImageSourceInfo(SRC_JPG);
1140     src.srcType_ = SrcType::NETWORK;
1141     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1142 
1143     ctx->measureFinish_ = true;
1144     ctx->FailCallback("test error");
1145     EXPECT_EQ(ctx->needErrorCallBack_, false);
1146 }
1147 
1148 /**
1149  * @tc.name: FailCallback002
1150  * @tc.desc: Test FailCallback
1151  * @tc.type: FUNC
1152  */
1153 HWTEST_F(ImageProviderTestNg, FailCallback002, TestSize.Level1)
1154 {
1155     auto src = ImageSourceInfo(SRC_JPG);
1156     src.srcType_ = SrcType::MEMORY;
1157     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1158 
1159     ctx->measureFinish_ = false;
1160     ctx->FailCallback("test error");
1161     EXPECT_EQ(ctx->Downloadable(), false);
1162 }
1163 
1164 /**
1165  * @tc.name: DataReadyCallback001
1166  * @tc.desc: Test DataReadyCallback
1167  * @tc.type: FUNC
1168  */
1169 HWTEST_F(ImageProviderTestNg, DataReadyCallback001, TestSize.Level1)
1170 {
1171     auto src = ImageSourceInfo(SRC_JPG);
1172     src.srcType_ = SrcType::NETWORK;
1173     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1174 
1175     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
1176     SizeF size(LENGTH_100, LENGTH_100);
1177     ctx->measureFinish_ = true;
1178 
1179     ctx->DataReadyCallback(AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size));
1180     EXPECT_EQ(ctx->GetImageSize(), size);
1181 }
1182 
1183 /**
1184  * @tc.name: ResizableCalcDstSize001
1185  * @tc.desc: Test ResizableCalcDstSize
1186  * @tc.type: FUNC
1187  */
1188 HWTEST_F(ImageProviderTestNg, ResizableCalcDstSize001, TestSize.Level1)
1189 {
1190     auto src = ImageSourceInfo(SRC_JPG);
1191     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1192 
1193     ctx->autoResize_ = true;
1194     bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == ctx->GetSourceInfo().GetSrcType());
1195     ctx->ResizableCalcDstSize();
1196     EXPECT_FALSE(isPixelMapResource);
1197 }
1198 
1199 /**
1200  * @tc.name: ResizableCalcDstSize002
1201  * @tc.desc: Test ResizableCalcDstSize
1202  * @tc.type: FUNC
1203  */
1204 HWTEST_F(ImageProviderTestNg, ResizableCalcDstSize002, TestSize.Level1)
1205 {
1206     auto src = ImageSourceInfo(SRC_JPG);
1207     src.srcType_ = SrcType::DATA_ABILITY_DECODED;
1208     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1209 
1210     ctx->autoResize_ = false;
1211     bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == ctx->GetSourceInfo().GetSrcType());
1212     ctx->ResizableCalcDstSize();
1213     EXPECT_TRUE(isPixelMapResource);
1214 }
1215 
1216 /**
1217  * @tc.name: DownloadOnProgress001
1218  * @tc.desc: Test DownloadOnProgress
1219  * @tc.type: FUNC
1220  */
1221 HWTEST_F(ImageProviderTestNg, DownloadOnProgress001, TestSize.Level1)
1222 {
1223     uint32_t dlNow = 0;
1224     uint32_t dlTotal = 0;
1225     auto src = ImageSourceInfo(SRC_JPG);
1226     src.srcType_ = SrcType::DATA_ABILITY_DECODED;
1227     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1228 
__anon2fe2f2800b02(const uint32_t& dlNow, const uint32_t& dlTotal) 1229     ctx->onProgressCallback_ = [](const uint32_t& dlNow, const uint32_t& dlTotal) {};
1230     ctx->DownloadOnProgress(dlNow, dlTotal);
1231 
1232     ctx->onProgressCallback_ = nullptr;
1233     bool isPixelMapResource = (SrcType::DATA_ABILITY == ctx->GetSourceInfo().GetSrcType());
1234     ctx->DownloadOnProgress(dlNow, dlTotal);
1235     EXPECT_FALSE(isPixelMapResource);
1236 }
1237 
1238 /**
1239  * @tc.name: MakeCanvasImageIfNeed002
1240  * @tc.desc: Test MakeCanvasImageIfNeed
1241  * @tc.type: FUNC
1242  */
1243 HWTEST_F(ImageProviderTestNg, MakeCanvasImageIfNeed002, TestSize.Level1)
1244 {
1245     auto src = ImageSourceInfo(SRC_JPG);
1246     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1247     ctx->stateManager_->state_ = ImageLoadingState::MAKE_CANVAS_IMAGE;
1248     SizeF dstSize(LENGTH_100, LENGTH_100);
__anon2fe2f2800c02() 1249     std::function<void()> func = []() {};
1250     ctx->pendingMakeCanvasImageTask_ = func;
1251     auto res = ctx->MakeCanvasImageIfNeed(dstSize, true, ImageFit::COVER);
1252     EXPECT_TRUE(res);
1253 }
1254 
1255 /**
1256  * @tc.name: MakeCanvasImage001
1257  * @tc.desc: Test MakeCanvasImage
1258  * @tc.type: FUNC
1259  */
1260 HWTEST_F(ImageProviderTestNg, MakeCanvasImage001, TestSize.Level1)
1261 {
1262     auto src = ImageSourceInfo(SRC_THUMBNAIL);
1263     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1264     EXPECT_NE(ctx, nullptr);
1265 
1266     SizeF size(LENGTH_100, LENGTH_100);
1267     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
1268     auto pixmapObj = AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size);
1269     EXPECT_NE(pixmapObj, nullptr);
1270 
1271     int32_t backupApiVersion = AceApplicationInfo::GetInstance().GetApiTargetVersion();
1272     AceApplicationInfo::GetInstance().SetApiTargetVersion(static_cast<int32_t>(PlatformVersion::VERSION_THIRTEEN));
1273 
1274     pixmapObj->MakeCanvasImage(ctx, size, true, true);
1275     EXPECT_NE(ctx->canvasImage_, nullptr);
1276 
1277     pixmapObj = AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size);
1278     EXPECT_NE(pixmapObj, nullptr);
1279 
1280     pixmapObj->MakeCanvasImage(ctx, size, true, false);
1281     EXPECT_NE(ctx->canvasImage_, nullptr);
1282 
1283     AceApplicationInfo::GetInstance().SetApiTargetVersion(backupApiVersion);
1284 
1285     pixmapObj->MakeCanvasImage(ctx, size, true, false);
1286     EXPECT_NE(ctx->canvasImage_, nullptr);
1287 }
1288 
1289 /**
1290  * @tc.name: CreatePixmap001
1291  * @tc.desc: Test PixelMapImageObject Create
1292  * @tc.type: FUNC
1293  */
1294 HWTEST_F(ImageProviderTestNg, CreatePixmap001, TestSize.Level1)
1295 {
1296     auto src = ImageSourceInfo(SRC_THUMBNAIL);
1297 
1298     SizeF size(LENGTH_100, LENGTH_100);
1299     auto pixmap = AceType::MakeRefPtr<MockPixelMap>();
1300     auto pixmapObj = AceType::MakeRefPtr<PixelMapImageObject>(pixmap, src, size);
1301     EXPECT_NE(pixmapObj, nullptr);
1302 
1303     pixmap = nullptr;
1304     auto data = AceType::MakeRefPtr<PixmapData>(pixmap);
1305     EXPECT_NE(data, nullptr);
1306 
1307     auto pixelMapImageObject = pixmapObj->Create(src, data);
1308     EXPECT_EQ(pixelMapImageObject, nullptr);
1309 }
1310 
1311 /**
1312  * @tc.name: ImageLoadingContext001
1313  * @tc.desc: Test ImageLoadingContext.
1314  * @tc.type: FUNC
1315  */
1316 HWTEST_F(ImageProviderTestNg, ImageLoadingContext001, TestSize.Level1)
1317 {
1318     auto src = ImageSourceInfo(SRC_JPG);
1319     src.srcType_ = SrcType::NETWORK;
1320 
1321     int32_t backupApiVersion = AceApplicationInfo::GetInstance().GetApiTargetVersion();
1322     AceApplicationInfo::GetInstance().SetApiTargetVersion(static_cast<int32_t>(PlatformVersion::VERSION_THIRTEEN));
1323 
1324     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1325     EXPECT_NE(ctx, nullptr);
1326 
1327     auto src1 = ImageSourceInfo(SRC_JPG);
1328     src1.srcType_ = SrcType::PIXMAP;
1329 
1330     auto ctx1 = AceType::MakeRefPtr<ImageLoadingContext>(src1, LoadNotifier(nullptr, nullptr, nullptr), true);
1331     EXPECT_NE(ctx1, nullptr);
1332 
1333     AceApplicationInfo::GetInstance().SetApiTargetVersion(backupApiVersion);
1334 
1335     EXPECT_TRUE(ctx1->syncLoad_);
1336 }
1337 
1338 /**
1339  * @tc.name: OnMakeCanvasImage001
1340  * @tc.desc: Test OnMakeCanvasImage
1341  * @tc.type: FUNC
1342  */
1343 HWTEST_F(ImageProviderTestNg, OnMakeCanvasImage001, TestSize.Level1)
1344 {
1345     auto src = ImageSourceInfo(SRC_JPG);
1346     src.srcType_ = SrcType::PIXMAP;
1347 
1348     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1349     EXPECT_NE(ctx, nullptr);
1350 
1351     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(1000, 1000), nullptr);
1352     EXPECT_NE(ctx->imageObj_, nullptr);
1353 
1354     ctx->imageFit_ = ImageFit::NONE;
1355     ctx->sourceSizePtr_ = nullptr;
1356     ctx->dstSize_ = SizeF(200, 200);
1357     ctx->autoResize_ = false;
1358 
1359     ctx->OnMakeCanvasImage();
1360     EXPECT_FALSE(ctx->GetSourceSize());
1361     EXPECT_EQ(ctx->dstRect_.Height(), 200);
1362 }
1363 
1364 /**
1365  * @tc.name: OnMakeCanvasImage002
1366  * @tc.desc: Test OnMakeCanvasImage
1367  * @tc.type: FUNC
1368  */
1369 HWTEST_F(ImageProviderTestNg, OnMakeCanvasImage002, TestSize.Level1)
1370 {
1371     auto src = ImageSourceInfo(SRC_JPG);
1372     src.srcType_ = SrcType::DATA_ABILITY_DECODED;
1373 
1374     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1375     EXPECT_NE(ctx, nullptr);
1376 
1377     ctx->imageObj_ = AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(1000, 1000), nullptr);
1378     EXPECT_NE(ctx->imageObj_, nullptr);
1379 
1380     ctx->sourceSizePtr_ = nullptr;
1381     ctx->dstSize_ = SizeF(200, 200);
1382     ctx->autoResize_ = false;
1383 
1384     ctx->OnMakeCanvasImage();
1385     EXPECT_FALSE(ctx->GetSourceSize());
1386     EXPECT_EQ(ctx->dstRect_.Height(), 200);
1387 
1388     ctx->autoResize_ = true;
1389     ctx->OnMakeCanvasImage();
1390     EXPECT_FALSE(ctx->GetSourceSize());
1391     EXPECT_EQ(ctx->dstRect_.Height(), 200);
1392 }
1393 
1394 /**
1395  * @tc.name: ResizableCalcDstSize003
1396  * @tc.desc: Test ResizableCalcDstSize
1397  * @tc.type: FUNC
1398  */
1399 HWTEST_F(ImageProviderTestNg, ResizableCalcDstSize003, TestSize.Level1)
1400 {
1401     auto src = ImageSourceInfo(SRC_JPG);
1402     src.srcType_ = SrcType::DATA_ABILITY_DECODED;
1403 
1404     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1405     EXPECT_NE(ctx, nullptr);
1406 
1407     ctx->sourceSizePtr_ = nullptr;
1408     ctx->dstSize_ = SizeF(200, 200);
1409 
1410     ctx->ResizableCalcDstSize();
1411     EXPECT_EQ(ctx->GetSourceInfo().GetSrcType(), SrcType::DATA_ABILITY_DECODED);
1412 }
1413 
1414 /**
1415  * @tc.name: ResizableCalcDstSize004
1416  * @tc.desc: Test ResizableCalcDstSize
1417  * @tc.type: FUNC
1418  */
1419 HWTEST_F(ImageProviderTestNg, ResizableCalcDstSize004, TestSize.Level1)
1420 {
1421     auto src = ImageSourceInfo(SRC_JPG);
1422     src.srcType_ = SrcType::PIXMAP;
1423 
1424     auto ctx = AceType::MakeRefPtr<ImageLoadingContext>(src, LoadNotifier(nullptr, nullptr, nullptr), true);
1425     EXPECT_NE(ctx, nullptr);
1426 
1427     ctx->sourceSizePtr_ = nullptr;
1428     ctx->dstSize_ = SizeF(200, 200);
1429     ctx->autoResize_ = false;
1430 
1431     ctx->ResizableCalcDstSize();
1432     EXPECT_EQ(ctx->GetSourceInfo().GetSrcType(), SrcType::PIXMAP);
1433 }
1434 
1435 /**
1436  * @tc.name: MakeCanvasImageIfNeed003
1437  * @tc.desc: Test MakeCanvasImageIfNeed
1438  * @tc.type: FUNC
1439  */
1440 HWTEST_F(ImageProviderTestNg, MakeCanvasImageIfNeed003, TestSize.Level1)
1441 {
1442     auto ctx =
1443         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
1444     EXPECT_NE(ctx, nullptr);
1445 
1446     ctx->imageObj_ =
1447         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
1448     EXPECT_NE(ctx->imageObj_, nullptr);
1449 
1450     SizeF dstSize(LENGTH_100, LENGTH_100);
__anon2fe2f2800d02() 1451     std::function<void()> func = []() {};
1452     ctx->pendingMakeCanvasImageTask_ = func;
1453     ctx->stateManager_->state_ = ImageLoadingState::DATA_READY;
1454 
1455     auto res = ctx->MakeCanvasImageIfNeed(dstSize, true, ImageFit::COVER);
1456     EXPECT_TRUE(res);
1457     EXPECT_EQ(ctx->dstSize_, SizeF(LENGTH_100, LENGTH_100));
1458     EXPECT_EQ(ctx->stateManager_->state_, ImageLoadingState::LOAD_SUCCESS);
1459 
1460     res = ctx->MakeCanvasImageIfNeed(SizeF(LENGTH_65, LENGTH_65), true, ImageFit::COVER, std::nullopt, true);
1461     EXPECT_FALSE(res);
1462     EXPECT_EQ(ctx->sizeLevel_, LENGTH_128);
1463 }
1464 
1465 /**
1466  * @tc.name: MakeCanvasImageIfNeed004
1467  * @tc.desc: Test MakeCanvasImageIfNeed when firstLoadImage_ is false
1468  * @tc.type: FUNC
1469  */
1470 HWTEST_F(ImageProviderTestNg, MakeCanvasImageIfNeed004, TestSize.Level1)
1471 {
1472     auto ctx =
1473         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
1474     EXPECT_NE(ctx, nullptr);
1475 
1476     ctx->imageObj_ =
1477         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
1478     EXPECT_NE(ctx->imageObj_, nullptr);
1479 
1480     SizeF dstSize(LENGTH_100, LENGTH_100);
__anon2fe2f2800e02() 1481     std::function<void()> func = []() {};
1482     ctx->pendingMakeCanvasImageTask_ = func;
1483     ctx->firstLoadImage_ = false;
1484     ctx->stateManager_->state_ = ImageLoadingState::DATA_READY;
1485 
1486     auto res = ctx->MakeCanvasImageIfNeed(dstSize, true, ImageFit::COVER);
1487     EXPECT_TRUE(res);
1488     EXPECT_EQ(ctx->dstSize_, SizeF(LENGTH_100, LENGTH_100));
1489     EXPECT_EQ(ctx->stateManager_->state_, ImageLoadingState::LOAD_SUCCESS);
1490 }
1491 
1492 /**
1493  * @tc.name: GetImageSize002
1494  * @tc.desc: Test GetImageSize
1495  * @tc.type: FUNC
1496  */
1497 HWTEST_F(ImageProviderTestNg, GetImageSize002, TestSize.Level1)
1498 {
1499     auto ctx =
1500         AceType::MakeRefPtr<ImageLoadingContext>(ImageSourceInfo(), LoadNotifier(nullptr, nullptr, nullptr), true);
1501     EXPECT_NE(ctx, nullptr);
1502 
1503     ctx->imageObj_ =
1504         AceType::MakeRefPtr<NG::StaticImageObject>(ImageSourceInfo(SRC_JPG), SizeF(LENGTH_128, LENGTH_128), nullptr);
1505     EXPECT_NE(ctx->imageObj_, nullptr);
1506 
1507     ctx->imageObj_->SetOrientation(ImageRotateOrientation::LEFT);
1508     auto imageSize = ctx->GetImageSize();
1509     EXPECT_EQ(imageSize, SizeF(LENGTH_128, LENGTH_128));
1510 
1511     ctx->imageObj_->SetOrientation(ImageRotateOrientation::RIGHT);
1512     imageSize = ctx->GetImageSize();
1513     EXPECT_EQ(imageSize, SizeF(LENGTH_128, LENGTH_128));
1514 }
1515 } // namespace OHOS::Ace::NG
1516