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 #include "base/memory/referenced.h"
18 // Add the following two macro definitions to test the private and protected method.
19 #define private public
20 #define protected public
21 #include "base/geometry/animatable_dimension.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS::Ace {
27 namespace {
28 const double DEFAULT_DOUBLE1 = 1.0;
29 constexpr double DEFAULT_DOUBLE2 = 2.0;
30 const std::string DEFAULT_STR("2.0");
31 }
32
33 class AnimatableDimensionTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void AnimatableDimensionTest::SetUpTestCase()
42 {
43 GTEST_LOG_(INFO) << "AnimatableDimensionTest SetUpTestCase";
44 }
45
TearDownTestCase()46 void AnimatableDimensionTest::TearDownTestCase()
47 {
48 GTEST_LOG_(INFO) << "AnimatableDimensionTest TearDownTestCase";
49 }
50
SetUp()51 void AnimatableDimensionTest::SetUp()
52 {
53 GTEST_LOG_(INFO) << "AnimatableDimensionTest SetUp";
54 }
55
TearDown()56 void AnimatableDimensionTest::TearDown()
57 {
58 GTEST_LOG_(INFO) << "AnimatableDimensionTest TearDown";
59 }
60
61 /**
62 * @tc.name: AnimatableDimensionTest001
63 * @tc.desc: Test the function operator= of the class AnimatableDimension.
64 * @tc.type: FUNC
65 */
66 HWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest001, TestSize.Level1)
67 {
68 /**
69 * @tc.steps1: initialize parameters.
70 */
71 AnimatableDimension animatableDimensionObj1;
72 Dimension dimension(DEFAULT_DOUBLE1);
73 CalcDimension calcDimension(DEFAULT_STR);
74
75 /**
76 * @tc.steps2: Call the function operator= with given Dimension object.
77 * @tc.expected: The return value of the function Value() is 1.0.
78 */
79 animatableDimensionObj1 = dimension;
80 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE1);
81
82 /**
83 * @tc.steps3: Call the function operator= with given CalcDimension object.
84 * @tc.expected: The return value of the function CalcValue() is DEFAULT_STR.
85 */
86 AnimatableDimension animatableDimensionObj2;
87 animatableDimensionObj2 = calcDimension;
88 EXPECT_EQ(animatableDimensionObj2.CalcValue(), DEFAULT_STR);
89
90 /**
91 * @tc.steps4: Call the function operator= with given AnimatableDimension object.
92 * @tc.expected: The return value of the function Value() is 1.0.
93 */
94 AnimatableDimension animatableDimensionObj3;
95 animatableDimensionObj3 = animatableDimensionObj1;
96 EXPECT_DOUBLE_EQ(animatableDimensionObj3.Value(), DEFAULT_DOUBLE1);
97
98 /**
99 * @tc.steps5: Call the function operator= with given AnimatableDimension object.
100 * @tc.expected: The return value of the function CalcValue() is DEFAULT_STR.
101 */
102 animatableDimensionObj3 = animatableDimensionObj2;
103 EXPECT_EQ(animatableDimensionObj3.CalcValue(), DEFAULT_STR);
104
105 /**
106 * @tc.steps6: Call the function MoveTo.
107 * @tc.expected: The return value of the function Value() is 2.0.
108 */
109 animatableDimensionObj1.MoveTo(DEFAULT_DOUBLE2);
110 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
111 }
112
113 /**
114 * @tc.name: AnimatableDimensionTest002
115 * @tc.desc: Test the function AnimateTo of the class AnimatableDimension.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest002, TestSize.Level1)
119 {
120 /**
121 * @tc.steps1: initialize parameters.
122 * @tc.expected: The value of isFirstAssign_ is true.
123 */
124 AnimatableDimension animatableDimensionObj1;
125 EXPECT_TRUE(animatableDimensionObj1.isFirstAssign_);
126
127 /**
128 * @tc.steps2: Test the function AnimateTo firstly, enter the first if-branch.
129 * @tc.expected: The value of isFirstAssign_ is set to false and the return value of
130 * the function Value is 2.0
131 */
132 animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE2);
133 EXPECT_FALSE(animatableDimensionObj1.isFirstAssign_);
134 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
135
136 /**
137 * @tc.steps3: Test the function AnimateTo secondly, enter the second if-branch.
138 * @tc.expected: The return value of the function Value is 2.0 and the value of
139 * animationController_ is set to null.
140 */
141 animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE2);
142 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
143 EXPECT_EQ(animatableDimensionObj1.animationController_, nullptr);
144
145 /**
146 * @tc.steps4: Test the function AnimateTo thirdly, the function will run until the end of it.
147 * @tc.expected: The value of animationController_ is set to non-null.
148 */
149 animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE1);
150 EXPECT_NE(animatableDimensionObj1.animationController_, nullptr);
151 }
152
153 /**
154 * @tc.name: AnimatableDimensionTest003
155 * @tc.desc: Test the function ResetController of the class AnimatableDimension.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest003, TestSize.Level1)
159 {
160 /**
161 * @tc.steps1: initialize parameters.
162 */
163 AnimatableDimension animatableDimensionObj1;
164 animatableDimensionObj1.ResetController();
165 animatableDimensionObj1.animationController_ = AceType::MakeRefPtr<Animator>(nullptr);
166
167 /**
168 * @tc.steps2: call the function ResetController.
169 * @tc.expected: The value of animationController_ is changed from non-null to null.
170 */
171 EXPECT_NE(animatableDimensionObj1.animationController_, nullptr);
172 animatableDimensionObj1.ResetController();
173 EXPECT_EQ(animatableDimensionObj1.animationController_, nullptr);
174 }
175
176 /**
177 * @tc.name: AnimatableDimensionTest004
178 * @tc.desc: Test the function OnAnimationCallback of the class AnimatableDimension.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest004, TestSize.Level1)
182 {
183 /**
184 * @tc.steps1: initialize parameters.
185 */
186 AnimatableDimension animatableDimensionObj1;
187 bool flagCbk = false;
188
189 /**
190 * @tc.steps2: Call the function OnAnimationCallback.
191 * @tc.expected: The return value of the function Value() is 1.0 and the value of animationCallback_ is null.
192 */
193 animatableDimensionObj1.OnAnimationCallback(DEFAULT_DOUBLE1);
194 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE1);
195 EXPECT_EQ(animatableDimensionObj1.animationCallback_, nullptr);
196
197 /**
198 * @tc.steps3: Set the animationCallback_ as the function which changes the value of flagCbk.
199 */
__anon0ded957d0202() 200 animatableDimensionObj1.SetContextAndCallback(nullptr, [&flagCbk] () { flagCbk = true; });
201
202 /**
203 * @tc.steps4: Call the function OnAnimationCallback again.
204 * @tc.expected: The callback function is called and the value of flagCbk is set to true.
205 */
206 animatableDimensionObj1.OnAnimationCallback(DEFAULT_DOUBLE2);
207 EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
208 EXPECT_NE(animatableDimensionObj1.animationCallback_, nullptr);
209 EXPECT_TRUE(flagCbk);
210 }
211
212 /**
213 * @tc.name: AnimatableDimensionTest005
214 * @tc.desc: Test the function ResetAnimatableDimension of the class AnimatableDimension.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest005, TestSize.Level1)
218 {
219 /**
220 * @tc.steps1: initialize parameters.
221 */
222 AnimatableDimension animatableDimensionObj1;
223 animatableDimensionObj1.isFirstAssign_ = false;
224
225 /**
226 * @tc.steps2: call the function ResetAnimatableDimension.
227 * @tc.expected: The value of flagCbk isFirstAssign_ is set to true.
228 */
229 animatableDimensionObj1.ResetAnimatableDimension();
230 EXPECT_TRUE(animatableDimensionObj1.isFirstAssign_);
231 }
232 } // namespace OHOS::Ace