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