1 /*
2  * Copyright (c) 2024 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/base/modifier.h"
21 namespace OHOS::Ace {
22 
23 struct AnimationCallbacks {
24     std::function<void()> finishCb;
25     std::function<void()> repeatCb;
26 };
27 class MockImplicitAnimation : public AceType {
28     DECLARE_ACE_TYPE(MockImplicitAnimation, AceType);
29 
30 public:
MockImplicitAnimation(WeakPtr<NG::PropertyBase> prop,AnimationCallbacks cbs,int32_t ticks)31     MockImplicitAnimation(WeakPtr<NG::PropertyBase> prop, AnimationCallbacks cbs, int32_t ticks)
32         : cbs_(std::move(cbs)), prop_(std::move(prop)), remainingTicks_(ticks)
33     {}
34 
Update(AnimationCallbacks cbs,int32_t ticks)35     void Update(AnimationCallbacks cbs, int32_t ticks)
36     {
37         cbs_ = std::move(cbs);
38         remainingTicks_ = ticks;
39         paused_ = false;
40     }
41 
42     /**
43      * @brief move to next frame
44      */
45     void Next();
46 
Finished()47     bool Finished() const
48     {
49         return remainingTicks_ <= 0;
50     }
51 
Pause()52     void Pause()
53     {
54         paused_ = true;
55     }
56 
Resume()57     void Resume()
58     {
59         paused_ = false;
60     }
61 
62     /**
63      * @brief immediately terminate the animation without updating the value
64      */
65     void End();
66 
67     /**
68      * @brief jump to the end of the animation and update the value
69      */
70     void JumpToEnd();
71 
72 private:
73     void UpdateProp(const WeakPtr<NG::PropertyBase>& propWk) const;
74 
75     AnimationCallbacks cbs_;
76     WeakPtr<NG::PropertyBase> prop_;
77     int32_t remainingTicks_ = 0;
78     bool paused_ = false;
79 };
80 } // namespace OHOS::Ace
81 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
82