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 "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 
23 using namespace testing;
24 
25 namespace {
26     const int STEPMS = 10; // step ms
27 }
28 
29 META_BEGIN_NAMESPACE()
30 
SetUp()31 void AnimationTestBase::SetUp()
32 {
33     ::testing::Test::SetUp();
34     clock_ = GetObjectRegistry().Create<IManualClock>(META_NS::ClassId::ManualClock);
35 }
36 
Time() const37 TimeSpan AnimationTestBase::Time() const
38 {
39     return clock_->GetTime();
40 }
41 
IncrementClockTime(const TimeSpan& time)42 void AnimationTestBase::IncrementClockTime(const TimeSpan& time)
43 {
44     clock_->IncrementTime(time);
45 }
46 
Update(const TimeSpan& time)47 void AnimationTestBase::Update(const TimeSpan& time)
48 {
49     IncrementClockTime(time);
50     GetAnimationController()->Step(clock_);
51 }
52 
GetTestClock() const53 IClock::Ptr AnimationTestBase::GetTestClock() const
54 {
55     return clock_;
56 }
57 
58 
GetAnimationController()59 IAnimationController::Ptr AnimationTestBase::GetAnimationController()
60 {
61     return META_NS::GetAnimationController();
62 }
63 
RunFrames(uint32_t frames, std::function<void(uint32_t frame)> updateFn)64 void AnimationTestBase::RunFrames(uint32_t frames, std::function<void(uint32_t frame)> updateFn)
65 {
66     IncrementClockTime(TimeSpan::Milliseconds(STEPMS));
67     for (uint32_t i = 0; i < frames; i++) {
68         GetAnimationController()->Step(GetTestClock());
69         updateFn(i + 1);
70         if (i < frames - 1)
71             IncrementClockTime(TimeSpan::Milliseconds(STEPMS));
72     }
73 }
74 
StepAnimationController(int64_t stepMs)75 void AnimationTestBase::StepAnimationController(int64_t stepMs)
76 {
77     clock_->IncrementTime(TimeSpan::Milliseconds(stepMs));
78     GetAnimationController()->Step(GetTestClock());
79 }
80 
StepAnimations( const std::vector<IAnimation::Ptr> animations, uint32_t frames, std::function<void(uint32_t frame)> updateFn)81 void AnimationTestBase::StepAnimations(
82     const std::vector<IAnimation::Ptr> animations, uint32_t frames, std::function<void(uint32_t frame)> updateFn)
83 {
84     StepAnimations(animations, frames, STEPMS, updateFn);
85 }
86 
StepAnimations(const std::vector<IAnimation::Ptr> animations, uint32_t frames, int64_t frameStepMs, std::function<void(uint32_t frame)> updateFn)87 void AnimationTestBase::StepAnimations(const std::vector<IAnimation::Ptr> animations, uint32_t frames,
88     int64_t frameStepMs, std::function<void(uint32_t frame)> updateFn)
89 {
90     IncrementClockTime(TimeSpan::Milliseconds(frameStepMs));
91     for (uint32_t i = 0; i < frames; i++) {
92         for (auto& animation : animations) {
93             animation->Step(GetTestClock());
94         }
95         if (updateFn) {
96             updateFn(i + 1);
97         }
98         if (i < frames - 1)
99             IncrementClockTime(TimeSpan::Milliseconds(frameStepMs));
100     }
101 }
102 
103 META_END_NAMESPACE()