1 // Copyright (c) 2012 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 #include "ui/gfx/transform_util.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/geometry/point.h"
9 #include "ui/gfx/geometry/point3_f.h"
10 #include "ui/gfx/geometry/rect.h"
15 TEST(TransformUtilTest
, GetScaleTransform
) {
16 const Point
kAnchor(20, 40);
17 const float kScale
= 0.5f
;
19 Transform scale
= GetScaleTransform(kAnchor
, kScale
);
21 const int kOffset
= 10;
22 for (int sign_x
= -1; sign_x
<= 1; ++sign_x
) {
23 for (int sign_y
= -1; sign_y
<= 1; ++sign_y
) {
24 Point
test(kAnchor
.x() + sign_x
* kOffset
,
25 kAnchor
.y() + sign_y
* kOffset
);
26 scale
.TransformPoint(&test
);
28 EXPECT_EQ(Point(kAnchor
.x() + sign_x
* kOffset
* kScale
,
29 kAnchor
.y() + sign_y
* kOffset
* kScale
),
35 TEST(TransformUtilTest
, SnapRotation
) {
36 Transform
result(Transform::kSkipInitialization
);
38 transform
.RotateAboutZAxis(89.99);
40 Rect
viewport(1920, 1200);
41 bool snapped
= SnapTransform(&result
, transform
, viewport
);
43 EXPECT_TRUE(snapped
) << "Viewport should snap for this rotation.";
46 TEST(TransformUtilTest
, SnapRotationDistantViewport
) {
47 const int kOffset
= 5000;
48 Transform
result(Transform::kSkipInitialization
);
51 transform
.RotateAboutZAxis(89.99);
53 Rect
viewport(kOffset
, kOffset
, 1920, 1200);
54 bool snapped
= SnapTransform(&result
, transform
, viewport
);
56 EXPECT_FALSE(snapped
) << "Distant viewport shouldn't snap by more than 1px.";
59 TEST(TransformUtilTest
, NoSnapRotation
) {
60 Transform
result(Transform::kSkipInitialization
);
62 const int kOffset
= 5000;
64 transform
.RotateAboutZAxis(89.9);
66 Rect
viewport(kOffset
, kOffset
, 1920, 1200);
67 bool snapped
= SnapTransform(&result
, transform
, viewport
);
69 EXPECT_FALSE(snapped
) << "Viewport should not snap for this rotation.";
72 // Translations should always be snappable, the most we would move is 0.5
73 // pixels towards either direction to the nearest value in each component.
74 TEST(TransformUtilTest
, SnapTranslation
) {
75 Transform
result(Transform::kSkipInitialization
);
78 transform
.Translate3d(
79 SkDoubleToMScalar(1.01), SkDoubleToMScalar(1.99), SkDoubleToMScalar(3.0));
81 Rect
viewport(1920, 1200);
82 bool snapped
= SnapTransform(&result
, transform
, viewport
);
84 EXPECT_TRUE(snapped
) << "Viewport should snap for this translation.";
87 TEST(TransformUtilTest
, SnapTranslationDistantViewport
) {
88 Transform
result(Transform::kSkipInitialization
);
90 const int kOffset
= 5000;
92 transform
.Translate3d(
93 SkDoubleToMScalar(1.01), SkDoubleToMScalar(1.99), SkDoubleToMScalar(3.0));
95 Rect
viewport(kOffset
, kOffset
, 1920, 1200);
96 bool snapped
= SnapTransform(&result
, transform
, viewport
);
99 << "Distant viewport should still snap by less than 1px.";
102 TEST(TransformUtilTest
, SnapScale
) {
103 Transform
result(Transform::kSkipInitialization
);
106 transform
.Scale3d(SkDoubleToMScalar(5.0),
107 SkDoubleToMScalar(2.00001),
108 SkDoubleToMScalar(1.0));
109 Rect
viewport(1920, 1200);
110 bool snapped
= SnapTransform(&result
, transform
, viewport
);
112 EXPECT_TRUE(snapped
) << "Viewport should snap for this scaling.";
115 TEST(TransformUtilTest
, NoSnapScale
) {
116 Transform
result(Transform::kSkipInitialization
);
120 SkDoubleToMScalar(5.0), SkDoubleToMScalar(2.1), SkDoubleToMScalar(1.0));
121 Rect
viewport(1920, 1200);
122 bool snapped
= SnapTransform(&result
, transform
, viewport
);
124 EXPECT_FALSE(snapped
) << "Viewport shouldn't snap for this scaling.";
127 TEST(TransformUtilTest
, SnapCompositeTransform
) {
128 Transform
result(Transform::kSkipInitialization
);
131 transform
.Translate3d(SkDoubleToMScalar(30.5), SkDoubleToMScalar(20.0),
132 SkDoubleToMScalar(10.1));
133 transform
.RotateAboutZAxis(89.99);
134 transform
.Scale3d(SkDoubleToMScalar(1.0),
135 SkDoubleToMScalar(3.00001),
136 SkDoubleToMScalar(2.0));
138 Rect
viewport(1920, 1200);
139 bool snapped
= SnapTransform(&result
, transform
, viewport
);
140 ASSERT_TRUE(snapped
) << "Viewport should snap all components.";
144 point
= Point3F(viewport
.origin());
145 result
.TransformPoint(&point
);
146 EXPECT_EQ(Point3F(31.f
, 20.f
, 10.f
), point
) << "Transformed origin";
148 point
= Point3F(viewport
.top_right());
149 result
.TransformPoint(&point
);
150 EXPECT_EQ(Point3F(31.f
, 1940.f
, 10.f
), point
) << "Transformed top-right";
152 point
= Point3F(viewport
.bottom_left());
153 result
.TransformPoint(&point
);
154 EXPECT_EQ(Point3F(-3569.f
, 20.f
, 10.f
), point
) << "Transformed bottom-left";
156 point
= Point3F(viewport
.bottom_right());
157 result
.TransformPoint(&point
);
158 EXPECT_EQ(Point3F(-3569.f
, 1940.f
, 10.f
), point
)
159 << "Transformed bottom-right";
162 TEST(TransformUtilTest
, NoSnapSkewedCompositeTransform
) {
163 Transform
result(Transform::kSkipInitialization
);
167 transform
.RotateAboutZAxis(89.99);
168 transform
.Scale3d(SkDoubleToMScalar(1.0),
169 SkDoubleToMScalar(3.00001),
170 SkDoubleToMScalar(2.0));
171 transform
.Translate3d(SkDoubleToMScalar(30.5), SkDoubleToMScalar(20.0),
172 SkDoubleToMScalar(10.1));
173 transform
.SkewX(20.0);
174 Rect
viewport(1920, 1200);
175 bool snapped
= SnapTransform(&result
, transform
, viewport
);
176 EXPECT_FALSE(snapped
) << "Skewed viewport should not snap.";
179 TEST(TransformUtilTest
, TransformAboutPivot
) {
181 transform
.Scale(3, 4);
182 transform
= TransformAboutPivot(Point(7, 8), transform
);
187 transform
.TransformPoint(&point
);
188 EXPECT_EQ(Point(-14, -24).ToString(), point
.ToString());
191 transform
.TransformPoint(&point
);
192 EXPECT_EQ(Point(-11, -20).ToString(), point
.ToString());
195 TEST(TransformUtilTest
, MatrixDistance
) {
197 Transform
b(0, 1, 1, 1,
201 // Each entry in |a| should be 1 away from the corresponding entry in |b|.
202 // This means the matrix distance should be sqrt(num_entries) = 4.
203 EXPECT_EQ(4, MatrixDistance(a
, b
));
206 EXPECT_EQ(0, MatrixDistance(b
, c
));