• 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 "animator/animator_manager.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 #include "animator/easing_equation.h"
22 #include "common/task_manager.h"
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace {
27 const int16_t START_POS = 0;
28 const int16_t END_POS = 100;
29 const uint16_t TIME = 300;
30 } // namespace
31 class TestAnimatorCallback : public AnimatorCallback {
32 public:
TestAnimatorCallback(UIView * view)33     explicit TestAnimatorCallback(UIView* view) : view_(view), animator_(nullptr) {}
34 
~TestAnimatorCallback()35     virtual ~TestAnimatorCallback()
36     {
37         if (animator_ != nullptr) {
38             delete animator_;
39             animator_ = nullptr;
40         }
41     }
42 
Init()43     bool Init()
44     {
45         if (animator_ == nullptr) {
46             animator_ = new Animator(this, view_, TIME, false);
47             if (animator_ == nullptr) {
48                 return false;
49             }
50             return true;
51         }
52         return false;
53     }
54 
Callback(UIView * view)55     void Callback(UIView* view) override
56     {
57         int16_t pos = EasingEquation::LinearEaseNone(START_POS, END_POS, animator_->GetRunTime(), animator_->GetTime());
58         view_->SetX(pos);
59     }
60 
OnStop(UIView & view)61     void OnStop(UIView& view) override
62     {
63         view_->SetX(END_POS);
64     }
65 
GetAnimator() const66     Animator* GetAnimator() const
67     {
68         return animator_;
69     }
70 
71 protected:
72     UIView* view_;
73     Animator* animator_;
74 };
75 
76 class AnimatorTest : public testing::Test {
77 public:
78     static void SetUpTestCase(void);
79     static void TearDownTestCase(void);
80     static Animator* animator_;
81 };
82 
83 Animator* AnimatorTest::animator_ = nullptr;
84 
SetUpTestCase(void)85 void AnimatorTest::SetUpTestCase(void)
86 {
87     if (animator_ == nullptr) {
88         animator_ = new Animator();
89     }
90 }
91 
TearDownTestCase(void)92 void AnimatorTest::TearDownTestCase(void)
93 {
94     if (animator_ != nullptr) {
95         delete animator_;
96         animator_ = nullptr;
97     }
98 }
99 
100 /**
101  * @tc.name: AnimatorGetState_001
102  * @tc.desc: Verify Start function, equal.
103  * @tc.type: FUNC
104  * @tc.require: AR000DSMQM
105  */
106 HWTEST_F(AnimatorTest, AnimatorGetState_001, TestSize.Level1)
107 {
108     if (animator_ == nullptr) {
109         EXPECT_NE(0, 0);
110         return;
111     }
112     animator_->Start();
113     EXPECT_EQ(animator_->GetState(), Animator::START);
114     animator_->Stop();
115     EXPECT_EQ(animator_->GetState(), Animator::STOP);
116     animator_->Pause();
117     EXPECT_EQ(animator_->GetState(), Animator::PAUSE);
118     animator_->Resume();
119     EXPECT_EQ(animator_->GetState(), Animator::START);
120 }
121 
122 /**
123  * @tc.name: AnimatorSetState_001
124  * @tc.desc: Verify SetState function, equal.
125  * @tc.type: FUNC
126  * @tc.require: AR000DSMQM
127  */
128 HWTEST_F(AnimatorTest, AnimatorSetState_001, TestSize.Level1)
129 {
130     if (animator_ == nullptr) {
131         EXPECT_NE(0, 0);
132         return;
133     }
134     animator_->SetState(Animator::START);
135     EXPECT_EQ(animator_->GetState(), Animator::START);
136     animator_->SetState(Animator::STOP);
137     EXPECT_EQ(animator_->GetState(), Animator::STOP);
138     animator_->SetState(Animator::PAUSE);
139     EXPECT_EQ(animator_->GetState(), Animator::PAUSE);
140 }
141 
142 /**
143  * @tc.name: AnimatorSetTime_001
144  * @tc.desc: Verify SetTime function, equal.
145  * @tc.type: FUNC
146  * @tc.require: AR000DSMQM
147  */
148 HWTEST_F(AnimatorTest, AnimatorSetTime_001, TestSize.Level1)
149 {
150     if (animator_ == nullptr) {
151         EXPECT_NE(0, 0);
152         return;
153     }
154     animator_->SetTime(TIME);
155     EXPECT_EQ(animator_->GetTime(), TIME);
156 }
157 
158 /**
159  * @tc.name: AnimatorSetRunTime_001
160  * @tc.desc: Verify SetRunTime function, equal.
161  * @tc.type: FUNC
162  * @tc.require: AR000DSMQM
163  */
164 HWTEST_F(AnimatorTest, AnimatorSetRunTime_001, TestSize.Level1)
165 {
166     if (animator_ == nullptr) {
167         EXPECT_NE(0, 0);
168         return;
169     }
170     animator_->SetRunTime(TIME);
171     EXPECT_EQ(animator_->GetRunTime(), TIME);
172 }
173 
174 /**
175  * @tc.name: AnimatorIsRepeat_001
176  * @tc.desc: Verify IsRepeat function, equal.
177  * @tc.type: FUNC
178  * @tc.require: AR000DSMQM
179  */
180 HWTEST_F(AnimatorTest, AnimatorIsRepeat_001, TestSize.Level1)
181 {
182     if (animator_ == nullptr) {
183         EXPECT_NE(0, 0);
184         return;
185     }
186     EXPECT_EQ(animator_->IsRepeat(), false);
187 }
188 
189 /**
190  * @tc.name: AnimatorManagerAddAndRemove_001
191  * @tc.desc: Verify AddAndRemove function, equal.
192  * @tc.type: FUNC
193  * @tc.require: AR000DSMQM
194  */
195 HWTEST_F(AnimatorTest, AnimatorManagerAddAndRemove_001, TestSize.Level0)
196 {
197     UIView* view = new UIView();
198     view->SetX(START_POS);
199     auto callback = new TestAnimatorCallback(view);
200     if (!callback->Init()) {
201         EXPECT_NE(0, 0);
202         return;
203     }
204     Animator* animator = callback->GetAnimator();
205     AnimatorManager::GetInstance()->Init();
206     animator->Start();
207     EXPECT_EQ(animator->GetState(), Animator::START);
208     TaskManager::GetInstance()->SetTaskRun(true);
209     while (1) {
210         TaskManager::GetInstance()->TaskHandler();
211         if (animator->GetState() == Animator::STOP) {
212             break;
213         }
214     }
215     EXPECT_EQ(view->GetX(), END_POS);
216 
217     view->SetX(START_POS);
218     animator->Start();
219     EXPECT_EQ(animator->GetState(), Animator::START);
220     for (uint16_t i = 0; i < TIME; i++) {
221         TaskManager::GetInstance()->TaskHandler();
222     }
223     EXPECT_EQ(view->GetX(), START_POS);
224     TaskManager::GetInstance()->SetTaskRun(false);
225     TaskManager::GetInstance()->Remove(AnimatorManager::GetInstance());
226     delete callback;
227     delete view;
228 }
229 } // namespace OHOS
230