• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "animation_test_base.h"
17 
18 #include <gmock/gmock-matchers.h>
19 
20 #include <meta/interface/builtin_objects.h>
21 #include <meta/interface/intf_object_registry.h>
22 
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24 
25 void AnimationTestBase::SetUp()
26 {
27     ::testing::Test::SetUp();
28     clock_ = GetObjectRegistry().Create<IManualClock>(META_NS::ClassId::ManualClock);
29 }
30 
Time() const31 TimeSpan AnimationTestBase::Time() const
32 {
33     return clock_->GetTime();
34 }
35 
IncrementClockTime(const TimeSpan & time)36 void AnimationTestBase::IncrementClockTime(const TimeSpan& time)
37 {
38     clock_->IncrementTime(time);
39 }
40 
Update(const TimeSpan & time)41 void AnimationTestBase::Update(const TimeSpan& time)
42 {
43     IncrementClockTime(time);
44     GetAnimationController()->Step(clock_);
45 }
46 
GetTestClock() const47 IClock::Ptr AnimationTestBase::GetTestClock() const
48 {
49     return clock_;
50 }
51 
GetAnimationController()52 IAnimationController::Ptr AnimationTestBase::GetAnimationController()
53 {
54     return META_NS::GetAnimationController();
55 }
56 
RunFrames(uint32_t frames,std::function<void (uint32_t frame)> updateFn)57 void AnimationTestBase::RunFrames(uint32_t frames, std::function<void(uint32_t frame)> updateFn)
58 {
59     // Sleep for a bit before first update to allow e.g. animations to have some time
60     // to proceed before the first animation frame.
61     IncrementClockTime(TimeSpan::Milliseconds(10)); // 10: param
62     for (uint32_t i = 0; i < frames; i++) {
63         GetAnimationController()->Step(GetTestClock());
64         updateFn(i + 1);
65         if (i < frames - 1)
66             IncrementClockTime(TimeSpan::Milliseconds(10)); // 10: param
67     }
68 }
69 
StepAnimationController(int64_t stepMs)70 void AnimationTestBase::StepAnimationController(int64_t stepMs)
71 {
72     clock_->IncrementTime(TimeSpan::Milliseconds(stepMs));
73     GetAnimationController()->Step(GetTestClock());
74 }
75 
StepAnimations(const std::vector<IAnimation::Ptr> animations,uint32_t frames,std::function<void (uint32_t frame)> updateFn)76 void AnimationTestBase::StepAnimations(
77     const std::vector<IAnimation::Ptr> animations, uint32_t frames, std::function<void(uint32_t frame)> updateFn)
78 {
79     StepAnimations(animations, frames, 10, updateFn); // 10: param
80 }
81 
StepAnimations(const std::vector<IAnimation::Ptr> animations,uint32_t frames,int64_t frameStepMs,std::function<void (uint32_t frame)> updateFn)82 void AnimationTestBase::StepAnimations(const std::vector<IAnimation::Ptr> animations, uint32_t frames,
83     int64_t frameStepMs, std::function<void(uint32_t frame)> updateFn)
84 {
85     IncrementClockTime(TimeSpan::Milliseconds(frameStepMs));
86     for (uint32_t i = 0; i < frames; i++) {
87         for (auto& animation : animations) {
88             animation->Step(GetTestClock());
89         }
90         if (updateFn) {
91             updateFn(i + 1);
92         }
93         if (i < frames - 1)
94             IncrementClockTime(TimeSpan::Milliseconds(frameStepMs));
95     }
96 }
97 
98 META_END_NAMESPACE()
99