From 39e6a1cfd2bc38a036136722b2c7de0f92386334 Mon Sep 17 00:00:00 2001
From: zhirong <215782872@qq.com>
Date: Mon, 1 Aug 2022 17:16:44 +0800
Subject: [PATCH] add ut for wm_math
Signed-off-by: zhirong <215782872@qq.com>
Change-Id: Ie2f2eda340e7f759fc53673f29451b2506118f93
Signed-off-by: zhirong <215782872@qq.com>
---
.../config/rk3568/window_manager_config.xml | 13 +--
utils/include/wm_math.h | 2 +
utils/src/wm_math.cpp | 5 +
utils/test/unittest/BUILD.gn | 9 ++
utils/test/unittest/window_helper_test.cpp | 85 +++++++++++++++
utils/test/unittest/wm_math_test.cpp | 100 ++++++++++++++++++
wmserver/src/window_manager_config.cpp | 12 +--
7 files changed, 208 insertions(+), 18 deletions(-)
create mode 100644 utils/test/unittest/wm_math_test.cpp
diff --git a/resources/config/rk3568/window_manager_config.xml b/resources/config/rk3568/window_manager_config.xml
index 155219c9..c552138c 100644
--- a/resources/config/rk3568/window_manager_config.xml
+++ b/resources/config/rk3568/window_manager_config.xml
@@ -33,16 +33,9 @@
350
-
-
-
-
-
-
-
-
-
-
+
diff --git a/utils/include/wm_math.h b/utils/include/wm_math.h
index e98fa518..e0eda530 100644
--- a/utils/include/wm_math.h
+++ b/utils/include/wm_math.h
@@ -174,6 +174,8 @@ struct Matrix4 {
void Invert();
// Extract the scale component from the matrix
Vector3 GetScale() const;
+ // Get the translation component of the matrix
+ Vector3 GetTranslation() const;
static const Matrix4 Identity;
static constexpr int MAT_SIZE = 4;
};
diff --git a/utils/src/wm_math.cpp b/utils/src/wm_math.cpp
index b84efdcf..599d01e7 100644
--- a/utils/src/wm_math.cpp
+++ b/utils/src/wm_math.cpp
@@ -178,6 +178,11 @@ Vector3 Matrix4::GetScale() const
return retVal;
}
+Vector3 Matrix4::GetTranslation() const
+{
+ return Vector3(mat_[3][0], mat_[3][1], mat_[3][2]);
+}
+
Plane::Plane(const Vector3& normal, float d)
: normal_(normal), d_(d)
{
diff --git a/utils/test/unittest/BUILD.gn b/utils/test/unittest/BUILD.gn
index 196191f8..0525e1a4 100644
--- a/utils/test/unittest/BUILD.gn
+++ b/utils/test/unittest/BUILD.gn
@@ -24,6 +24,7 @@ group("unittest") {
":utils_screen_info_test",
":utils_window_helper_test",
":utils_window_property_test",
+ ":utils_wm_math_test",
]
}
@@ -63,6 +64,14 @@ ohos_unittest("utils_window_property_test") {
deps = [ ":utils_unittest_common" ]
}
+ohos_unittest("utils_wm_math_test") {
+ module_out_path = module_out_path
+
+ sources = [ "wm_math_test.cpp" ]
+
+ deps = [ ":utils_unittest_common" ]
+}
+
ohos_unittest("utils_window_helper_test") {
module_out_path = module_out_path
diff --git a/utils/test/unittest/window_helper_test.cpp b/utils/test/unittest/window_helper_test.cpp
index 23634dae..a315e62b 100644
--- a/utils/test/unittest/window_helper_test.cpp
+++ b/utils/test/unittest/window_helper_test.cpp
@@ -127,6 +127,91 @@ HWTEST_F(WindowHelperTest, WindowStringUtil, Function | SmallTest | Level1)
}
ASSERT_TRUE(true);
}
+
+/**
+ * @tc.name: CalculateOriginPosition
+ * @tc.desc: CalculateOriginPosition test
+ * @tc.type: FUNC
+ */
+HWTEST_F(WindowHelperTest, CalculateOriginPosition, Function | SmallTest | Level1)
+{
+ Rect rect1 { 0, 0, 10, 10 }, rect2 { 100, 100, 200, 200 };
+ PointInfo point = WindowHelper::CalculateOriginPosition(rect1, rect2, PointInfo { 200, 200 });
+ PointInfo expectPoint { 5, 5 };
+ ASSERT_EQ(true, point.x == expectPoint.x);
+ ASSERT_EQ(true, point.y == expectPoint.y);
+
+ Transform transform;
+ transform.scaleX_ = 0.66f;
+ transform.scaleY_ = 1.5f;
+ transform.rotationY_ = 30;
+ transform.translateX_ = 100;
+ transform.translateY_ = 200;
+ transform.translateZ_ = 50;
+ Rect rect { 50, 50, 240, 320 };
+ TransformHelper::Matrix4 mat = WindowHelper::ComputeRectTransformMat4(transform, rect);
+ TransformHelper::Vector3 a = TransformHelper::Transform(
+ { static_cast(rect.posX_), static_cast(rect.posY_), 0 }, mat);
+ TransformHelper::Vector3 b = TransformHelper::Transform(
+ { static_cast(rect.posX_ + rect.width_), static_cast(rect.posY_), 0 }, mat);
+ TransformHelper::Vector3 c = TransformHelper::Transform(
+ { static_cast(rect.posX_), static_cast(rect.posY_+ rect.height_), 0 }, mat);
+ TransformHelper::Plane plane = TransformHelper::Plane(a, b, c);
+
+ TransformHelper::Vector3 expectOriginPoint(0, 0, 0);
+ TransformHelper::Vector3 tranformedPoint = TransformHelper::Transform(expectOriginPoint, mat);
+ PointInfo actialOriginPoint = WindowHelper::CalculateOriginPosition(mat, plane,
+ { static_cast(tranformedPoint.x_), static_cast(tranformedPoint.y_) });
+ const float errorRange = 2.f;
+ ASSERT_LT(std::abs(expectOriginPoint.x_ - actialOriginPoint.x), errorRange);
+ ASSERT_LT(std::abs(expectOriginPoint.y_ - actialOriginPoint.y), errorRange);
+}
+
+/**
+ * @tc.name: TransformRect
+ * @tc.desc: TransformRect test
+ * @tc.type: FUNC
+ */
+HWTEST_F(WindowHelperTest, TransformRect, Function | SmallTest | Level1)
+{
+ Transform transform;
+ Rect rect { 0, 0, 10, 20 };
+ transform.scaleX_ = transform.scaleY_ = 2.0f;
+ TransformHelper::Matrix4 mat = WindowHelper::ComputeRectTransformMat4(transform, rect);
+ Rect transformRect = WindowHelper::TransformRect(mat, rect);
+ ASSERT_EQ(rect.width_ * transform.scaleX_, transformRect.width_);
+ ASSERT_EQ(rect.height_ * transform.scaleY_, transformRect.height_);
+}
+
+/**
+ * @tc.name: CalculateHotZoneScale
+ * @tc.desc: CalculateHotZoneScale test
+ * @tc.type: FUNC
+ */
+HWTEST_F(WindowHelperTest, CalculateHotZoneScale, Function | SmallTest | Level1)
+{
+ Transform transform;
+ transform.scaleX_ = 0.66f;
+ transform.scaleY_ = 1.5f;
+ transform.rotationY_ = 30;
+ transform.pivotX_ = transform.pivotY_ = 0.5f;
+ Rect rect { -1, -2, 2, 4 };
+ TransformHelper::Matrix4 mat = WindowHelper::ComputeRectTransformMat4(transform, rect);
+ TransformHelper::Vector3 a = TransformHelper::Transform(
+ { static_cast(rect.posX_), static_cast(rect.posY_), 0 }, mat);
+ TransformHelper::Vector3 b = TransformHelper::Transform(
+ { static_cast(rect.posX_ + rect.width_), static_cast(rect.posY_), 0 }, mat);
+ TransformHelper::Vector3 c = TransformHelper::Transform(
+ { static_cast(rect.posX_), static_cast(rect.posY_+ rect.height_), 0 }, mat);
+ TransformHelper::Plane plane = TransformHelper::Plane(a, b, c);
+
+ const float errorRange = 0.01f;
+ TransformHelper::Vector2 hotZoneScale = WindowHelper::CalculateHotZoneScale(mat, plane);
+ TransformHelper::Vector3 xv = TransformHelper::Transform(TransformHelper::Vector3(1, 0, 0), mat);
+ TransformHelper::Vector3 yv = TransformHelper::Transform(TransformHelper::Vector3(0, 1, 0), mat);
+ ASSERT_LT(std::abs(TransformHelper::Vector2(xv.x_, xv.y_).Length() - hotZoneScale.x_), errorRange);
+ ASSERT_LT(std::abs(TransformHelper::Vector2(yv.x_, yv.y_).Length() - hotZoneScale.y_), errorRange);
+}
}
} // namespace Rosen
} // namespace OHOS
\ No newline at end of file
diff --git a/utils/test/unittest/wm_math_test.cpp b/utils/test/unittest/wm_math_test.cpp
new file mode 100644
index 00000000..6482d32c
--- /dev/null
+++ b/utils/test/unittest/wm_math_test.cpp
@@ -0,0 +1,100 @@
+/*
+ * 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
+
+#include "wm_math.h"
+
+using namespace testing;
+using namespace testing::ext;
+
+namespace OHOS {
+namespace Rosen {
+using namespace TransformHelper;
+class WmMathTest : public testing::Test {
+public:
+ static void SetUpTestCase();
+ static void TearDownTestCase();
+ virtual void SetUp() override;
+ virtual void TearDown() override;
+};
+
+void WmMathTest::SetUpTestCase()
+{
+}
+
+void WmMathTest::TearDownTestCase()
+{
+}
+
+void WmMathTest::SetUp()
+{
+}
+
+void WmMathTest::TearDown()
+{
+}
+
+namespace {
+/**
+ * @tc.name: MathHalper
+ * @tc.desc: MathHalper test
+ * @tc.type: FUNC
+ */
+HWTEST_F(WmMathTest, MathHalper, Function | SmallTest | Level2)
+{
+ {
+ const float t = 0.5f;
+ ASSERT_EQ(true, MathHelper::NearZero(0));
+ ASSERT_EQ(true, MathHelper::NearZero(t * MathHelper::NAG_ZERO));
+ ASSERT_EQ(true, MathHelper::NearZero(t * MathHelper::POS_ZERO));
+ }
+ {
+ float radians = MathHelper::PI, degrees = 180.f;
+ ASSERT_EQ(true, MathHelper::NearZero(MathHelper::ToDegrees(radians) - degrees));
+ ASSERT_EQ(true, MathHelper::NearZero(MathHelper::ToRadians(degrees) - radians));
+ }
+ {
+ int a = 1, b = 2, c = 3;
+ ASSERT_EQ(true, MathHelper::Max(a, b, c) == c);
+ ASSERT_EQ(true, MathHelper::Min(a, b, c) == a);
+ ASSERT_EQ(true, MathHelper::Clamp(a, b, c) == b);
+ ASSERT_EQ(true, MathHelper::Clamp(b, a, c) == b);
+ ASSERT_EQ(true, MathHelper::Clamp(c, a, b) == b);
+ }
+}
+
+/**
+ * @tc.name: TransformMatrix
+ * @tc.desc: Create transform matrix
+ * Get scale component from transform matrix
+ * Get translation component from transform matrix
+ * @tc.type: FUNC
+ */
+HWTEST_F(WmMathTest, TransformMatrix, Function | SmallTest | Level2)
+{
+ Vector3 scale(1.5f, 0.7f, 2.2f), translation(100.f, 132.f, 20.f);
+ Matrix4 transformMat = CreateScale(scale.x_, scale.y_, scale.z_);
+ float theta = 2.34f;
+ transformMat *= CreateRotationY(theta);
+ transformMat *= CreateTranslation(translation);
+ Vector3 scaleComp = transformMat.GetScale();
+ Vector3 translationComp = transformMat.GetTranslation();
+ ASSERT_EQ(true, MathHelper::NearZero((scale - scaleComp).Length()));
+ ASSERT_EQ(true, MathHelper::NearZero((translation - translationComp).Length()));
+}
+}
+} // namespace Rosen
+} // namespace OHOS
\ No newline at end of file
diff --git a/wmserver/src/window_manager_config.cpp b/wmserver/src/window_manager_config.cpp
index 70345a8c..94ed6337 100644
--- a/wmserver/src/window_manager_config.cpp
+++ b/wmserver/src/window_manager_config.cpp
@@ -224,17 +224,13 @@ void WindowManagerConfig::DumpConfig(const std::map& co
WLOGFI("[WmConfig] %{public}s", conf.second.stringValue_.c_str());
break;
case ValueType::INTS:
- if (conf.second.intsValue_) {
- for (auto& num : *conf.second.intsValue_) {
- WLOGFI("[WmConfig] Num: %{public}d", num);
- }
+ for (auto& num : *conf.second.intsValue_) {
+ WLOGFI("[WmConfig] Num: %{public}d", num);
}
break;
case ValueType::FLOATS:
- if (conf.second.floatsValue_) {
- for (auto& num : *conf.second.floatsValue_) {
- WLOGFI("[WmConfig] Num: %{public}f", num);
- }
+ for (auto& num : *conf.second.floatsValue_) {
+ WLOGFI("[WmConfig] Num: %{public}f", num);
}
break;
default: