Commit a109fd05 authored by jamesr@chromium.org's avatar jamesr@chromium.org

Provide gtest printers for ui/gfx geometry types

This provides PrintTo(..) functions for the geometric types in ui/gfx/
so that failing gtest assertions pretty-print the values instead of
dumping the bytes of the objects. This way gtest assertions on these
types can be written in the form:

  EXPECT_EQ(a, b);

instead of
  EXPECT_EQ(a.ToString(), b.ToString());

which is currently used (inconsistently) to provide more readable
failure messages.

This is a bit tricky since gtest uses streams and somewhat complicated
template expansion magic to pretty print values and we don't want to
link the stream-based code into production code paths. This declares the
PrintTo() functions in the headers defining each of the geometry types,
using the <iosfwd> header to keep stream bloat to a minimum, and defines
the formatters in gfx_test_support. This way every test that
instantiates these printers has to declare a dependency on
gfx_test_support or it fails to link, but a test does not have to
#include any particular header to see the correct printer declaration
and thus expand the template correctly.

If we were to declare the PrintTo() functions in a test only header such
a gfx_util.h then the pretty printers would work reliably only if
*every* target in a translation unit that instantiated the comparison
saw this declaration. If some unit tests in a target #included the
header and some did not then it would be intederminate whether the
template instantiation linked in to the test binary was the pretty
printed one or the hex dump one (and in practice this appears to be
inconsistent across clang and gcc for whatever reason).

