From b1ee76f9f121e0721a437f8ebd403c11b605da6b Mon Sep 17 00:00:00 2001 From: Zhang-Dong-hui Date: Mon, 10 Apr 2023 01:10:09 +0000 Subject: [PATCH] add patternlock modifier Signed-off-by: Zhang-Dong-hui Change-Id: I6fc171b074759873bec9f8c7603aad4e8d639ab0 --- .../core/components_ng/pattern/BUILD.gn | 1 + .../patternlock/patternlock_modifier.cpp | 282 +++++++++++++++ .../patternlock/patternlock_modifier.h | 103 ++++++ .../patternlock/patternlock_paint_method.cpp | 226 ++---------- .../patternlock/patternlock_paint_method.h | 85 ++--- .../patternlock/patternlock_pattern.cpp | 37 +- .../pattern/patternlock/patternlock_pattern.h | 27 +- .../test/pattern/patternlock/BUILD.gn | 4 +- .../patternlock_paint_method_test_ng.cpp | 280 --------------- .../patternlock_pattern_test_ng.cpp | 340 +++++++++++++++--- 10 files changed, 778 insertions(+), 607 deletions(-) create mode 100644 frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.cpp create mode 100644 frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.h delete mode 100644 frameworks/core/components_ng/test/pattern/patternlock/patternlock_paint_method_test_ng.cpp diff --git a/frameworks/core/components_ng/pattern/BUILD.gn b/frameworks/core/components_ng/pattern/BUILD.gn index e4c67ada050..f0a8ddc5d61 100644 --- a/frameworks/core/components_ng/pattern/BUILD.gn +++ b/frameworks/core/components_ng/pattern/BUILD.gn @@ -225,6 +225,7 @@ build_component_ng("pattern_ng") { "panel/sliding_panel_pattern.cpp", "patternlock/patternlock_layout_algorithm.cpp", "patternlock/patternlock_model_ng.cpp", + "patternlock/patternlock_modifier.cpp", "patternlock/patternlock_paint_method.cpp", "patternlock/patternlock_pattern.cpp", "picker/date_time_animation_controller.cpp", diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.cpp b/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.cpp new file mode 100644 index 00000000000..067508dd539 --- /dev/null +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.cpp @@ -0,0 +1,282 @@ +/* + * Copyright (c) 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 "core/components_ng/pattern/patternlock/patternlock_modifier.h" + +#include +#include +#include + +#include "base/geometry/dimension.h" +#include "base/geometry/ng/offset_t.h" +#include "base/geometry/ng/size_t.h" +#include "base/log/log_wrapper.h" +#include "base/memory/ace_type.h" +#include "base/utils/utils.h" +#include "core/components/common/properties/color.h" +#include "core/components_ng/base/modifier.h" +#include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" +#include "core/components_ng/render/drawing.h" +#include "core/components_ng/render/drawing_prop_convertor.h" + +namespace OHOS::Ace::NG { +namespace { +constexpr int32_t PATTERN_LOCK_COL_COUNT = 3; +constexpr int32_t RADIUS_TO_DIAMETER = 2; +constexpr float GRADUAL_CHANGE_POINT = 0.5; +// the scale of active circle radius and normal circle radius +constexpr float SCALE_ACTIVE_CIRCLE_RADIUS = 16.00 / 14.00; +// the scale of selected circle radius and normal circle radius +constexpr float SCALE_SELECTED_CIRCLE_RADIUS = 26.00 / 14.00; +constexpr int32_t MAX_ALPHA = 255; +} // namespace + +PatternLockCell::PatternLockCell(int32_t column, int32_t row) +{ + column_ = column; + row_ = row; + code_ = PATTERN_LOCK_COL_COUNT * (row - 1) + (column - 1); +} + +PatternLockModifier::PatternLockModifier() +{ + sideLength_ = AceType::MakeRefPtr(0.0f); + circleRadius_ = AceType::MakeRefPtr(0.0f); + regularColor_ = AceType::MakeRefPtr(LinearColor(Color::BLACK)); + selectedColor_ = AceType::MakeRefPtr(LinearColor(Color::BLACK)); + activeColor_ = AceType::MakeRefPtr(LinearColor(Color::BLACK)); + pathColor_ = AceType::MakeRefPtr(LinearColor(Color::BLUE)); + pathStrokeWidth_ = AceType::MakeRefPtr(0.0f); + offset_ = AceType::MakeRefPtr(OffsetF()); + cellCenter_ = AceType::MakeRefPtr(OffsetF()); + isMoveEventValid_ = AceType::MakeRefPtr(false); + + AttachProperty(sideLength_); + AttachProperty(circleRadius_); + AttachProperty(regularColor_); + AttachProperty(selectedColor_); + AttachProperty(activeColor_); + AttachProperty(pathColor_); + AttachProperty(pathStrokeWidth_); + AttachProperty(cellCenter_); + AttachProperty(offset_); + AttachProperty(isMoveEventValid_); +} + +void PatternLockModifier::onDraw(DrawingContext& context) +{ + auto& canvas = context.canvas; + PaintLockLine(canvas, offset_->Get()); + for (int i = 0; i < PATTERN_LOCK_COL_COUNT; i++) { + for (int j = 0; j < PATTERN_LOCK_COL_COUNT; j++) { + PaintLockCircle(canvas, offset_->Get(), i + 1, j + 1); + } + } +} + +void PatternLockModifier::PaintLockLine(RSCanvas& canvas, const OffsetF& offset) +{ + size_t count = choosePoint_.size(); + if (count == 0) { + return; + } + + float sideLength = sideLength_->Get(); + float pathStrokeWidth = pathStrokeWidth_->Get(); + if (LessOrEqual(pathStrokeWidth, 0.0)) { + return; + } + float handleStrokeWidth = std::min(pathStrokeWidth, sideLength / PATTERN_LOCK_COL_COUNT); + pathStrokeWidth = std::max(handleStrokeWidth, 0.0f); + + auto pathColor = pathColor_->Get(); + auto cellCenter = cellCenter_->Get(); + RSPen pen; + pen.SetAntiAlias(true); + pen.SetColor(ToRSColor(pathColor)); + pen.SetWidth(pathStrokeWidth); + pen.SetCapStyle(RSPen::CapStyle::ROUND_CAP); + + Color pathColorAlpha255 = pathColor.ToColor().ChangeAlpha(MAX_ALPHA); + pen.SetColor(pathColorAlpha255.GetValue()); + canvas.AttachPen(pen); + for (size_t i = 0; i < count - 1; i++) { + OffsetF pointBegin = GetCircleCenterByXY(offset, choosePoint_[i].GetColumn(), choosePoint_[i].GetRow()); + OffsetF pointEnd = GetCircleCenterByXY(offset, choosePoint_[i + 1].GetColumn(), choosePoint_[i + 1].GetRow()); + canvas.DrawLine(RSPoint(pointBegin.GetX(), pointBegin.GetY()), RSPoint(pointEnd.GetX(), pointEnd.GetY())); + } + if (isMoveEventValid_->Get()) { + OffsetF pointBegin = + GetCircleCenterByXY(offset, choosePoint_[count - 1].GetColumn(), choosePoint_[count - 1].GetRow()); + float x1 = pointBegin.GetX(); + float y1 = pointBegin.GetY(); + float x2 = offset.GetX() + cellCenter.GetX(); + float y2 = offset.GetY() + cellCenter.GetY(); + x2 = x2 > offset.GetX() + sideLength ? offset.GetX() + sideLength : x2; + x2 = x2 < offset.GetX() ? offset.GetX() : x2; + y2 = y2 > offset.GetY() + sideLength ? offset.GetY() + sideLength : y2; + y2 = y2 < offset.GetY() ? offset.GetY() : y2; + + std::vector colors = { pathColorAlpha255.GetValue(), pathColorAlpha255.GetValue(), + pathColorAlpha255.ChangeOpacity(0.0).GetValue() }; + std::vector pos = { 0.0, GRADUAL_CHANGE_POINT, 1.0 }; + auto shader = pen.GetShaderEffect(); + shader->CreateLinearGradient(RSPoint(x1, y1), RSPoint(x2, y2), colors, pos, RSTileMode::CLAMP); + pen.SetShaderEffect(shader); + canvas.DrawLine(RSPoint(x1, y1), RSPoint(x2, y2)); + } + canvas.DetachPen(); + canvas.Restore(); +} + +void PatternLockModifier::PaintLockCircle(RSCanvas& canvas, const OffsetF& offset, int32_t x, int32_t y) +{ + auto activeColor = activeColor_->Get(); + auto regularColor = regularColor_->Get(); + auto selectedColor = selectedColor_->Get(); + auto sideLength = sideLength_->Get(); + auto circleRadius = circleRadius_->Get(); + + RSBrush brush; + brush.SetAntiAlias(true); + brush.SetColor(ToRSColor(regularColor)); + OffsetF cellcenter = GetCircleCenterByXY(offset, x, y); + OffsetF firstCellcenter = GetCircleCenterByXY(offset, 1, 1); + float offsetX = cellcenter.GetX(); + float offsetY = cellcenter.GetY(); + const int32_t radiusCount = RADIUS_TO_DIAMETER * PATTERN_LOCK_COL_COUNT; + float handleCircleRadius = std::min(circleRadius, sideLength / SCALE_SELECTED_CIRCLE_RADIUS / radiusCount); + circleRadius = std::max(handleCircleRadius, 0.0f); + if (CheckChoosePoint(x, y)) { + const int32_t lastIndexFir = 1; + if (CheckChoosePointIsLastIndex(x, y, lastIndexFir)) { + if (isMoveEventValid_->Get()) { + brush.SetColor(ToRSColor(activeColor)); + canvas.AttachBrush(brush); + auto radius = circleRadius * 2 * SCALE_ACTIVE_CIRCLE_RADIUS; + canvas.DrawCircle( + RSPoint(offsetX, offsetY), std::min(static_cast(radius), firstCellcenter.GetX())); + } else { + brush.SetColor(ToRSColor(selectedColor)); + canvas.AttachBrush(brush); + canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius * SCALE_ACTIVE_CIRCLE_RADIUS); + } + } else { + brush.SetColor(ToRSColor(selectedColor)); + canvas.AttachBrush(brush); + canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius * SCALE_ACTIVE_CIRCLE_RADIUS); + } + } else { + canvas.AttachBrush(brush); + canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius); + } + canvas.DetachBrush(); +} + +bool PatternLockModifier::CheckChoosePoint(int32_t x, int32_t y) const +{ + for (auto it : choosePoint_) { + if (it.GetColumn() == x && it.GetRow() == y) { + return true; + } + } + return false; +} + +bool PatternLockModifier::CheckChoosePointIsLastIndex(int32_t x, int32_t y, int32_t index) const +{ + if (!choosePoint_.empty() && static_cast(choosePoint_.size()) >= index) { + if (choosePoint_.at(choosePoint_.size() - static_cast(index)).GetColumn() == x && + choosePoint_.at(choosePoint_.size() - static_cast(index)).GetRow() == y) { + return true; + } + } + return false; +} + +OffsetF PatternLockModifier::GetCircleCenterByXY(const OffsetF& offset, int32_t x, int32_t y) +{ + float sideLength = sideLength_->Get(); + OffsetF cellCenter; + int32_t scale = RADIUS_TO_DIAMETER; + cellCenter.SetX(offset.GetX() + sideLength / PATTERN_LOCK_COL_COUNT / scale * (x * scale - 1)); + cellCenter.SetY(offset.GetY() + sideLength / PATTERN_LOCK_COL_COUNT / scale * (y * scale - 1)); + return cellCenter; +} + +void PatternLockModifier::SetSideLength(float sideLength) +{ + CHECK_NULL_VOID(sideLength_); + sideLength_->Set(sideLength); +} + +void PatternLockModifier::SetCircleRadius(float circleRadius) +{ + CHECK_NULL_VOID(circleRadius_); + circleRadius_->Set(circleRadius); +} + +void PatternLockModifier::SetRegularColor(const LinearColor& regularColor) +{ + CHECK_NULL_VOID(regularColor_); + regularColor_->Set(regularColor); +} + +void PatternLockModifier::SetSelectColor(const LinearColor& selectedColor) +{ + CHECK_NULL_VOID(selectedColor_); + selectedColor_->Set(selectedColor); +} + +void PatternLockModifier::SetActiveColor(const LinearColor& activeColor) +{ + CHECK_NULL_VOID(activeColor_); + activeColor_->Set(activeColor); +} + +void PatternLockModifier::SetPathColor(const LinearColor& pathColor) +{ + CHECK_NULL_VOID(pathColor_); + pathColor_->Set(pathColor); +} + +void PatternLockModifier::SetPathStrokeWidth(float pathStrokeWidth) +{ + CHECK_NULL_VOID(pathStrokeWidth_); + pathStrokeWidth_->Set(pathStrokeWidth); +} + +void PatternLockModifier::SetContentOffset(const OffsetF& offset) +{ + CHECK_NULL_VOID(offset_); + offset_->Set(offset); +} + +void PatternLockModifier::SetIsMoveEventValid(bool isMoveEventValid) +{ + isMoveEventValid_->Set(isMoveEventValid); +} + +void PatternLockModifier::SetCellCenterOffset(const OffsetF& cellcenter) +{ + CHECK_NULL_VOID(cellCenter_); + cellCenter_->Set(cellcenter); +} + +void PatternLockModifier::SetChoosePoint(const std::vector& choosePoint) +{ + choosePoint_ = choosePoint; +} +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.h b/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.h new file mode 100644 index 00000000000..8ef386d89d7 --- /dev/null +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 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. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_MODIFIER_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_MODIFIER_H + +#include + +#include "base/geometry/dimension.h" +#include "base/geometry/ng/offset_t.h" +#include "base/memory/ace_type.h" +#include "base/memory/referenced.h" +#include "core/components/common/properties/color.h" +#include "core/components_ng/base/modifier.h" +#include "core/components_ng/pattern/progress/progress_date.h" +#include "core/components_ng/property/property.h" +#include "core/components_ng/render/animation_utils.h" +#include "core/components_ng/render/drawing.h" +#include "core/components_ng/render/paint_wrapper.h" + +namespace OHOS::Ace::NG { +class PatternLockCell { +public: + PatternLockCell(int32_t column, int32_t row); + ~PatternLockCell() = default; + int32_t GetColumn() const + { + return column_; + } + int32_t GetRow() const + { + return row_; + } + int32_t GetCode() const + { + return code_; + } + +private: + int32_t column_; + int32_t row_; + int32_t code_; +}; + +class PatternLockModifier : public ContentModifier { + DECLARE_ACE_TYPE(PatternLockModifier, ContentModifier); + +public: + PatternLockModifier(); + + ~PatternLockModifier() override = default; + void onDraw(DrawingContext& context) override; + + void SetSideLength(float sideLength); + void SetCircleRadius(float circleRadius); + void SetRegularColor(const LinearColor& regularColor); + void SetSelectColor(const LinearColor& selectedColor); + void SetActiveColor(const LinearColor& activeColor); + void SetPathColor(const LinearColor& pathColor); + void SetPathStrokeWidth(float pathStrokeWidth); + void SetIsMoveEventValid(bool isMoveEventValid); + void SetCellCenterOffset(const OffsetF& cellCenter); + void SetContentOffset(const OffsetF& offset); + void SetChoosePoint(const std::vector& choosePoint); + +private: + void PaintLockLine(RSCanvas& canvas, const OffsetF& offset); + void PaintLockCircle(RSCanvas& canvas, const OffsetF& offset, int32_t x, int32_t y); + + bool CheckChoosePoint(int32_t x, int32_t y) const; + bool CheckChoosePointIsLastIndex(int32_t x, int32_t y, int32_t index) const; + OffsetF GetCircleCenterByXY(const OffsetF& offset, int32_t x, int32_t y); + + RefPtr regularColor_; + RefPtr selectedColor_; + RefPtr activeColor_; + RefPtr pathColor_; + RefPtr offset_; + RefPtr sideLength_; + RefPtr circleRadius_; + RefPtr pathStrokeWidth_; + RefPtr isMoveEventValid_; + RefPtr cellCenter_; + + std::vector choosePoint_; + + ACE_DISALLOW_COPY_AND_MOVE(PatternLockModifier); +}; +} // namespace OHOS::Ace::NG + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_MODIFIER_H diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.cpp b/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.cpp index 4819ec8a296..e998167912c 100644 --- a/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.cpp +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.cpp @@ -15,217 +15,51 @@ #include "core/components_ng/pattern/patternlock/patternlock_paint_method.h" -#include - #include "core/components/theme/theme_manager.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" #include "core/components_ng/pattern/patternlock/patternlock_pattern.h" +#include "core/components_ng/property/measure_utils.h" #include "core/components_ng/render/drawing.h" #include "core/components_ng/render/drawing_prop_convertor.h" #include "core/components_v2/pattern_lock/pattern_lock_theme.h" namespace OHOS::Ace::NG { -namespace { -constexpr int32_t RADIUS_TO_DIAMETER = 2; -constexpr float SCALE_ACTIVE_CIRCLE_RADIUS = 16.00 / 14.00; -constexpr float GRADUAL_CHANGE_POINT = 0.5; -constexpr float SCALE_SELECTED_CIRCLE_RADIUS = 26.00 / 14.00; -} // namespace - -CanvasDrawFunction PatternLockPaintMethod::GetContentDrawFunction(PaintWrapper* paintWrapper) +void PatternLockPaintMethod::UpdateContentModifier(PaintWrapper* paintWrapper) { - const auto& paintProperty = DynamicCast(paintWrapper->GetPaintProperty()); - InitializeParam(paintProperty); - auto paintFunc = [weak = WeakClaim(this), paintWrapper](RSCanvas& canvas) { - auto patternlock = weak.Upgrade(); - auto offset = paintWrapper->GetGeometryNode()->GetContentOffset(); - if (patternlock) { - patternlock->PaintLockLine(canvas, offset); - for (int i = 0; i < PATTERN_LOCK_COL_COUNT; i++) { - for (int j = 0; j < PATTERN_LOCK_COL_COUNT; j++) { - patternlock->PaintLockCircle(canvas, offset, i + 1, j + 1); - } - } - } - }; - return paintFunc; + CHECK_NULL_VOID(patternlockModifier_); + GetThemeProp(); + auto paintProperty = DynamicCast(paintWrapper->GetPaintProperty()); + CHECK_NULL_VOID(paintProperty); + sideLength_ = paintProperty->GetSideLength().value_or(sideLength_); + circleRadius_ = paintProperty->GetCircleRadius().value_or(circleRadius_); + regularColor_ = paintProperty->GetRegularColor().value_or(regularColor_); + selectedColor_ = paintProperty->GetSelectedColor().value_or(selectedColor_); + activeColor_ = paintProperty->GetActiveColor().value_or(activeColor_); + pathColor_ = paintProperty->GetPathColor().value_or(pathColor_); + pathStrokeWidth_ = paintProperty->GetPathStrokeWidth().value_or(pathStrokeWidth_); + + patternlockModifier_->SetSideLength(sideLength_.ConvertToPx()); + patternlockModifier_->SetCircleRadius(circleRadius_.ConvertToPx()); + patternlockModifier_->SetRegularColor(LinearColor(regularColor_)); + patternlockModifier_->SetSelectColor(LinearColor(selectedColor_)); + patternlockModifier_->SetActiveColor(LinearColor(activeColor_)); + patternlockModifier_->SetPathColor(LinearColor(pathColor_)); + patternlockModifier_->SetPathStrokeWidth(pathStrokeWidth_.ConvertToPx()); + patternlockModifier_->SetIsMoveEventValid(isMoveEventValid_); + patternlockModifier_->SetCellCenterOffset(cellCenter_); + patternlockModifier_->SetContentOffset(paintWrapper->GetContentOffset()); + patternlockModifier_->SetChoosePoint(choosePoint_); } -void PatternLockPaintMethod::InitializeParam(const RefPtr& patternLockPaintProperty) +void PatternLockPaintMethod::GetThemeProp() { auto pipeline = PipelineBase::GetCurrentContext(); CHECK_NULL_VOID(pipeline); auto patternLockTheme = pipeline->GetTheme(); CHECK_NULL_VOID(patternLockTheme); - if (patternLockPaintProperty->HasSideLength()) { - sideLength_ = patternLockPaintProperty->GetSideLengthValue(); - } else { - sideLength_ = 300.0_vp; - } - if (patternLockPaintProperty->HasCircleRadius() && - patternLockPaintProperty->GetCircleRadiusValue().IsNonNegative()) { - circleRadius_ = patternLockPaintProperty->GetCircleRadiusValue(); - } else { - circleRadius_ = 14.0_vp; - } - if (patternLockPaintProperty->HasRegularColor()) { - regularColor_ = patternLockPaintProperty->GetRegularColorValue(); - } else { - regularColor_ = patternLockTheme->GetRegularColor(); - } - if (patternLockPaintProperty->HasSelectedColor()) { - selectedColor_ = patternLockPaintProperty->GetSelectedColorValue(); - } else { - selectedColor_ = patternLockTheme->GetSelectedColor(); - } - if (patternLockPaintProperty->HasActiveColor()) { - activeColor_ = patternLockPaintProperty->GetActiveColorValue(); - } else { - activeColor_ = patternLockTheme->GetActiveColor(); - } - if (patternLockPaintProperty->HasPathColor()) { - pathColor_ = patternLockPaintProperty->GetPathColorValue(); - } else { - pathColor_ = patternLockTheme->GetPathColor(); - } - if (patternLockPaintProperty->HasPathStrokeWidth()) { - pathStrokeWidth_ = patternLockPaintProperty->GetPathStrokeWidthValue(); - } else { - pathStrokeWidth_ = 34.0_vp; - } - if (patternLockPaintProperty->HasAutoReset()) { - autoReset_ = patternLockPaintProperty->GetAutoResetValue(); - } else { - autoReset_ = true; - } -} - -OffsetF PatternLockPaintMethod::GetCircleCenterByXY(const OffsetF& offset, int16_t x, int16_t y) -{ - OffsetF cellCenter; - int16_t scale = RADIUS_TO_DIAMETER; - cellCenter.SetX(offset.GetX() + sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT / scale * (x * scale - 1)); - cellCenter.SetY(offset.GetY() + sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT / scale * (y * scale - 1)); - return cellCenter; -} - -void PatternLockPaintMethod::PaintLockLine(RSCanvas& canvas, const OffsetF& offset) -{ - constexpr int32_t MAX_ALPHA = 255; - size_t count = choosePoint_.size(); - if (count == 0) { - return; - } - - if (LessOrEqual(pathStrokeWidth_.Value(), 0.0)) { - return; - } - float handleStrokeWidth = pathStrokeWidth_.ConvertToPx() > sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT - ? sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT - : pathStrokeWidth_.ConvertToPx(); - pathStrokeWidth_ = Dimension(handleStrokeWidth < 0 ? 0 : handleStrokeWidth); - - RSPen pen; - pen.SetAntiAlias(true); - pen.SetColor(ToRSColor(pathColor_)); - pen.SetWidth(pathStrokeWidth_.ConvertToPx()); - pen.SetCapStyle(RSPen::CapStyle::ROUND_CAP); - - Color pathColorAlpha255 = pathColor_.ChangeAlpha(MAX_ALPHA); - pen.SetColor(pathColorAlpha255.GetValue()); - canvas.AttachPen(pen); - for (size_t i = 0; i < count - 1; i++) { - OffsetF pointBegin = GetCircleCenterByXY(offset, choosePoint_[i].GetColumn(), choosePoint_[i].GetRow()); - OffsetF pointEnd = GetCircleCenterByXY(offset, choosePoint_[i + 1].GetColumn(), choosePoint_[i + 1].GetRow()); - canvas.DrawLine(RSPoint(pointBegin.GetX(), pointBegin.GetY()), RSPoint(pointEnd.GetX(), pointEnd.GetY())); - } - if (isMoveEventValid_) { - OffsetF pointBegin = - GetCircleCenterByXY(offset, choosePoint_[count - 1].GetColumn(), choosePoint_[count - 1].GetRow()); - float x1 = pointBegin.GetX(); - float y1 = pointBegin.GetY(); - float x2 = offset.GetX() + cellCenter_.GetX(); - float y2 = offset.GetY() + cellCenter_.GetY(); - x2 = x2 > offset.GetX() + sideLength_.ConvertToPx() ? offset.GetX() + sideLength_.ConvertToPx() : x2; - x2 = x2 < offset.GetX() ? offset.GetX() : x2; - y2 = y2 > offset.GetY() + sideLength_.ConvertToPx() ? offset.GetY() + sideLength_.ConvertToPx() : y2; - y2 = y2 < offset.GetY() ? offset.GetY() : y2; - - std::vector colors = { pathColorAlpha255.GetValue(), pathColorAlpha255.GetValue(), - pathColorAlpha255.ChangeOpacity(0.0).GetValue() }; - std::vector pos = { 0.0, GRADUAL_CHANGE_POINT, 1.0 }; - auto shader = pen.GetShaderEffect(); - shader->CreateLinearGradient(RSPoint(x1, y1), RSPoint(x2, y2), colors, pos, RSTileMode::CLAMP); - pen.SetShaderEffect(shader); - canvas.DrawLine(RSPoint(x1, y1), RSPoint(x2, y2)); - } - canvas.DetachPen(); - canvas.Restore(); + regularColor_ = patternLockTheme->GetRegularColor(); + selectedColor_ = patternLockTheme->GetSelectedColor(); + activeColor_ = patternLockTheme->GetActiveColor(); + pathColor_ = patternLockTheme->GetPathColor(); } - -void PatternLockPaintMethod::PaintLockCircle(RSCanvas& canvas, const OffsetF& offset, int16_t x, int16_t y) -{ - RSBrush brush; - brush.SetAntiAlias(true); - brush.SetColor(ToRSColor(regularColor_)); - - OffsetF cellcenter = GetCircleCenterByXY(offset, x, y); - OffsetF firstCellcenter = GetCircleCenterByXY(offset, 1, 1); - float offsetX = cellcenter.GetX(); - float offsetY = cellcenter.GetY(); - circleRadius_ = circleRadius_.Unit() == DimensionUnit::PERCENT - ? Dimension(circleRadius_.Value() * sideLength_.ConvertToPx()) - : circleRadius_; - const int16_t radiusCount = RADIUS_TO_DIAMETER * PATTERN_LOCK_COL_COUNT; - float handleCircleRadius = - circleRadius_.ConvertToPx() > (sideLength_.ConvertToPx() / SCALE_SELECTED_CIRCLE_RADIUS / radiusCount) - ? (sideLength_.ConvertToPx() / SCALE_SELECTED_CIRCLE_RADIUS / radiusCount) - : circleRadius_.ConvertToPx(); - circleRadius_ = Dimension(handleCircleRadius < 0 ? 0 : handleCircleRadius); - if (CheckChoosePoint(x, y)) { - const int16_t lastIndexFir = 1; - if (CheckChoosePointIsLastIndex(x, y, lastIndexFir)) { - if (isMoveEventValid_) { - brush.SetColor(ToRSColor(activeColor_)); - canvas.AttachBrush(brush); - auto radius = circleRadius_.ConvertToPx() * 2 * SCALE_ACTIVE_CIRCLE_RADIUS; - canvas.DrawCircle( - RSPoint(offsetX, offsetY), std::min(static_cast(radius), firstCellcenter.GetX())); - } else { - brush.SetColor(ToRSColor(selectedColor_)); - canvas.AttachBrush(brush); - canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius_.ConvertToPx() * SCALE_ACTIVE_CIRCLE_RADIUS); - } - } else { - brush.SetColor(ToRSColor(selectedColor_)); - canvas.AttachBrush(brush); - canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius_.ConvertToPx() * SCALE_ACTIVE_CIRCLE_RADIUS); - } - } else { - canvas.AttachBrush(brush); - canvas.DrawCircle(RSPoint(offsetX, offsetY), circleRadius_.ConvertToPx()); - } -} - -bool PatternLockPaintMethod::CheckChoosePoint(int16_t x, int16_t y) const -{ - for (auto it : choosePoint_) { - if (it.GetColumn() == x && it.GetRow() == y) { - return true; - } - } - return false; -} - -bool PatternLockPaintMethod::CheckChoosePointIsLastIndex(int16_t x, int16_t y, int16_t index) const -{ - if (!choosePoint_.empty() && static_cast(choosePoint_.size()) >= index) { - if (choosePoint_.at(choosePoint_.size() - static_cast(index)).GetColumn() == x && - choosePoint_.at(choosePoint_.size() - static_cast(index)).GetRow() == y) { - return true; - } - } - return false; -} - } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.h b/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.h index 9ad2b53b7f5..01503c540e1 100644 --- a/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.h +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.h @@ -13,81 +13,62 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H -#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H +#include + +#include "base/geometry/dimension.h" +#include "base/geometry/ng/offset_t.h" #include "base/memory/ace_type.h" +#include "base/memory/referenced.h" #include "base/utils/macros.h" +#include "base/utils/noncopyable.h" +#include "base/utils/utils.h" +#include "core/components_ng/pattern/patternlock/patternlock_modifier.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" -#include "core/components_ng/render/drawing.h" #include "core/components_ng/render/node_paint_method.h" +#include "core/components_ng/render/paint_wrapper.h" namespace OHOS::Ace::NG { - -constexpr int32_t PATTERN_LOCK_COL_COUNT = 3; - -class PatternLockCell { -public: - PatternLockCell(int16_t column, int16_t row) - { - column_ = column; - row_ = row; - code_ = PATTERN_LOCK_COL_COUNT * (row - 1) + (column - 1); - }; - ~PatternLockCell() = default; - int16_t GetColumn() const - { - return column_; - } - int16_t GetRow() const - { - return row_; - } - int16_t GetCode() const - { - return code_; - } - -private: - int16_t column_; - int16_t row_; - int16_t code_; -}; - -class PatternLockPaintMethod : public NodePaintMethod { +class ACE_EXPORT PatternLockPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(PatternLockPaintMethod, NodePaintMethod) public: - PatternLockPaintMethod(std::vector& choosePoint, OffsetF& cellCenter, bool isMoveEventValid) - : choosePoint_(choosePoint), cellCenter_(cellCenter), isMoveEventValid_(isMoveEventValid) {}; + PatternLockPaintMethod(const OffsetF& cellCenter, bool isMoveEventValid, + const std::vector& choosePoint, const RefPtr& patternlockModifier) + : cellCenter_(cellCenter), isMoveEventValid_(isMoveEventValid), choosePoint_(choosePoint), + patternlockModifier_(patternlockModifier) + {} ~PatternLockPaintMethod() override = default; - CanvasDrawFunction GetContentDrawFunction(PaintWrapper* paintWrapper) override; + RefPtr GetContentModifier(PaintWrapper* paintWrapper) override + { + CHECK_NULL_RETURN(patternlockModifier_, nullptr); + return patternlockModifier_; + } -private: - void PaintLockLine(RSCanvas& canvas, const OffsetF& offset); - void PaintLockCircle(RSCanvas& canvas, const OffsetF& offset, int16_t x, int16_t y); + void UpdateContentModifier(PaintWrapper* paintWrapper) override; - bool CheckChoosePoint(int16_t x, int16_t y) const; - bool CheckChoosePointIsLastIndex(int16_t x, int16_t y, int16_t index) const; - void InitializeParam(const RefPtr& patternLockPaintProperty); - OffsetF GetCircleCenterByXY(const OffsetF& offset, int16_t x, int16_t y); +private: + void GetThemeProp(); - Dimension sideLength_; - Dimension circleRadius_; + Dimension sideLength_ = 300.0_vp; + Dimension circleRadius_ = 14.0_vp; Color regularColor_; Color selectedColor_; Color activeColor_; Color pathColor_; - Dimension pathStrokeWidth_; - bool autoReset_ {}; + Dimension pathStrokeWidth_ = 34.0_vp; - std::vector choosePoint_; OffsetF cellCenter_; bool isMoveEventValid_; -}; + std::vector choosePoint_; + RefPtr patternlockModifier_; + ACE_DISALLOW_COPY_AND_MOVE(PatternLockPaintMethod); +}; } // namespace OHOS::Ace::NG -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PAINT_METHOD_H diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.cpp b/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.cpp index 8ce01937dde..537e284ee8d 100644 --- a/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.cpp +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.cpp @@ -27,9 +27,10 @@ #include "core/pipeline_ng/pipeline_context.h" namespace OHOS::Ace::NG { - namespace { +constexpr int32_t PATTERN_LOCK_COL_COUNT = 3; constexpr int32_t RADIUS_TO_DIAMETER = 2; +// the scale of selected circle radius and normal circle radius constexpr float SCALE_SELECTED_CIRCLE_RADIUS = 26.00 / 14.00; } // namespace @@ -61,9 +62,6 @@ void PatternLockPattern::InitTouchEvent(RefPtr& gestureHub, Ref CHECK_NULL_VOID(pattern); pattern->HandleTouchEvent(info); }; - if (touchDownListener) { - gestureHub->RemoveTouchEvent(touchDownListener); - } touchDownListener = MakeRefPtr(std::move(touchDownTask)); gestureHub->AddTouchEvent(touchDownListener); } @@ -89,7 +87,7 @@ void PatternLockPattern::HandleTouchEvent(const TouchEventInfo& info) } } -bool PatternLockPattern::AddChoosePoint(const OffsetF& offset, int16_t x, int16_t y) +bool PatternLockPattern::AddChoosePoint(const OffsetF& offset, int32_t x, int32_t y) { auto host = GetHost(); CHECK_NULL_RETURN(host, false); @@ -101,7 +99,7 @@ bool PatternLockPattern::AddChoosePoint(const OffsetF& offset, int16_t x, int16_ circleRadius_ = patternLockPaintProperty->GetCircleRadiusValue(); } - const int16_t scale = RADIUS_TO_DIAMETER; + const int32_t scale = RADIUS_TO_DIAMETER; float offsetX = sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT / scale * (scale * x - 1); float offsetY = sideLength_.ConvertToPx() / PATTERN_LOCK_COL_COUNT / scale * (scale * y - 1); OffsetF centerOffset; @@ -120,7 +118,7 @@ bool PatternLockPattern::AddChoosePoint(const OffsetF& offset, int16_t x, int16_ return false; } -bool PatternLockPattern::CheckChoosePoint(int16_t x, int16_t y) const +bool PatternLockPattern::CheckChoosePoint(int32_t x, int32_t y) const { for (auto it : choosePoint_) { if (it.GetColumn() == x && it.GetRow() == y) { @@ -130,20 +128,20 @@ bool PatternLockPattern::CheckChoosePoint(int16_t x, int16_t y) const return false; } -void PatternLockPattern::AddPassPoint(int16_t x, int16_t y) +void PatternLockPattern::AddPassPoint(int32_t x, int32_t y) { if (choosePoint_.empty()) { return; } passPointCount_ = 0; PatternLockCell lastCell = choosePoint_.back(); - int16_t lastX = lastCell.GetColumn(); - int16_t lastY = lastCell.GetRow(); - int16_t lastCode = lastCell.GetCode(); - int16_t nowCode = PATTERN_LOCK_COL_COUNT * (y - 1) + (x - 1); + int32_t lastX = lastCell.GetColumn(); + int32_t lastY = lastCell.GetRow(); + int32_t lastCode = lastCell.GetCode(); + int32_t nowCode = PATTERN_LOCK_COL_COUNT * (y - 1) + (x - 1); std::vector passPointVec; - for (int16_t i = 1; i <= PATTERN_LOCK_COL_COUNT; i++) { - for (int16_t j = 1; j <= PATTERN_LOCK_COL_COUNT; j++) { + for (int32_t i = 1; i <= PATTERN_LOCK_COL_COUNT; i++) { + for (int32_t j = 1; j <= PATTERN_LOCK_COL_COUNT; j++) { PatternLockCell passPoint = PatternLockCell(i, j); if ((passPoint.GetCode() >= nowCode && passPoint.GetCode() >= lastCode) || (passPoint.GetCode() <= nowCode && passPoint.GetCode() <= lastCode)) { @@ -163,7 +161,7 @@ void PatternLockPattern::AddPassPoint(int16_t x, int16_t y) if (passPointLength == 0) { return; } - passPointCount_ = static_cast(passPointLength); + passPointCount_ = static_cast(passPointLength); if (nowCode > lastCode) { choosePoint_.emplace_back(passPointVec.front()); if (passPointLength > 1) { @@ -213,8 +211,8 @@ void PatternLockPattern::OnTouchDown(const TouchEventInfo& info) HandleReset(); cellCenter_ = touchPoint; bool isAdd = false; - for (int16_t i = 0; i < PATTERN_LOCK_COL_COUNT && !isAdd; i++) { - for (int16_t j = 0; j < PATTERN_LOCK_COL_COUNT && !isAdd; j++) { + for (int32_t i = 0; i < PATTERN_LOCK_COL_COUNT && !isAdd; i++) { + for (int32_t j = 0; j < PATTERN_LOCK_COL_COUNT && !isAdd; j++) { isAdd = AddChoosePoint(touchPoint, i + 1, j + 1); } } @@ -239,8 +237,8 @@ void PatternLockPattern::OnTouchMove(const TouchEventInfo& info) } cellCenter_ = touchPoint; bool isAdd = false; - for (int16_t i = 0; i < PATTERN_LOCK_COL_COUNT && !isAdd; i++) { - for (int16_t j = 0; j < PATTERN_LOCK_COL_COUNT && !isAdd; j++) { + for (int32_t i = 0; i < PATTERN_LOCK_COL_COUNT && !isAdd; i++) { + for (int32_t j = 0; j < PATTERN_LOCK_COL_COUNT && !isAdd; j++) { isAdd = AddChoosePoint(touchPoint, i + 1, j + 1); } } @@ -269,5 +267,4 @@ void PatternLockPattern::OnTouchUp() host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); } - } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.h b/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.h index 05937a71a18..c253653d312 100644 --- a/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.h +++ b/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.h @@ -13,23 +13,24 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERNLOCK_PATTERNLOCK_PATTERN_H -#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERNLOCK_PATTERNLOCK_PATTERN_H +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PATTERN_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PATTERN_H #include "base/geometry/axis.h" +#include "base/memory/ace_type.h" #include "base/memory/referenced.h" #include "core/components/common/layout/constants.h" #include "core/components_ng/event/event_hub.h" #include "core/components_ng/pattern/pattern.h" #include "core/components_ng/pattern/patternlock/patternlock_event_hub.h" #include "core/components_ng/pattern/patternlock/patternlock_layout_algorithm.h" +#include "core/components_ng/pattern/patternlock/patternlock_modifier.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_method.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" #include "core/components_v2/pattern_lock/pattern_lock_controller.h" #include "core/pipeline_ng/pipeline_context.h" namespace OHOS::Ace::NG { - class PatternLockPattern : public Pattern { DECLARE_ACE_TYPE(PatternLockPattern, Pattern); @@ -55,11 +56,15 @@ public: RefPtr CreateNodePaintMethod() override { - auto paintMethod = MakeRefPtr(choosePoint_, cellCenter_, isMoveEventValid_); + if (!patternLockModifier_) { + patternLockModifier_ = AceType::MakeRefPtr(); + } + auto paintMethod = + MakeRefPtr(cellCenter_, isMoveEventValid_, choosePoint_, patternLockModifier_); return paintMethod; } - bool OnDirtyLayoutWrapperSwap(const RefPtr& /*dirty*/, const DirtySwapConfig& /*config*/) override + bool OnDirtyLayoutWrapperSwap(const RefPtr& dirty, const DirtySwapConfig& config) override { return true; } @@ -90,9 +95,9 @@ private: void OnTouchMove(const TouchEventInfo& info); void OnTouchUp(); - bool CheckChoosePoint(int16_t x, int16_t y) const; - bool AddChoosePoint(const OffsetF& offset, int16_t x, int16_t y); - void AddPassPoint(int16_t x, int16_t y); + bool CheckChoosePoint(int32_t x, int32_t y) const; + bool AddChoosePoint(const OffsetF& offset, int32_t x, int32_t y); + void AddPassPoint(int32_t x, int32_t y); void HandleReset(); bool CheckAutoReset() const; @@ -103,15 +108,17 @@ private: bool isMoveEventValid_ = false; std::vector choosePoint_; - int16_t passPointCount_ = 0; + int32_t passPointCount_ = 0; OffsetF cellCenter_; mutable bool autoReset_ = true; Dimension sideLength_ = 300.0_vp; Dimension circleRadius_ = 14.0_vp; + RefPtr patternLockModifier_; + ACE_DISALLOW_COPY_AND_MOVE(PatternLockPattern); }; } // namespace OHOS::Ace::NG -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERNLOCK_PATTERNLOCK_PATTERN_H +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PATTERNLOCK_PATTERNLOCK_PATTERN_H diff --git a/frameworks/core/components_ng/test/pattern/patternlock/BUILD.gn b/frameworks/core/components_ng/test/pattern/patternlock/BUILD.gn index 11e517c06ca..52fca463a3c 100644 --- a/frameworks/core/components_ng/test/pattern/patternlock/BUILD.gn +++ b/frameworks/core/components_ng/test/pattern/patternlock/BUILD.gn @@ -92,7 +92,6 @@ ohos_unittest("patternlock_pattern_test_ng") { "$ace_root/frameworks/core/components_ng/gestures/recognizers/recognizer_group.cpp", # mock - "$ace_root/frameworks/base/test/mock/mock_ressched_report.cpp", "$ace_root/frameworks/base/test/mock/mock_system_properties.cpp", "$ace_root/frameworks/core/animation/test/mock/mock_animator.cpp", @@ -100,6 +99,7 @@ ohos_unittest("patternlock_pattern_test_ng") { "$ace_root/frameworks/core/components_ng/test/mock/animation/mock_geometry_transition.cpp", "$ace_root/frameworks/core/components_ng/test/mock/base/mock_stage_manager.cpp", "$ace_root/frameworks/core/components_ng/test/mock/render/mock_drawing_convertor.cpp", + "$ace_root/frameworks/core/components_ng/test/mock/render/mock_modifier_adapter.cpp", "$ace_root/frameworks/core/components_ng/test/mock/render/mock_render_context.cpp", "$ace_root/frameworks/core/components_ng/test/mock/render/mock_render_context_creator.cpp", "$ace_root/frameworks/core/pipeline_ng/test/mock/mock_element_register.cpp", @@ -109,9 +109,9 @@ ohos_unittest("patternlock_pattern_test_ng") { # self "$ace_root/frameworks/core/components_ng/pattern/patternlock/patternlock_layout_algorithm.cpp", "$ace_root/frameworks/core/components_ng/pattern/patternlock/patternlock_model_ng.cpp", + "$ace_root/frameworks/core/components_ng/pattern/patternlock/patternlock_modifier.cpp", "$ace_root/frameworks/core/components_ng/pattern/patternlock/patternlock_paint_method.cpp", "$ace_root/frameworks/core/components_ng/pattern/patternlock/patternlock_pattern.cpp", - "patternlock_paint_method_test_ng.cpp", "patternlock_pattern_test_ng.cpp", ] diff --git a/frameworks/core/components_ng/test/pattern/patternlock/patternlock_paint_method_test_ng.cpp b/frameworks/core/components_ng/test/pattern/patternlock/patternlock_paint_method_test_ng.cpp deleted file mode 100644 index 7722b69a3ab..00000000000 --- a/frameworks/core/components_ng/test/pattern/patternlock/patternlock_paint_method_test_ng.cpp +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Copyright (c) 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" -#define private public -#define protected public -#include "core/components_ng/pattern/patternlock/patternlock_layout_algorithm.h" -#include "core/components_ng/pattern/patternlock/patternlock_paint_method.h" -#include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" -#include "core/components_ng/pattern/patternlock/patternlock_pattern.h" -#include "core/components_ng/test/mock/rosen/mock_canvas.h" -#include "core/components_ng/test/mock/theme/mock_theme_manager.h" -#include "core/components_v2/pattern_lock/pattern_lock_theme.h" -#include "core/pipeline_ng/test/mock/mock_pipeline_base.h" -using namespace testing; -using namespace testing::ext; -namespace OHOS::Ace::NG { -struct TestProperty { - std::optional sideLength = std::nullopt; - std::optional circleRadius = std::nullopt; - std::optional regularColor = std::nullopt; - std::optional selectedColor = std::nullopt; - std::optional activeColor = std::nullopt; - std::optional pathColor = std::nullopt; - std::optional strokeWidth = std::nullopt; - std::optional autoReset = std::nullopt; -}; - -class PatternLockPaintMethodTestNg : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp() override {} - void TearDown() override {} -}; - -void PatternLockPaintMethodTestNg::SetUpTestCase() -{ - MockPipelineBase::SetUp(); -} - -void PatternLockPaintMethodTestNg::TearDownTestCase() -{ - MockPipelineBase::TearDown(); -} - -/** - * @tc.name: PatternLockPaintMethodTest001 - * @tc.desc: Test PatternLockPaintMethod can create correct Draw Function. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockPaintMethodTest001, TestSize.Level1) -{ - /** - * @tc.step: step1. create patternLock PaintMethod. - */ - std::vector vecCell; - OffsetF offset; - auto paintMethod = AceType::MakeRefPtr(vecCell, offset, false); - /** - * @tc.step: step2. create Draw Function and call it. - */ - RefPtr geometryNode = AceType::MakeRefPtr(); - EXPECT_TRUE(geometryNode != nullptr); - geometryNode->SetContentSize(SizeF()); - auto patternLockPaintProperty = AceType::MakeRefPtr(); - PaintWrapper paintWrapper(nullptr, geometryNode, patternLockPaintProperty); - auto drawFunc = paintMethod->GetContentDrawFunction(&paintWrapper); - EXPECT_TRUE(drawFunc != nullptr); - Testing::MockCanvas canvas; - EXPECT_CALL(canvas, AttachBrush(_)).WillRepeatedly(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawCircle(_, _)).Times(AtLeast(1)); - drawFunc(canvas); -} - -/** - * @tc.name: PatternLockPaintMethodTest002 - * @tc.desc: Test PatternLockPaintMethod can initialize Params correctly. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockPaintMethodTest002, TestSize.Level1) -{ - /** - * @tc.step: step1. create patternLock PaintMethod and PatternLockTheme. - */ - std::vector vecCell; - OffsetF offset; - PatternLockPaintMethod paintMethod(vecCell, offset, false); - // create mock theme manager - auto themeManager = AceType::MakeRefPtr(); - MockPipelineBase::GetCurrent()->SetThemeManager(themeManager); - auto patternlockTheme = AceType::MakeRefPtr(); - patternlockTheme->regularColor_ = Color::BLACK; - patternlockTheme->selectedColor_ = Color::BLUE; - patternlockTheme->activeColor_ = Color::RED; - patternlockTheme->pathColor_ = Color::GRAY; - EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(patternlockTheme)); - /** - * @tc.case: case1. call InitializeParam with invalid PatternLockPaintProperty. - */ - auto patternLockPaintProperty = AceType::MakeRefPtr(); - paintMethod.InitializeParam(patternLockPaintProperty); - EXPECT_EQ(paintMethod.sideLength_, 300.0_vp); - EXPECT_EQ(paintMethod.circleRadius_, 14.0_vp); - EXPECT_EQ(paintMethod.pathStrokeWidth_, 34.0_vp); - EXPECT_EQ(paintMethod.autoReset_, true); - EXPECT_EQ(paintMethod.regularColor_, Color::BLACK); - EXPECT_EQ(paintMethod.selectedColor_, Color::BLUE); - EXPECT_EQ(paintMethod.activeColor_, Color::RED); - EXPECT_EQ(paintMethod.pathColor_, Color::GRAY); - /** - * @tc.case: case2. call InitializeParam with valid PatternLockPaintProperty. - */ - patternLockPaintProperty->UpdateSideLength(Dimension(30.0)); - patternLockPaintProperty->UpdateCircleRadius(Dimension(20.0)); - patternLockPaintProperty->UpdatePathStrokeWidth(Dimension(10.0)); - patternLockPaintProperty->UpdateAutoReset(false); - patternLockPaintProperty->UpdateRegularColor(Color::RED); - patternLockPaintProperty->UpdateSelectedColor(Color::GREEN); - patternLockPaintProperty->UpdateActiveColor(Color::BLACK); - patternLockPaintProperty->UpdatePathColor(Color::WHITE); - paintMethod.InitializeParam(patternLockPaintProperty); - EXPECT_EQ(paintMethod.sideLength_, patternLockPaintProperty->GetSideLengthValue()); - EXPECT_EQ(paintMethod.circleRadius_, patternLockPaintProperty->GetCircleRadiusValue()); - EXPECT_EQ(paintMethod.pathStrokeWidth_, patternLockPaintProperty->GetPathStrokeWidthValue()); - EXPECT_EQ(paintMethod.autoReset_, patternLockPaintProperty->GetAutoResetValue()); - EXPECT_EQ(paintMethod.regularColor_, patternLockPaintProperty->GetRegularColorValue()); - EXPECT_EQ(paintMethod.selectedColor_, patternLockPaintProperty->GetSelectedColorValue()); - EXPECT_EQ(paintMethod.activeColor_, patternLockPaintProperty->GetActiveColorValue()); - EXPECT_EQ(paintMethod.pathColor_, patternLockPaintProperty->GetPathColorValue()); -} - -/** - * @tc.name: PatternLockPaintMethodTest003 - * @tc.desc: Test GetCircleCenterByXY function can get correct offset. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockPaintMethodTest003, TestSize.Level1) -{ - std::vector vecCell; - OffsetF offset; - PatternLockPaintMethod paintMethod(vecCell, offset, false); - paintMethod.sideLength_ = Dimension(36.0); - int16_t x = 1; - int16_t y = 1; - auto cellCenter = paintMethod.GetCircleCenterByXY(OffsetF(1.0, 1.0), x, y); - EXPECT_EQ(cellCenter.GetX(), 7.0); - EXPECT_EQ(cellCenter.GetY(), 7.0); -} - -/** - * @tc.name: PatternLockPaintMethodTest004 - * @tc.desc: Test PaintLockLine function. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockPaintMethodTest004, TestSize.Level1) -{ - Testing::MockCanvas canvas; - /** - * @tc.case: case1. PatternLock's choosePoint count = 0. - */ - std::vector vecCell; - OffsetF offset; - PatternLockPaintMethod paintMethod(vecCell, offset, false); - EXPECT_CALL(canvas, Restore()).Times(0); - paintMethod.PaintLockLine(canvas, OffsetF()); - /** - * @tc.case: case2. pathStrokeWidth_ <= 0. - */ - std::vector vecCell2 = { PatternLockCell(0, 1), PatternLockCell(0, 2) }; - PatternLockPaintMethod paintMethod2(vecCell2, offset, false); - EXPECT_CALL(canvas, Restore()).Times(0); - paintMethod2.PaintLockLine(canvas, OffsetF()); - /** - * @tc.case: case3. isMoveEventValid_ is flase. - */ - std::vector vecCell3 = { PatternLockCell(0, 1), PatternLockCell(0, 2), PatternLockCell(1, 2) }; - PatternLockPaintMethod paintMethod3(vecCell3, offset, false); - paintMethod3.pathStrokeWidth_ = Dimension(10.0); - EXPECT_CALL(canvas, AttachPen(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawLine(_, _)).Times(vecCell3.size() - 1); - EXPECT_CALL(canvas, DetachPen()).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, Restore()).Times(1); - paintMethod3.PaintLockLine(canvas, OffsetF()); - /** - * @tc.case: case4. isMoveEventValid_ is true. - */ - std::vector vecCell4 = { PatternLockCell(0, 1), PatternLockCell(0, 2), PatternLockCell(1, 2), - PatternLockCell(2, 2) }; - PatternLockPaintMethod paintMethod4(vecCell4, offset, true); - paintMethod4.pathStrokeWidth_ = Dimension(10.0); - EXPECT_CALL(canvas, AttachPen(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawLine(_, _)).Times(vecCell4.size()); - EXPECT_CALL(canvas, DetachPen()).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, Restore()).Times(1); - paintMethod4.PaintLockLine(canvas, OffsetF()); -} - -/** - * @tc.name: PatternLockPaintMethodTest005 - * @tc.desc: Test PaintLockCircle function. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockPaintMethodTest005, TestSize.Level1) -{ - Testing::MockCanvas canvas; - /** - * @tc.case: case1. Current Point (x, y) is not checked. - */ - std::vector vecCell = { PatternLockCell(0, 1), PatternLockCell(0, 2), PatternLockCell(1, 2) }; - OffsetF offset; - PatternLockPaintMethod paintMethod(vecCell, offset, true); - EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); - paintMethod.PaintLockCircle(canvas, OffsetF(), 1, 4); - /** - * @tc.case: case2. Current Point (x, y) is checked but the selected Point is not the last Point. - */ - EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); - paintMethod.PaintLockCircle(canvas, OffsetF(), 0, 1); - /** - * @tc.case: case3. last Point (x, y) is checked and isMoveEventValid_ is true. - */ - EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); - paintMethod.PaintLockCircle(canvas, OffsetF(), 1, 2); - /** - * @tc.case: case4. last Point (x, y) is checked but isMoveEventValid_ is false. - */ - PatternLockPaintMethod paintMethod2(vecCell, offset, false); - EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); - EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); - paintMethod2.PaintLockCircle(canvas, OffsetF(), 1, 2); -} - -/** - * @tc.name: PatternLockLayoutAlgorithmTest001 - * @tc.desc: Test GetCircleCenterByXY function can get correct offset. - * @tc.type: FUNC - */ -HWTEST_F(PatternLockPaintMethodTestNg, PatternLockLayoutAlgorithmTest001, TestSize.Level1) -{ - constexpr Dimension sideLength = Dimension(20.0); - PatternLockLayoutAlgorithm layoutAlgorithm(sideLength); - /** - * @tc.case: case1. selfIdealSize's width is null. - */ - LayoutConstraintF constraint1; - auto size1 = layoutAlgorithm.MeasureContent(constraint1, nullptr); - EXPECT_EQ(size1.value(), SizeF(20.0, 20.0)); - /** - * @tc.case: case2. selfIdealSize's width is not null but selfIdealSize is invalid. - */ - LayoutConstraintF constraint2; - constraint2.selfIdealSize.width_ = 10.0; - auto size2 = layoutAlgorithm.MeasureContent(constraint2, nullptr); - EXPECT_EQ(size2.value(), SizeF(20.0, 20.0)); - /** - * @tc.case: case3. selfIdealSize's width is valid. - */ - LayoutConstraintF constraint3; - constraint3.selfIdealSize.width_ = 10.0; - constraint3.selfIdealSize.height_ = .0; - auto size3 = layoutAlgorithm.MeasureContent(constraint3, nullptr); - EXPECT_EQ(size3.value(), SizeF(10.0, 10.0)); -} -} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/test/pattern/patternlock/patternlock_pattern_test_ng.cpp b/frameworks/core/components_ng/test/pattern/patternlock/patternlock_pattern_test_ng.cpp index 8de910c69aa..c1b662b0b8b 100644 --- a/frameworks/core/components_ng/test/pattern/patternlock/patternlock_pattern_test_ng.cpp +++ b/frameworks/core/components_ng/test/pattern/patternlock/patternlock_pattern_test_ng.cpp @@ -14,17 +14,23 @@ */ #include "gtest/gtest.h" + +#include "base/geometry/ng/offset_t.h" #define private public #define protected public #include "base/memory/ace_type.h" #include "base/memory/referenced.h" #include "core/components/common/properties/color.h" #include "core/components_ng/base/view_stack_processor.h" +#include "core/components_ng/pattern/patternlock/patternlock_layout_algorithm.h" #include "core/components_ng/pattern/patternlock/patternlock_model_ng.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_method.h" #include "core/components_ng/pattern/patternlock/patternlock_paint_property.h" #include "core/components_ng/pattern/patternlock/patternlock_pattern.h" +#include "core/components_ng/test/mock/rosen/mock_canvas.h" +#include "core/components_ng/test/mock/theme/mock_theme_manager.h" #include "core/components_v2/pattern_lock/pattern_lock_component.h" +#include "core/components_v2/pattern_lock/pattern_lock_theme.h" #include "core/pipeline_ng/test/mock/mock_pipeline_base.h" using namespace testing; @@ -34,9 +40,9 @@ namespace { const Dimension SIDE_LENGTH = 300.0_vp; const Dimension CIRCLE_RADIUS = 14.0_vp; const Color REGULAR_COLOR = Color::BLACK; -const Color SELECTED_COLOR = Color::BLACK; -const Color ACTIVE_COLOR = Color::BLACK; -const Color PATH_COLOR = Color::BLUE; +const Color SELECTED_COLOR = Color::BLUE; +const Color ACTIVE_COLOR = Color::RED; +const Color PATH_COLOR = Color::GRAY; const Dimension PATH_STROKE_WIDTH = 34.0_vp; } // namespace @@ -51,7 +57,7 @@ struct TestProperty { std::optional autoReset = std::nullopt; }; -class PatternLockPropertyTestNg : public testing::Test { +class PatternLockPatternTestNg : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); @@ -59,12 +65,12 @@ public: void TearDown() override {} }; -void PatternLockPropertyTestNg::SetUpTestCase() +void PatternLockPatternTestNg::SetUpTestCase() { MockPipelineBase::SetUp(); } -void PatternLockPropertyTestNg::TearDownTestCase() +void PatternLockPatternTestNg::TearDownTestCase() { MockPipelineBase::TearDown(); } @@ -74,7 +80,7 @@ void PatternLockPropertyTestNg::TearDownTestCase() * @tc.desc: Set PatternLock value into PatternLockPaintProperty and get it. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPaintPropertyTest001, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPaintPropertyTest001, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -82,7 +88,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPaintPropertyTest001, TestSize.Le PatternLockModelNG patternLockModelNG; auto controller = patternLockModelNG.Create(); auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); ViewStackProcessor::GetInstance()->Push(frameNode); /** @@ -112,7 +118,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPaintPropertyTest001, TestSize.Le * @tc.expected: step3. Check the PatternLock property value */ auto patternLockPaintProperty = frameNode->GetPaintProperty(); - EXPECT_FALSE(patternLockPaintProperty == nullptr); + ASSERT_NE(patternLockPaintProperty, nullptr); EXPECT_EQ(patternLockPaintProperty->GetSideLengthValue(), SIDE_LENGTH); EXPECT_EQ(patternLockPaintProperty->GetCircleRadiusValue(), CIRCLE_RADIUS); EXPECT_EQ(patternLockPaintProperty->GetRegularColorValue(), REGULAR_COLOR); @@ -128,7 +134,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPaintPropertyTest001, TestSize.Le * @tc.desc: Test PatternLock onComplete event. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockEventTest002, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockEventTest002, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -159,9 +165,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockEventTest002, TestSize.Level1) * @tc.expected: step3. Check the event result value */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto eventHub = frameNode->GetEventHub(); - EXPECT_FALSE(eventHub == nullptr); + ASSERT_NE(eventHub, nullptr); eventHub->UpdateCompleteEvent(&patternCompleteEvent); EXPECT_FALSE(afterComplete.empty()); EXPECT_EQ(afterComplete.back(), 6); @@ -182,7 +188,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockEventTest002, TestSize.Level1) * @tc.desc: Test PatternLock pattern method HandleReset. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest003, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest003, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -194,9 +200,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest003, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call HandleReset @@ -214,7 +220,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest003, TestSize.Level1) * @tc.steps: step4. PatternLock Init PatternLockController. */ pattern->InitPatternLockController(); - EXPECT_FALSE(pattern->patternLockController_->resetImpl_ == nullptr); + ASSERT_NE(pattern->patternLockController_->resetImpl_, nullptr); pattern->patternLockController_->resetImpl_(); EXPECT_EQ(pattern->isMoveEventValid_, false); EXPECT_EQ(pattern->choosePoint_.empty(), true); @@ -226,7 +232,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest003, TestSize.Level1) * @tc.desc: Test PatternLock pattern method CheckAutoReset. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest004, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest004, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -239,9 +245,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest004, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call CheckAutoReset @@ -251,7 +257,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest004, TestSize.Level1) EXPECT_EQ(pattern->autoReset_, true); EXPECT_EQ(result1, true); auto patternLockPaintProperty = frameNode->GetPaintProperty(); - EXPECT_FALSE(patternLockPaintProperty == nullptr); + ASSERT_NE(patternLockPaintProperty, nullptr); patternLockPaintProperty->Reset(); bool result2 = pattern->CheckAutoReset(); EXPECT_EQ(result2, true); @@ -267,7 +273,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest004, TestSize.Level1) * @tc.desc: Test PatternLock pattern method AddPassPoint. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest005, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest005, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -278,9 +284,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest005, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call AddPassPoint * @tc.expected: step3. Check the return value of PatternLock pattern method AddPassPoint @@ -330,7 +336,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest005, TestSize.Level1) * @tc.desc: Test PatternLock pattern method CheckChoosePoint. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest006, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest006, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -342,9 +348,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest006, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call CheckChoosePoint @@ -363,7 +369,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest006, TestSize.Level1) * @tc.desc: Test PatternLock pattern method AddChoosePoint. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest007, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest007, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -375,9 +381,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest007, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call AddChoosePoint @@ -391,7 +397,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest007, TestSize.Level1) bool result2 = pattern->AddChoosePoint(offset, 1, 1); EXPECT_EQ(result2, false); auto patternLockPaintProperty = frameNode->GetPaintProperty(); - EXPECT_FALSE(patternLockPaintProperty == nullptr); + ASSERT_NE(patternLockPaintProperty, nullptr); patternLockPaintProperty->UpdateSideLength(SIDE_LENGTH); patternLockPaintProperty->UpdateCircleRadius(CIRCLE_RADIUS); offsetX = 150.0f; @@ -422,7 +428,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest007, TestSize.Level1) * @tc.desc: Test PatternLock pattern method OnTouchUp. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest008, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest008, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -434,9 +440,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest008, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call OnTouchUp @@ -456,7 +462,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest008, TestSize.Level1) * @tc.desc: Test PatternLock pattern method OnTouchMove. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest009, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest009, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -468,9 +474,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest009, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call OnTouchMove @@ -497,7 +503,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest009, TestSize.Level1) * @tc.desc: Test PatternLock pattern method OnTouchDown. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest010, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest010, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -509,9 +515,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest010, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call OnTouchDown @@ -539,7 +545,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest010, TestSize.Level1) * @tc.desc: Test PatternLock pattern method HandleTouchEvent. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest011, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest011, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -551,9 +557,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest011, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call HandleTouchEvent @@ -605,7 +611,7 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest011, TestSize.Level1) * @tc.desc: Test PatternLock pattern method InitTouchEvent. * @tc.type: FUNC */ -HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest012, TestSize.Level1) +HWTEST_F(PatternLockPatternTestNg, PatternLockPatternTest012, TestSize.Level1) { /** * @tc.steps: step1. Init PatternLock node @@ -617,9 +623,9 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest012, TestSize.Level1) * @tc.steps: step2. Get PatternLock pattern object */ auto frameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - EXPECT_FALSE(frameNode == nullptr); + ASSERT_NE(frameNode, nullptr); auto pattern = frameNode->GetPattern(); - EXPECT_FALSE(pattern == nullptr); + ASSERT_NE(pattern, nullptr); /** * @tc.steps: step3. Set PatternLock pattern variable and call InitTouchEvent @@ -627,12 +633,252 @@ HWTEST_F(PatternLockPropertyTestNg, PatternLockPatternTest012, TestSize.Level1) */ auto gestureHub = frameNode->GetOrCreateGestureEventHub(); pattern->InitTouchEvent(gestureHub, pattern->touchDownListener_); - EXPECT_FALSE(pattern->touchDownListener_ == nullptr); + ASSERT_NE(pattern->touchDownListener_, nullptr); auto touchEvent = gestureHub->touchEventActuator_->touchEvents_.back(); - EXPECT_FALSE(touchEvent == nullptr); + ASSERT_NE(touchEvent, nullptr); auto info = TouchEventInfo(""); touchEvent->callback_(info); pattern->InitTouchEvent(gestureHub, pattern->touchDownListener_); EXPECT_EQ(gestureHub->touchEventActuator_->touchEvents_.back(), touchEvent); } + +/** + * @tc.name: PatternLockPaintMethodTest001 + * @tc.desc: Test PatternLockPaintMethod GetThemeProp and UpdateContentModifier Function. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockPaintMethodTest001, TestSize.Level1) +{ + /** + * @tc.step: step1. create patternLock PaintMethod and PatternLockTheme. + */ + std::vector vecCell; + auto modifier = AceType::MakeRefPtr(); + PatternLockPaintMethod paintMethod(OffsetF(), false, vecCell, modifier); + auto patternLockPaintProperty = AceType::MakeRefPtr(); + RefPtr geometryNode = AceType::MakeRefPtr(); + ASSERT_NE(geometryNode, nullptr); + geometryNode->SetContentSize(SizeF()); + // create mock theme manager + auto themeManager = AceType::MakeRefPtr(); + MockPipelineBase::GetCurrent()->SetThemeManager(themeManager); + auto patternlockTheme = AceType::MakeRefPtr(); + patternlockTheme->regularColor_ = REGULAR_COLOR; + patternlockTheme->selectedColor_ = SELECTED_COLOR; + patternlockTheme->activeColor_ = ACTIVE_COLOR; + patternlockTheme->pathColor_ = PATH_COLOR; + EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(patternlockTheme)); + /** + * @tc.case: case1. call GetThemeProp with PatternLockTheme. + */ + paintMethod.GetThemeProp(); + EXPECT_EQ(paintMethod.sideLength_, SIDE_LENGTH); + EXPECT_EQ(paintMethod.circleRadius_, CIRCLE_RADIUS); + EXPECT_EQ(paintMethod.pathStrokeWidth_, PATH_STROKE_WIDTH); + EXPECT_EQ(paintMethod.regularColor_, REGULAR_COLOR); + EXPECT_EQ(paintMethod.selectedColor_, SELECTED_COLOR); + EXPECT_EQ(paintMethod.activeColor_, ACTIVE_COLOR); + EXPECT_EQ(paintMethod.pathColor_, PATH_COLOR); + /** + * @tc.case: case2. call UpdateContentModifier with unvalid PatternLockPaintProperty. + */ + PaintWrapper paintWrapper(nullptr, geometryNode, patternLockPaintProperty); + paintMethod.UpdateContentModifier(&paintWrapper); + EXPECT_EQ(paintMethod.sideLength_, SIDE_LENGTH); + EXPECT_EQ(paintMethod.circleRadius_, CIRCLE_RADIUS); + EXPECT_EQ(paintMethod.pathStrokeWidth_, PATH_STROKE_WIDTH); + EXPECT_EQ(paintMethod.regularColor_, REGULAR_COLOR); + EXPECT_EQ(paintMethod.selectedColor_, SELECTED_COLOR); + EXPECT_EQ(paintMethod.activeColor_, ACTIVE_COLOR); + EXPECT_EQ(paintMethod.pathColor_, PATH_COLOR); + /** + * @tc.case: case3. call UpdateContentModifier with valid PatternLockPaintProperty. + */ + patternLockPaintProperty->UpdateSideLength(Dimension(30.0)); + patternLockPaintProperty->UpdateCircleRadius(Dimension(20.0)); + patternLockPaintProperty->UpdatePathStrokeWidth(Dimension(10.0)); + patternLockPaintProperty->UpdateAutoReset(false); + patternLockPaintProperty->UpdateRegularColor(Color::RED); + patternLockPaintProperty->UpdateSelectedColor(Color::GREEN); + patternLockPaintProperty->UpdateActiveColor(Color::BLACK); + patternLockPaintProperty->UpdatePathColor(Color::WHITE); + paintMethod.UpdateContentModifier(&paintWrapper); + EXPECT_EQ(paintMethod.sideLength_, patternLockPaintProperty->GetSideLengthValue()); + EXPECT_EQ(paintMethod.circleRadius_, patternLockPaintProperty->GetCircleRadiusValue()); + EXPECT_EQ(paintMethod.pathStrokeWidth_, patternLockPaintProperty->GetPathStrokeWidthValue()); + EXPECT_EQ(paintMethod.regularColor_, patternLockPaintProperty->GetRegularColorValue()); + EXPECT_EQ(paintMethod.selectedColor_, patternLockPaintProperty->GetSelectedColorValue()); + EXPECT_EQ(paintMethod.activeColor_, patternLockPaintProperty->GetActiveColorValue()); + EXPECT_EQ(paintMethod.pathColor_, patternLockPaintProperty->GetPathColorValue()); +} + +/** + * @tc.name: PatternLockModifierTest001 + * @tc.desc: Test PatternLockModifier onDraw function. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockModifierTest001, TestSize.Level1) +{ + PatternLockModifier patternlockModifier; + Testing::MockCanvas rsCanvas; + DrawingContext context { rsCanvas, 100.0f, 100.0f }; + EXPECT_CALL(rsCanvas, AttachBrush(_)).WillRepeatedly(ReturnRef(rsCanvas)); + EXPECT_CALL(rsCanvas, DrawCircle(_, _)).Times(9); + EXPECT_CALL(rsCanvas, DetachBrush()).WillRepeatedly(ReturnRef(rsCanvas)); + patternlockModifier.onDraw(context); +} + +/** + * @tc.name: PatternLockModifierTest002 + * @tc.desc: Test GetCircleCenterByXY function can get correct offset. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockModifierTest002, TestSize.Level1) +{ + auto modifier = AceType::MakeRefPtr(); + modifier->SetSideLength(36.0); + int32_t x = 1; + int32_t y = 1; + auto cellCenter = modifier->GetCircleCenterByXY(OffsetF(1.0, 1.0), x, y); + EXPECT_EQ(cellCenter.GetX(), 7.0); + EXPECT_EQ(cellCenter.GetY(), 7.0); +} + +/** + * @tc.name: PatternLockModifierTest003 + * @tc.desc: Test PaintLockLine function. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockModifierTest003, TestSize.Level1) +{ + Testing::MockCanvas canvas; + OffsetF offset; + /** + * @tc.case: case1. PatternLock's choosePoint count = 0. + */ + std::vector vecCell; + auto patternlockModifier1 = AceType::MakeRefPtr(); + patternlockModifier1->SetChoosePoint(vecCell); + EXPECT_CALL(canvas, Restore()).Times(0); + patternlockModifier1->PaintLockLine(canvas, offset); + /** + * @tc.case: case2. pathStrokeWidth_ <= 0. + */ + std::vector vecCell2 = { PatternLockCell(0, 1), PatternLockCell(0, 2) }; + auto patternlockModifier2 = AceType::MakeRefPtr(); + patternlockModifier2->SetChoosePoint(vecCell2); + patternlockModifier2->SetPathStrokeWidth(0.0); + EXPECT_CALL(canvas, Restore()).Times(0); + patternlockModifier2->PaintLockLine(canvas, offset); + /** + * @tc.case: case3. isMoveEventValid_ is flase. + */ + std::vector vecCell3 = { PatternLockCell(0, 1), PatternLockCell(0, 2), PatternLockCell(1, 2) }; + auto patternlockModifier3 = AceType::MakeRefPtr(); + patternlockModifier3->SetChoosePoint(vecCell3); + patternlockModifier3->SetPathStrokeWidth(Dimension(10.0).ConvertToPx()); + EXPECT_CALL(canvas, AttachPen(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawLine(_, _)).Times(vecCell3.size() - 1); + EXPECT_CALL(canvas, DetachPen()).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, Restore()).Times(1); + patternlockModifier3->PaintLockLine(canvas, offset); + /** + * @tc.case: case4. isMoveEventValid_ is true. + */ + std::vector vecCell4 = { PatternLockCell(0, 1), PatternLockCell(0, 2), PatternLockCell(1, 2), + PatternLockCell(2, 2) }; + auto patternlockModifier4 = AceType::MakeRefPtr(); + patternlockModifier4->SetChoosePoint(vecCell4); + patternlockModifier4->SetPathStrokeWidth(Dimension(10.0).ConvertToPx()); + patternlockModifier4->SetIsMoveEventValid(true); + EXPECT_CALL(canvas, AttachPen(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawLine(_, _)).Times(vecCell4.size()); + EXPECT_CALL(canvas, DetachPen()).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, Restore()).Times(1); + patternlockModifier4->PaintLockLine(canvas, offset); +} + +/** + * @tc.name: PatternLockModifierTest004 + * @tc.desc: Test PaintLockCircle function. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockModifierTest004, TestSize.Level1) +{ + Testing::MockCanvas canvas; + OffsetF offset; + std::vector vecCell = { PatternLockCell(0, 0), PatternLockCell(0, 2), PatternLockCell(1, 2) }; + auto patternlockModifier = AceType::MakeRefPtr(); + patternlockModifier->SetChoosePoint(vecCell); + patternlockModifier->SetSideLength(36.0); + /** + * @tc.case: case1. Current Point (x, y) is not checked. + */ + patternlockModifier->SetCircleRadius(-4.0); + EXPECT_FALSE(patternlockModifier->CheckChoosePoint(1, 4)); + EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); + EXPECT_CALL(canvas, DetachBrush()).WillOnce(ReturnRef(canvas)); + patternlockModifier->PaintLockCircle(canvas, offset, 1, 4); + /** + * @tc.case: case2. Current Point (x, y) is checked but the selected Point is not the last Point. + */ + patternlockModifier->SetCircleRadius(10.0); + EXPECT_TRUE(patternlockModifier->CheckChoosePoint(0, 0)); + EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); + EXPECT_CALL(canvas, DetachBrush()).WillOnce(ReturnRef(canvas)); + patternlockModifier->PaintLockCircle(canvas, offset, 0, 0); + /** + * @tc.case: case3. last Point (x, y) is checked and isMoveEventValid_ is false. + */ + patternlockModifier->SetCircleRadius(3.0); + EXPECT_FALSE(patternlockModifier->isMoveEventValid_->Get()); + EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); + EXPECT_CALL(canvas, DetachBrush()).WillOnce(ReturnRef(canvas)); + patternlockModifier->PaintLockCircle(canvas, offset, 1, 2); + /** + * @tc.case: case4. last Point (x, y) is checked but isMoveEventValid_ is true. + */ + patternlockModifier->SetIsMoveEventValid(true); + EXPECT_TRUE(patternlockModifier->isMoveEventValid_->Get()); + EXPECT_TRUE(patternlockModifier->CheckChoosePointIsLastIndex(1, 2, 1)); + EXPECT_CALL(canvas, AttachBrush(_)).WillOnce(ReturnRef(canvas)); + EXPECT_CALL(canvas, DrawCircle(_, _)).Times(1); + EXPECT_CALL(canvas, DetachBrush()).WillOnce(ReturnRef(canvas)); + patternlockModifier->PaintLockCircle(canvas, offset, 1, 2); +} + +/** + * @tc.name: PatternLockLayoutAlgorithmTest001 + * @tc.desc: Test GetCircleCenterByXY function can get correct offset. + * @tc.type: FUNC + */ +HWTEST_F(PatternLockPatternTestNg, PatternLockLayoutAlgorithmTest001, TestSize.Level1) +{ + constexpr Dimension sideLength = Dimension(20.0); + PatternLockLayoutAlgorithm layoutAlgorithm(sideLength); + /** + * @tc.case: case1. selfIdealSize's width is null. + */ + LayoutConstraintF constraint1; + auto size1 = layoutAlgorithm.MeasureContent(constraint1, nullptr); + EXPECT_EQ(size1.value(), SizeF(20.0, 20.0)); + /** + * @tc.case: case2. selfIdealSize's width is not null but selfIdealSize is invalid. + */ + LayoutConstraintF constraint2; + constraint2.selfIdealSize.width_ = 10.0; + auto size2 = layoutAlgorithm.MeasureContent(constraint2, nullptr); + EXPECT_EQ(size2.value(), SizeF(20.0, 20.0)); + /** + * @tc.case: case3. selfIdealSize's width is valid. + */ + LayoutConstraintF constraint3; + constraint3.selfIdealSize.width_ = 10.0; + constraint3.selfIdealSize.height_ = .0; + auto size3 = layoutAlgorithm.MeasureContent(constraint3, nullptr); + EXPECT_EQ(size3.value(), SizeF(10.0, 10.0)); +} } // namespace OHOS::Ace::NG -- Gitee