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 OpenAnimation()41 void OpenAnimation() 42 { 43 inScope_ = true; 44 } 45 std::vector<RefPtr<MockImplicitAnimation>> CloseAnimation(); 46 IsAnimationOpen()47 bool IsAnimationOpen() const 48 { 49 return inScope_; 50 } 51 52 /** 53 * @brief Configure number of ticks for future animations 54 */ SetTicks(int32_t tick)55 void SetTicks(int32_t tick) 56 { 57 ticks_ = tick; 58 } 59 60 struct AnimationParams { 61 AnimationCallbacks callbacks; 62 AnimationOperation type = AnimationOperation::PLAY; 63 ResetAnimationParams64 void Reset() 65 { 66 callbacks.finishCb = nullptr; 67 callbacks.repeatCb = nullptr; 68 type = AnimationOperation::PLAY; 69 } 70 }; 71 72 void SetParams(const AnimationOption& option, AnimationCallbacks&& cbs); 73 AddActiveProp(const WeakPtr<PropertyBase> & prop)74 void AddActiveProp(const WeakPtr<PropertyBase>& prop) 75 { 76 if (inScope_) { 77 activeProps_.insert(prop); 78 } 79 } 80 81 /** 82 * @brief advance all animations by one frame 83 * 84 */ 85 void Tick(); 86 87 /** 88 * @brief Force update animations by @c delta to simulate velocity animation 89 * 90 */ 91 void TickByVelocity(float velocity); 92 93 void Reset(); 94 95 bool AllFinished(); 96 97 private: 98 void CancelAnimations(); 99 100 AnimationParams params_; 101 std::list<RefPtr<MockImplicitAnimation>> animations_; 102 std::set<WeakPtr<PropertyBase>> activeProps_; 103 std::map<WeakPtr<PropertyBase>, WeakPtr<MockImplicitAnimation>> propToAnimation_; 104 105 int32_t ticks_ = 1; 106 bool inScope_ = false; 107 bool enabled_ = false; 108 }; 109 } // namespace OHOS::Ace::NG 110 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_MANAGER_H 111