!1288 添加wm_math的ut

Merge pull request !1288 from 杨志荣/maste
This commit is contained in:
openharmony_ci
2022-08-02 03:23:46 +00:00
committed by Gitee
7 changed files with 208 additions and 18 deletions
@@ -33,16 +33,9 @@
<timing>
<!--duration of animation when add/remove window, unit is ms-->
<duration>350</duration>
<!--timing curve of animation, config it as below:-->
<!--name="ease"-->
<!--name="easeIn"-->
<!--name="easeOut"-->
<!--name="easeInOut"-->
<!--name="default"-->
<!--name="linear"-->
<!--name="spring"-->
<!--name="interactiveSpring"-->
<!--name="cubic" value=<float float float float>-->
<!--timing curve of animation, config it as below:
name=ease, easeIn, easeOut, easeInOut, default, linear,
spring, interactiveSpring, cubic(float float float float)-->
<curve name="easeOut"></curve>
</timing>
<!--scaleX and scaleY of animation start state-->
+2
View File
@@ -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;
};
+5
View File
@@ -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)
{
+9
View File
@@ -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
@@ -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<float>(rect.posX_), static_cast<float>(rect.posY_), 0 }, mat);
TransformHelper::Vector3 b = TransformHelper::Transform(
{ static_cast<float>(rect.posX_ + rect.width_), static_cast<float>(rect.posY_), 0 }, mat);
TransformHelper::Vector3 c = TransformHelper::Transform(
{ static_cast<float>(rect.posX_), static_cast<float>(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<int32_t>(tranformedPoint.x_), static_cast<int32_t>(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<float>(rect.posX_), static_cast<float>(rect.posY_), 0 }, mat);
TransformHelper::Vector3 b = TransformHelper::Transform(
{ static_cast<float>(rect.posX_ + rect.width_), static_cast<float>(rect.posY_), 0 }, mat);
TransformHelper::Vector3 c = TransformHelper::Transform(
{ static_cast<float>(rect.posX_), static_cast<float>(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
+100
View File
@@ -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 <gtest/gtest.h>
#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
+4 -8
View File
@@ -224,17 +224,13 @@ void WindowManagerConfig::DumpConfig(const std::map<std::string, ConfigItem>& 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: