1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TransformTestHelper_h
6 #define TransformTestHelper_h
8 #include "platform/PlatformExport.h"
9 #include "platform/transforms/TransformationMatrix.h"
10 #include "wtf/text/WTFString.h"
12 #include <gtest/gtest.h>
14 // This file should only be included in test source files.
16 #define EXPECT_TRANSFORMS_ALMOST_EQ(a, b) \
17 EXPECT_PRED_FORMAT2(::blink::TransformTestHelper::transformsAreAlmostEqual, a, b)
18 #define ASSERT_TRANSFORMS_ALMOST_EQ(a, b) \
19 ASSERT_PRED_FORMAT2(::blink::TransformTestHelper::transformsAreAlmostEqual, a, b)
22 namespace TransformTestHelper
{
24 // These are helpers for the macros above.
26 bool floatsAreAlmostEqual(float a
, float b
)
28 // This trick was taken from ui/gfx/test/gfx_util.cc.
29 return ::testing::FloatLE("a", "b", a
, b
) && ::testing::FloatLE("b", "a", b
, a
);
32 String
transformationMatrixToString(const TransformationMatrix
& matrix
)
34 return String::format(
35 "(row-major) [ [ %.2f %.2f %.2f %.2f ] [ %.2f %.2f %.2f %.2f ] [ %.2f %.2f %.2f %.2f ] [ %.2f %.2f %.2f %.2f ] ]",
36 matrix
.m11(), matrix
.m21(), matrix
.m31(), matrix
.m41(),
37 matrix
.m12(), matrix
.m22(), matrix
.m32(), matrix
.m42(),
38 matrix
.m13(), matrix
.m23(), matrix
.m33(), matrix
.m43(),
39 matrix
.m14(), matrix
.m24(), matrix
.m34(), matrix
.m44());
42 const TransformationMatrix
& toTransformationMatrix(const TransformationMatrix
& matrix
) { return matrix
; }
44 TransformationMatrix
toTransformationMatrix(const SkMatrix44
& skMatrix44
)
46 TransformationMatrix matrix
;
48 skMatrix44
.getDouble(0, 0),
49 skMatrix44
.getDouble(1, 0),
50 skMatrix44
.getDouble(2, 0),
51 skMatrix44
.getDouble(3, 0),
52 skMatrix44
.getDouble(0, 1),
53 skMatrix44
.getDouble(1, 1),
54 skMatrix44
.getDouble(2, 1),
55 skMatrix44
.getDouble(3, 1),
56 skMatrix44
.getDouble(0, 2),
57 skMatrix44
.getDouble(1, 2),
58 skMatrix44
.getDouble(2, 2),
59 skMatrix44
.getDouble(3, 2),
60 skMatrix44
.getDouble(0, 3),
61 skMatrix44
.getDouble(1, 3),
62 skMatrix44
.getDouble(2, 3),
63 skMatrix44
.getDouble(3, 3));
67 template <typename T1
, typename T2
>
68 ::testing::AssertionResult
transformsAreAlmostEqual(
69 const char* lhsExpr
, const char* rhsExpr
,
70 const T1
& lhs
, const T2
& rhs
)
72 return transformsAreAlmostEqual(lhsExpr
, rhsExpr
, toTransformationMatrix(lhs
), toTransformationMatrix(rhs
));
76 ::testing::AssertionResult
transformsAreAlmostEqual(
77 const char* lhsExpr
, const char* rhsExpr
,
78 const TransformationMatrix
& lhs
, const TransformationMatrix
& rhs
)
81 return ::testing::AssertionSuccess();
83 if (floatsAreAlmostEqual(lhs
.m11(), rhs
.m11())
84 && floatsAreAlmostEqual(lhs
.m12(), rhs
.m12())
85 && floatsAreAlmostEqual(lhs
.m13(), rhs
.m13())
86 && floatsAreAlmostEqual(lhs
.m14(), rhs
.m14())
87 && floatsAreAlmostEqual(lhs
.m21(), rhs
.m21())
88 && floatsAreAlmostEqual(lhs
.m22(), rhs
.m22())
89 && floatsAreAlmostEqual(lhs
.m23(), rhs
.m23())
90 && floatsAreAlmostEqual(lhs
.m24(), rhs
.m24())
91 && floatsAreAlmostEqual(lhs
.m31(), rhs
.m31())
92 && floatsAreAlmostEqual(lhs
.m32(), rhs
.m32())
93 && floatsAreAlmostEqual(lhs
.m33(), rhs
.m33())
94 && floatsAreAlmostEqual(lhs
.m34(), rhs
.m34())
95 && floatsAreAlmostEqual(lhs
.m41(), rhs
.m41())
96 && floatsAreAlmostEqual(lhs
.m42(), rhs
.m42())
97 && floatsAreAlmostEqual(lhs
.m43(), rhs
.m43())
98 && floatsAreAlmostEqual(lhs
.m44(), rhs
.m44()))
99 return ::testing::AssertionSuccess();
101 return ::testing::AssertionFailure()
102 << "Value of: " << rhsExpr
103 << "\n Actual: " << transformationMatrixToString(rhs
).ascii().data()
104 << "\nExpected: " << lhsExpr
105 << "\nWhich is: " << transformationMatrixToString(lhs
).ascii().data();
108 } // namespace TransformTestHelper
111 #endif // TransformTestHelper_h