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_animator.h"
17 #include "common/screen.h"
18 #include "components/ui_label.h"
19 #include "test_resource_config.h"
20 
21 namespace OHOS {
22 namespace {
23 const int16_t LABEL_HEIGHT = 29;
24 const int16_t BUTTON_WIDTH = BUTTON_WIDHT3;
25 const int16_t BUTTON_HEIGHT = BUTTON_HEIGHT3;
26 const int16_t DELTA_X_COORDINATE = 5;
27 const int16_t DELTA_Y_COORDINATE = 12;
28 } // namespace
29 
SetUp()30 void UITestAnimator::SetUp()
31 {
32     if (container_ == nullptr) {
33         container_ = new UIScrollView();
34         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
35         uiViewGroupFrame_ = new UIViewGroup();
36         uiViewGroupFrame_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
37         uiViewGroupFrame_->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
38         uiViewGroupFrame_->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
39         uiViewGroupFrame_->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
40         uiViewGroupFrame_->SetStyle(STYLE_BACKGROUND_OPA, 0);
41         uiViewGroupFrame_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE);
42         container_->Add(uiViewGroupFrame_);
43         UILabel* label = new UILabel();
44         container_->Add(label);
45         // 264: label width; 48: label height
46         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 264, 48);
47         label->SetText("动画效果");
48         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
49         image_ = new UIImageView();
50         image_->SetSrc(RED_IMAGE_PATH);
51         image_->SetPosition(0, 5);                                 // 5:y-coordinate
52         callback_ = new ImageEaseAnimatorCallback(image_, 0, 338); // 338:endPos
53         animator_ = callback_->GetAnimator();
54         uiViewGroupFrame_->Add(image_);
55         uiViewGroupFrame_->Resize(Screen::GetInstance().GetWidth() - VIEW_DISTANCE_TO_LEFT_SIDE,
56                                   image_->GetHeight() + 10); // 10: increase y-coordinate
57     }
58     if (scroll_ == nullptr) {
59         scroll_ = new UIScrollView();
60         // 20: increase y-coordinate
61         uint16_t y = uiViewGroupFrame_->GetY() + uiViewGroupFrame_->GetHeight() + 20;
62         scroll_->SetPosition(0, y, Screen::GetInstance().GetWidth(),
63                              Screen::GetInstance().GetHeight() - y - 64); // 64: decrease y-coordinate
64         container_->Add(scroll_);
65         positionX_ = 0;
66     }
67 }
68 
TearDown()69 void UITestAnimator::TearDown()
70 {
71     DeleteChildren(container_);
72     container_ = nullptr;
73     image_ = nullptr;
74     scroll_ = nullptr;
75     delete callback_;
76     callback_ = nullptr;
77     uiViewGroupFrame_ = nullptr;
78 }
79 
GetTestView()80 const UIView* UITestAnimator::GetTestView()
81 {
82     UIKitAnimatorTestBackEasing001();
83     UIKitAnimatorTestCircEasing002();
84     UIKitAnimatorTestCubicEasing003();
85     UIKitAnimatorTestSineEasing004();
86     UIKitAnimatorTestQuadEasing005();
87     UIKitAnimatorTestQuintEasing006();
88     UIKitAnimatorTestLinearEasing007();
89     UIKitAnimatorTestFPS008();
90     return container_;
91 }
92 
SetUpLabel(const char * title,int16_t x,int16_t y)93 void UITestAnimator::SetUpLabel(const char* title, int16_t x, int16_t y)
94 {
95     UILabel* label = GetTitleLabel(title);
96     scroll_->Add(label);
97     label->SetPosition(x, y, 288, LABEL_HEIGHT); // 288: label width
98 }
99 
SetUpButton(UILabelButton * btn,const char * title,int16_t x,int16_t y)100 void UITestAnimator::SetUpButton(UILabelButton* btn, const char* title, int16_t x, int16_t y)
101 {
102     if (btn == nullptr) {
103         return;
104     }
105     scroll_->Add(btn);
106     btn->SetPosition(x, y, BUTTON_WIDHT3, BUTTON_HEIGHT3);
107     btn->SetText(title);
108     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
109     btn->SetOnClickListener(this);
110     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
111     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
112     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
113     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
114     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
115     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
116     scroll_->Invalidate();
117 }
118 
UIKitAnimatorTestBackEasing001()119 void UITestAnimator::UIKitAnimatorTestBackEasing001()
120 {
121     backOvershootBtn_ = new UILabelButton();
122     backEaseInBtn_ = new UILabelButton();
123     backEaseOutBtn_ = new UILabelButton();
124     backEaseInOutBtn_ = new UILabelButton();
125     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
126     positionY_ = 0;
127     SetUpLabel("back动画效果: ", positionX_, positionY_);
128     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
129     SetUpButton(backOvershootBtn_, "overshoot+", positionX_, positionY_);
130     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
131     SetUpButton(backEaseInBtn_, "BackEaseIn", positionX_, positionY_);
132     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
133     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
134     SetUpButton(backEaseOutBtn_, "BackEaseOut", positionX_, positionY_);
135     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
136     SetUpButton(backEaseInOutBtn_, "BackEaseInOut", positionX_, positionY_);
137 }
138 
UIKitAnimatorTestCircEasing002()139 void UITestAnimator::UIKitAnimatorTestCircEasing002()
140 {
141     circEaseInBtn_ = new UILabelButton();
142     circEaseOutBtn_ = new UILabelButton();
143     circEaseInOutBtn_ = new UILabelButton();
144     // 2: half of screen width
145     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
146     positionY_ = 0;
147     SetUpLabel("circ动画效果: ", positionX_, positionY_);
148     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
149     SetUpButton(circEaseInBtn_, "CircEaseIn", positionX_, positionY_);
150     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
151     SetUpButton(circEaseOutBtn_, "CircEaseOut", positionX_, positionY_);
152     // 2: half of screen width
153     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
154     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
155     SetUpButton(circEaseInOutBtn_, "CircEaseInOut", positionX_, positionY_);
156 }
157 
UIKitAnimatorTestCubicEasing003()158 void UITestAnimator::UIKitAnimatorTestCubicEasing003()
159 {
160     cubicEaseInBtn_ = new UILabelButton();
161     cubicEaseOutBtn_ = new UILabelButton();
162     cubicEaseInOutBtn_ = new UILabelButton();
163     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
164     positionY_ = 148; // 148: y-coordinate
165     SetUpLabel("cubic动画效果: ", positionX_, positionY_);
166     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
167     SetUpButton(cubicEaseInBtn_, "CubicEaseIn", positionX_, positionY_);
168     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
169     SetUpButton(cubicEaseOutBtn_, "CubicEaseOut", positionX_, positionY_);
170     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
171     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
172     SetUpButton(cubicEaseInOutBtn_, "CubicEaseInOut", positionX_, positionY_);
173 }
174 
UIKitAnimatorTestSineEasing004()175 void UITestAnimator::UIKitAnimatorTestSineEasing004()
176 {
177     sineEaseInBtn_ = new UILabelButton();
178     sineEaseOutBtn_ = new UILabelButton();
179     sineEaseInOutBtn_ = new UILabelButton();
180     // 2: half of screen width
181     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
182     positionY_ = 148; // 148: y-coordinate
183     SetUpLabel("sine动画效果: ", positionX_, positionY_);
184     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
185     SetUpButton(sineEaseInBtn_, "SineEaseIn", positionX_, positionY_);
186     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
187     SetUpButton(sineEaseOutBtn_, "SineEaseOut", positionX_, positionY_);
188     // 2: half of screen width
189     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
190     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
191     SetUpButton(sineEaseInOutBtn_, "SineEaseInOut", positionX_, positionY_);
192 }
193 
UIKitAnimatorTestQuadEasing005()194 void UITestAnimator::UIKitAnimatorTestQuadEasing005()
195 {
196     quadEaseInBtn_ = new UILabelButton();
197     quadEaseOutBtn_ = new UILabelButton();
198     quadEaseInOutBtn_ = new UILabelButton();
199     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
200     positionY_ = 296; // 296: y-coordinate
201     SetUpLabel("quad动画效果: ", positionX_, positionY_);
202     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
203     SetUpButton(quadEaseInBtn_, "QuadEaseIn", positionX_, positionY_);
204     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
205     SetUpButton(quadEaseOutBtn_, "QuadEaseOut", positionX_, positionY_);
206     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
207     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
208     SetUpButton(quadEaseInOutBtn_, "QuadEaseInOut", positionX_, positionY_);
209 }
210 
UIKitAnimatorTestQuintEasing006()211 void UITestAnimator::UIKitAnimatorTestQuintEasing006()
212 {
213     quintEaseInBtn_ = new UILabelButton();
214     quintEaseOutBtn_ = new UILabelButton();
215     quintEaseInOutBtn_ = new UILabelButton();
216     // 2: half of screen width
217     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
218     positionY_ = 296; // 296: y-coordinate
219     SetUpLabel("quint动画效果: ", positionX_, positionY_);
220     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
221     SetUpButton(quintEaseInBtn_, "QuintEaseIn", positionX_, positionY_);
222     positionX_ += BUTTON_WIDTH + DELTA_Y_COORDINATE;
223     SetUpButton(quintEaseOutBtn_, "QuintEaseOut", positionX_, positionY_);
224     // 2: half of screen width
225     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE;
226     positionY_ += BUTTON_HEIGHT + DELTA_Y_COORDINATE;
227     SetUpButton(quintEaseInOutBtn_, "QuintEaseInOut", positionX_, positionY_);
228 }
229 
UIKitAnimatorTestLinearEasing007()230 void UITestAnimator::UIKitAnimatorTestLinearEasing007()
231 {
232     linearEaseNoneBtn_ = new UILabelButton();
233     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
234     positionY_ = 444; // 444: y-coordinate
235     SetUpLabel("linear动画效果: ", positionX_, positionY_);
236     positionY_ += LABEL_HEIGHT + DELTA_X_COORDINATE;
237     SetUpButton(linearEaseNoneBtn_, "LinearEaseNone", positionX_, positionY_);
238 }
239 
UIKitAnimatorTestFPS008()240 void UITestAnimator::UIKitAnimatorTestFPS008()
241 {
242     fpsLabel_ = new UILabel();
243     // 90:x-coordinate, 0:y-coordinate, 90:new width, 20:new height
244     fpsLabel_->SetPosition(Screen::GetInstance().GetWidth() - 90, TEXT_DISTANCE_TO_TOP_SIDE, 90,
245                            TITLE_LABEL_DEFAULT_HEIGHT);
246     fpsLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
247     char buf[10] = "FPS";
248     fpsLabel_->SetText(reinterpret_cast<char*>(buf));
249     OnFPSChangedListener::SetFPSCalculateType(SysInfo::FPS_CT_PRECISE_SAMPLING);
250     container_->Add(fpsLabel_);
251     fpsLabel_->Invalidate();
252 }
253 
OnClick(UIView & view,const ClickEvent & event)254 bool UITestAnimator::OnClick(UIView& view, const ClickEvent& event)
255 {
256     static double overshoot = 1.7;
257     if (&view == backOvershootBtn_) {
258         overshoot += 1;
259         EasingEquation::SetBackOvershoot(overshoot);
260     } else if (&view == backEaseInBtn_) {
261         callback_->SetEasingFunc(EasingEquation::BackEaseIn);
262     } else if (&view == backEaseOutBtn_) {
263         callback_->SetEasingFunc(EasingEquation::BackEaseOut);
264     } else if (&view == backEaseInOutBtn_) {
265         callback_->SetEasingFunc(EasingEquation::BackEaseInOut);
266     } else if (&view == circEaseInBtn_) {
267         callback_->SetEasingFunc(EasingEquation::CircEaseIn);
268     } else if (&view == circEaseOutBtn_) {
269         callback_->SetEasingFunc(EasingEquation::CircEaseOut);
270     } else if (&view == circEaseInOutBtn_) {
271         callback_->SetEasingFunc(EasingEquation::CircEaseInOut);
272     } else if (&view == cubicEaseInBtn_) {
273         callback_->SetEasingFunc(EasingEquation::CubicEaseIn);
274     } else if (&view == cubicEaseOutBtn_) {
275         callback_->SetEasingFunc(EasingEquation::CubicEaseOut);
276     } else if (&view == cubicEaseInOutBtn_) {
277         callback_->SetEasingFunc(EasingEquation::CubicEaseInOut);
278     } else if (&view == linearEaseNoneBtn_) {
279         callback_->SetEasingFunc(EasingEquation::LinearEaseNone);
280     } else if (&view == quadEaseInBtn_) {
281         callback_->SetEasingFunc(EasingEquation::QuadEaseIn);
282     } else if (&view == quadEaseOutBtn_) {
283         callback_->SetEasingFunc(EasingEquation::QuadEaseOut);
284     } else if (&view == quadEaseInOutBtn_) {
285         callback_->SetEasingFunc(EasingEquation::QuadEaseInOut);
286     } else if (&view == quintEaseInBtn_) {
287         callback_->SetEasingFunc(EasingEquation::QuintEaseIn);
288     } else if (&view == quintEaseOutBtn_) {
289         callback_->SetEasingFunc(EasingEquation::QuintEaseOut);
290     } else if (&view == quintEaseInOutBtn_) {
291         callback_->SetEasingFunc(EasingEquation::QuintEaseInOut);
292     } else if (&view == sineEaseInBtn_) {
293         callback_->SetEasingFunc(EasingEquation::SineEaseIn);
294     } else if (&view == sineEaseOutBtn_) {
295         callback_->SetEasingFunc(EasingEquation::SineEaseOut);
296     } else if (&view == sineEaseInOutBtn_) {
297         callback_->SetEasingFunc(EasingEquation::SineEaseInOut);
298     }
299     animator_->Start();
300     container_->Invalidate();
301     return true;
302 }
303 
OnFPSChanged(float newFPS)304 void UITestAnimator::OnFPSChanged(float newFPS)
305 {
306     char buf[10] = "FPS";
307     fpsLabel_->SetText(reinterpret_cast<char*>(buf));
308     fpsLabel_->Invalidate();
309 }
310 } // namespace OHOS
311