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 #include "test/mock/core/animation/mock_animation_manager.h"
17 
18 namespace OHOS::Ace::NG {
CancelAnimations()19 void MockAnimationManager::CancelAnimations()
20 {
21     for (auto&& prop : activeProps_) {
22         auto it = propToAnimation_.find(prop);
23         if (it == propToAnimation_.end()) {
24             continue;
25         }
26         auto anim = it->second.Upgrade();
27         if (anim) {
28             anim->End();
29         }
30     }
31 }
32 
CloseAnimation()33 std::vector<RefPtr<MockImplicitAnimation>> MockAnimationManager::CloseAnimation()
34 {
35     inScope_ = false;
36     if (activeProps_.empty()) {
37         return {};
38     }
39     if (params_.type == AnimationOperation::CANCEL) {
40         CancelAnimations();
41         return {};
42     }
43     // capture active props in animation
44     std::vector<RefPtr<MockImplicitAnimation>> res;
45     for (auto&& prop : activeProps_) {
46         auto anim = propToAnimation_[prop].Upgrade();
47         if (anim) {
48             // update existing animation instead
49             anim->Update(params_.callbacks, ticks_);
50             continue;
51         }
52         res.emplace_back(AceType::MakeRefPtr<MockImplicitAnimation>(prop, params_.callbacks, ticks_));
53         propToAnimation_[prop] = res.back();
54         animations_.emplace_back(res.back());
55     }
56     activeProps_.clear();
57     return res;
58 }
59 
Tick()60 void MockAnimationManager::Tick()
61 {
62     for (auto it = animations_.begin(); it != animations_.end();) {
63         auto&& anim = *it;
64         if (!anim || anim->Finished()) {
65             it = animations_.erase(it);
66         } else {
67             anim->Next();
68             ++it;
69         }
70     }
71 }
72 
Reset()73 void MockAnimationManager::Reset()
74 {
75     propToAnimation_.clear();
76     activeProps_.clear();
77     animations_.clear();
78     params_.callbacks.finishCb = nullptr;
79     params_.callbacks.repeatCb = nullptr;
80     ticks_ = 1;
81     inScope_ = false;
82 }
83 } // namespace OHOS::Ace::NG