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 }; SetParams(int32_t duration,AnimationCallbacks && param)64 void SetParams(int32_t duration, AnimationCallbacks&& param) 65 { 66 params_.callbacks = std::move(param); 67 params_.type = (duration <= 0) ? AnimationOperation::CANCEL : AnimationOperation::PLAY; 68 } 69 AddActiveProp(const WeakPtr<PropertyBase> & prop)70 void AddActiveProp(const WeakPtr<PropertyBase>& prop) 71 { 72 activeProps_.insert(prop); 73 } 74 75 /** 76 * @brief advance all animations by one frame 77 * 78 */ 79 void Tick(); 80 81 void Reset(); 82 83 private: 84 void CancelAnimations(); 85 86 AnimationParams params_; 87 std::list<RefPtr<MockImplicitAnimation>> animations_; 88 std::set<WeakPtr<PropertyBase>> activeProps_; 89 std::map<WeakPtr<PropertyBase>, WeakPtr<MockImplicitAnimation>> propToAnimation_; 90 91 int32_t ticks_ = 1; 92 bool inScope_ = false; 93 bool enabled_ = false; 94 }; 95 } // namespace OHOS::Ace::NG 96 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_MANAGER_H 97