User interface of Apps Developer Tool:
[chromium-blink-merge.git] / cc / base / math_util_unittest.cc
blobd62280ddb5ff25b9016559a9862c9f440dc7c95d
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().setDouble(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 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
48 result);
51 TEST(MathUtilTest, EnclosingRectOfVerticesUsesCorrectInitialBounds) {
52 gfx::PointF vertices[3];
53 int num_vertices = 3;
55 vertices[0] = gfx::PointF(-10, -100);
56 vertices[1] = gfx::PointF(-100, -10);
57 vertices[2] = gfx::PointF(-30, -30);
59 // The bounds of the enclosing rect should be -100 to -10 for both x and y.
60 // However, if there is a bug where the initial xmin/xmax/ymin/ymax are
61 // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
62 // then the enclosing clipped rect will be computed incorrectly.
63 gfx::RectF result =
64 MathUtil::ComputeEnclosingRectOfVertices(vertices, num_vertices);
66 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
67 result);
70 TEST(MathUtilTest, SmallestAngleBetweenVectors) {
71 gfx::Vector2dF x(1, 0);
72 gfx::Vector2dF y(0, 1);
73 gfx::Vector2dF test_vector(0.5, 0.5);
75 // Orthogonal vectors are at an angle of 90 degress.
76 EXPECT_EQ(90, MathUtil::SmallestAngleBetweenVectors(x, y));
78 // A vector makes a zero angle with itself.
79 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(x, x));
80 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(y, y));
81 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(test_vector, test_vector));
83 // Parallel but reversed vectors are at 180 degrees.
84 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(x, -x));
85 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(y, -y));
86 EXPECT_FLOAT_EQ(
87 180, MathUtil::SmallestAngleBetweenVectors(test_vector, -test_vector));
89 // The test vector is at a known angle.
90 EXPECT_FLOAT_EQ(
91 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, x)));
92 EXPECT_FLOAT_EQ(
93 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, y)));
96 TEST(MathUtilTest, VectorProjection) {
97 gfx::Vector2dF x(1, 0);
98 gfx::Vector2dF y(0, 1);
99 gfx::Vector2dF test_vector(0.3f, 0.7f);
101 // Orthogonal vectors project to a zero vector.
102 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(x, y));
103 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(y, x));
105 // Projecting a vector onto the orthonormal basis gives the corresponding
106 // component of the vector.
107 EXPECT_VECTOR_EQ(gfx::Vector2dF(test_vector.x(), 0),
108 MathUtil::ProjectVector(test_vector, x));
109 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, test_vector.y()),
110 MathUtil::ProjectVector(test_vector, y));
112 // Finally check than an arbitrary vector projected to another one gives a
113 // vector parallel to the second vector.
114 gfx::Vector2dF target_vector(0.5, 0.2f);
115 gfx::Vector2dF projected_vector =
116 MathUtil::ProjectVector(test_vector, target_vector);
117 EXPECT_EQ(projected_vector.x() / target_vector.x(),
118 projected_vector.y() / target_vector.y());
121 } // namespace
122 } // namespace cc