1 // Copyright 2014 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/test/gfx_util.h"
11 #include "ui/gfx/geometry/box_f.h"
12 #include "ui/gfx/geometry/point.h"
13 #include "ui/gfx/geometry/point3_f.h"
14 #include "ui/gfx/geometry/point_f.h"
15 #include "ui/gfx/geometry/quad_f.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/rect_f.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "ui/gfx/geometry/size_f.h"
20 #include "ui/gfx/geometry/vector2d.h"
21 #include "ui/gfx/geometry/vector2d_f.h"
22 #include "ui/gfx/geometry/vector3d_f.h"
23 #include "ui/gfx/transform.h"
29 std::string ColorAsString(SkColor color) {
30 std::ostringstream stream;
31 stream << std::hex << std::uppercase << "#" << std::setfill('0')
32 << std::setw(2) << SkColorGetA(color)
33 << std::setw(2) << SkColorGetR(color)
34 << std::setw(2) << SkColorGetG(color)
35 << std::setw(2) << SkColorGetB(color);
39 bool FloatAlmostEqual(float a, float b) {
40 // FloatLE is the gtest predicate for less than or almost equal to.
41 return ::testing::FloatLE("a", "b", a, b) &&
42 ::testing::FloatLE("b", "a", b, a);
47 ::testing::AssertionResult AssertBoxFloatEqual(const char* lhs_expr,
51 if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
52 FloatAlmostEqual(lhs.y(), rhs.y()) &&
53 FloatAlmostEqual(lhs.z(), rhs.z()) &&
54 FloatAlmostEqual(lhs.width(), rhs.width()) &&
55 FloatAlmostEqual(lhs.height(), rhs.height()) &&
56 FloatAlmostEqual(lhs.depth(), rhs.depth())) {
57 return ::testing::AssertionSuccess();
59 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
60 << "\n Actual: " << rhs.ToString()
61 << "\nExpected: " << lhs_expr
62 << "\nWhich is: " << lhs.ToString();
65 ::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
69 if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
70 FloatAlmostEqual(lhs.y(), rhs.y()) &&
71 FloatAlmostEqual(lhs.width(), rhs.width()) &&
72 FloatAlmostEqual(lhs.height(), rhs.height())) {
73 return ::testing::AssertionSuccess();
75 return ::testing::AssertionFailure()
76 << "Value of: " << rhs_expr << "\n Actual: " << rhs.ToString()
77 << "\nExpected: " << lhs_expr << "\nWhich is: " << lhs.ToString();
80 ::testing::AssertionResult AssertSkColorsEqual(const char* lhs_expr,
85 return ::testing::AssertionSuccess();
87 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
88 << "\n Actual: " << ColorAsString(rhs)
89 << "\nExpected: " << lhs_expr
90 << "\nWhich is: " << ColorAsString(lhs);
93 void PrintTo(const BoxF& box, ::std::ostream* os) {
94 *os << box.ToString();
97 void PrintTo(const Point& point, ::std::ostream* os) {
98 *os << point.ToString();
101 void PrintTo(const Point3F& point, ::std::ostream* os) {
102 *os << point.ToString();
105 void PrintTo(const PointF& point, ::std::ostream* os) {
106 *os << point.ToString();
109 void PrintTo(const QuadF& quad, ::std::ostream* os) {
110 *os << quad.ToString();
113 void PrintTo(const Rect& rect, ::std::ostream* os) {
114 *os << rect.ToString();
117 void PrintTo(const RectF& rect, ::std::ostream* os) {
118 *os << rect.ToString();
121 void PrintTo(const Size& size, ::std::ostream* os) {
122 *os << size.ToString();
125 void PrintTo(const SizeF& size, ::std::ostream* os) {
126 *os << size.ToString();
129 void PrintTo(const Transform& transform, ::std::ostream* os) {
130 *os << transform.ToString();
133 void PrintTo(const Vector2d& vector, ::std::ostream* os) {
134 *os << vector.ToString();
137 void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
138 *os << vector.ToString();
141 void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
142 *os << vector.ToString();