!34409 【质量加固】Slider支持通过Builder设置Slider样式

Merge pull request !34409 from yangziyong/slider_tdd_plus2
This commit is contained in:
openharmony_ci 2024-06-06 08:25:49 +00:00 committed by Gitee
commit 2deba7d467
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 520 additions and 0 deletions

View File

@ -18,6 +18,7 @@ ace_unittest("slider_test_ng") {
sources = [
"slider_Extend_test_ng.cpp",
"slider_builder_test_ng.cpp",
"slider_content_modifier_test_ng.cpp",
"slider_modifier_test_ng.cpp",
"slider_pattern_test_ng.cpp",
"slider_test_ng.cpp",

View File

@ -0,0 +1,519 @@
/*
* Copyright (c) 2022-2023 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"
#define private public
#define protected public
#include "test/mock/base/mock_task_executor.h"
#include "test/mock/core/render/mock_paragraph.h"
#include "base/geometry/axis.h"
#include "base/geometry/dimension.h"
#include "base/geometry/point.h"
#include "base/memory/ace_type.h"
#include "base/utils/utils.h"
#include "core/components/theme/app_theme.h"
#include "core/components_ng/base/view_stack_processor.h"
#include "core/components_ng/layout/layout_wrapper.h"
#include "core/components_ng/pattern/image/image_pattern.h"
#include "core/components_ng/pattern/slider/slider_event_hub.h"
#include "core/components_ng/pattern/slider/slider_layout_algorithm.h"
#include "core/components_ng/pattern/slider/slider_layout_property.h"
#include "core/components_ng/pattern/slider/slider_model.h"
#include "core/components_ng/pattern/slider/slider_model_ng.h"
#include "core/components_ng/pattern/slider/slider_paint_method.h"
#include "core/components_ng/pattern/slider/slider_paint_property.h"
#include "core/components_ng/pattern/slider/slider_pattern.h"
#include "core/components_ng/pattern/slider/slider_style.h"
#include "core/components_ng/render/drawing_mock.h"
#include "test/mock/core/rosen/mock_canvas.h"
#include "test/mock/core/common/mock_theme_manager.h"
#include "core/components_v2/inspector/inspector_constants.h"
#include "test/mock/core/pipeline/mock_pipeline_context.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS::Ace::NG {
namespace {
const Color TEST_COLOR = Color::BLUE;
constexpr float SLIDER_CONTENT_MODIFIER_TRACK_THICKNESS = 10.0f;
constexpr float SLIDER_CONTENT_MODIFIER_TRACK_BORDER_RADIUS = 10.0f;
constexpr float SLIDER_CONTENT_MODIFIER_SELECTED_BORDER_RADIUS = 10.0f;
constexpr float SLIDER_CONTENT_MODIFIER_STEP_SIZE = 10.0f;
constexpr float SLIDER_CONTENT_MODIFIER_STEP_RATIO = 10000.0f;
const PointF POINTF_START { 10.0f, 10.0f };
const PointF POINTF_END { 20.0f, 20.0f };
} // namespace
class SliderContentModifierTestNg : public testing::Test {
public:
void TearDown() override;
static void SetUpTestSuite();
static void TearDownTestSuite();
private:
void SetSliderContentModifier(SliderContentModifier& sliderContentModifier);
void MockCanvasFunction(Testing::MockCanvas& canvas);
void MockTipsCanvasFunction(Testing::MockCanvas& canvas);
void MockParagraphFunction(RefPtr<MockParagraph>& paragraph, Testing::MockCanvas& canvas);
};
void SliderContentModifierTestNg::SetUpTestSuite()
{
MockPipelineContext::SetUp();
}
void SliderContentModifierTestNg::TearDownTestSuite()
{
MockPipelineContext::TearDown();
}
void SliderContentModifierTestNg::TearDown()
{
MockParagraph::TearDown();
}
void SliderContentModifierTestNg::SetSliderContentModifier(SliderContentModifier& sliderContentModifier)
{
sliderContentModifier.InitializeShapeProperty();
sliderContentModifier.SetTrackThickness(SLIDER_CONTENT_MODIFIER_TRACK_THICKNESS);
sliderContentModifier.SetTrackBorderRadius(SLIDER_CONTENT_MODIFIER_TRACK_BORDER_RADIUS);
sliderContentModifier.SetSelectedBorderRadius(SLIDER_CONTENT_MODIFIER_SELECTED_BORDER_RADIUS);
sliderContentModifier.SetTrackBackgroundColor(SliderModelNG::CreateSolidGradient(TEST_COLOR));
sliderContentModifier.SetShowSteps(true);
sliderContentModifier.SetStepSize(SLIDER_CONTENT_MODIFIER_STEP_SIZE);
sliderContentModifier.SetStepColor(TEST_COLOR);
sliderContentModifier.SetStepRatio(SLIDER_CONTENT_MODIFIER_STEP_RATIO);
SizeF blockSize;
sliderContentModifier.SetBlockSize(blockSize);
}
void SliderContentModifierTestNg::MockCanvasFunction(Testing::MockCanvas& canvas)
{
EXPECT_CALL(canvas, AttachBrush(_)).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, DrawRoundRect(_)).WillRepeatedly(Return());
EXPECT_CALL(canvas, DetachBrush()).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, DrawCircle(_, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, AttachPen(_)).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, DetachPen()).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, Save()).WillRepeatedly(Return());
EXPECT_CALL(canvas, Scale(_, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, Translate(_, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, Restore()).WillRepeatedly(Return());
EXPECT_CALL(canvas, ClipRect(_, _, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, DrawPath(_)).WillRepeatedly(Return());
}
void SliderContentModifierTestNg::MockTipsCanvasFunction(Testing::MockCanvas& canvas)
{
EXPECT_CALL(canvas, Save()).WillRepeatedly(Return());
EXPECT_CALL(canvas, Scale(_, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, Translate(_, _)).WillRepeatedly(Return());
EXPECT_CALL(canvas, Restore()).WillRepeatedly(Return());
EXPECT_CALL(canvas, AttachPen(_)).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, AttachBrush(_)).WillRepeatedly(ReturnRef(canvas));
EXPECT_CALL(canvas, DrawPath(_)).WillRepeatedly(Return());
EXPECT_CALL(canvas, ClipPath(_, _, _)).WillRepeatedly(Return());
}
void SliderContentModifierTestNg::MockParagraphFunction(RefPtr<MockParagraph>& paragraph, Testing::MockCanvas& canvas)
{
EXPECT_CALL(*paragraph, Paint(An<RSCanvas&>(), _, _)).WillRepeatedly(Return());
EXPECT_CALL(*paragraph, Layout(_)).WillRepeatedly(Return());
EXPECT_CALL(*paragraph, PushStyle(_)).WillRepeatedly(Return());
EXPECT_CALL(*paragraph, AddText(_)).WillRepeatedly(Return());
EXPECT_CALL(*paragraph, Build()).WillRepeatedly(Return());
EXPECT_CALL(*paragraph, GetTextWidth()).WillRepeatedly(Return(1.0f));
EXPECT_CALL(*paragraph, GetHeight()).WillRepeatedly(Return(1.0f));
}
/**
* @tc.name: SliderContentModifierTest001
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest001, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateValue(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest002
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest002, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateMin(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(100.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest003
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest003, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateMax(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(0.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest004
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest004, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateStep(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(0.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(100.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest005
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest005, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateValue(100.0f);
sliderPaintProperty->UpdateMin(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(100.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest006
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest006, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateValue(100.0f);
sliderPaintProperty->UpdateMax(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest007
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest007, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateValue(100.0f);
sliderPaintProperty->UpdateStep(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(100.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest008
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest008, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateMin(100.0f);
sliderPaintProperty->UpdateMax(200.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(100.0f, config.min_);
EXPECT_EQ(200.0f, config.max_);
EXPECT_EQ(1.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest009
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest009, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateMin(100.0f);
sliderPaintProperty->UpdateStep(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(100.0f, config.value_);
EXPECT_EQ(100.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(100.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
/**
* @tc.name: SliderContentModifierTest010
* @tc.desc: SetBuilderFunc and get value
* @tc.type: FUNC
*/
HWTEST_F(SliderContentModifierTestNg, SliderContentModifierTest010, TestSize.Level1)
{
/**
* @tc.steps: step1. Init Slider node.
*/
auto sliderPattern = AceType::MakeRefPtr<SliderPattern>();
ASSERT_NE(sliderPattern, nullptr);
auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::SLIDER_ETS_TAG, -1, sliderPattern);
sliderPattern->AttachToFrameNode(frameNode);
ASSERT_NE(frameNode, nullptr);
auto sliderPaintProperty = frameNode->GetPaintProperty<SliderPaintProperty>();
ASSERT_NE(sliderPaintProperty, nullptr);
auto eventHub = frameNode->GetEventHub<NG::SliderEventHub>();
CHECK_NULL_VOID(eventHub);
eventHub->SetEnabled(true);
sliderPaintProperty->UpdateMax(100.0f);
sliderPaintProperty->UpdateStep(100.0f);
auto node = [](SliderConfiguration config) -> RefPtr<FrameNode> {
EXPECT_EQ(0.0f, config.value_);
EXPECT_EQ(0.0f, config.min_);
EXPECT_EQ(100.0f, config.max_);
EXPECT_EQ(100.0f, config.step_);
EXPECT_EQ(true, config.enabled_);
return nullptr;
};
/**
* @tc.steps: step2. Set parameters to pattern builderFunc
*/
sliderPattern->SetBuilderFunc(node);
sliderPattern->BuildContentModifierNode();
}
} // namespace OHOS::Ace::NG