123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "gtest/gtest.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "base/memory/referenced.h"
1923b3eb3cSopenharmony_ci// Add the following two macro definitions to test the private and protected method.
2023b3eb3cSopenharmony_ci#define private public
2123b3eb3cSopenharmony_ci#define protected public
2223b3eb3cSopenharmony_ci#include "base/geometry/animatable_dimension.h"
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ciusing namespace testing;
2523b3eb3cSopenharmony_ciusing namespace testing::ext;
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_cinamespace OHOS::Ace {
2823b3eb3cSopenharmony_cinamespace {
2923b3eb3cSopenharmony_ciconst double DEFAULT_DOUBLE1 = 1.0;
3023b3eb3cSopenharmony_ciconstexpr double DEFAULT_DOUBLE2 = 2.0;
3123b3eb3cSopenharmony_ciconst std::string DEFAULT_STR("2.0");
3223b3eb3cSopenharmony_ci} // namespace
3323b3eb3cSopenharmony_ci
3423b3eb3cSopenharmony_ciclass AnimatableDimensionTest : public testing::Test {};
3523b3eb3cSopenharmony_ci
3623b3eb3cSopenharmony_ci/**
3723b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest001
3823b3eb3cSopenharmony_ci * @tc.desc: Test the function operator= of the class AnimatableDimension.
3923b3eb3cSopenharmony_ci * @tc.type: FUNC
4023b3eb3cSopenharmony_ci */
4123b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest001, TestSize.Level1)
4223b3eb3cSopenharmony_ci{
4323b3eb3cSopenharmony_ci    /**
4423b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
4523b3eb3cSopenharmony_ci     */
4623b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
4723b3eb3cSopenharmony_ci    Dimension dimension(DEFAULT_DOUBLE1);
4823b3eb3cSopenharmony_ci    CalcDimension calcDimension(DEFAULT_STR);
4923b3eb3cSopenharmony_ci
5023b3eb3cSopenharmony_ci    /**
5123b3eb3cSopenharmony_ci     * @tc.steps2: Call the function operator= with given Dimension object.
5223b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function Value() is 1.0.
5323b3eb3cSopenharmony_ci     */
5423b3eb3cSopenharmony_ci    animatableDimensionObj1 = dimension;
5523b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE1);
5623b3eb3cSopenharmony_ci
5723b3eb3cSopenharmony_ci    /**
5823b3eb3cSopenharmony_ci     * @tc.steps3: Call the function operator= with given CalcDimension object.
5923b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function CalcValue() is DEFAULT_STR.
6023b3eb3cSopenharmony_ci     */
6123b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj2;
6223b3eb3cSopenharmony_ci    animatableDimensionObj2 = calcDimension;
6323b3eb3cSopenharmony_ci    EXPECT_EQ(animatableDimensionObj2.CalcValue(), DEFAULT_STR);
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ci    /**
6623b3eb3cSopenharmony_ci     * @tc.steps4: Call the function operator= with given AnimatableDimension object.
6723b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function Value() is 1.0.
6823b3eb3cSopenharmony_ci     */
6923b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj3;
7023b3eb3cSopenharmony_ci    animatableDimensionObj3 = animatableDimensionObj1;
7123b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj3.Value(), DEFAULT_DOUBLE1);
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    /**
7423b3eb3cSopenharmony_ci     * @tc.steps5: Call the function operator= with given AnimatableDimension object.
7523b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function CalcValue() is DEFAULT_STR.
7623b3eb3cSopenharmony_ci     */
7723b3eb3cSopenharmony_ci    animatableDimensionObj3 = animatableDimensionObj2;
7823b3eb3cSopenharmony_ci    EXPECT_EQ(animatableDimensionObj3.CalcValue(), DEFAULT_STR);
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ci    /**
8123b3eb3cSopenharmony_ci     * @tc.steps6: Call the function MoveTo.
8223b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function Value() is 2.0.
8323b3eb3cSopenharmony_ci     */
8423b3eb3cSopenharmony_ci    animatableDimensionObj1.MoveTo(DEFAULT_DOUBLE2);
8523b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
8623b3eb3cSopenharmony_ci}
8723b3eb3cSopenharmony_ci
8823b3eb3cSopenharmony_ci/**
8923b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest002
9023b3eb3cSopenharmony_ci * @tc.desc: Test the function AnimateTo of the class AnimatableDimension.
9123b3eb3cSopenharmony_ci * @tc.type: FUNC
9223b3eb3cSopenharmony_ci */
9323b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest002, TestSize.Level1)
9423b3eb3cSopenharmony_ci{
9523b3eb3cSopenharmony_ci    /**
9623b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
9723b3eb3cSopenharmony_ci     * @tc.expected: The value of isFirstAssign_ is true.
9823b3eb3cSopenharmony_ci     */
9923b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
10023b3eb3cSopenharmony_ci    EXPECT_TRUE(animatableDimensionObj1.isFirstAssign_);
10123b3eb3cSopenharmony_ci
10223b3eb3cSopenharmony_ci    /**
10323b3eb3cSopenharmony_ci     * @tc.steps2: Test the function AnimateTo firstly, enter the first if-branch.
10423b3eb3cSopenharmony_ci     * @tc.expected: The value of isFirstAssign_ is set to false and the return value of
10523b3eb3cSopenharmony_ci     *               the function Value is 2.0
10623b3eb3cSopenharmony_ci     */
10723b3eb3cSopenharmony_ci    animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE2);
10823b3eb3cSopenharmony_ci    EXPECT_FALSE(animatableDimensionObj1.isFirstAssign_);
10923b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
11023b3eb3cSopenharmony_ci
11123b3eb3cSopenharmony_ci    /**
11223b3eb3cSopenharmony_ci     * @tc.steps3: Test the function AnimateTo secondly, enter the second if-branch.
11323b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function Value is 2.0 and the value of
11423b3eb3cSopenharmony_ci     *               animationController_ is set to null.
11523b3eb3cSopenharmony_ci     */
11623b3eb3cSopenharmony_ci    animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE2);
11723b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
11823b3eb3cSopenharmony_ci    EXPECT_EQ(animatableDimensionObj1.animationController_, nullptr);
11923b3eb3cSopenharmony_ci
12023b3eb3cSopenharmony_ci    /**
12123b3eb3cSopenharmony_ci     * @tc.steps4: Test the function AnimateTo thirdly, the function will run until the end of it.
12223b3eb3cSopenharmony_ci     * @tc.expected: The value of animationController_ is set to non-null.
12323b3eb3cSopenharmony_ci     */
12423b3eb3cSopenharmony_ci    animatableDimensionObj1.AnimateTo(DEFAULT_DOUBLE1);
12523b3eb3cSopenharmony_ci    EXPECT_NE(animatableDimensionObj1.animationController_, nullptr);
12623b3eb3cSopenharmony_ci}
12723b3eb3cSopenharmony_ci
12823b3eb3cSopenharmony_ci/**
12923b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest003
13023b3eb3cSopenharmony_ci * @tc.desc: Test the function ResetController of the class AnimatableDimension.
13123b3eb3cSopenharmony_ci * @tc.type: FUNC
13223b3eb3cSopenharmony_ci */
13323b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest003, TestSize.Level1)
13423b3eb3cSopenharmony_ci{
13523b3eb3cSopenharmony_ci    /**
13623b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
13723b3eb3cSopenharmony_ci     */
13823b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
13923b3eb3cSopenharmony_ci    animatableDimensionObj1.ResetController();
14023b3eb3cSopenharmony_ci    animatableDimensionObj1.animationController_ = CREATE_ANIMATOR(nullptr);
14123b3eb3cSopenharmony_ci
14223b3eb3cSopenharmony_ci    /**
14323b3eb3cSopenharmony_ci     * @tc.steps2: call the function ResetController.
14423b3eb3cSopenharmony_ci     * @tc.expected: The value of animationController_ is changed from non-null to null.
14523b3eb3cSopenharmony_ci     */
14623b3eb3cSopenharmony_ci    EXPECT_NE(animatableDimensionObj1.animationController_, nullptr);
14723b3eb3cSopenharmony_ci    animatableDimensionObj1.ResetController();
14823b3eb3cSopenharmony_ci    EXPECT_EQ(animatableDimensionObj1.animationController_, nullptr);
14923b3eb3cSopenharmony_ci}
15023b3eb3cSopenharmony_ci
15123b3eb3cSopenharmony_ci/**
15223b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest004
15323b3eb3cSopenharmony_ci * @tc.desc: Test the function OnAnimationCallback of the class AnimatableDimension.
15423b3eb3cSopenharmony_ci * @tc.type: FUNC
15523b3eb3cSopenharmony_ci */
15623b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest004, TestSize.Level1)
15723b3eb3cSopenharmony_ci{
15823b3eb3cSopenharmony_ci    /**
15923b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
16023b3eb3cSopenharmony_ci     */
16123b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
16223b3eb3cSopenharmony_ci    bool flagCbk = false;
16323b3eb3cSopenharmony_ci
16423b3eb3cSopenharmony_ci    /**
16523b3eb3cSopenharmony_ci     * @tc.steps2: Call the function OnAnimationCallback.
16623b3eb3cSopenharmony_ci     * @tc.expected: The return value of the function Value() is 1.0 and the value of animationCallback_ is null.
16723b3eb3cSopenharmony_ci     */
16823b3eb3cSopenharmony_ci    animatableDimensionObj1.OnAnimationCallback(DEFAULT_DOUBLE1);
16923b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE1);
17023b3eb3cSopenharmony_ci    EXPECT_EQ(animatableDimensionObj1.animationCallback_, nullptr);
17123b3eb3cSopenharmony_ci
17223b3eb3cSopenharmony_ci    /**
17323b3eb3cSopenharmony_ci     * @tc.steps3: Set the animationCallback_ as the function which changes the value of flagCbk.
17423b3eb3cSopenharmony_ci     */
17523b3eb3cSopenharmony_ci    animatableDimensionObj1.SetContextAndCallback(nullptr, [&flagCbk]() { flagCbk = true; });
17623b3eb3cSopenharmony_ci
17723b3eb3cSopenharmony_ci    /**
17823b3eb3cSopenharmony_ci     * @tc.steps4: Call the function OnAnimationCallback again.
17923b3eb3cSopenharmony_ci     * @tc.expected: The callback function is called and the value of flagCbk is set to true.
18023b3eb3cSopenharmony_ci     */
18123b3eb3cSopenharmony_ci    animatableDimensionObj1.OnAnimationCallback(DEFAULT_DOUBLE2);
18223b3eb3cSopenharmony_ci    EXPECT_DOUBLE_EQ(animatableDimensionObj1.Value(), DEFAULT_DOUBLE2);
18323b3eb3cSopenharmony_ci    EXPECT_NE(animatableDimensionObj1.animationCallback_, nullptr);
18423b3eb3cSopenharmony_ci    EXPECT_TRUE(flagCbk);
18523b3eb3cSopenharmony_ci}
18623b3eb3cSopenharmony_ci
18723b3eb3cSopenharmony_ci/**
18823b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest005
18923b3eb3cSopenharmony_ci * @tc.desc: Test the function ResetAnimatableDimension of the class AnimatableDimension.
19023b3eb3cSopenharmony_ci * @tc.type: FUNC
19123b3eb3cSopenharmony_ci */
19223b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest005, TestSize.Level1)
19323b3eb3cSopenharmony_ci{
19423b3eb3cSopenharmony_ci    /**
19523b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
19623b3eb3cSopenharmony_ci     */
19723b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
19823b3eb3cSopenharmony_ci    animatableDimensionObj1.isFirstAssign_ = false;
19923b3eb3cSopenharmony_ci
20023b3eb3cSopenharmony_ci    /**
20123b3eb3cSopenharmony_ci     * @tc.steps2: call the function ResetAnimatableDimension.
20223b3eb3cSopenharmony_ci     * @tc.expected: The value of flagCbk isFirstAssign_ is set to true.
20323b3eb3cSopenharmony_ci     */
20423b3eb3cSopenharmony_ci    animatableDimensionObj1.ResetAnimatableDimension();
20523b3eb3cSopenharmony_ci    EXPECT_TRUE(animatableDimensionObj1.isFirstAssign_);
20623b3eb3cSopenharmony_ci}
20723b3eb3cSopenharmony_ci
20823b3eb3cSopenharmony_ci/**
20923b3eb3cSopenharmony_ci * @tc.name: AnimatableDimensionTest006
21023b3eb3cSopenharmony_ci * @tc.desc: Test the function operator=(const AnimatableDimension& newDimension) of the class AnimatableDimension.
21123b3eb3cSopenharmony_ci * @tc.type: FUNC
21223b3eb3cSopenharmony_ci */
21323b3eb3cSopenharmony_ciHWTEST_F(AnimatableDimensionTest, AnimatableDimensionTest006, TestSize.Level1)
21423b3eb3cSopenharmony_ci{
21523b3eb3cSopenharmony_ci    /**
21623b3eb3cSopenharmony_ci     * @tc.steps1: initialize parameters.
21723b3eb3cSopenharmony_ci     */
21823b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj1;
21923b3eb3cSopenharmony_ci    bool flagCbk = false;
22023b3eb3cSopenharmony_ci    animatableDimensionObj1.SetContextAndCallback(nullptr, [&flagCbk]() { flagCbk = true; });
22123b3eb3cSopenharmony_ci    AnimatableDimension animatableDimensionObj2;
22223b3eb3cSopenharmony_ci    animatableDimensionObj1 = animatableDimensionObj2;
22323b3eb3cSopenharmony_ci    ASSERT_NE(animatableDimensionObj1.animationCallback_, nullptr);
22423b3eb3cSopenharmony_ci    animatableDimensionObj1.SetContextAndCallback(nullptr, nullptr);
22523b3eb3cSopenharmony_ci    animatableDimensionObj1 = animatableDimensionObj2;
22623b3eb3cSopenharmony_ci    ASSERT_EQ(animatableDimensionObj1.animationCallback_, nullptr);
22723b3eb3cSopenharmony_ci}
22823b3eb3cSopenharmony_ci} // namespace OHOS::Ace
229