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