新增tdd

Signed-off-by: kangchongtao <kangchongtao@huawei.com>
This commit is contained in:
kangchongtao 2024-06-20 22:13:29 +08:00
parent f1688495fe
commit f918577c28
5 changed files with 246 additions and 5 deletions

View File

@ -35,6 +35,7 @@ ace_unittest("util_test") {
sources = [
"base_utils_test.cpp",
"json_util_test.cpp",
"node_object_test.cpp",
"uobject_test.cpp",
]

View File

@ -16,9 +16,9 @@
#include <memory>
#include "gtest/gtest.h"
#include "test/mock/core/pipeline/mock_pipeline_context.h"
#include "base/geometry/dimension.h"
#include "test/mock/core/pipeline/mock_pipeline_context.h"
using namespace testing;
using namespace testing::ext;
@ -35,6 +35,7 @@ const Dimension DIMENSION_LPX(DEFAULT_DOUBLE, DimensionUnit::LPX);
const Dimension DIMENSION_PCT(DEFAULT_DOUBLE, DimensionUnit::PERCENT);
const Dimension DIMENSION_AUTO(DEFAULT_DOUBLE, DimensionUnit::AUTO);
const Dimension DIMENSION_CALC(DEFAULT_DOUBLE, DimensionUnit::CALC);
const Dimension DIMENSION_NONE(DEFAULT_DOUBLE, DimensionUnit::NONE);
const std::string DIMENSION_PX_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE).append("px");
const std::string DIMENSION_VP_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE).append("vp");
@ -42,9 +43,9 @@ const std::string DIMENSION_FP_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE)
const std::string DIMENSION_LPX_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE).append("lpx");
const std::string DIMENSION_PCT_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE * 100).append("%");
const std::string DIMENSION_AUTO_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE).append("auto");
const std::string DIMENSION_NONE_STR = StringUtils::DoubleToString(DEFAULT_DOUBLE).append("none");
} // namespace
class DimensionTest : public testing::Test {
public:
static void SetUpTestSuite()
@ -75,6 +76,7 @@ HWTEST_F(DimensionTest, DimensionTest001, TestSize.Level1)
EXPECT_DOUBLE_EQ(DIMENSION_PCT.ConvertToVp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_AUTO.ConvertToVp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_CALC.ConvertToVp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_NONE.ConvertToVp(), DEFAULT_DOUBLE);
}
/**
@ -95,6 +97,7 @@ HWTEST_F(DimensionTest, DimensionTest002, TestSize.Level1)
EXPECT_DOUBLE_EQ(DIMENSION_PCT.ConvertToPx(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_AUTO.ConvertToPx(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_CALC.ConvertToPx(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_NONE.ConvertToPx(), DEFAULT_DOUBLE);
}
/**
@ -114,6 +117,11 @@ HWTEST_F(DimensionTest, DimensionTest003, TestSize.Level1)
EXPECT_EQ(DIMENSION_LPX.ToString(), DIMENSION_LPX_STR);
EXPECT_EQ(DIMENSION_PCT.ToString(), DIMENSION_PCT_STR);
EXPECT_EQ(DIMENSION_AUTO.ToString(), DIMENSION_AUTO_STR);
Dimension dimension(DEFAULT_DOUBLE);
dimension.SetUnit(static_cast<DimensionUnit>(7));
EXPECT_EQ(dimension.ToString(), DIMENSION_PX_STR);
dimension.SetUnit(DimensionUnit::NONE);
EXPECT_EQ(dimension.ToString(), DIMENSION_NONE_STR);
}
/**
@ -249,4 +257,62 @@ HWTEST_F(DimensionTest, DimensionTest005, TestSize.Level1)
EXPECT_FALSE(DIMENSION_CALC.NormalizeToPx(DEFAULT_DOUBLE, DEFAULT_DOUBLE, DEFAULT_DOUBLE, DEFAULT_DOUBLE, result));
EXPECT_DOUBLE_EQ(result, ZERO_DOUBLE);
}
/**
* @tc.name: DimensionTest006
* @tc.desc: ConvertToFp().
* @tc.type: FUNC
*/
HWTEST_F(DimensionTest, DimensionTest006, TestSize.Level1)
{
/**
* @tc.steps1: Test the function ConvertToFp of the class Dimension.
* @tc.expected: The return values are equal to DEFAULT_DOUBLE or ZERO_DOUBLE
*/
EXPECT_DOUBLE_EQ(DIMENSION_PX.ConvertToFp(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_VP.ConvertToFp(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_FP.ConvertToFp(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_LPX.ConvertToFp(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_PCT.ConvertToFp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_AUTO.ConvertToFp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_CALC.ConvertToFp(), ZERO_DOUBLE);
EXPECT_DOUBLE_EQ(DIMENSION_NONE.ConvertToFp(), DEFAULT_DOUBLE);
}
/**
* @tc.name: DimensionTest007
* @tc.desc: FromString().
* @tc.type: FUNC
*/
HWTEST_F(DimensionTest, DimensionTest007, TestSize.Level1)
{
/**
* @tc.steps1: Test the function FromString of the class Dimension.
* @tc.expected: The return values are equal to DEFAULT_DOUBLE or ZERO_DOUBLE
*/
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_PX_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_VP_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_FP_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_LPX_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_PCT_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString(DIMENSION_AUTO_STR).Value(), DEFAULT_DOUBLE);
EXPECT_DOUBLE_EQ(Dimension::FromString("").Value(), ZERO_DOUBLE);
}
/**
* @tc.name: DimensionTest008
* @tc.desc: ConvertToPxWithSize().
* @tc.type: FUNC
*/
HWTEST_F(DimensionTest, DimensionTest008, TestSize.Level1)
{
/**
* @tc.steps1: Test the function ConvertToPxWithSize of the class Dimension.
* @tc.expected: The return values are equal to DEFAULT_DOUBLE * size.
*/
Dimension dimension(DEFAULT_DOUBLE, DimensionUnit::PERCENT);
EXPECT_DOUBLE_EQ(dimension.ConvertToPxWithSize(5.0), DEFAULT_DOUBLE * 5.0);
dimension.SetUnit(DimensionUnit::PX);
EXPECT_DOUBLE_EQ(dimension.ConvertToPxWithSize(5.0), DEFAULT_DOUBLE);
}
} // namespace OHOS::Ace

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include "base/json/json_util.h"
#include "base/json/node_object.h"
#include "base/json/uobject.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS::Ace {
namespace {} // namespace
class NodeObjectTest : public testing::Test {};
/**
* @tc.name: NodeObjectTest001
* @tc.desc: AddItemToObject()
* @tc.type: FUNC
*/
HWTEST_F(NodeObjectTest, NodeObjectTest001, TestSize.Level1)
{
/**
* @tc.steps: step1. Create a NodeObject.
*/
auto nodeObject = NodeObject::Create();
/**
* @tc.steps: step2. Save value through AdditemToObject.
* @tc.expected: step2. Asserting values through the get method.
*/
std::string stringValue = "test";
double doubleValue = 1.0;
size_t sizeValue = 10;
int32_t int32Value = 5;
int64_t int64Value = 1;
bool boolValue = false;
nodeObject->Put(nullptr, const_cast<char*>(stringValue.c_str()));
nodeObject->Put(nullptr, nullptr);
nodeObject->Put(nullptr, doubleValue);
nodeObject->Put(nullptr, sizeValue);
nodeObject->Put(nullptr, int32Value);
nodeObject->Put(nullptr, int64Value);
nodeObject->Put(nullptr, boolValue);
std::string str = "string";
nodeObject->Put(const_cast<char*>(str.c_str()), const_cast<char*>(stringValue.c_str()));
EXPECT_EQ(nodeObject->GetString("string", ""), "test");
EXPECT_EQ(nodeObject->GetString("", "test"), "test");
str = "double";
nodeObject->Put(const_cast<char*>(str.c_str()), doubleValue);
EXPECT_EQ(nodeObject->GetDouble("double", 0), 1.0);
EXPECT_EQ(nodeObject->GetDouble("", 0), 0);
str = "int32_t";
nodeObject->Put(const_cast<char*>(str.c_str()), int32Value);
EXPECT_EQ(nodeObject->GetInt("int32_t", 0), 5);
EXPECT_EQ(nodeObject->GetInt("", 0), 0);
str = "int64_t";
nodeObject->Put(const_cast<char*>(str.c_str()), int64Value);
EXPECT_EQ(nodeObject->GetInt64("int64_t", 0), 1);
EXPECT_EQ(nodeObject->GetInt64("", 0), 0);
str = "bool";
nodeObject->Put(const_cast<char*>(str.c_str()), boolValue);
EXPECT_EQ(nodeObject->GetBool("bool", true), false);
EXPECT_EQ(nodeObject->GetBool("", true), true);
}
} // namespace OHOS::Ace

View File

@ -248,4 +248,19 @@ HWTEST_F(TransformUtilTest, TransformUtilTest008, TestSize.Level1)
EXPECT_EQ(result.GetOperations()[3].rotateOperation_, RotateOperation());
EXPECT_EQ(result.GetOperations()[4].perspectiveOperation_, PerspectiveOperation());
}
/**
* @tc.name: TransformUtilTest009
* @tc.desc: Test the functions of the class ComposeTransform.
* @tc.type: FUNC
*/
HWTEST_F(TransformUtilTest, TransformUtilTest009, TestSize.Level1)
{
DecomposedTransform decomposedTransform;
decomposedTransform.skew[0] = 1.0f;
decomposedTransform.skew[1] = 1.0f;
decomposedTransform.skew[2] = 1.0f;
EXPECT_EQ(TransformUtil::ComposeTransform(decomposedTransform).GetScaleX(), 1.0);
EXPECT_EQ(TransformUtil::ComposeTransform(decomposedTransform).GetScaleY(), 1.0);
}
} // namespace OHOS::Ace

