• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_MANAGER_H
18 #include "test/mock/core/animation/mock_implicit_animation.h"
19 
20 #include "core/components_ng/base/modifier.h"
21 #include "frameworks/base/utils/singleton.h"
22 namespace OHOS::Ace::NG {
23 
24 class MockAnimationManager : public Singleton<MockAnimationManager> {
25 private:
26     MockAnimationManager() = default;
27     ~MockAnimationManager() override = default;
28     friend Singleton<MockAnimationManager>;
29     ACE_DISALLOW_COPY_AND_MOVE(MockAnimationManager);
30 
31 public:
Enable(bool value)32     static void Enable(bool value)
33     {
34         GetInstance().enabled_ = value;
35     }
Enabled()36     static bool Enabled()
37     {
38         return GetInstance().enabled_;
39     }
40 
41     /**
42      * @brief Controls the animation manager version to maintain backward compatibility in tests when applying bug fixes.
43      *
44      */
45     enum class Version {
46         V0 = 0,
47         V1 = 1, // introduces bugfixes to animation callback triggers
48     };
Version()49     static Version Version()
50     {
51         return GetInstance().runningVersion_;
52     }
SetVersion(enum Version value)53     static void SetVersion(enum Version value)
54     {
55         GetInstance().runningVersion_ = value;
56     }
57 
OpenAnimation()58     void OpenAnimation()
59     {
60         inScope_ = true;
61     }
62     std::vector<RefPtr<MockImplicitAnimation>> CloseAnimation();
63 
IsAnimationOpen()64     bool IsAnimationOpen() const
65     {
66         return inScope_;
67     }
68 
69     /**
70      * @brief Configure number of ticks for future animations
71      */
SetTicks(int32_t tick)72     void SetTicks(int32_t tick)
73     {
74         ticks_ = tick;
75     }
76 
77     struct AnimationParams {
78         AnimationCallbacks callbacks;
79         AnimationOperation type = AnimationOperation::PLAY;
80 
ResetAnimationParams81         void Reset()
82         {
83             callbacks.finishCb = nullptr;
84             callbacks.repeatCb = nullptr;
85             type = AnimationOperation::PLAY;
86         }
87     };
88 
89     void SetParams(const AnimationOption& option, AnimationCallbacks&& cbs);
90 
AddActiveProp(const WeakPtr<PropertyBase> & prop)91     void AddActiveProp(const WeakPtr<PropertyBase>& prop)
92     {
93         if (inScope_) {
94             activeProps_.insert(prop);
95         }
96     }
97 
98     /**
99      * @brief advance all animations by one frame
100      *
101      */
102     void Tick();
103 
104     /**
105      * @brief Force update animations by @c delta to simulate velocity animation
106      *
107      */
108     template<typename T>
109     void TickByVelocity(const T& delta);
110 
111     void Reset();
112 
113     bool AllFinished();
114 
115 private:
116     void CancelAnimations();
117 
118     AnimationParams params_;
119     std::list<RefPtr<MockImplicitAnimation>> animations_;
120     std::set<WeakPtr<PropertyBase>> activeProps_;
121     std::map<WeakPtr<PropertyBase>, WeakPtr<MockImplicitAnimation>> propToAnimation_;
122 
123     int32_t ticks_ = 1;
124     enum Version runningVersion_ = Version::V0;
125     bool inScope_ = false;
126     bool enabled_ = false;
127 };
128 } // namespace OHOS::Ace::NG
129 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_MANAGER_H
130