Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / base / math_util_unittest.cc
blob6e276c25d64736057f975c17ce1b4ee6c08f7c46
1 // Copyright 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 "cc/base/math_util.h"
7 #include <cmath>
9 #include "cc/test/geometry_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/rect_f.h"
14 #include "ui/gfx/transform.h"
16 namespace cc {
17 namespace {
19 TEST(MathUtilTest, ProjectionOfPerpendicularPlane) {
20 // In this case, the m33() element of the transform becomes zero, which could
21 // cause a divide-by-zero when projecting points/quads.
23 gfx::Transform transform;
24 transform.MakeIdentity();
25 transform.matrix().set(2, 2, 0);
27 gfx::RectF rect = gfx::RectF(0, 0, 1, 1);
28 gfx::RectF projected_rect = MathUtil::ProjectClippedRect(transform, rect);
30 EXPECT_EQ(0, projected_rect.x());
31 EXPECT_EQ(0, projected_rect.y());
32 EXPECT_TRUE(projected_rect.IsEmpty());
35 TEST(MathUtilTest, EnclosingClippedRectUsesCorrectInitialBounds) {
36 HomogeneousCoordinate h1(-100, -100, 0, 1);
37 HomogeneousCoordinate h2(-10, -10, 0, 1);
38 HomogeneousCoordinate h3(10, 10, 0, -1);
39 HomogeneousCoordinate h4(100, 100, 0, -1);
41 // The bounds of the enclosing clipped rect should be -100 to -10 for both x
42 // and y. However, if there is a bug where the initial xmin/xmax/ymin/ymax are
43 // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
44 // then the enclosing clipped rect will be computed incorrectly.
45 gfx::RectF result = MathUtil::ComputeEnclosingClippedRect(h1, h2, h3, h4);
47 // Due to floating point math in ComputeClippedPointForEdge this result
48 // is fairly imprecise. 0.15f was empirically determined.
49 EXPECT_RECT_NEAR(
50 gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), result, 0.15f);
53 TEST(MathUtilTest, EnclosingRectOfVerticesUsesCorrectInitialBounds) {
54 gfx::PointF vertices[3];
55 int num_vertices = 3;
57 vertices[0] = gfx::PointF(-10, -100);
58 vertices[1] = gfx::PointF(-100, -10);
59 vertices[2] = gfx::PointF(-30, -30);
61 // The bounds of the enclosing rect should be -100 to -10 for both x and y.
62 // However, if there is a bug where the initial xmin/xmax/ymin/ymax are
63 // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
64 // then the enclosing clipped rect will be computed incorrectly.
65 gfx::RectF result =
66 MathUtil::ComputeEnclosingRectOfVertices(vertices, num_vertices);
68 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
69 result);
72 TEST(MathUtilTest, SmallestAngleBetweenVectors) {
73 gfx::Vector2dF x(1, 0);
74 gfx::Vector2dF y(0, 1);
75 gfx::Vector2dF test_vector(0.5, 0.5);
77 // Orthogonal vectors are at an angle of 90 degress.
78 EXPECT_EQ(90, MathUtil::SmallestAngleBetweenVectors(x, y));
80 // A vector makes a zero angle with itself.
81 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(x, x));
82 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(y, y));
83 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(test_vector, test_vector));
85 // Parallel but reversed vectors are at 180 degrees.
86 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(x, -x));
87 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(y, -y));
88 EXPECT_FLOAT_EQ(
89 180, MathUtil::SmallestAngleBetweenVectors(test_vector, -test_vector));
91 // The test vector is at a known angle.
92 EXPECT_FLOAT_EQ(
93 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, x)));
94 EXPECT_FLOAT_EQ(
95 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, y)));
98 TEST(MathUtilTest, VectorProjection) {
99 gfx::Vector2dF x(1, 0);
100 gfx::Vector2dF y(0, 1);
101 gfx::Vector2dF test_vector(0.3f, 0.7f);
103 // Orthogonal vectors project to a zero vector.
104 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(x, y));
105 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(y, x));
107 // Projecting a vector onto the orthonormal basis gives the corresponding
108 // component of the vector.
109 EXPECT_VECTOR_EQ(gfx::Vector2dF(test_vector.x(), 0),
110 MathUtil::ProjectVector(test_vector, x));
111 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, test_vector.y()),
112 MathUtil::ProjectVector(test_vector, y));
114 // Finally check than an arbitrary vector projected to another one gives a
115 // vector parallel to the second vector.
116 gfx::Vector2dF target_vector(0.5, 0.2f);
117 gfx::Vector2dF projected_vector =
118 MathUtil::ProjectVector(test_vector, target_vector);
119 EXPECT_EQ(projected_vector.x() / target_vector.x(),
120 projected_vector.y() / target_vector.y());
123 } // namespace
124 } // namespace cc