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_PROXY_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_PROXY_H 18 #include "test/mock/core/animation/mock_animation_manager.h" 19 20 namespace OHOS::Ace::NG { 21 /* mocking RSAnimatableProperty */ 22 template<typename T> 23 class MockAnimationProxy : public Singleton<MockAnimationProxy<T>> { 24 private: 25 MockAnimationProxy() = default; 26 ~MockAnimationProxy() = default; 27 friend Singleton<MockAnimationProxy>; 28 ACE_DISALLOW_COPY_AND_MOVE(MockAnimationProxy); 29 30 public: RegisterProperty(const WeakPtr<AnimatableProperty<T>> & ptr,T value)31 void RegisterProperty(const WeakPtr<AnimatableProperty<T>>& ptr, T value) 32 { 33 props_[ptr] = { value, value }; 34 } 35 RecordPropChange(const WeakPtr<AnimatableProperty<T>> & ptr,T targetValue)36 void RecordPropChange(const WeakPtr<AnimatableProperty<T>>& ptr, T targetValue) 37 { 38 auto& prop = props_[ptr]; 39 if (!MockAnimationManager::GetInstance().IsAnimationOpen()) { 40 prop = { targetValue, targetValue }; 41 return; 42 } 43 44 prop.endValue_ = targetValue; 45 MockAnimationManager::GetInstance().AddActiveProp(ptr); 46 } 47 GetEndValue(const WeakPtr<AnimatableProperty<T>> & ptr)48 T GetEndValue(const WeakPtr<AnimatableProperty<T>>& ptr) 49 { 50 auto it = props_.find(ptr); 51 if (it == props_.end()) { 52 return {}; 53 } 54 return it->second.endValue_; 55 } 56 GetStagingValue(const WeakPtr<AnimatableProperty<T>> & ptr)57 T GetStagingValue(const WeakPtr<AnimatableProperty<T>>& ptr) 58 { 59 auto it = props_.find(ptr); 60 if (it == props_.end()) { 61 return {}; 62 } 63 return it->second.stagingValue_; 64 } 65 66 /* move staging value by one frame */ Next(const WeakPtr<AnimatableProperty<T>> & ptr,int32_t remainingTicks)67 void Next(const WeakPtr<AnimatableProperty<T>>& ptr, int32_t remainingTicks) 68 { 69 auto it = props_.find(ptr); 70 if (it == props_.end() || remainingTicks == 0) { 71 return; 72 } 73 T delta = (it->second.endValue_ - it->second.stagingValue_) / remainingTicks; 74 it->second.stagingValue_ += delta; 75 } 76 77 private: 78 struct PropertyImpl { 79 T stagingValue_; 80 T endValue_; 81 }; 82 std::map<WeakPtr<AnimatableProperty<T>>, PropertyImpl> props_; 83 }; 84 } // namespace OHOS::Ace::NG 85 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_PROXY_H 86