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_animator.h"
17  
18  #include <climits>
19  #include <gtest/gtest.h>
20  #include "gfx_utils/file.h"
21  #include "test_resource_config.h"
22  
23  using namespace testing::ext;
24  namespace OHOS {
25  namespace {
26      const Point INIT_POS = { 50, 50 };
27      const int16_t INIT_WIDTH = 94;
28      const int16_t INIT_HEIGHT = 94;
29      const uint8_t IMAGE_SIZE = 3;
30  }
31  
32  class UIImageAnimatorViewTest : public testing::Test {
33  public:
34      static void SetUpTestCase(void);
35      static void TearDownTestCase(void);
36      static UIImageAnimatorView* imageAnimator_;
37  };
38  
39  UIImageAnimatorView* UIImageAnimatorViewTest::imageAnimator_ = nullptr;
40  
SetUpTestCase(void)41  void UIImageAnimatorViewTest::SetUpTestCase(void)
42  {
43      if (imageAnimator_ == nullptr) {
44          imageAnimator_ = new UIImageAnimatorView();
45      }
46  }
47  
TearDownTestCase(void)48  void UIImageAnimatorViewTest::TearDownTestCase(void)
49  {
50      if (imageAnimator_ != nullptr) {
51          delete imageAnimator_;
52          imageAnimator_ = nullptr;
53      }
54  }
55  
56  static ImageAnimatorInfo g_imageAnimatorInfo[IMAGE_SIZE] = {
57      { BLUE_RGB888_IMAGE_PATH, INIT_POS, INIT_WIDTH, INIT_HEIGHT, IMG_SRC_FILE_PATH },
58      { BLUE_ARGB8888_IMAGE_PATH, INIT_POS, INIT_WIDTH, INIT_HEIGHT, IMG_SRC_FILE_PATH },
59      { BLUE_RGB565_IMAGE_PATH, INIT_POS, INIT_WIDTH, INIT_HEIGHT, IMG_SRC_FILE_PATH },
60  };
61  
62  /**
63   * @tc.name: UIImageAnimatorViewGetViewType_001
64   * @tc.desc: Verify GetViewType function, equal.
65   * @tc.type: FUNC
66   * @tc.require: AR000DSMQL
67   */
68  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewGetViewType_001, TestSize.Level1)
69  {
70      if (imageAnimator_ == nullptr) {
71          EXPECT_EQ(1, 0);
72          return;
73      }
74      EXPECT_EQ(imageAnimator_->GetViewType(), UI_IMAGE_ANIMATOR_VIEW);
75  }
76  
77  /**
78   * @tc.name: UIImageAnimatorViewSetTimeOfPause_001
79   * @tc.desc: Verify SetTimeOfPause function, equal.
80   * @tc.type: FUNC
81   * @tc.require: AR000DSMQL
82   */
83  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetTimeOfPause_001, TestSize.Level1)
84  {
85      if (imageAnimator_ == nullptr) {
86          EXPECT_EQ(1, 0);
87          return;
88      }
89      uint16_t timeOfPause = 1;
90  
91      imageAnimator_->SetTimeOfPause(timeOfPause);
92      EXPECT_EQ(imageAnimator_->GetTimeOfPause(), timeOfPause);
93  }
94  
95  /**
96   * @tc.name: UIImageAnimatorViewSetTimeOfUpdate_001
97   * @tc.desc: Verify SetTimeOfUpdate function, equal.
98   * @tc.type: FUNC
99   * @tc.require: AR000DSMQL
100   */
101  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetTimeOfUpdate_001, TestSize.Level1)
102  {
103      if (imageAnimator_ == nullptr) {
104          EXPECT_EQ(1, 0);
105          return;
106      }
107      const uint16_t animatorTime = 300;
108  
109      imageAnimator_->SetTimeOfUpdate(animatorTime);
110      EXPECT_EQ(imageAnimator_->GetTimeOfUpdate(), animatorTime);
111  }
112  
113  /**
114   * @tc.name: UIImageAnimatorViewSetImageAnimatorSrc_001
115   * @tc.desc: Verify SetImageAnimatorSrc function, equal.
116   * @tc.type: FUNC
117   * @tc.require: AR000DSMQL
118   */
119  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetImageAnimatorSrc_001, TestSize.Level1)
120  {
121      if (imageAnimator_ == nullptr) {
122          EXPECT_EQ(1, 0);
123          return;
124      }
125      const uint8_t imageSize = IMAGE_SIZE;
126  
127      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, imageSize);
128      EXPECT_EQ(imageAnimator_->GetImageAnimatorImageNum(), imageSize);
129  }
130  
131  /**
132   * @tc.name: UIImageAnimatorViewSetImageAnimatorSrc_002
133   * @tc.desc: Verify SetImageAnimatorSrc function, equal.
134   * @tc.type: FUNC
135   * @tc.require: AR000DSMPV
136   */
137  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetImageAnimatorSrc_002, TestSize.Level1)
138  {
139      if (imageAnimator_ == nullptr) {
140          EXPECT_EQ(1, 0);
141          return;
142      }
143      ImageAnimatorInfo* info = &g_imageAnimatorInfo[0];
144      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_SIZE);
145  
146      EXPECT_EQ(imageAnimator_->GetImageAnimatorSrc(), info);
147  }
148  
149  /**
150   * @tc.name: UIImageAnimatorViewSetSizeFixed_001
151   * @tc.desc: Verify SetSizeFixed function, equal.
152   * @tc.type: FUNC
153   * @tc.require: AR000DSMPV
154   */
155  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetSizeFixed_001, TestSize.Level1)
156  {
157      if (imageAnimator_ == nullptr) {
158          EXPECT_EQ(1, 0);
159          return;
160      }
161      bool fixed = true;
162  
163      imageAnimator_->SetSizeFixed(fixed);
164      EXPECT_EQ(imageAnimator_->IsSizeFixed(), fixed);
165  }
166  
167  /**
168   * @tc.name: UIImageAnimatorViewSetRepeat_001
169   * @tc.desc: Verify SetRepeat function, equal.
170   * @tc.type: FUNC
171   * @tc.require: AR000DSMPV
172   */
173  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetRepeat_001, TestSize.Level1)
174  {
175      if (imageAnimator_ == nullptr) {
176          EXPECT_EQ(1, 0);
177          return;
178      }
179      bool repeat = true;
180  
181      imageAnimator_->SetRepeat(repeat);
182      EXPECT_EQ(imageAnimator_->IsRepeat(), repeat);
183  }
184  
185  /**
186   * @tc.name: UIImageAnimatorViewSetReverse_001
187   * @tc.desc: Verify SetReverse function, equal.
188   * @tc.type: FUNC
189   * @tc.require: AR000DSMPV
190   */
191  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetReverse_001, TestSize.Level1)
192  {
193      if (imageAnimator_ == nullptr) {
194          EXPECT_EQ(1, 0);
195          return;
196      }
197      bool reverse = true;
198  
199      imageAnimator_->SetReverse(reverse);
200      EXPECT_EQ(imageAnimator_->IsReverse(), reverse);
201  }
202  
203  /**
204   * @tc.name: UIImageAnimatorViewSetStartPosition_001
205   * @tc.desc: Verify SetStartPosition function, equal.
206   * @tc.type: FUNC
207   * @tc.require: AR000DSMPV
208   */
209  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetStartPosition_001, TestSize.Level1)
210  {
211      if (imageAnimator_ == nullptr) {
212          EXPECT_EQ(1, 0);
213          return;
214      }
215      uint8_t state = Animator::STOP;
216  
217      EXPECT_EQ(imageAnimator_->GetState(), state);
218  }
219  
220  /**
221   * @tc.name: UIImageAnimatorViewStart_010
222   * @tc.desc: Verify Start function, equal.
223   * @tc.type: FUNC
224   * @tc.require: AR000DSMPV
225   */
226  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewStart_001, TestSize.Level1)
227  {
228      if (imageAnimator_ == nullptr) {
229          EXPECT_EQ(1, 0);
230          return;
231      }
232      imageAnimator_->Start();
233      EXPECT_EQ(imageAnimator_->GetState(), Animator::START);
234  }
235  
236  /**
237   * @tc.name: UIImageAnimatorViewStop_001
238   * @tc.desc: Verify Stop function, equal.
239   * @tc.type: FUNC
240   * @tc.require: AR000DSMPV
241   */
242  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewStop_001, TestSize.Level1)
243  {
244      if (imageAnimator_ == nullptr) {
245          EXPECT_EQ(1, 0);
246          return;
247      }
248      imageAnimator_->Stop();
249      EXPECT_EQ(imageAnimator_->GetState(), Animator::STOP);
250  }
251  
252  /**
253   * @tc.name: UIImageAnimatorViewPause_001
254   * @tc.desc: Verify Pause function, equal.
255   * @tc.type: FUNC
256   * @tc.require: AR000DSMPV
257   */
258  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewPause_001, TestSize.Level1)
259  {
260      if (imageAnimator_ == nullptr) {
261          EXPECT_EQ(1, 0);
262          return;
263      }
264      imageAnimator_->Pause();
265      EXPECT_EQ(imageAnimator_->GetState(), Animator::PAUSE);
266  }
267  
268  /**
269   * @tc.name: UIImageAnimatorViewResume_001
270   * @tc.desc: Verify Resume function, equal.
271   * @tc.type: FUNC
272   * @tc.require: AR000DSMPV
273   */
274  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewResume_001, TestSize.Level1)
275  {
276      if (imageAnimator_ == nullptr) {
277          EXPECT_EQ(1, 0);
278          return;
279      }
280      imageAnimator_->Resume();
281      EXPECT_EQ(imageAnimator_->GetState(), Animator::START);
282  }
283  
284  /**
285   * @tc.name: UIImageAnimatorViewSetFillMode_001
286   * @tc.desc: Verify SetFillMode function, equal.
287   * @tc.type: FUNC
288   * @tc.require: SR000F3PEF
289   */
290  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetFillMode_001, TestSize.Level0)
291  {
292      if (imageAnimator_ == nullptr) {
293          EXPECT_EQ(1, 0);
294          return;
295      }
296  
297      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_SIZE);
298      imageAnimator_->SetFillMode(true);
299      imageAnimator_->SetReverse(false);
300      imageAnimator_->Start();
301      imageAnimator_->Stop();
302      EXPECT_STREQ(imageAnimator_->GetPath(), g_imageAnimatorInfo[2].imagePath); // 2: the last image
303  }
304  
305  /**
306   * @tc.name: UIImageAnimatorViewSetFillMode_002
307   * @tc.desc: Verify SetFillMode function, equal.
308   * @tc.type: FUNC
309   * @tc.require: AR000F4E5J
310   */
311  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetFillMode_002, TestSize.Level1)
312  {
313      if (imageAnimator_ == nullptr) {
314          EXPECT_EQ(1, 0);
315          return;
316      }
317  
318      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_SIZE);
319      imageAnimator_->SetFillMode(true);
320      imageAnimator_->SetReverse(true);
321      imageAnimator_->Start();
322      imageAnimator_->Stop();
323      EXPECT_STREQ(imageAnimator_->GetPath(), g_imageAnimatorInfo[0].imagePath);
324  }
325  
326  /**
327   * @tc.name: UIImageAnimatorViewSetFillMode_003
328   * @tc.desc: Verify SetFillMode function, equal.
329   * @tc.type: FUNC
330   * @tc.require: AR000F4E5J
331   */
332  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetFillMode_003, TestSize.Level1)
333  {
334      if (imageAnimator_ == nullptr) {
335          EXPECT_EQ(1, 0);
336          return;
337      }
338  
339      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_SIZE);
340      imageAnimator_->SetFillMode(false);
341      imageAnimator_->SetReverse(false);
342      imageAnimator_->Start();
343      imageAnimator_->Stop();
344      EXPECT_STREQ(imageAnimator_->GetPath(), g_imageAnimatorInfo[0].imagePath);
345  }
346  
347  /**
348   * @tc.name: UIImageAnimatorViewSetFillMode_004
349   * @tc.desc: Verify SetFillMode function, equal.
350   * @tc.type: FUNC
351   * @tc.require: AR000F4E5J
352   */
353  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetFillMode_004, TestSize.Level1)
354  {
355      if (imageAnimator_ == nullptr) {
356          EXPECT_EQ(1, 0);
357          return;
358      }
359  
360      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, IMAGE_SIZE);
361      imageAnimator_->SetFillMode(false);
362      imageAnimator_->SetReverse(true);
363      imageAnimator_->Start();
364      imageAnimator_->Stop();
365      EXPECT_STREQ(imageAnimator_->GetPath(), g_imageAnimatorInfo[2].imagePath); // 2: the last image
366  }
367  
368  /**
369   * @tc.name: UIImageAnimatorViewSetImageAnimatorSrc_003
370   * @tc.desc: Verify SetImageAnimatorSrc function, equal.
371   */
372  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetImageAnimatorSrc_003, TestSize.Level1)
373  {
374      if (imageAnimator_ == nullptr) {
375          EXPECT_EQ(1, 0);
376          return;
377      }
378      const uint8_t imageSize = IMAGE_SIZE;
379      const uint16_t timeOfUpdate = 1;
380  
381      imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, imageSize, timeOfUpdate);
382      EXPECT_EQ(imageAnimator_->GetTimeOfUpdate(), timeOfUpdate);
383  }
384  
385  /**
386   * @tc.name: UIImageAnimatorViewSetRepeatTimes_001
387   * @tc.desc: Verify SetRepeatTimes function, equal.
388   */
389  HWTEST_F(UIImageAnimatorViewTest, UIImageAnimatorViewSetRepeatTimes_001, TestSize.Level1)
390  {
391      if (imageAnimator_ == nullptr) {
392          EXPECT_EQ(1, 0);
393          return;
394      }
395      uint32_t times = 1;
396  
397      imageAnimator_->SetRepeatTimes(times);
398      EXPECT_EQ(imageAnimator_->GetRepeatTimes(), times);
399  }
400  } // namespace OHOS
401