• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (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 #endif
153 
154 /**
155  * @tc.name: UIImageViewSetSrc_004
156  * @tc.desc: Verify SetSrc function, equal.
157  * @tc.type: FUNC
158  * @tc.require: AR000DSMQ1
159  */
160 HWTEST_F(UIImageViewTest, UIImageViewSetSrc_004, TestSize.Level0)
161 {
162     if (imageView_ == nullptr) {
163         EXPECT_EQ(1, 0);
164         return;
165     }
166     char* srcPath = nullptr;
167     imageView_->SetSrc(srcPath);
168     EXPECT_EQ(imageView_->GetPath(), srcPath);
169     const char* strPath2 = static_cast<const char*>(BLUE_RGB888_IMAGE_PATH);
170     imageView_->SetSrc(strPath2);
171     EXPECT_STREQ(imageView_->GetPath(), strPath2);
172     ImageInfo* srcPath2 = nullptr;
173     imageView_->SetSrc(srcPath2);
174     EXPECT_EQ(imageView_->GetImageInfo(), srcPath2);
175 }
176 
177 /**
178  * @tc.name: UIImageViewSetSrc001
179  * @tc.desc: Verify SetSrc function, equal.
180  * @tc.type: FUNC
181  * @tc.require: SR000FQNFC
182  */
183 HWTEST_F(UIImageViewTest, UIImageViewSetSrc001, TestSize.Level0)
184 {
185     if (imageView_ == nullptr) {
186         EXPECT_EQ(1, 0);
187         return;
188     }
189     const ImageInfo* imageInfo = nullptr;
190     imageView_->SetSrc(imageInfo);
191     EXPECT_EQ(imageView_->GetImageInfo(), nullptr);
192 }
193 
194 /**
195  * @tc.name: UIImageViewSetSrc002
196  * @tc.desc: Verify SetSrc function, equal.
197  * @tc.type: FUNC
198  * @tc.require: AR000FQNFD
199  */
200 HWTEST_F(UIImageViewTest, UIImageViewSetSrc002, TestSize.Level0)
201 {
202     if (imageView_ == nullptr) {
203         EXPECT_EQ(1, 0);
204         return;
205     }
206     const ImageInfo imageInfo {0};
207     imageView_->SetSrc(&imageInfo);
208     EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
209 }
210 
211 /**
212  * @tc.name: UIImageViewSetSrc003
213  * @tc.desc: Verify SetSrc function, equal.
214  * @tc.type: FUNC
215  * @tc.require: AR000FQNFD
216  */
217 HWTEST_F(UIImageViewTest, UIImageViewSetSrc003, TestSize.Level0)
218 {
219     if (imageView_ == nullptr) {
220         EXPECT_EQ(1, 0);
221         return;
222     }
223     ImageHeader header {A8, 0, 0, 0, 0, 0};
224     const ImageInfo imageInfo {header, 0, nullptr, nullptr};
225     imageView_->SetSrc(&imageInfo);
226     EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
227 }
228 
229 /**
230  * @tc.name: UIImageViewSetSrc004
231  * @tc.desc: Verify SetSrc function, equal.
232  * @tc.type: FUNC
233  * @tc.require: AR000FQNFD
234  */
235 HWTEST_F(UIImageViewTest, UIImageViewSetSrc004, TestSize.Level0)
236 {
237     if (imageView_ == nullptr) {
238         EXPECT_EQ(1, 0);
239         return;
240     }
241     ImageHeader header {A8, 0, 0, 0, 0, 0};
242     const ImageInfo imageInfo {header, 0, nullptr, nullptr};
243     imageView_->SetSrc(&imageInfo);
244     ColorMode colorMode = static_cast<ColorMode>(imageView_->GetImageInfo()->header.colorMode);
245     uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(colorMode);
246     EXPECT_EQ(pxSize, 8); // 8 bits
247 }
248 
249 /**
250  * @tc.name: UIImageViewSetAutoEnable_001
251  * @tc.desc: Verify SetAutoEnable function, equal.
252  * @tc.type: FUNC
253  * @tc.require: AR000DSMQ1
254  */
255 HWTEST_F(UIImageViewTest, UIImageViewSetAutoEnable_001, TestSize.Level1)
256 {
257     if (imageView_ == nullptr) {
258         EXPECT_EQ(1, 0);
259         return;
260     }
261     imageView_->SetAutoEnable(true);
262     EXPECT_EQ(imageView_->GetAutoEnable(), true);
263 }
264 
265 /**
266  * @tc.name: UIImageViewSetParent_001
267  * @tc.desc: Verify SetParent function, equal.
268  * @tc.type: FUNC
269  * @tc.require: AR000DSMQ1
270  */
271 HWTEST_F(UIImageViewTest, UIImageViewSetParent_001, TestSize.Level1)
272 {
273     if (imageView_ == nullptr) {
274         EXPECT_EQ(1, 0);
275         return;
276     }
277     UIView uiView;
278     imageView_->SetParent(nullptr);
279     EXPECT_EQ(imageView_->GetParent(), nullptr);
280     imageView_->SetParent(&uiView);
281     EXPECT_NE(imageView_->GetParent(), nullptr);
282 }
283 
284 /**
285  * @tc.name: UIImageViewSetNextSibling_001
286  * @tc.desc: Verify SetNextSibling function, equal.
287  * @tc.type: FUNC
288  * @tc.require: AR000DSMQ1
289  */
290 HWTEST_F(UIImageViewTest, UIImageViewSetNextSibling_001, TestSize.Level1)
291 {
292     if (imageView_ == nullptr) {
293         EXPECT_EQ(1, 0);
294         return;
295     }
296     UIView uiView;
297     imageView_->SetNextSibling(nullptr);
298     EXPECT_EQ(imageView_->GetNextSibling(), nullptr);
299     imageView_->SetNextSibling(&uiView);
300     EXPECT_NE(imageView_->GetNextSibling(), nullptr);
301 }
302 
303 /**
304  * @tc.name: UIImageViewSetVisible_001
305  * @tc.desc: Verify SetVisible function, equal.
306  * @tc.type: FUNC
307  * @tc.require: AR000DSMQ1
308  */
309 HWTEST_F(UIImageViewTest, UIImageViewSetVisible_001, TestSize.Level1)
310 {
311     if (imageView_ == nullptr) {
312         EXPECT_EQ(1, 0);
313         return;
314     }
315     imageView_->SetVisible(true);
316     EXPECT_EQ(imageView_->IsVisible(), true);
317     imageView_->SetVisible(false);
318     EXPECT_EQ(imageView_->IsVisible(), false);
319 }
320 
321 /**
322  * @tc.name: UIImageViewSetTouchable_001
323  * @tc.desc: Verify SetTouchable function, equal.
324  * @tc.type: FUNC
325  * @tc.require: AR000DSMQ1
326  */
327 HWTEST_F(UIImageViewTest, UIImageViewSetTouchable_001, TestSize.Level1)
328 {
329     if (imageView_ == nullptr) {
330         EXPECT_EQ(1, 0);
331         return;
332     }
333     imageView_->SetTouchable(true);
334     EXPECT_EQ(imageView_->IsTouchable(), true);
335     imageView_->SetTouchable(false);
336     EXPECT_EQ(imageView_->IsTouchable(), false);
337 }
338 
339 /**
340  * @tc.name: UIImageViewSetDraggable_001
341  * @tc.desc: Verify SetDraggable function, equal.
342  * @tc.type: FUNC
343  * @tc.require: AR000DSMQ1
344  */
345 HWTEST_F(UIImageViewTest, UIImageViewSetDraggable_001, TestSize.Level0)
346 {
347     if (imageView_ == nullptr) {
348         EXPECT_EQ(1, 0);
349         return;
350     }
351     imageView_->SetDraggable(true);
352     EXPECT_EQ(imageView_->IsDraggable(), true);
353     imageView_->SetDraggable(false);
354     EXPECT_EQ(imageView_->IsDraggable(), false);
355 }
356 
357 /**
358  * @tc.name: UIImageViewSetDragParentInstead_001
359  * @tc.desc: Verify SetDragParentInstead function, equal.
360  * @tc.type: FUNC
361  * @tc.require: AR000DSMQ1
362  */
363 HWTEST_F(UIImageViewTest, UIImageViewSetDragParentInstead_001, TestSize.Level0)
364 {
365     if (imageView_ == nullptr) {
366         EXPECT_EQ(1, 0);
367         return;
368     }
369     imageView_->SetDragParentInstead(true);
370     EXPECT_EQ(imageView_->IsDragParentInstead(), true);
371     imageView_->SetDragParentInstead(false);
372     EXPECT_EQ(imageView_->IsDragParentInstead(), false);
373 }
374 
375 /**
376  * @tc.name: UIImageViewResize_001
377  * @tc.desc: Verify Resize function, equal.
378  * @tc.type: FUNC
379  * @tc.require: AR000DSMQ1
380  */
381 HWTEST_F(UIImageViewTest, UIImageViewResize_001, TestSize.Level0)
382 {
383     if (imageView_ == nullptr) {
384         EXPECT_EQ(1, 0);
385         return;
386     }
387     const int16_t currPosX = 100;
388     const int16_t currPosY = 300;
389 
390     imageView_->Resize(currPosX, currPosY);
391     EXPECT_EQ(imageView_->GetWidth(), currPosX);
392     EXPECT_EQ(imageView_->GetHeight(), currPosY);
393 }
394 
395 /**
396  * @tc.name: UIImageViewSetListener_001
397  * @tc.desc: Verify SetListener function, equal.
398  * @tc.type: FUNC
399  * @tc.require: AR000DSMQ1
400  */
401 HWTEST_F(UIImageViewTest, UIImageViewSetListener_001, TestSize.Level0)
402 {
403     if (imageView_ == nullptr) {
404         EXPECT_EQ(1, 0);
405         return;
406     }
407     imageView_->SetOnDragListener(nullptr);
408     imageView_->SetOnClickListener(nullptr);
409     imageView_->SetOnLongPressListener(nullptr);
410 
411     EXPECT_EQ(imageView_->GetOnDragListener(), nullptr);
412     EXPECT_EQ(imageView_->GetOnClickListener(), nullptr);
413     EXPECT_EQ(imageView_->GetOnLongPressListener(), nullptr);
414 }
415 
416 /**
417  * @tc.name: UIImageViewSetViewId_001
418  * @tc.desc: Verify SetViewId function, equal.
419  * @tc.type: FUNC
420  * @tc.require: AR000DSMQ1
421  */
422 HWTEST_F(UIImageViewTest, UIImageViewSetViewId_001, TestSize.Level0)
423 {
424     if (imageView_ == nullptr) {
425         EXPECT_EQ(1, 0);
426         return;
427     }
428     imageView_->SetViewId(nullptr);
429     EXPECT_EQ(imageView_->GetViewId(), nullptr);
430 }
431 
432 /**
433  * @tc.name: UIImageViewSetViewIndex_001
434  * @tc.desc: Verify SetViewIndex function, equal.
435  * @tc.type: FUNC
436  * @tc.require: AR000DSMQ1
437  */
438 HWTEST_F(UIImageViewTest, UIImageViewSetViewIndex_001, TestSize.Level0)
439 {
440     if (imageView_ == nullptr) {
441         EXPECT_EQ(1, 0);
442         return;
443     }
444     const int16_t index = 101;
445     imageView_->SetViewIndex(index);
446     EXPECT_EQ(imageView_->GetViewIndex(), index);
447 }
448 
449 /**
450  * @tc.name: UIImageViewSetBlurLevel_001
451  * @tc.desc: Verify SetBlurLevel function, equal.
452  * @tc.type: FUNC
453  * @tc.require: AR000DSMQ1
454  */
455 HWTEST_F(UIImageViewTest, UIImageViewSetBlurLevel_001, TestSize.Level0)
456 {
457     if (imageView_ == nullptr) {
458         EXPECT_EQ(1, 0);
459         return;
460     }
461     BlurLevel level = LEVEL0;
462     imageView_->SetBlurLevel(level);
463     EXPECT_EQ(imageView_->GetBlurLevel(), level);
464 }
465 
466 /**
467  * @tc.name: UIImageViewSetTransformAlgorithm_001
468  * @tc.desc: Verify SetTransformAlgorithm function, equal.
469  * @tc.type: FUNC
470  * @tc.require: AR000DSMQ1
471  */
472 HWTEST_F(UIImageViewTest, UIImageViewSetTransformAlgorithm_001, TestSize.Level0)
473 {
474     if (imageView_ == nullptr) {
475         EXPECT_EQ(1, 0);
476         return;
477     }
478     TransformAlgorithm algorithm = NEAREST_NEIGHBOR;
479     imageView_->SetTransformAlgorithm(algorithm);
480     EXPECT_EQ(imageView_->GetTransformAlgorithm(), algorithm);
481 }
482 } // namespace OHOS