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 "ui_test_image_animator.h"
17 #include "common/screen.h"
18 #include "test_resource_config.h"
19 
20 namespace OHOS {
21 namespace {
22 static ImageAnimatorInfo g_imageAnimatorInfo[4] = {
23     {IMAGE_ANIMATOR_0_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
24     {IMAGE_ANIMATOR_1_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
25     {IMAGE_ANIMATOR_2_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
26     {IMAGE_ANIMATOR_3_PATH, {84, 108}, 116, 116, IMG_SRC_FILE_PATH},
27 };
28 
29 static ImageAnimatorInfo g_imageAnimatorInfo2[4] = {
30     {BLUE_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
31     {RED_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
32     {GREEN_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
33     {YELLOW_IMAGE_PATH, {84, 108}, 94, 94, IMG_SRC_FILE_PATH},
34 };
35 } // namespace
36 
SetUp()37 void UITestImageAnimator::SetUp()
38 {
39     if (container_ == nullptr) {
40         container_ = new UIScrollView();
41         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
42 
43         UIViewGroup* uiViewGroup = new UIViewGroup();
44         // 320: width; 390: height
45         uiViewGroup->SetPosition(0, 0, 320, 390);
46         container_->Add(uiViewGroup);
47         UILabel* label = new UILabel();
48         uiViewGroup->Add(label);
49         // 288: width; 48: height
50         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
51         label->SetText("UIImageAnimator效果");
52         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
53         UIViewGroup* uiViewGroupFrame_ = new UIViewGroup();
54         uiViewGroup->Add(uiViewGroupFrame_);
55         // 288: width; 336: height
56         uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
57         uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
58         uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
59         uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
60         uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
61         uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
62 
63         imageAnimator_ = new UIImageAnimatorView();
64         imageAnimator_->SetPosition(50, 50, 200, 200); // 50 : offset 50 : offset 200 : offset 200: offset
65         imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo, 4, 100); // 4: the number of images, 100: updating time
66         imageAnimator_->Start();
67         listener_ = new TestAnimatorStopListener(container_);
68         listener_->Init();
69         uiViewGroupFrame_->Add(imageAnimator_);
70         imageAnimator_->LayoutCenterOfParent();
71     }
72 
73     if (layout_ == nullptr) {
74         layout_ = new GridLayout();
75         uint16_t height = Screen::GetInstance().GetHeight();
76         layout_->SetPosition(370, 0, 320, height - 50); // 370: x , 320: width, 50: offset
77         container_->Add(layout_);
78         layout_->SetLayoutDirection(LAYOUT_VER);
79         layout_->SetRows(9); // 9 : rows
80         layout_->SetCols(2); // 2 : columns
81     }
82 
83     InitImageInfo();
84 }
85 
InitImageInfo()86 void UITestImageAnimator::InitImageInfo()
87 {
88     const uint16_t pixelByteSize = 4; // 4: bytes per pixel
89     const uint16_t width = 100;       // 100: image's width
90     const uint16_t height = 100;      // 100: image's height
91     uint32_t dataSize = width * height * pixelByteSize;
92 
93     uint8_t* srcData1 = static_cast<uint8_t*>(UIMalloc(dataSize));
94     if (srcData1 == nullptr) {
95         return;
96     }
97     for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
98         srcData1[i] = 255;            // 255: pixel value
99         srcData1[i + 1] = 0;          // 1: set green channel
100         srcData1[i + 2] = 0;          // 2: set red channel
101         srcData1[i + 3] = OPA_OPAQUE; // 3: set alpha channel
102     }
103     imageInfo1_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
104     if (imageInfo1_ == nullptr) {
105         UIFree(srcData1);
106         return;
107     }
108     SetImageInfoPara(imageInfo1_, width, height, dataSize, srcData1);
109 
110     uint8_t* srcData2 = static_cast<uint8_t*>(UIMalloc(dataSize));
111     if (srcData2 == nullptr) {
112         UIFree(imageInfo1_);
113         UIFree(srcData1);
114         return;
115     }
116     for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
117         srcData2[i] = 0;              // set blue channel
118         srcData2[i + 1] = 255;        // 1: set green channel 255: pixel value
119         srcData2[i + 2] = 0;          // 2: set red channel
120         srcData2[i + 3] = OPA_OPAQUE; // 3: set alpha channel
121     }
122     imageInfo2_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
123     if (imageInfo2_ == nullptr) {
124         UIFree(srcData2);
125         UIFree(imageInfo1_);
126         UIFree(srcData1);
127         return;
128     }
129     SetImageInfoPara(imageInfo2_, width, height, dataSize, srcData2);
130 
131     uint8_t* srcData3 = static_cast<uint8_t*>(UIMalloc(dataSize));
132     bool isReturn = false;
133     SetSrcData3(srcData3, srcData2, srcData1, dataSize, isReturn, pixelByteSize);
134     if (isReturn) {
135         return;
136     }
137     SetImageInfoPara(imageInfo3_, width, height, dataSize, srcData3);
138 
139     SetimageAnimatorImageInfos();
140 }
141 
SetSrcData3(uint8_t * srcData3,uint8_t * srcData2,uint8_t * srcData1,uint32_t dataSize,bool & isReturn,const uint16_t pixelByteSize)142 void UITestImageAnimator::SetSrcData3(uint8_t* srcData3, uint8_t* srcData2, uint8_t* srcData1,
143                                       uint32_t dataSize, bool& isReturn, const uint16_t pixelByteSize)
144 {
145     if (srcData3 == nullptr) {
146         UIFree(imageInfo2_);
147         UIFree(srcData2);
148         UIFree(imageInfo1_);
149         UIFree(srcData1);
150         isReturn = true;
151         return;
152     }
153     for (uint32_t i = 0; i < dataSize; i += pixelByteSize) {
154         srcData3[i] = 0;              // set blue channel
155         srcData3[i + 1] = 0;          // 1: set green channel
156         srcData3[i + 2] = 255;        // 2: set red channel 255: pixel value
157         srcData3[i + 3] = OPA_OPAQUE; // 3: set alpha channel
158     }
159     imageInfo3_ = static_cast<ImageInfo*>(UIMalloc(sizeof(ImageInfo)));
160     if (imageInfo3_ == nullptr) {
161         UIFree(srcData3);
162         UIFree(imageInfo2_);
163         UIFree(srcData2);
164         UIFree(imageInfo1_);
165         UIFree(srcData1);
166         isReturn = true;
167         return;
168     }
169 }
170 
SetImageInfoPara(ImageInfo * imageInfo,const uint16_t width,const uint16_t height,uint32_t dataSize,uint8_t * srcData)171 void UITestImageAnimator::SetImageInfoPara(ImageInfo* imageInfo,
172                                            const uint16_t width,
173                                            const uint16_t height,
174                                            uint32_t dataSize,
175                                            uint8_t* srcData)
176 {
177     imageInfo->header.width = width;
178     imageInfo->header.height = height;
179     imageInfo->header.colorMode = ARGB8888;
180     imageInfo->dataSize = dataSize;
181     imageInfo->data = srcData;
182 }
183 
SetimageAnimatorImageInfos()184 void UITestImageAnimator::SetimageAnimatorImageInfos()
185 {
186     imageAnimatorImageInfo_[0].imageInfo = imageInfo1_;
187     imageAnimatorImageInfo_[0].pos = {84, 108};
188     imageAnimatorImageInfo_[0].width = 100;  // 100: width value
189     imageAnimatorImageInfo_[0].height = 100; // 100: height value
190     imageAnimatorImageInfo_[0].imageType = IMG_SRC_IMAGE_INFO;
191 
192     imageAnimatorImageInfo_[1].imageInfo = imageInfo2_;
193     imageAnimatorImageInfo_[1].pos = {84, 108};
194     imageAnimatorImageInfo_[1].width = 100;  // 100: width value
195     imageAnimatorImageInfo_[1].height = 100; // 100: height value
196     imageAnimatorImageInfo_[1].imageType = IMG_SRC_IMAGE_INFO;
197 
198     imageAnimatorImageInfo_[2].imageInfo = imageInfo3_;        // 2: image index
199     imageAnimatorImageInfo_[2].pos = {84, 108};                // 2: image index
200     imageAnimatorImageInfo_[2].width = 100;                    // 2: image index, 100: width value
201     imageAnimatorImageInfo_[2].height = 100;                   // 2: image index, 100: height value
202     imageAnimatorImageInfo_[2].imageType = IMG_SRC_IMAGE_INFO; // 2: image index
203 }
204 
TearDown()205 void UITestImageAnimator::TearDown()
206 {
207     DeleteChildren(container_);
208     container_ = nullptr;
209     imageAnimator_ = nullptr;
210     layout_ = nullptr;
211     delete listener_;
212     listener_ = nullptr;
213 
214     UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo1_->data)));
215     imageInfo1_->data = nullptr;
216     UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo2_->data)));
217     imageInfo2_->data = nullptr;
218     UIFree(reinterpret_cast<void*>(const_cast<uint8_t*>(imageInfo3_->data)));
219     imageInfo3_->data = nullptr;
220     UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo1_)));
221     imageInfo1_ = nullptr;
222     UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo2_)));
223     imageInfo2_ = nullptr;
224     UIFree(reinterpret_cast<void*>(const_cast<ImageInfo*>(imageInfo3_)));
225     imageInfo3_ = nullptr;
226 }
227 
GetTestView()228 const UIView* UITestImageAnimator::GetTestView()
229 {
230     UIKitImageAnimatorTestStart001();
231     UIKitImageAnimatorTestStop002();
232     UIKitImageAnimatorTestPause003();
233     UIKitImageAnimatorTestResume004();
234     UIKitImageAnimatorTestSetImageAnimatorSrc005();
235     UIKitImageAnimatorTestSetTickOfUpdate006();
236     UIKitImageAnimatorTestSetSizeFixed007();
237     UIKitImageAnimatorTestSetRepeat008();
238     UIKitImageAnimatorTestSetReverse009();
239     UIKitImageAnimatorTestSetAnimatorStopListener010();
240     UIKitImageAnimatorTestSetRepeatTimes011();
241     UIKitImageAnimatorTestSetFillModeTrueForward012();
242     UIKitImageAnimatorTestSetFillModeTrueBackward013();
243     UIKitImageAnimatorTestSetFillModeFalseForward014();
244     UIKitImageAnimatorTestSetFillModeFalseBackward015();
245     UIKitImageAnimatorTestSetImageInfo016();
246 
247     layout_->LayoutChildren();
248     return container_;
249 }
250 
SetUpButton(UILabelButton * btn,const char * title)251 void UITestImageAnimator::SetUpButton(UILabelButton* btn, const char* title)
252 {
253     if (btn == nullptr) {
254         return;
255     }
256     layout_->Add(btn);
257     btn->Resize(BUTTON_WIDHT2, BUTTON_HEIGHT2);
258     btn->SetText(title);
259     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
260     btn->SetOnClickListener(this);
261     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
262     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
263     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
264     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
265     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
266     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
267 }
268 
UIKitImageAnimatorTestStart001()269 void UITestImageAnimator::UIKitImageAnimatorTestStart001()
270 {
271     startBtn_ = new UILabelButton();
272     SetUpButton(startBtn_, "Start");
273 }
274 
UIKitImageAnimatorTestStop002()275 void UITestImageAnimator::UIKitImageAnimatorTestStop002()
276 {
277     stopBtn_ = new UILabelButton();
278     SetUpButton(stopBtn_, "Stop");
279 }
280 
UIKitImageAnimatorTestPause003()281 void UITestImageAnimator::UIKitImageAnimatorTestPause003()
282 {
283     pauseBtn_ = new UILabelButton();
284     SetUpButton(pauseBtn_, "Pause");
285 }
286 
UIKitImageAnimatorTestResume004()287 void UITestImageAnimator::UIKitImageAnimatorTestResume004()
288 {
289     resumeBtn_ = new UILabelButton();
290     SetUpButton(resumeBtn_, "Resume");
291 }
292 
UIKitImageAnimatorTestSetImageAnimatorSrc005()293 void UITestImageAnimator::UIKitImageAnimatorTestSetImageAnimatorSrc005()
294 {
295     setImageBtn_ = new UILabelButton();
296     SetUpButton(setImageBtn_, "设置图片");
297 }
298 
UIKitImageAnimatorTestSetTickOfUpdate006()299 void UITestImageAnimator::UIKitImageAnimatorTestSetTickOfUpdate006()
300 {
301     setSpeedBtn_ = new UILabelButton();
302     SetUpButton(setSpeedBtn_, "设置速度");
303 }
304 
UIKitImageAnimatorTestSetSizeFixed007()305 void UITestImageAnimator::UIKitImageAnimatorTestSetSizeFixed007()
306 {
307     fixedBtn_ = new UILabelButton();
308     SetUpButton(fixedBtn_, "图片大小固定");
309 }
310 
UIKitImageAnimatorTestSetRepeat008()311 void UITestImageAnimator::UIKitImageAnimatorTestSetRepeat008()
312 {
313     repeatBtn_ = new UILabelButton();
314     noRepeatBtn_ = new UILabelButton();
315     SetUpButton(repeatBtn_, "循环播放");
316     SetUpButton(noRepeatBtn_, "播放一次 ");
317 }
318 
UIKitImageAnimatorTestSetReverse009()319 void UITestImageAnimator::UIKitImageAnimatorTestSetReverse009()
320 {
321     reverseOrderBtn_ = new UILabelButton();
322     positiveOrderBtn_ = new UILabelButton();
323     SetUpButton(reverseOrderBtn_, "逆序播放");
324     SetUpButton(positiveOrderBtn_, "正序播放");
325 }
326 
UIKitImageAnimatorTestSetAnimatorStopListener010()327 void UITestImageAnimator::UIKitImageAnimatorTestSetAnimatorStopListener010()
328 {
329     listenerBtn_ = new UILabelButton();
330     SetUpButton(listenerBtn_, "播放结束监听");
331 }
332 
UIKitImageAnimatorTestSetRepeatTimes011()333 void UITestImageAnimator::UIKitImageAnimatorTestSetRepeatTimes011()
334 {
335     repeatTimesBtn_ = new UILabelButton();
336     SetUpButton(repeatTimesBtn_, "播放次数+");
337 }
338 
UIKitImageAnimatorTestSetFillModeTrueForward012()339 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeTrueForward012()
340 {
341     fillModeTrueForwardBtn_ = new UILabelButton();
342     SetUpButton(fillModeTrueForwardBtn_, "fillMode true 正播");
343 }
344 
UIKitImageAnimatorTestSetFillModeTrueBackward013()345 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeTrueBackward013()
346 {
347     fillModeTrueBackwardBtn_ = new UILabelButton();
348     SetUpButton(fillModeTrueBackwardBtn_, "fillMode true 逆播");
349 }
350 
UIKitImageAnimatorTestSetFillModeFalseForward014()351 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeFalseForward014()
352 {
353     fillModeFalseForwardBtn_ = new UILabelButton();
354     SetUpButton(fillModeFalseForwardBtn_, "fillMode false 正播");
355 }
356 
UIKitImageAnimatorTestSetFillModeFalseBackward015()357 void UITestImageAnimator::UIKitImageAnimatorTestSetFillModeFalseBackward015()
358 {
359     fillModeFalseBackwardBtn_ = new UILabelButton();
360     SetUpButton(fillModeFalseBackwardBtn_, "fillMode false 逆播");
361 }
362 
UIKitImageAnimatorTestSetImageInfo016()363 void UITestImageAnimator::UIKitImageAnimatorTestSetImageInfo016()
364 {
365     setImageInfoBtn_ = new UILabelButton();
366     SetUpButton(setImageInfoBtn_, "设置ImageInfo");
367 }
368 
OnClick(UIView & view,const ClickEvent & event)369 bool UITestImageAnimator::OnClick(UIView& view, const ClickEvent& event)
370 {
371     if (&view == startBtn_) {
372         imageAnimator_->Start();
373     } else if (&view == stopBtn_) {
374         imageAnimator_->Stop();
375     } else if (&view == pauseBtn_) {
376         imageAnimator_->Pause();
377     } else if (&view == resumeBtn_) {
378         imageAnimator_->Resume();
379     } else if (&view == setImageBtn_) {
380         imageAnimator_->Stop();
381         imageAnimator_->SetImageAnimatorSrc(g_imageAnimatorInfo2, 4); // 4: the number of images
382         imageAnimator_->Start();
383     } else if (&view == setSpeedBtn_) {
384         imageAnimator_->Stop();
385         imageAnimator_->SetTimeOfUpdate(10); // 10: set time of update
386         imageAnimator_->Start();
387     } else if (&view == fixedBtn_) {
388         imageAnimator_->SetSizeFixed(true);
389         imageAnimator_->SetPosition(50, 50, 200, 200); // 50 : offset 50 : offset 200 : offset 200 :offset
390     } else if (&view == repeatBtn_) {
391         imageAnimator_->Stop();
392         imageAnimator_->SetRepeat(true);
393         imageAnimator_->Start();
394     } else if (&view == noRepeatBtn_) {
395         imageAnimator_->Stop();
396         imageAnimator_->SetRepeat(false);
397         imageAnimator_->Start();
398     } else if (&view == reverseOrderBtn_) {
399         imageAnimator_->Stop();
400         imageAnimator_->SetReverse(true);
401         imageAnimator_->Start();
402     } else if (&view == positiveOrderBtn_) {
403         imageAnimator_->Stop();
404         imageAnimator_->SetReverse(false);
405         imageAnimator_->Start();
406     } else if (&view == listenerBtn_) {
407         imageAnimator_->SetAnimatorStopListener(listener_);
408     } else if (&view == repeatTimesBtn_) {
409         imageAnimator_->Stop();
410         imageAnimator_->SetRepeatTimes(imageAnimator_->GetRepeatTimes() + 1);
411         imageAnimator_->Start();
412     } else if (&view == fillModeTrueForwardBtn_) {
413         imageAnimator_->Stop();
414         imageAnimator_->SetFillMode(true);
415         imageAnimator_->SetRepeat(false);
416         imageAnimator_->SetReverse(false);
417         imageAnimator_->Start();
418     } else if (&view == fillModeTrueBackwardBtn_) {
419         imageAnimator_->Stop();
420         imageAnimator_->SetFillMode(true);
421         imageAnimator_->SetRepeat(false);
422         imageAnimator_->SetReverse(true);
423         imageAnimator_->Start();
424     } else if (&view == fillModeFalseForwardBtn_) {
425         imageAnimator_->Stop();
426         imageAnimator_->SetFillMode(false);
427         imageAnimator_->SetRepeat(false);
428         imageAnimator_->SetReverse(false);
429         imageAnimator_->Start();
430     } else if (&view == fillModeFalseBackwardBtn_) {
431         imageAnimator_->Stop();
432         imageAnimator_->SetFillMode(false);
433         imageAnimator_->SetRepeat(false);
434         imageAnimator_->SetReverse(true);
435         imageAnimator_->Start();
436     } else if (&view == setImageInfoBtn_) {
437         imageAnimator_->Stop();
438         imageAnimator_->SetImageAnimatorSrc(imageAnimatorImageInfo_, 3); // 3: the number of images
439         imageAnimator_->Start();
440     }
441     return true;
442 }
443 } // namespace OHOS
444