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 #ifndef UI_TEST_ANIMATOR_H
17 #define UI_TEST_ANIMATOR_H
18 
19 #include "animator/easing_equation.h"
20 #include "components/ui_image_view.h"
21 #include "components/ui_label.h"
22 #include "components/ui_label_button.h"
23 #include "components/ui_scroll_view.h"
24 #include "gfx_utils/sys_info.h"
25 #include "ui_test.h"
26 
27 namespace OHOS {
28 class ImageEaseAnimatorCallback : public AnimatorCallback {
29 public:
ImageEaseAnimatorCallback(UIView * uiView,int16_t startPos,int16_t endPos)30     ImageEaseAnimatorCallback(UIView* uiView, int16_t startPos, int16_t endPos)
31         : startPos_(startPos),
32           endPos_(endPos),
33           easingFunc_(EasingEquation::LinearEaseNone),
34           animator_(new Animator(this, uiView, 1000, true)) // 1000:duration of animator_, in milliseconds.
35     {
36     }
37 
~ImageEaseAnimatorCallback()38     virtual ~ImageEaseAnimatorCallback()
39     {
40         delete animator_;
41     }
42 
GetAnimator()43     Animator* GetAnimator() const
44     {
45         return animator_;
46     }
47 
SetEasingFunc(EasingFunc easingFunc)48     void SetEasingFunc(EasingFunc easingFunc)
49     {
50         easingFunc_ = easingFunc;
51     }
52 
Callback(UIView * view)53     virtual void Callback(UIView* view)
54     {
55         if (view == nullptr) {
56             return;
57         }
58         int16_t pos = easingFunc_(startPos_, endPos_, animator_->GetRunTime(), animator_->GetTime());
59         Rect invalidatedArea = view->GetRect();
60         view->SetPosition(pos, view->GetY());
61         invalidatedArea.Join(invalidatedArea, view->GetRect());
62         view->InvalidateRect(invalidatedArea);
63     }
64 
65 protected:
66     int16_t startPos_;
67     int16_t endPos_;
68     EasingFunc easingFunc_;
69     Animator* animator_;
70 };
71 
72 class UITestAnimator : public UITest, public UIView::OnClickListener, public SysInfo::OnFPSChangedListener {
73 public:
UITestAnimator()74     UITestAnimator() {}
~UITestAnimator()75     ~UITestAnimator() {}
76     void SetUp() override;
77     void TearDown() override;
78     const UIView* GetTestView() override;
79 
80     void SetUpButton(UILabelButton* btn, const char* title, int16_t x, int16_t y);
81     void SetUpLabel(const char* title, int16_t x, int16_t y);
82 
83     bool OnClick(UIView& view, const ClickEvent& event) override;
84     void OnFPSChanged(float newFPS) override;
85 
86     void UIKitAnimatorTestBackEasing001();
87     void UIKitAnimatorTestCircEasing002();
88     void UIKitAnimatorTestCubicEasing003();
89     void UIKitAnimatorTestSineEasing004();
90     void UIKitAnimatorTestQuadEasing005();
91     void UIKitAnimatorTestQuintEasing006();
92     void UIKitAnimatorTestLinearEasing007();
93     void UIKitAnimatorTestFPS008();
94 
95 private:
96     UIScrollView* container_ = nullptr;
97     UIScrollView* scroll_ = nullptr;
98     UIImageView* image_ = nullptr;
99     ImageEaseAnimatorCallback* callback_ = nullptr;
100     Animator* animator_ = nullptr;
101     UIViewGroup* uiViewGroupFrame_ = nullptr;
102 
103     UILabelButton* backOvershootBtn_ = nullptr;
104     UILabelButton* backEaseInBtn_ = nullptr;
105     UILabelButton* backEaseOutBtn_ = nullptr;
106     UILabelButton* backEaseInOutBtn_ = nullptr;
107     UILabelButton* circEaseInBtn_ = nullptr;
108     UILabelButton* circEaseOutBtn_ = nullptr;
109     UILabelButton* circEaseInOutBtn_ = nullptr;
110     UILabelButton* cubicEaseInBtn_ = nullptr;
111     UILabelButton* cubicEaseOutBtn_ = nullptr;
112     UILabelButton* cubicEaseInOutBtn_ = nullptr;
113     UILabelButton* linearEaseNoneBtn_ = nullptr;
114     UILabelButton* quadEaseInBtn_ = nullptr;
115     UILabelButton* quadEaseOutBtn_ = nullptr;
116     UILabelButton* quadEaseInOutBtn_ = nullptr;
117     UILabelButton* quintEaseInBtn_ = nullptr;
118     UILabelButton* quintEaseOutBtn_ = nullptr;
119     UILabelButton* quintEaseInOutBtn_ = nullptr;
120     UILabelButton* sineEaseInBtn_ = nullptr;
121     UILabelButton* sineEaseOutBtn_ = nullptr;
122     UILabelButton* sineEaseInOutBtn_ = nullptr;
123     UILabel* fpsLabel_ = nullptr;
124 };
125 } // namespace OHOS
126 #endif // UI_TEST_ANIMATOR_H
127