View File

@ -1214,9 +1214,7 @@ HWTEST_F(UINodeTestNg, UINodeTestNg044, TestSize.Level1)
ASSERT_NE(testNode, nullptr);
int32_t testId = 0;
testNode->RegisterUpdateJSInstanceCallback([&testId](int32_t newId) {
testId = newId;
});
testNode->RegisterUpdateJSInstanceCallback([&testId](int32_t newId) { testId = newId; });
/**
* @tc.steps: step2. attach context
@ -1537,4 +1535,83 @@ HWTEST_F(UINodeTestNg, DumpTreeById002, TestSize.Level1)
result = parent->UINode::DumpTreeById(1, "");
EXPECT_TRUE(result);
}
/**
* @tc.name: UINodeTestNg045
* @tc.desc: IsContextTransparent()GetPageNodeCountAndDepth()
* @tc.type: FUNC
*/
HWTEST_F(UINodeTestNg, UINodeTestNg045, TestSize.Level1)
{
/**
* @tc.steps: step1. create frame node
*/
auto parentId = ElementRegister::GetInstance()->MakeUniqueId();
auto childId = ElementRegister::GetInstance()->MakeUniqueId();
auto parent = FrameNode::CreateFrameNode("parent", parentId, AceType::MakeRefPtr<Pattern>(), true);
auto child = FrameNode::CreateFrameNode("child", childId, AceType::MakeRefPtr<Pattern>(), true);
/**
* @tc.steps: step2. call IsContextTransparent
* @tc.expected: result is True
*/
parent->AddChild(child);
EXPECT_TRUE(parent->UINode::IsContextTransparent());
int32_t count = 0;
int32_t depth = 0;
parent->GetPageNodeCountAndDepth(&count, &depth);
EXPECT_EQ(parent->depth_, INT32_MAX);
EXPECT_EQ(parent->depth_, INT32_MAX);
auto child1 = FrameNode::CreateFrameNode(
"child1", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
auto child2 = FrameNode::CreateFrameNode(
"child2", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
child2->tag_ = V2::JS_VIEW_ETS_TAG;
child1->AddChild(child2);
child1->AddChild(ONE);
std::list<int32_t> removedElmtId;
parent->UINode::CollectRemovedChildren(child1->GetChildren(), removedElmtId, true);
parent->UINode::GetFrameChildByIndexWithoutExpanded(0);
parent->UINode::SetJSViewActive(false);
parent->UINode::RenderCustomChild(0);
std::vector<RefPtr<UINode>> res;
parent->DFSAllChild(child1, res);
EXPECT_EQ(res.size(), 2);
}
/**
* @tc.name: UINodeTestNg046
* @tc.desc: IsContextTransparent()GetPageNodeCountAndDepth()
* @tc.type: FUNC
*/
HWTEST_F(UINodeTestNg, UINodeTestNg046, TestSize.Level1)
{
/**
* @tc.steps: step1. create frame node
*/
auto parentId = ElementRegister::GetInstance()->MakeUniqueId();
auto childId = ElementRegister::GetInstance()->MakeUniqueId();
auto parent = FrameNode::CreateFrameNode("parent", parentId, AceType::MakeRefPtr<Pattern>(), true);
auto child = FrameNode::CreateFrameNode("child", childId, AceType::MakeRefPtr<Pattern>(), true);
/**
* @tc.steps: step2. call IsContextTransparent
* @tc.expected: result is True
*/
parent->AddChild(child);
parent->AddChild(ONE);
auto child1 = FrameNode::CreateFrameNode(
"child1", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
auto child2 = FrameNode::CreateFrameNode(
"child2", ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<Pattern>(), true);
child2->tag_ = V2::JS_VIEW_ETS_TAG;
child1->AddChild(child2);
std::list<int32_t> removedElmtId;
EXPECT_TRUE(parent->UINode::GetContextWithCheck());
EXPECT_EQ(parent->UINode::GetFrameNodeIndex(child, true), 0);
EXPECT_EQ(parent->UINode::GetFrameNodeIndex(child1, false), -1);
}
} // namespace OHOS::Ace::NG