Review URL: https://codereview.chromium.org/368903003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282270 0039d316-1c4b-4281-b951-d872f2087c98
parent a5a068d6
......@@ -225,6 +225,7 @@
'../testing/gtest.gyp:gtest',
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_test_support',
'components_resources.gyp:components_resources',
......
......@@ -1331,10 +1331,11 @@
'../testing/gtest.gyp:gtest',
'../third_party/WebKit/public/blink.gyp:blink',
'../ui/base/ui_base.gyp:ui_base',
'../v8/tools/gyp/v8.gyp:v8',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../ui/gl/gl.gyp:gl',
'../v8/tools/gyp/v8.gyp:v8',
# The following dependency provides the missing symbol
# HeapProfilerStart in Linux component builds.
'../webkit/child/webkit_child.gyp:webkit_child',
......@@ -1391,6 +1392,7 @@
'../testing/gtest.gyp:gtest',
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gl/gl.gyp:gl',
'content.gyp:content',
......@@ -1489,6 +1491,7 @@
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../ui/gl/gl.gyp:gl',
'content.gyp:content',
],
......
......@@ -104,6 +104,7 @@ test("gl_tests") {
"//testing/gtest",
"//third_party/angle:translator",
"//ui/gfx",
"//ui/gfx:gfx_test_support",
"//ui/gfx/geometry",
"//ui/gl",
#"//gpu/command_buffer/client", # These are all part of //gpu, needed?
......@@ -230,6 +231,7 @@ test("gpu_unittests") {
"//testing/gtest",
"//third_party/angle:translator",
"//ui/gfx",
"//ui/gfx:gfx_test_support",
"//ui/gfx/geometry",
"//ui/gl",
"//gpu/command_buffer/client:gles2_c_lib",
......
......@@ -178,6 +178,7 @@
'../ui/gl/gl.gyp:gl',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'command_buffer/command_buffer.gyp:gles2_utils',
'command_buffer_client',
'command_buffer_common',
......@@ -304,6 +305,7 @@
'../testing/gtest.gyp:gtest',
'<(angle_path)/src/build_angle.gyp:translator',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gl/gl.gyp:gl',
'command_buffer/command_buffer.gyp:gles2_utils',
......
......@@ -1023,6 +1023,7 @@
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../url/url.gyp:url_lib',
],
'sources': [
......@@ -1296,6 +1297,7 @@
'../testing/perf/perf_test.gyp:perf_test',
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_test_support',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gl/gl.gyp:gl',
'media',
......
......@@ -132,6 +132,7 @@
'../testing/gtest.gyp:gtest',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'mojo_geometry_lib',
'mojo_run_all_unittests',
'mojo_surfaces_bindings',
......@@ -402,6 +403,8 @@
'../base/base.gyp:base',
'../base/base.gyp:test_support_base',
'../testing/gtest.gyp:gtest',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_test_support',
'mojo_environment_chromium',
'mojo_geometry_bindings',
'mojo_geometry_lib',
......
......@@ -217,6 +217,7 @@ test("printing_unittests") {
"//testing/gtest",
"//ui/base",
"//ui/gfx",
"//ui/gfx:gfx_test_support",
"//ui/gfx/geometry",
]
}
......
......@@ -214,6 +214,7 @@
'../ui/base/ui_base.gyp:ui_base',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/gfx/gfx.gyp:gfx_test_support',
'printing',
],
'sources': [
......
......@@ -274,6 +274,7 @@
'<(DEPTH)/base/base.gyp:test_support_base',
'<(DEPTH)/skia/skia.gyp:skia',
'<(DEPTH)/testing/gtest.gyp:gtest',
'../gfx/gfx.gyp:gfx',
'../gfx/gfx.gyp:gfx_geometry',
'../gfx/gfx.gyp:gfx_test_support',
'dom4_keycode_converter',
......
......@@ -401,6 +401,7 @@ test("gfx_unittests") {
deps = [
":gfx",
":gfx_test_support",
"//base",
"//base/test:test_support",
"//testing/gtest",
......
......@@ -5,6 +5,9 @@
#ifndef UI_GFX_GEOMETRY_BOX_F_H_
#define UI_GFX_GEOMETRY_BOX_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point3_f.h"
#include "ui/gfx/geometry/vector3d_f.h"
......@@ -155,6 +158,11 @@ inline BoxF operator+(const BoxF& b, const Vector3dF& v) {
b.depth());
}
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const BoxF& box, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_BOX_F_H_
......@@ -5,6 +5,9 @@
#ifndef UI_GFX_GEOMETRY_POINT_H_
#define UI_GFX_GEOMETRY_POINT_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point_base.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d.h"
......@@ -85,6 +88,11 @@ inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
extern template class PointBase<Point, int, Vector2d>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Point& point, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_POINT_H_
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_GEOMETRY_POINT3_F_H_
#define UI_GFX_GEOMETRY_POINT3_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point_f.h"
......@@ -115,6 +116,11 @@ inline Point3F ScalePoint(const Point3F& p, float scale) {
return ScalePoint(p, scale, scale, scale);
}
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Point3F& point, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_POINT3_F_H_
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_GEOMETRY_POINT_F_H_
#define UI_GFX_GEOMETRY_POINT_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point_base.h"
......@@ -70,6 +71,11 @@ inline PointF ScalePoint(const PointF& p, float scale) {
extern template class PointBase<PointF, float, Vector2dF>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const PointF& point, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_POINT_F_H_
......@@ -7,6 +7,7 @@
#include <algorithm>
#include <cmath>
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point_f.h"
......@@ -104,6 +105,11 @@ GFX_EXPORT QuadF operator+(const QuadF& lhs, const Vector2dF& rhs);
// inverse of the vector.
GFX_EXPORT QuadF operator-(const QuadF& lhs, const Vector2dF& rhs);
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const QuadF& quad, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_QUAD_F_H_
......@@ -20,34 +20,34 @@ TEST(QuadTest, Construction) {
QuadF q2(e, e, e, e);
QuadF q3(a, b, c, d);
QuadF q4(BoundingRect(a, c));
EXPECT_EQ(q1.ToString(), q2.ToString());
EXPECT_EQ(q3.ToString(), q4.ToString());
EXPECT_EQ(q1, q2);
EXPECT_EQ(q3, q4);
// Verify getters.
EXPECT_EQ(q3.p1().ToString(), a.ToString());
EXPECT_EQ(q3.p2().ToString(), b.ToString());
EXPECT_EQ(q3.p3().ToString(), c.ToString());
EXPECT_EQ(q3.p4().ToString(), d.ToString());
EXPECT_EQ(q3.p1(), a);
EXPECT_EQ(q3.p2(), b);
EXPECT_EQ(q3.p3(), c);
EXPECT_EQ(q3.p4(), d);
// Verify setters.
q3.set_p1(b);
q3.set_p2(c);
q3.set_p3(d);
q3.set_p4(a);
EXPECT_EQ(q3.p1().ToString(), b.ToString());
EXPECT_EQ(q3.p2().ToString(), c.ToString());
EXPECT_EQ(q3.p3().ToString(), d.ToString());
EXPECT_EQ(q3.p4().ToString(), a.ToString());
EXPECT_EQ(q3.p1(), b);
EXPECT_EQ(q3.p2(), c);
EXPECT_EQ(q3.p3(), d);
EXPECT_EQ(q3.p4(), a);
// Verify operator=(Rect)
EXPECT_NE(q1.ToString(), q4.ToString());
EXPECT_NE(q1, q4);
q1 = BoundingRect(a, c);
EXPECT_EQ(q1.ToString(), q4.ToString());
EXPECT_EQ(q1, q4);
// Verify operator=(Quad)
EXPECT_NE(q1.ToString(), q3.ToString());
EXPECT_NE(q1, q3);
q1 = q3;
EXPECT_EQ(q1.ToString(), q3.ToString());
EXPECT_EQ(q1, q3);
}
TEST(QuadTest, AddingVectors) {
......@@ -64,8 +64,8 @@ TEST(QuadTest, AddingVectors) {
PointF(5.5f, -1.5f),
PointF(5.5f, -0.5f),
PointF(4.5f, -0.5f));
EXPECT_EQ(expected1.ToString(), added.ToString());
EXPECT_EQ(expected1.ToString(), q1.ToString());
EXPECT_EQ(expected1, added);
EXPECT_EQ(expected1, q1);
QuadF q2(a, b, c, d);
QuadF subtracted = q2 - v;
......@@ -74,14 +74,14 @@ TEST(QuadTest, AddingVectors) {
PointF(-1.5f, 3.5f),
PointF(-1.5f, 4.5f),
PointF(-2.5f, 4.5f));
EXPECT_EQ(expected2.ToString(), subtracted.ToString());
EXPECT_EQ(expected2.ToString(), q2.ToString());
EXPECT_EQ(expected2, subtracted);
EXPECT_EQ(expected2, q2);
QuadF q3(a, b, c, d);
q3 += v;
q3 -= v;
EXPECT_EQ(QuadF(a, b, c, d).ToString(), q3.ToString());
EXPECT_EQ(q3.ToString(), (q3 + v - v).ToString());
EXPECT_EQ(QuadF(a, b, c, d), q3);
EXPECT_EQ(q3, (q3 + v - v));
}
TEST(QuadTest, IsRectilinear) {
......@@ -237,7 +237,7 @@ TEST(QuadTest, IsCounterClockwise) {
TEST(QuadTest, BoundingBox) {
RectF r(3.2f, 5.4f, 7.007f, 12.01f);
EXPECT_EQ(r.ToString(), QuadF(r).BoundingBox().ToString());
EXPECT_EQ(r, QuadF(r).BoundingBox());
PointF a(1.3f, 1.4f);
PointF b(-0.7f, 4.9f);
......@@ -247,8 +247,8 @@ TEST(QuadTest, BoundingBox) {
float top = 1.4f;
float right = 2.1f;
float bottom = 6.2f;
EXPECT_EQ(RectF(left, top, right - left, bottom - top).ToString(),
QuadF(a, b, c, d).BoundingBox().ToString());
EXPECT_EQ(RectF(left, top, right - left, bottom - top),
QuadF(a, b, c, d).BoundingBox());
}
TEST(QuadTest, ContainsPoint) {
......@@ -348,12 +348,11 @@ TEST(QuadTest, Scale) {
PointF b_scaled = ScalePoint(b, 1.5f);
PointF c_scaled = ScalePoint(c, 1.5f);
PointF d_scaled = ScalePoint(d, 1.5f);
EXPECT_EQ(q1.ToString(),
QuadF(a_scaled, b_scaled, c_scaled, d_scaled).ToString());
EXPECT_EQ(q1, QuadF(a_scaled, b_scaled, c_scaled, d_scaled));
QuadF q2;
q2.Scale(1.5f);
EXPECT_EQ(q2.ToString(), q2.ToString());
EXPECT_EQ(q2, q2);
}
} // namespace gfx
......@@ -13,6 +13,7 @@
#define UI_GFX_GEOMETRY_RECT_H_
#include <cmath>
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point.h"
......@@ -134,6 +135,11 @@ inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Rect& rect, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_RECT_H_
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_GEOMETRY_RECT_F_H_
#define UI_GFX_GEOMETRY_RECT_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/point_f.h"
......@@ -108,6 +109,11 @@ GFX_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2);
extern template class RectBase<RectF, PointF, SizeF, InsetsF, Vector2dF, float>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const RectF& rect, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_RECT_F_H_
......@@ -8,6 +8,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/test/gfx_util.h"
#if defined(OS_WIN)
#include <windows.h>
......@@ -228,64 +229,64 @@ TEST(RectTest, Subtract) {
// Matching
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(10, 10, 20, 20));
EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
EXPECT_EQ(Rect(0, 0, 0, 0), result);
// Contains
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(5, 5, 30, 30));
EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
EXPECT_EQ(Rect(0, 0, 0, 0), result);
// No intersection
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(30, 30, 30, 30));
EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 20), result);
// Not a complete intersection in either direction
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(15, 15, 20, 20));
EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 20), result);
// Complete intersection in the x-direction, top edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(10, 15, 20, 20));
EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 5), result);
// Complete intersection in the x-direction, top edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(5, 15, 30, 20));
EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 5), result);
// Complete intersection in the x-direction, bottom edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(5, 5, 30, 20));
EXPECT_EQ(Rect(10, 25, 20, 5).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 25, 20, 5), result);
// Complete intersection in the x-direction, none of the edges is fully
// covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(5, 15, 30, 1));
EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 20), result);
// Complete intersection in the y-direction, left edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(10, 10, 10, 30));
EXPECT_EQ(Rect(20, 10, 10, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(20, 10, 10, 20), result);
// Complete intersection in the y-direction, left edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(5, 5, 20, 30));
EXPECT_EQ(Rect(25, 10, 5, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(25, 10, 5, 20), result);
// Complete intersection in the y-direction, right edge is fully covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(20, 5, 20, 30));
EXPECT_EQ(Rect(10, 10, 10, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 10, 20), result);
// Complete intersection in the y-direction, none of the edges is fully
// covered.
result = Rect(10, 10, 20, 20);
result.Subtract(Rect(15, 5, 1, 30));
EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
EXPECT_EQ(Rect(10, 10, 20, 20), result);
}
TEST(RectTest, IsEmpty) {
......@@ -569,13 +570,13 @@ TEST(RectTest, ToEnclosingRect) {
TEST(RectTest, ToNearestRect) {
Rect rect;
EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
rect = Rect(-1, -1, 3, 3);
EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
RectF rectf(-1.00001f, -0.999999f, 3.0000001f, 2.999999f);
EXPECT_EQ(rect.ToString(), ToNearestRect(rectf).ToString());
EXPECT_EQ(rect, ToNearestRect(rectf));
}
TEST(RectTest, ToFlooredRect) {
......@@ -651,7 +652,7 @@ TEST(RectTest, ScaleToEnclosedRect) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
Rect result = ScaleToEnclosedRect(tests[i].input_rect,
tests[i].input_scale);
EXPECT_EQ(tests[i].expected_rect.ToString(), result.ToString());
EXPECT_EQ(tests[i].expected_rect, result);
}
}
......@@ -695,7 +696,7 @@ TEST(RectTest, ScaleToEnclosingRect) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
Rect result = ScaleToEnclosingRect(tests[i].input_rect,
tests[i].input_scale);
EXPECT_EQ(tests[i].expected_rect.ToString(), result.ToString());
EXPECT_EQ(tests[i].expected_rect, result);
}
}
......@@ -714,7 +715,7 @@ TEST(RectTest, ToRectF) {
RectF b(10, 20, 30, 40);
RectF intersect = IntersectRects(a, b);
EXPECT_EQ(b.ToString(), intersect.ToString());
EXPECT_EQ(b, intersect);
EXPECT_EQ(a, b);
EXPECT_EQ(b, a);
......@@ -744,7 +745,7 @@ TEST(RectTest, BoundingRect) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) {
Rect actual = BoundingRect(int_tests[i].a, int_tests[i].b);
EXPECT_EQ(int_tests[i].expected.ToString(), actual.ToString());
EXPECT_EQ(int_tests[i].expected, actual);
}
struct {
......@@ -781,7 +782,7 @@ TEST(RectTest, BoundingRect) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) {
RectF actual = BoundingRect(float_tests[i].a, float_tests[i].b);
EXPECT_EQ(float_tests[i].expected.ToString(), actual.ToString());
EXPECT_RECTF_EQ(float_tests[i].expected, actual);
}
}
......@@ -818,40 +819,37 @@ TEST(RectTest, IsExpressibleAsRect) {
TEST(RectTest, Offset) {
Rect i(1, 2, 3, 4);
EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (i + Vector2d(1, -1)).ToString());
EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (Vector2d(1, -1) + i).ToString());
EXPECT_EQ(Rect(2, 1, 3, 4), (i + Vector2d(1, -1)));
EXPECT_EQ(Rect(2, 1, 3, 4), (Vector2d(1, -1) + i));
i += Vector2d(1, -1);
EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), i.ToString());
EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), (i - Vector2d(1, -1)).ToString());
EXPECT_EQ(Rect(2, 1, 3, 4), i);
EXPECT_EQ(Rect(1, 2, 3, 4), (i - Vector2d(1, -1)));
i -= Vector2d(1, -1);
EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), i.ToString());
EXPECT_EQ(Rect(1, 2, 3, 4), i);
RectF f(1.1f, 2.2f, 3.3f, 4.4f);
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
(f + Vector2dF(1.1f, -1.1f)).ToString());
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
(Vector2dF(1.1f, -1.1f) + f).ToString());
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f), (f + Vector2dF(1.1f, -1.1f)));
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f), (Vector2dF(1.1f, -1.1f) + f));
f += Vector2dF(1.1f, -1.1f);
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), f.ToString());
EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(),
(f - Vector2dF(1.1f, -1.1f)).ToString());
EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f), f);
EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f), (f - Vector2dF(1.1f, -1.1f)));
f -= Vector2dF(1.1f, -1.1f);
EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(), f.ToString());
EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f), f);
}
TEST(RectTest, Corners) {
Rect i(1, 2, 3, 4);
RectF f(1.1f, 2.1f, 3.1f, 4.1f);
EXPECT_EQ(Point(1, 2).ToString(), i.origin().ToString());
EXPECT_EQ(Point(4, 2).ToString(), i.top_right().ToString());
EXPECT_EQ(Point(1, 6).ToString(), i.bottom_left().ToString());
EXPECT_EQ(Point(4, 6).ToString(), i.bottom_right().ToString());
EXPECT_EQ(Point(1, 2), i.origin());
EXPECT_EQ(Point(4, 2), i.top_right());
EXPECT_EQ(Point(1, 6), i.bottom_left());
EXPECT_EQ(Point(4, 6), i.bottom_right());
EXPECT_EQ(PointF(1.1f, 2.1f).ToString(), f.origin().ToString());
EXPECT_EQ(PointF(4.2f, 2.1f).ToString(), f.top_right().ToString());
EXPECT_EQ(PointF(1.1f, 6.2f).ToString(), f.bottom_left().ToString());
EXPECT_EQ(PointF(4.2f, 6.2f).ToString(), f.bottom_right().ToString());
EXPECT_EQ(PointF(1.1f, 2.1f), f.origin());
EXPECT_EQ(PointF(4.2f, 2.1f), f.top_right());
EXPECT_EQ(PointF(1.1f, 6.2f), f.bottom_left());
EXPECT_EQ(PointF(4.2f, 6.2f), f.bottom_right());
}
TEST(RectTest, ManhattanDistanceToPoint) {
......
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_GEOMETRY_SIZE_H_
#define UI_GFX_GEOMETRY_SIZE_H_
#include <iosfwd>
#include <string>
#include "base/compiler_specific.h"
......@@ -62,6 +63,11 @@ inline bool operator!=(const Size& lhs, const Size& rhs) {
extern template class SizeBase<Size, int>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Size& size, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_SIZE_H_
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_GEOMETRY_SIZE_F_H_
#define UI_GFX_GEOMETRY_SIZE_F_H_
#include <iosfwd>
#include <string>
#include "base/compiler_specific.h"
......@@ -49,6 +50,11 @@ inline SizeF ScaleSize(const SizeF& p, float scale) {
extern template class SizeBase<SizeF, float>;
#endif
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const SizeF& size, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_SIZE_F_H_
......@@ -10,6 +10,7 @@
#ifndef UI_GFX_GEOMETRY_VECTOR2D_H_
#define UI_GFX_GEOMETRY_VECTOR2D_H_
#include <iosfwd>
#include <string>
#include "base/basictypes.h"
......@@ -86,6 +87,11 @@ inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
return result;
}
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Vector2d& vector, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_VECTOR2D_H_
......@@ -10,6 +10,7 @@
#ifndef UI_GFX_GEOMETRY_VECTOR2D_F_H_
#define UI_GFX_GEOMETRY_VECTOR2D_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/gfx_export.h"
......@@ -107,6 +108,11 @@ inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
return ScaleVector2d(v, scale, scale);
}
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Vector2dF& vector, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_VECTOR2D_F_H_
......@@ -10,6 +10,7 @@
#ifndef UI_GFX_GEOMETRY_VECTOR3D_F_H_
#define UI_GFX_GEOMETRY_VECTOR3D_F_H_
#include <iosfwd>
#include <string>
#include "ui/gfx/geometry/vector2d_f.h"
......@@ -119,6 +120,11 @@ inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
return ScaleVector3d(v, scale, scale, scale);
}
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Vector3dF& vector, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_
......@@ -414,6 +414,7 @@
},
{
'target_name': 'gfx_test_support',
'type': 'static_library',
'sources': [
'test/gfx_util.cc',
'test/gfx_util.h',
......@@ -433,15 +434,7 @@
],
},
}],
['OS!="ios"', {
'type': 'static_library',
}, { # OS=="ios"
# None of the sources in this target are built on iOS, resulting in
# link errors when building targets that depend on this target
# because the static library isn't found. If this target is changed
# to have sources that are built on iOS, the target should be changed
# to be of type static_library on all platforms.
'type': 'none',
['OS=="ios"', {
# The cocoa files don't apply to iOS.
'sources/': [
['exclude', 'cocoa']
......
......@@ -8,6 +8,20 @@
#include <sstream>
#include <string>
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point3_f.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/geometry/vector2d_f.h"
#include "ui/gfx/geometry/vector3d_f.h"
#include "ui/gfx/transform.h"
namespace gfx {
namespace {
......@@ -48,6 +62,21 @@ bool FloatAlmostEqual(float a, float b) {
<< "\nWhich is: " << lhs.ToString();
}
::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
const char* rhs_expr,
const RectF& lhs,
const RectF& rhs) {
if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
FloatAlmostEqual(lhs.y(), rhs.y()) &&
FloatAlmostEqual(lhs.width(), rhs.width()) &&
FloatAlmostEqual(lhs.height(), rhs.height())) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure()
<< "Value of: " << rhs_expr << "\n Actual: " << rhs.ToString()
<< "\nExpected: " << lhs_expr << "\nWhich is: " << lhs.ToString();
}
::testing::AssertionResult AssertSkColorsEqual(const char* lhs_expr,
const char* rhs_expr,
SkColor lhs,
......@@ -61,4 +90,56 @@ bool FloatAlmostEqual(float a, float b) {
<< "\nWhich is: " << ColorAsString(lhs);
}
void PrintTo(const BoxF& box, ::std::ostream* os) {
*os << box.ToString();
}
void PrintTo(const Point& point, ::std::ostream* os) {
*os << point.ToString();
}
void PrintTo(const Point3F& point, ::std::ostream* os) {
*os << point.ToString();
}
void PrintTo(const PointF& point, ::std::ostream* os) {
*os << point.ToString();
}
void PrintTo(const QuadF& quad, ::std::ostream* os) {
*os << quad.ToString();
}
void PrintTo(const Rect& rect, ::std::ostream* os) {
*os << rect.ToString();
}
void PrintTo(const RectF& rect, ::std::ostream* os) {
*os << rect.ToString();
}
void PrintTo(const Size& size, ::std::ostream* os) {
*os << size.ToString();
}
void PrintTo(const SizeF& size, ::std::ostream* os) {
*os << size.ToString();
}
void PrintTo(const Transform& transform, ::std::ostream* os) {
*os << transform.ToString();
}
void PrintTo(const Vector2d& vector, ::std::ostream* os) {
*os << vector.ToString();
}
void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
*os << vector.ToString();
}
void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
*os << vector.ToString();
}
} // namespace gfx
......@@ -11,7 +11,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/box_f.h"
#include "ui/gfx/geometry/rect_f.h"
namespace gfx {
......@@ -24,6 +24,14 @@ namespace gfx {
const BoxF& lhs,
const BoxF& rhs);
#define EXPECT_RECTF_EQ(a, b) \
EXPECT_PRED_FORMAT2(::gfx::AssertRectFloatEqual, a, b)
::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
const char* rhs_expr,
const RectF& lhs,
const RectF& rhs);
#define EXPECT_SKCOLOR_EQ(a, b) \
EXPECT_PRED_FORMAT2(::gfx::AssertSkColorsEqual, a, b)
......
......@@ -5,6 +5,7 @@
#ifndef UI_GFX_TRANSFORM_H_
#define UI_GFX_TRANSFORM_H_
#include <iosfwd>
#include <string>
#include "base/compiler_specific.h"
......@@ -268,6 +269,11 @@ class GFX_EXPORT Transform {
// copy/assign are allowed.
};
// This is declared here for use in gtest-based unit tests but is defined in
// the gfx_test_support target. Depend on that to use this in your unit test.
// This should not be used in production code - call ToString() instead.
void PrintTo(const Transform& transform, ::std::ostream* os);
} // namespace gfx
#endif // UI_GFX_TRANSFORM_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment