1 /*
2  * Copyright (c) 2020-2021 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 "components/ui_image_view.h"
17 #include <climits>
18 #include <gtest/gtest.h>
19 #include "components/root_view.h"
20 #include "components/ui_view_group.h"
21 #include "draw/draw_utils.h"
22 #include "test_resource_config.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 class UIImageViewTest : public testing::Test {
28 public:
UIImageViewTest()29     UIImageViewTest() : imageView_(nullptr) {}
~UIImageViewTest()30     ~UIImageViewTest() {}
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33     void SetUp(void);
34     void TearDown(void);
35     UIImageView* imageView_;
36 };
37 
SetUp(void)38 void UIImageViewTest::SetUp(void)
39 {
40     if (imageView_ == nullptr) {
41         imageView_ = new UIImageView();
42     }
43 }
44 
TearDown(void)45 void UIImageViewTest::TearDown(void)
46 {
47     if (imageView_ != nullptr) {
48         delete imageView_;
49         imageView_ = nullptr;
50     }
51 }
52 
53 /**
54  * @tc.name: UIImageViewGetViewType_001
55  * @tc.desc: Verify GetViewType function, equal.
56  * @tc.type: FUNC
57  * @tc.require: AR000DSMQ1
58  */
59 HWTEST_F(UIImageViewTest, UIImageViewGetViewType_001, TestSize.Level1)
60 {
61     if (imageView_ == nullptr) {
62         EXPECT_EQ(1, 0);
63         return;
64     }
65     EXPECT_EQ(imageView_->GetViewType(), UI_IMAGE_VIEW);
66 }
67 
68 /**
69  * @tc.name: UIImageViewSetPosition_001
70  * @tc.desc: Verify SetPosition function, equal.
71  * @tc.type: FUNC
72  * @tc.require: AR000DSMQ1
73  */
74 HWTEST_F(UIImageViewTest, UIImageViewSetPosition_001, TestSize.Level0)
75 {
76     if (imageView_ == nullptr) {
77         EXPECT_EQ(1, 0);
78         return;
79     }
80     const int16_t initPosX = 10;
81     const int16_t initPosY = 10;
82     const int16_t initWidth = 100;
83     const int16_t initHeight = 150;
84 
85     imageView_->SetPosition(initPosX, initPosY);
86     imageView_->SetHeight(initWidth);
87     imageView_->SetWidth(initHeight);
88 
89     EXPECT_EQ(imageView_->GetX(), initPosX);
90     EXPECT_EQ(imageView_->GetY(), initPosY);
91     EXPECT_EQ(imageView_->GetHeight(), initWidth);
92     EXPECT_EQ(imageView_->GetWidth(), initHeight);
93 
94     imageView_->SetPosition(0, 0, 0, 0);
95     EXPECT_EQ(imageView_->GetX(), 0);
96     EXPECT_EQ(imageView_->GetY(), 0);
97     EXPECT_EQ(imageView_->GetWidth(), 0);
98     EXPECT_EQ(imageView_->GetHeight(), 0);
99 }
100 
101 #if defined(ENABLE_GIF) && (ENABLE_GIF == 1)
102 /**
103  * @tc.name: UIImageViewSetSrc_001
104  * @tc.desc: Verify SetSrc function, correct gif path, equal.
105  * @tc.type: FUNC
106  * @tc.require: SR000F3PEO
107  */
108 HWTEST_F(UIImageViewTest, UIImageViewSetSrc_001, TestSize.Level1)
109 {
110     if (imageView_ == nullptr) {
111         EXPECT_EQ(1, 0);
112         return;
113     }
114     const char* strPath1 = static_cast<const char*>(GIF_IMAGE_PATH1);
115     imageView_->SetSrc(strPath1);
116     EXPECT_EQ(imageView_->GetPath(), nullptr);
117 }
118 
119 /**
120  * @tc.name: UIImageViewSetSrc_002
121  * @tc.desc: Verify SetSrc function, error gif path, equal.
122  * @tc.type: FUNC
123  * @tc.require: AR000F3R70
124  */
125 HWTEST_F(UIImageViewTest, UIImageViewSetSrc_002, TestSize.Level1)
126 {
127     if (imageView_ == nullptr) {
128         EXPECT_EQ(1, 0);
129         return;
130     }
131     const char* strPathError = static_cast<const char*>(GIF_IMAGE_PATH_ERROR);
132     imageView_->SetSrc(strPathError);
133     EXPECT_EQ(imageView_->GetPath(), nullptr);
134 }
135 
136 /**
137  * @tc.name: UIImageViewSetSrc_003
138  * @tc.desc: Verify SetSrc function, tif image format path, equal.
139  * @tc.type: FUNC
140  * @tc.require: AR000F3R70
141  */
142 HWTEST_F(UIImageViewTest, UIImageViewSetSrc_003, TestSize.Level1)
143 {
144     if (imageView_ == nullptr) {
145         EXPECT_EQ(1, 0);
146         return;
147     }
148     const char* strPath = static_cast<const char*>(TIF_IMAGE_PATH);
149     imageView_->SetSrc(strPath);
150     EXPECT_STREQ(imageView_->GetPath(), strPath);
151 }
152 
153 /**
154  * @tc.name: UIImageViewGetGifImageAnimator_001
155  * @tc.desc: Verify GetGifImageAnimator function, equal.
156  */
157 HWTEST_F(UIImageViewTest, UIImageViewGetGifImageAnimator_001, TestSize.Level0)
158 {
159     if (imageView_ == nullptr) {
160         EXPECT_EQ(1, 0);
161         return;
162     }
163     EXPECT_EQ(imageView_->GetGifImageAnimator(), nullptr);
164 }
165 #endif
166 
167 /**
168  * @tc.name: UIImageViewSetSrc_004
169  * @tc.desc: Verify SetSrc function, equal.
170  * @tc.type: FUNC
171  * @tc.require: AR000DSMQ1
172  */
173 HWTEST_F(UIImageViewTest, UIImageViewSetSrc_004, TestSize.Level0)
174 {
175     if (imageView_ == nullptr) {
176         EXPECT_EQ(1, 0);
177         return;
178     }
179     char* srcPath = nullptr;
180     imageView_->SetSrc(srcPath);
181     EXPECT_EQ(imageView_->GetPath(), srcPath);
182     const char* strPath2 = static_cast<const char*>(BLUE_RGB888_IMAGE_PATH);
183     imageView_->SetSrc(strPath2);
184     EXPECT_STREQ(imageView_->GetPath(), strPath2);
185     ImageInfo* srcPath2 = nullptr;
186     imageView_->SetSrc(srcPath2);
187     EXPECT_EQ(imageView_->GetImageInfo(), srcPath2);
188 }
189 
190 /**
191  * @tc.name: UIImageViewSetSrc001
192  * @tc.desc: Verify SetSrc function, equal.
193  * @tc.type: FUNC
194  * @tc.require: SR000FQNFC
195  */
196 HWTEST_F(UIImageViewTest, UIImageViewSetSrc001, TestSize.Level0)
197 {
198     if (imageView_ == nullptr) {
199         EXPECT_EQ(1, 0);
200         return;
201     }
202     const ImageInfo* imageInfo = nullptr;
203     imageView_->SetSrc(imageInfo);
204     EXPECT_EQ(imageView_->GetImageInfo(), nullptr);
205 }
206 
207 /**
208  * @tc.name: UIImageViewSetSrc002
209  * @tc.desc: Verify SetSrc function, equal.
210  * @tc.type: FUNC
211  * @tc.require: AR000FQNFD
212  */
213 HWTEST_F(UIImageViewTest, UIImageViewSetSrc002, TestSize.Level0)
214 {
215     if (imageView_ == nullptr) {
216         EXPECT_EQ(1, 0);
217         return;
218     }
219     const ImageInfo imageInfo {0};
220     imageView_->SetSrc(&imageInfo);
221     EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
222 }
223 
224 /**
225  * @tc.name: UIImageViewSetSrc003
226  * @tc.desc: Verify SetSrc function, equal.
227  * @tc.type: FUNC
228  * @tc.require: AR000FQNFD
229  */
230 HWTEST_F(UIImageViewTest, UIImageViewSetSrc003, TestSize.Level0)
231 {
232     if (imageView_ == nullptr) {
233         EXPECT_EQ(1, 0);
234         return;
235     }
236     ImageHeader header {A8, 0, 0, 0, 0, 0};
237     const ImageInfo imageInfo {header, 0, nullptr, nullptr};
238     imageView_->SetSrc(&imageInfo);
239     EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
240 }
241 
242 /**
243  * @tc.name: UIImageViewSetSrc004
244  * @tc.desc: Verify SetSrc function, equal.
245  * @tc.type: FUNC
246  * @tc.require: AR000FQNFD
247  */
248 HWTEST_F(UIImageViewTest, UIImageViewSetSrc004, TestSize.Level0)
249 {
250     if (imageView_ == nullptr) {
251         EXPECT_EQ(1, 0);
252         return;
253     }
254     ImageHeader header {A8, 0, 0, 0, 0, 0};
255     const ImageInfo imageInfo {header, 0, nullptr, nullptr};
256     imageView_->SetSrc(&imageInfo);
257     ColorMode colorMode = static_cast<ColorMode>(imageView_->GetImageInfo()->header.colorMode);
258     uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(colorMode);
259     EXPECT_EQ(pxSize, 8); // 8 bits
260 }
261 
262 /**
263  * @tc.name: UIImageViewSetAutoEnable_001
264  * @tc.desc: Verify SetAutoEnable function, equal.
265  * @tc.type: FUNC
266  * @tc.require: AR000DSMQ1
267  */
268 HWTEST_F(UIImageViewTest, UIImageViewSetAutoEnable_001, TestSize.Level1)
269 {
270     if (imageView_ == nullptr) {
271         EXPECT_EQ(1, 0);
272         return;
273     }
274     imageView_->SetAutoEnable(true);
275     EXPECT_EQ(imageView_->GetAutoEnable(), true);
276 }
277 
278 /**
279  * @tc.name: UIImageViewSetParent_001
280  * @tc.desc: Verify SetParent function, equal.
281  * @tc.type: FUNC
282  * @tc.require: AR000DSMQ1
283  */
284 HWTEST_F(UIImageViewTest, UIImageViewSetParent_001, TestSize.Level1)
285 {
286     if (imageView_ == nullptr) {
287         EXPECT_EQ(1, 0);
288         return;
289     }
290     UIView uiView;
291     imageView_->SetParent(nullptr);
292     EXPECT_EQ(imageView_->GetParent(), nullptr);
293     imageView_->SetParent(&uiView);
294     EXPECT_NE(imageView_->GetParent(), nullptr);
295 }
296 
297 /**
298  * @tc.name: UIImageViewSetNextSibling_001
299  * @tc.desc: Verify SetNextSibling function, equal.
300  * @tc.type: FUNC
301  * @tc.require: AR000DSMQ1
302  */
303 HWTEST_F(UIImageViewTest, UIImageViewSetNextSibling_001, TestSize.Level1)
304 {
305     if (imageView_ == nullptr) {
306         EXPECT_EQ(1, 0);
307         return;
308     }
309     UIView uiView;
310     imageView_->SetNextSibling(nullptr);
311     EXPECT_EQ(imageView_->GetNextSibling(), nullptr);
312     imageView_->SetNextSibling(&uiView);
313     EXPECT_NE(imageView_->GetNextSibling(), nullptr);
314 }
315 
316 /**
317  * @tc.name: UIImageViewSetVisible_001
318  * @tc.desc: Verify SetVisible function, equal.
319  * @tc.type: FUNC
320  * @tc.require: AR000DSMQ1
321  */
322 HWTEST_F(UIImageViewTest, UIImageViewSetVisible_001, TestSize.Level1)
323 {
324     if (imageView_ == nullptr) {
325         EXPECT_EQ(1, 0);
326         return;
327     }
328     imageView_->SetVisible(true);
329     EXPECT_EQ(imageView_->IsVisible(), true);
330     imageView_->SetVisible(false);
331     EXPECT_EQ(imageView_->IsVisible(), false);
332 }
333 
334 /**
335  * @tc.name: UIImageViewSetTouchable_001
336  * @tc.desc: Verify SetTouchable function, equal.
337  * @tc.type: FUNC
338  * @tc.require: AR000DSMQ1
339  */
340 HWTEST_F(UIImageViewTest, UIImageViewSetTouchable_001, TestSize.Level1)
341 {
342     if (imageView_ == nullptr) {
343         EXPECT_EQ(1, 0);
344         return;
345     }
346     imageView_->SetTouchable(true);
347     EXPECT_EQ(imageView_->IsTouchable(), true);
348     imageView_->SetTouchable(false);
349     EXPECT_EQ(imageView_->IsTouchable(), false);
350 }
351 
352 /**
353  * @tc.name: UIImageViewSetDraggable_001
354  * @tc.desc: Verify SetDraggable function, equal.
355  * @tc.type: FUNC
356  * @tc.require: AR000DSMQ1
357  */
358 HWTEST_F(UIImageViewTest, UIImageViewSetDraggable_001, TestSize.Level0)
359 {
360     if (imageView_ == nullptr) {
361         EXPECT_EQ(1, 0);
362         return;
363     }
364     imageView_->SetDraggable(true);
365     EXPECT_EQ(imageView_->IsDraggable(), true);
366     imageView_->SetDraggable(false);
367     EXPECT_EQ(imageView_->IsDraggable(), false);
368 }
369 
370 /**
371  * @tc.name: UIImageViewSetDragParentInstead_001
372  * @tc.desc: Verify SetDragParentInstead function, equal.
373  * @tc.type: FUNC
374  * @tc.require: AR000DSMQ1
375  */
376 HWTEST_F(UIImageViewTest, UIImageViewSetDragParentInstead_001, TestSize.Level0)
377 {
378     if (imageView_ == nullptr) {
379         EXPECT_EQ(1, 0);
380         return;
381     }
382     imageView_->SetDragParentInstead(true);
383     EXPECT_EQ(imageView_->IsDragParentInstead(), true);
384     imageView_->SetDragParentInstead(false);
385     EXPECT_EQ(imageView_->IsDragParentInstead(), false);
386 }
387 
388 /**
389  * @tc.name: UIImageViewResize_001
390  * @tc.desc: Verify Resize function, equal.
391  * @tc.type: FUNC
392  * @tc.require: AR000DSMQ1
393  */
394 HWTEST_F(UIImageViewTest, UIImageViewResize_001, TestSize.Level0)
395 {
396     if (imageView_ == nullptr) {
397         EXPECT_EQ(1, 0);
398         return;
399     }
400     const int16_t currPosX = 100;
401     const int16_t currPosY = 300;
402 
403     imageView_->Resize(currPosX, currPosY);
404     EXPECT_EQ(imageView_->GetWidth(), currPosX);
405     EXPECT_EQ(imageView_->GetHeight(), currPosY);
406 }
407 
408 /**
409  * @tc.name: UIImageViewSetListener_001
410  * @tc.desc: Verify SetListener function, equal.
411  * @tc.type: FUNC
412  * @tc.require: AR000DSMQ1
413  */
414 HWTEST_F(UIImageViewTest, UIImageViewSetListener_001, TestSize.Level0)
415 {
416     if (imageView_ == nullptr) {
417         EXPECT_EQ(1, 0);
418         return;
419     }
420     imageView_->SetOnDragListener(nullptr);
421     imageView_->SetOnClickListener(nullptr);
422     imageView_->SetOnLongPressListener(nullptr);
423 
424     EXPECT_EQ(imageView_->GetOnDragListener(), nullptr);
425     EXPECT_EQ(imageView_->GetOnClickListener(), nullptr);
426     EXPECT_EQ(imageView_->GetOnLongPressListener(), nullptr);
427 }
428 
429 /**
430  * @tc.name: UIImageViewSetViewId_001
431  * @tc.desc: Verify SetViewId function, equal.
432  * @tc.type: FUNC
433  * @tc.require: AR000DSMQ1
434  */
435 HWTEST_F(UIImageViewTest, UIImageViewSetViewId_001, TestSize.Level0)
436 {
437     if (imageView_ == nullptr) {
438         EXPECT_EQ(1, 0);
439         return;
440     }
441     imageView_->SetViewId(nullptr);
442     EXPECT_EQ(imageView_->GetViewId(), nullptr);
443 }
444 
445 /**
446  * @tc.name: UIImageViewSetViewIndex_001
447  * @tc.desc: Verify SetViewIndex function, equal.
448  * @tc.type: FUNC
449  * @tc.require: AR000DSMQ1
450  */
451 HWTEST_F(UIImageViewTest, UIImageViewSetViewIndex_001, TestSize.Level0)
452 {
453     if (imageView_ == nullptr) {
454         EXPECT_EQ(1, 0);
455         return;
456     }
457     const int16_t index = 101;
458     imageView_->SetViewIndex(index);
459     EXPECT_EQ(imageView_->GetViewIndex(), index);
460 }
461 
462 /**
463  * @tc.name: UIImageViewSetBlurLevel_001
464  * @tc.desc: Verify SetBlurLevel function, equal.
465  * @tc.type: FUNC
466  * @tc.require: AR000DSMQ1
467  */
468 HWTEST_F(UIImageViewTest, UIImageViewSetBlurLevel_001, TestSize.Level0)
469 {
470     if (imageView_ == nullptr) {
471         EXPECT_EQ(1, 0);
472         return;
473     }
474     BlurLevel level = LEVEL0;
475     imageView_->SetBlurLevel(level);
476     EXPECT_EQ(imageView_->GetBlurLevel(), level);
477 }
478 
479 /**
480  * @tc.name: UIImageViewSetTransformAlgorithm_001
481  * @tc.desc: Verify SetTransformAlgorithm function, equal.
482  * @tc.type: FUNC
483  * @tc.require: AR000DSMQ1
484  */
485 HWTEST_F(UIImageViewTest, UIImageViewSetTransformAlgorithm_001, TestSize.Level0)
486 {
487     if (imageView_ == nullptr) {
488         EXPECT_EQ(1, 0);
489         return;
490     }
491     TransformAlgorithm algorithm = NEAREST_NEIGHBOR;
492     imageView_->SetTransformAlgorithm(algorithm);
493     EXPECT_EQ(imageView_->GetTransformAlgorithm(), algorithm);
494 }
495 
496 /**
497  * @tc.name: UIImageViewOnPreDraw_001
498  * @tc.desc: Verify OnPreDraw function, equal.
499  */
500 HWTEST_F(UIImageViewTest, UIImageViewOnPreDraw_001, TestSize.Level0)
501 {
502     if (imageView_ == nullptr) {
503         EXPECT_EQ(1, 0);
504         return;
505     }
506     Rect* invalidatedArea = new Rect();
507     EXPECT_EQ(imageView_->OnPreDraw(*invalidatedArea), true);
508     delete invalidatedArea;
509     invalidatedArea = nullptr;
510 }
511 
512 /**
513  * @tc.name: UIImageViewGetSrcType_001
514  * @tc.desc: Verify GetSrcType function, equal.
515  */
516 HWTEST_F(UIImageViewTest, UIImageViewGetSrcType_001, TestSize.Level0)
517 {
518     if (imageView_ == nullptr) {
519         EXPECT_EQ(1, 0);
520         return;
521     }
522     char* srcPath = nullptr;
523     imageView_->SetSrc(srcPath);
524     EXPECT_EQ(imageView_->GetSrcType(), 2); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
525 }
526 
527 /**
528  * @tc.name: UIImageViewSetResizeMode_001
529  * @tc.desc: Verify SetResizeMode function, equal.
530  */
531 HWTEST_F(UIImageViewTest, UIImageViewSetResizeMode_001, TestSize.Level0)
532 {
533     if (imageView_ == nullptr) {
534         EXPECT_EQ(1, 0);
535         return;
536     }
537     UIImageView::ImageResizeMode mode = UIImageView::NONE;
538     imageView_->SetResizeMode(mode);
539     EXPECT_EQ(imageView_->GetResizeMode(), mode); // 0 : IMG_SRC_VARIABLE 1 : IMG_SRC_FILE 2 : IMG_SRC_UNKNOWN
540 }
541 } // namespace OHOS
542