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 #include <gmock/gmock-matchers.h>
16 #include <gtest/gtest.h>
17
18 #include <meta/api/animation.h>
19 #include <meta/api/object.h>
20 #include <meta/interface/animation/builtin_animations.h>
21 #include <meta/interface/property/construct_property.h>
22
23 #include "TestRunner.h"
24 #include "helpers/animation_test_base.h"
25 #include "helpers/testing_objects.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 META_BEGIN_NAMESPACE()
31
32 namespace {
33
34 template<typename AnimationType>
35 Animation CreateAnimation(const IProperty::WeakPtr& property);
36
37 template<>
CreateAnimation(const IProperty::WeakPtr & property)38 Animation CreateAnimation<SequentialAnimation>(const IProperty::WeakPtr& property)
39 {
40 return SequentialAnimation(CreateInstance(ClassId::SequentialAnimation))
41 .Add(KeyframeAnimation<float>(CreateInstance(ClassId::KeyframeAnimation)));
42 }
43
44 template<>
CreateAnimation(const IProperty::WeakPtr & property)45 Animation CreateAnimation<ParallelAnimation>(const IProperty::WeakPtr& property)
46 {
47 return ParallelAnimation(CreateInstance(ClassId::ParallelAnimation))
48 .Add(KeyframeAnimation<float>(CreateInstance(ClassId::KeyframeAnimation)));
49 }
50
51 template<>
CreateAnimation(const IProperty::WeakPtr & property)52 Animation CreateAnimation<KeyframeAnimation<float>>(const IProperty::WeakPtr& property)
53 {
54 KeyframeAnimation<float> animation(CreateInstance(ClassId::KeyframeAnimation));
55 animation.SetFrom(0.0F);
56 animation.SetTo(1.0F);
57 animation.SetDuration(TimeSpan::Milliseconds(1000));
58 animation.SetProperty(property);
59 return animation;
60 }
61
62 } // namespace
63
64 template<typename AnimationType>
65 class AnimationSeekingTest : public AnimationTestBase {
66 public:
SetUpTestSuite()67 static void SetUpTestSuite()
68 {
69 SetTest();
70 }
TearDownTestSuite()71 static void TearDownTestSuite()
72 {
73 TearDownTest();
74 }
AnimationSeekingTest()75 AnimationSeekingTest()
76 {
77 property_ = ConstructProperty("animatedProperty", 0.0F);
78 startableInterface_ = interface_pointer_cast<IStartableAnimation>(CreateAnimation<AnimationType>(property_));
79 animationInterface_ = interface_pointer_cast<IAnimation>(startableInterface_);
80 }
81
82 protected:
83 IStartableAnimation::Ptr startableInterface_;
84 IAnimation::Ptr animationInterface_;
85 Property<float> property_;
86 };
87
88 using TestedTypes = ::testing::Types<KeyframeAnimation<float>, SequentialAnimation, ParallelAnimation>;
89 TYPED_TEST_SUITE(AnimationSeekingTest, TestedTypes);
90
91 /**
92 * @tc.name: SeekingSequentialAnimationDoesNotCauseAnimationStart
93 * @tc.desc: test SeekingSequentialAnimationDoesNotCauseAnimationStart
94 * @tc.type: FUNC
95 */
96 HWTYPED_TEST(AnimationSeekingTest, SeekingSequentialAnimationDoesNotCauseAnimationStart, TestSize.Level1)
97 {
98 // given
99 auto invoked = false;
100 this->animationInterface_->Running().GetProperty()->OnChanged()->AddHandler(
__anonb865c1980202null101 MakeCallback<IOnChanged>([&] { invoked = true; }));
102
103 // when
104 this->startableInterface_->Seek(0.25F);
105
106 // expected
107 const auto isRunning = this->animationInterface_->Running()->GetValue();
108 EXPECT_THAT(isRunning, testing::Eq(false));
109 EXPECT_THAT(invoked, testing::Eq(false));
110 }
111
112 /**
113 * @tc.name: SeekingRunningSequentialAnimationDoesNotChangeState
114 * @tc.desc: test SeekingRunningSequentialAnimationDoesNotChangeState
115 * @tc.type: FUNC
116 */
117 HWTYPED_TEST(AnimationSeekingTest, SeekingRunningSequentialAnimationDoesNotChangeState, TestSize.Level1)
118 {
119 // given
120 this->startableInterface_->Start();
121
122 auto invoked = false;
123 this->animationInterface_->Running().GetProperty()->OnChanged()->AddHandler(
__anonb865c1980302null124 MakeCallback<IOnChanged>([&] { invoked = true; }));
125
126 // when
127 this->startableInterface_->Seek(0.25F);
128
129 // expected
130 const auto isRunning = this->animationInterface_->Running()->GetValue();
131 EXPECT_THAT(isRunning, testing::Eq(true));
132 EXPECT_THAT(invoked, testing::Eq(false));
133 }
134
135 /**
136 * @tc.name: SeekingTo1000ProgressFinishesAnimation
137 * @tc.desc: test SeekingTo1000ProgressFinishesAnimation
138 * @tc.type: FUNC
139 */
140 HWTYPED_TEST(AnimationSeekingTest, SeekingTo1000ProgressFinishesAnimation, TestSize.Level1)
141 {
142 // given
143 this->startableInterface_->Start();
144
145 auto animationRunningChangeInvoke = false;
146 this->animationInterface_->Running().GetProperty()->OnChanged()->AddHandler(
__anonb865c1980402null147 MakeCallback<IOnChanged>([&] { animationRunningChangeInvoke = true; }));
148
149 auto onFinishedInvoked = false;
__anonb865c1980502null150 this->animationInterface_->OnFinished()->AddHandler(MakeCallback<IOnChanged>([&] { onFinishedInvoked = true; }));
151
152 // when
153 this->startableInterface_->Seek(1.0F);
154
155 // expected
156 const auto isRunning = this->animationInterface_->Running()->GetValue();
157 EXPECT_THAT(isRunning, testing::Eq(false));
158 EXPECT_THAT(animationRunningChangeInvoke, testing::Eq(true));
159 EXPECT_THAT(onFinishedInvoked, testing::Eq(true));
160 }
161
162 /**
163 * @tc.name: SeekingToBeginningDoesNotStopAnimation
164 * @tc.desc: test SeekingToBeginningDoesNotStopAnimation
165 * @tc.type: FUNC
166 */
167 HWTYPED_TEST(AnimationSeekingTest, SeekingToBeginningDoesNotStopAnimation, TestSize.Level1)
168 {
169 // given
170 this->startableInterface_->Start();
171 this->Update(TimeSpan::Milliseconds(500));
172
173 // when
174 auto animationRunningChangeInvoke = false;
175 this->animationInterface_->Running().GetProperty()->OnChanged()->AddHandler(
__anonb865c1980602null176 MakeCallback<IOnChanged>([&] { animationRunningChangeInvoke = true; }));
177
178 this->startableInterface_->Seek(0.0F);
179
180 // expected
181 auto propertyValue = this->property_->GetValue();
182
183 EXPECT_THAT(propertyValue, testing::Eq(0.0F));
184
185 const auto isRunning = this->animationInterface_->Running()->GetValue();
186 EXPECT_THAT(isRunning, testing::Eq(true));
187 EXPECT_THAT(animationRunningChangeInvoke, testing::Eq(false));
188 }
189
190 META_END_NAMESPACE()
191