1 /*
2  * Copyright (c) 2022-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 "gtest/gtest.h"
17 #include "include/command/rs_animation_command.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Rosen {
23 class RSAnimationCommandTest : public testing::Test {
24 public:
25     static void SetUpTestCase();
26     static void TearDownTestCase();
27     void SetUp() override;
28     void TearDown() override;
29     static void TestProcessor(NodeId, AnimationId, AnimationCallbackEvent);
30 };
31 
SetUpTestCase()32 void RSAnimationCommandTest::SetUpTestCase() {}
TearDownTestCase()33 void RSAnimationCommandTest::TearDownTestCase() {}
SetUp()34 void RSAnimationCommandTest::SetUp() {}
TearDown()35 void RSAnimationCommandTest::TearDown() {}
TestProcessor(NodeId nodeId,AnimationId animId,AnimationCallbackEvent event)36 void RSAnimationCommandTest::TestProcessor(NodeId nodeId, AnimationId animId, AnimationCallbackEvent event) {}
37 
38 /**
39  * @tc.name: TestRSAnimationCommand001
40  * @tc.desc: AnimationFinishCallback test.
41  * @tc.type: FUNC
42  */
43 HWTEST_F(RSAnimationCommandTest, TestRSAnimationCommand001, TestSize.Level1)
44 {
45     RSContext context;
46     NodeId targetId = static_cast<NodeId>(-1);
47     AnimationId animId = static_cast<AnimationId>(1);
48     AnimationCallbackEvent event = static_cast<AnimationCallbackEvent>(1);
49     AnimationCommandHelper::SetAnimationCallbackProcessor(TestProcessor);
50     AnimationCommandHelper::AnimationCallback(context, targetId, animId, event);
51     EXPECT_TRUE(targetId == -1);
52 }
53 
54 /**
55  * @tc.name: CreateParticleAnimation001
56  * @tc.desc: CreateParticleAnimation test.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(RSAnimationCommandTest, CreateParticleAnimation001, TestSize.Level1)
60 {
61     RSContext context;
62     NodeId targetId = static_cast<NodeId>(-1);
63     std::shared_ptr<RSRenderParticleAnimation> animation = nullptr;
64     AnimationCommandHelper::CreateParticleAnimation(context, targetId, animation);
65 
66     NodeId id = static_cast<NodeId>(1);
67     RSContext context2;
68 
69     std::shared_ptr<RSBaseRenderNode> node = std::make_shared<RSBaseRenderNode>(id);
70     context2.GetMutableNodeMap().RegisterRenderNode(node);
71     AnimationCommandHelper::CreateParticleAnimation(context2, id, nullptr);
72 
73     std::shared_ptr<RSRenderParticleAnimation> animation2 = std::make_shared<RSRenderParticleAnimation>();
74     AnimationCommandHelper::CreateParticleAnimation(context2, id, animation2);
75 
76     AnimationCommandHelper::CancelAnimation(context2, id, 0);
77     EXPECT_TRUE(targetId == -1);
78 }
79 
80 /**
81  * @tc.name: AnimationCallback001
82  * @tc.desc: test results of AnimationCallback
83  * @tc.type: FUNC
84  * @tc.require: issueI9P2KH
85  */
86 HWTEST_F(RSAnimationCommandTest, AnimationCallback001, TestSize.Level1)
87 {
88     RSContext context;
89     NodeId targetId = static_cast<NodeId>(-1);
90     AnimationId animId = static_cast<AnimationId>(1);
91     AnimationCallbackEvent event = static_cast<AnimationCallbackEvent>(1);
92     AnimationCommandHelper::AnimationCallback(context, targetId, animId, event);
93     EXPECT_TRUE(targetId == -1);
94 
95     AnimationCommandHelper::AnimationCallbackProcessor processor = [](NodeId nodeId, AnimationId animId,
__anone3b2f8020102(NodeId nodeId, AnimationId animId, AnimationCallbackEvent event) 96                                                                        AnimationCallbackEvent event) {};
97     AnimationCommandHelper::SetAnimationCallbackProcessor(processor);
98     AnimationCommandHelper::AnimationCallback(context, targetId, animId, event);
99     EXPECT_TRUE(targetId == -1);
100 }
101 
102 /**
103  * @tc.name: CreateAnimation001
104  * @tc.desc: test results of CreateAnimation
105  * @tc.type: FUNC
106  * @tc.require: issueIA61E9
107  */
108 HWTEST_F(RSAnimationCommandTest, CreateAnimation001, TestSize.Level1)
109 {
110     RSContext context;
111     NodeId targetId = 1;
112     std::shared_ptr<RSRenderAnimation> animation = nullptr;
113     AnimationCommandHelper::CreateAnimation(context, targetId, animation);
114     EXPECT_TRUE(targetId);
115 
116     animation = std::make_shared<RSRenderAnimation>();
117     AnimationCommandHelper::CreateAnimation(context, targetId, animation);
118     auto nod = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
119     EXPECT_TRUE(targetId == 1);
120 
121     targetId = 0;
122     auto node = context.GetNodeMap().GetRenderNode<RSRenderNode>(targetId);
123     PropertyId id = 0;
124     auto property = std::shared_ptr<RSRenderProperty<Drawing::Matrix>>();
125     auto modifier = std::make_shared<RSGeometryTransRenderModifier>(property);
126     node->modifiers_[id] = modifier;
127     AnimationCommandHelper::CreateAnimation(context, targetId, animation);
128     EXPECT_TRUE(modifier != nullptr);
129 
130     node->modifiers_.clear();
131     id = 1;
132     node->modifiers_[id] = modifier;
133     AnimationCommandHelper::CreateAnimation(context, targetId, animation);
134     EXPECT_TRUE(modifier != nullptr);
135 }
136 
137 /**
138  * @tc.name: CreateParticleAnimation002
139  * @tc.desc: test results of CreateParticleAnimation
140  * @tc.type: FUNC
141  * @tc.require: issueIA61E9
142  */
143 HWTEST_F(RSAnimationCommandTest, CreateParticleAnimation002, TestSize.Level1)
144 {
145     RSContext context;
146     NodeId targetId = 1;
147     std::shared_ptr<RSRenderParticleAnimation> animation = nullptr;
148     AnimationCommandHelper::CreateParticleAnimation(context, targetId, animation);
149     EXPECT_TRUE(targetId);
150 
151     animation = std::make_shared<RSRenderParticleAnimation>();
152     AnimationCommandHelper::CreateParticleAnimation(context, targetId, animation);
153     EXPECT_TRUE(targetId == 1);
154 
155     targetId = 0;
156     AnimationCommandHelper::CreateParticleAnimation(context, targetId, animation);
157     EXPECT_TRUE(animation != nullptr);
158 }
159 
160 /**
161  * @tc.name: CancelAnimation001
162  * @tc.desc: test results of CancelAnimation
163  * @tc.type: FUNC
164  * @tc.require: issueI9P2KH
165  */
166 HWTEST_F(RSAnimationCommandTest, CancelAnimation001, TestSize.Level1)
167 {
168     RSContext context;
169     NodeId targetId = 1;
170     PropertyId propertyId = 1;
171     AnimationCommandHelper::CancelAnimation(context, targetId, propertyId);
172     EXPECT_TRUE(targetId);
173 
174     targetId = 0;
175     AnimationCommandHelper::CancelAnimation(context, targetId, propertyId);
176     EXPECT_TRUE(targetId == 0);
177 }
178 
179 /**
180  * @tc.name: InteractiveAnimator001
181  * @tc.desc: InteractiveAnimator test.
182  * @tc.type: FUNC
183  * @tc.require: issueIA61E9
184  */
185 HWTEST_F(RSAnimationCommandTest, InteractiveAnimator001, TestSize.Level1)
186 {
187     RSContext context;
188     NodeId targetId = 0;
189     InteractiveImplictAnimatorId targetIdI = 0;
190     float fraction = 1.f;
191     std::vector<std::pair<NodeId, AnimationId>> animations;
192     EXPECT_TRUE(context.GetInteractiveImplictAnimatorMap().GetInteractiveImplictAnimator(targetId) == nullptr);
193     AnimationCommandHelper::DestoryInteractiveAnimator(context, targetIdI);
194     AnimationCommandHelper::InteractiveAnimatorAddAnimations(context, targetId, animations);
195     AnimationCommandHelper::PauseInteractiveAnimator(context, targetIdI);
196     AnimationCommandHelper::ContinueInteractiveAnimator(context, targetIdI);
197     AnimationCommandHelper::FinishInteractiveAnimator(context, targetIdI, RSInteractiveAnimationPosition::CURRENT);
198     AnimationCommandHelper::ReverseInteractiveAnimator(context, targetIdI);
199     AnimationCommandHelper::SetFractionInteractiveAnimator(context, targetIdI, fraction);
200     AnimationCommandHelper::CreateInteractiveAnimator(context, targetId, animations, false);
201     EXPECT_TRUE(context.GetInteractiveImplictAnimatorMap().GetInteractiveImplictAnimator(targetId) != nullptr);
202 
203     AnimationCommandHelper::InteractiveAnimatorAddAnimations(context, targetId, animations);
204     AnimationCommandHelper::PauseInteractiveAnimator(context, targetIdI);
205     AnimationCommandHelper::ContinueInteractiveAnimator(context, targetIdI);
206     AnimationCommandHelper::FinishInteractiveAnimator(context, targetIdI, RSInteractiveAnimationPosition::CURRENT);
207     AnimationCommandHelper::CreateInteractiveAnimator(context, targetId, animations, true);
208     AnimationCommandHelper::ReverseInteractiveAnimator(context, targetIdI);
209     AnimationCommandHelper::SetFractionInteractiveAnimator(context, targetIdI, fraction);
210     AnimationCommandHelper::DestoryInteractiveAnimator(context, targetIdI);
211     EXPECT_TRUE(context.GetInteractiveImplictAnimatorMap().GetInteractiveImplictAnimator(targetId) == nullptr);
212 }
213 } // namespace OHOS::Rosen
214