1/*
2 * Copyright (c) 2022 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
18#include "base/memory/referenced.h"
19// Add the following two macro definitions to test the private and protected method.
20#define private public
21#define protected public
22#include "base/geometry/animatable_matrix4.h"
23
24using namespace testing;
25using namespace testing::ext;
26
27namespace OHOS::Ace {
28namespace {
29const int32_t VALID_ROW0 = 0;
30const int32_t VALID_COL0 = 0;
31const double DEFAULT_DOUBLE0 = 0.0;
32} // namespace
33
34class AnimatableMatrix4Test : public testing::Test {};
35
36/**
37 * @tc.name: AnimatableMatrix4Test001
38 * @tc.desc: Test the function operator= of the class AnimatableMatrix4.
39 * @tc.type: FUNC
40 */
41HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test001, TestSize.Level1)
42{
43    /**
44     * @tc.steps: Test the function operator= with given Matrix4 object.
45     */
46    AnimatableMatrix4 animatableMatrix4Obj1;
47    Matrix4 matrixObj1 = Matrix4::CreateIdentity();
48    animatableMatrix4Obj1 = matrixObj1;
49    EXPECT_TRUE(animatableMatrix4Obj1.IsIdentityMatrix());
50    matrixObj1.Set(VALID_ROW0, VALID_COL0, DEFAULT_DOUBLE0);
51
52    /**
53     * @tc.steps: Test the function MoveTo with given Matrix4 object.
54     */
55    animatableMatrix4Obj1.MoveTo(matrixObj1);
56    EXPECT_DOUBLE_EQ(animatableMatrix4Obj1.Get(VALID_ROW0, VALID_COL0), DEFAULT_DOUBLE0);
57
58    /**
59     * @tc.steps: Test the function operator= with given AnimatableMatrix4 object.
60     */
61    AnimatableMatrix4 animatableMatrix4Obj2;
62    animatableMatrix4Obj2 = animatableMatrix4Obj1;
63    EXPECT_DOUBLE_EQ(animatableMatrix4Obj2.Get(VALID_ROW0, VALID_COL0), DEFAULT_DOUBLE0);
64}
65
66/**
67 * @tc.name: AnimatableMatrix4Test002
68 * @tc.desc: Test the function AnimateTo of the class AnimatableMatrix4.
69 * @tc.type: FUNC
70 */
71HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test002, TestSize.Level1)
72{
73    /**
74     * @tc.steps: initialize parameters.
75     */
76    AnimatableMatrix4 animatableMatrix4Obj1;
77    Matrix4 endValue = Matrix4::CreateIdentity();
78    EXPECT_TRUE(animatableMatrix4Obj1.isFirstAssign_);
79
80    /**
81     * @tc.steps: Test the function AnimateTo firstly, enter the first if-branch.
82     */
83    animatableMatrix4Obj1.AnimateTo(endValue);
84    EXPECT_FALSE(animatableMatrix4Obj1.isFirstAssign_);
85    EXPECT_TRUE(animatableMatrix4Obj1.IsIdentityMatrix());
86    EXPECT_EQ(animatableMatrix4Obj1.animationController_, nullptr);
87
88    /**
89     * @tc.steps: Test the function AnimateTo secondly, enter the second if-branch.
90     */
91    animatableMatrix4Obj1.AnimateTo(animatableMatrix4Obj1);
92    EXPECT_EQ(animatableMatrix4Obj1.animationController_, nullptr);
93
94    /**
95     * @tc.steps: Test the function AnimateTo thirdly, the function will run until the end of it.
96     */
97    animatableMatrix4Obj1.AnimateTo(endValue);
98    EXPECT_EQ(animatableMatrix4Obj1.animationController_, nullptr);
99}
100
101/**
102 * @tc.name: AnimatableMatrix4Test003
103 * @tc.desc: Test the function ResetController of the class AnimatableMatrix4.
104 * @tc.type: FUNC
105 */
106HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test003, TestSize.Level1)
107{
108    AnimatableMatrix4 animatableMatrix4Obj1;
109    animatableMatrix4Obj1.ResetController();
110    animatableMatrix4Obj1.animationController_ = CREATE_ANIMATOR(nullptr);
111    EXPECT_NE(animatableMatrix4Obj1.animationController_, nullptr);
112    animatableMatrix4Obj1.ResetController();
113    EXPECT_EQ(animatableMatrix4Obj1.animationController_, nullptr);
114}
115
116/**
117 * @tc.name: AnimatableMatrix4Test004
118 * @tc.desc: Test the function OnAnimationCallback of the class AnimatableMatrix4.
119 * @tc.type: FUNC
120 */
121HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test004, TestSize.Level1)
122{
123    /**
124     * @tc.steps: initialize parameters.
125     */
126    TransformOperation transformOperation;
127    AnimatableMatrix4 animatableMatrix4Obj1;
128    animatableMatrix4Obj1.OnAnimationCallback(transformOperation);
129    bool flagEventCbk = false;
130    animatableMatrix4Obj1.SetContextAndCallback(nullptr, [&flagEventCbk]() { flagEventCbk = true; });
131    /**
132     * @tc.steps: Set the animationCallback_ as the function which changes the value of flagEventCbk,
133     *            the value will be modified when the onReadyEvent is fired.
134     */
135    animatableMatrix4Obj1.OnAnimationCallback(transformOperation);
136    EXPECT_TRUE(flagEventCbk);
137}
138
139/**
140 * @tc.name: AnimatableMatrix4Test005
141 * @tc.desc: Test the function ResetAnimatableMatrix of the class AnimatableMatrix4.
142 * @tc.type: FUNC
143 */
144HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test005, TestSize.Level1)
145{
146    AnimatableMatrix4 animatableMatrix4Obj1;
147    animatableMatrix4Obj1.isFirstAssign_ = false;
148    animatableMatrix4Obj1.ResetAnimatableMatrix();
149    EXPECT_TRUE(animatableMatrix4Obj1.isFirstAssign_);
150}
151
152/**
153 * @tc.name: AnimatableMatrix4Test006
154 * @tc.desc: Test the function reload operator=
155 * @tc.type: FUNC
156 */
157HWTEST_F(AnimatableMatrix4Test, AnimatableMatrix4Test006, TestSize.Level1)
158{
159    AnimatableMatrix4 animatableMatrix4Obj1;
160    AnimatableMatrix4 animatableMatrix4Obj2;
161    ASSERT_EQ(animatableMatrix4Obj1.animationCallback_, nullptr);
162    ASSERT_EQ(animatableMatrix4Obj2.animationCallback_, nullptr);
163    // true true, both animationCallback_ and pipelineContext nullptr
164    animatableMatrix4Obj2 = animatableMatrix4Obj1;
165    ASSERT_EQ(animatableMatrix4Obj2.animationCallback_, nullptr);
166    // false true, animationCallback_ not nullptr, pipelineContext  nullptr
167    bool flagEventCbk = false;
168    animatableMatrix4Obj2.SetContextAndCallback(nullptr, [&flagEventCbk]() { flagEventCbk = true; });
169    animatableMatrix4Obj2 = animatableMatrix4Obj1;
170    ASSERT_NE(animatableMatrix4Obj2.animationCallback_, nullptr);
171}
172} // namespace OHOS::Ace
173