Roll src/third_party/skia 591a2ca:b3fe1b8
[chromium-blink-merge.git] / ui / gfx / test / gfx_util.cc
blobd8bf5f2af942e8b3eb54db125d47f78e8a39639e
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"
7 #include <iomanip>
8 #include <sstream>
9 #include <string>
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/scroll_offset.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/geometry/size_f.h"
21 #include "ui/gfx/geometry/vector2d.h"
22 #include "ui/gfx/geometry/vector2d_f.h"
23 #include "ui/gfx/geometry/vector3d_f.h"
24 #include "ui/gfx/transform.h"
26 namespace gfx {
28 namespace {
30 std::string ColorAsString(SkColor color) {
31 std::ostringstream stream;
32 stream << std::hex << std::uppercase << "#" << std::setfill('0')
33 << std::setw(2) << SkColorGetA(color)
34 << std::setw(2) << SkColorGetR(color)
35 << std::setw(2) << SkColorGetG(color)
36 << std::setw(2) << SkColorGetB(color);
37 return stream.str();
40 bool FloatAlmostEqual(float a, float b) {
41 // FloatLE is the gtest predicate for less than or almost equal to.
42 return ::testing::FloatLE("a", "b", a, b) &&
43 ::testing::FloatLE("b", "a", b, a);
46 } // namespace
48 ::testing::AssertionResult AssertBoxFloatEqual(const char* lhs_expr,
49 const char* rhs_expr,
50 const BoxF& lhs,
51 const BoxF& rhs) {
52 if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
53 FloatAlmostEqual(lhs.y(), rhs.y()) &&
54 FloatAlmostEqual(lhs.z(), rhs.z()) &&
55 FloatAlmostEqual(lhs.width(), rhs.width()) &&
56 FloatAlmostEqual(lhs.height(), rhs.height()) &&
57 FloatAlmostEqual(lhs.depth(), rhs.depth())) {
58 return ::testing::AssertionSuccess();
60 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
61 << "\n Actual: " << rhs.ToString()
62 << "\nExpected: " << lhs_expr
63 << "\nWhich is: " << lhs.ToString();
66 ::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
67 const char* rhs_expr,
68 const RectF& lhs,
69 const RectF& rhs) {
70 if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
71 FloatAlmostEqual(lhs.y(), rhs.y()) &&
72 FloatAlmostEqual(lhs.width(), rhs.width()) &&
73 FloatAlmostEqual(lhs.height(), rhs.height())) {
74 return ::testing::AssertionSuccess();
76 return ::testing::AssertionFailure()
77 << "Value of: " << rhs_expr << "\n Actual: " << rhs.ToString()
78 << "\nExpected: " << lhs_expr << "\nWhich is: " << lhs.ToString();
81 ::testing::AssertionResult AssertSkColorsEqual(const char* lhs_expr,
82 const char* rhs_expr,
83 SkColor lhs,
84 SkColor rhs) {
85 if (lhs == rhs) {
86 return ::testing::AssertionSuccess();
88 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
89 << "\n Actual: " << ColorAsString(rhs)
90 << "\nExpected: " << lhs_expr
91 << "\nWhich is: " << ColorAsString(lhs);
94 void PrintTo(const BoxF& box, ::std::ostream* os) {
95 *os << box.ToString();
98 void PrintTo(const Point& point, ::std::ostream* os) {
99 *os << point.ToString();
102 void PrintTo(const Point3F& point, ::std::ostream* os) {
103 *os << point.ToString();
106 void PrintTo(const PointF& point, ::std::ostream* os) {
107 *os << point.ToString();
110 void PrintTo(const QuadF& quad, ::std::ostream* os) {
111 *os << quad.ToString();
114 void PrintTo(const Rect& rect, ::std::ostream* os) {
115 *os << rect.ToString();
118 void PrintTo(const RectF& rect, ::std::ostream* os) {
119 *os << rect.ToString();
122 void PrintTo(const ScrollOffset& offset, ::std::ostream* os) {
123 *os << offset.ToString();
126 void PrintTo(const Size& size, ::std::ostream* os) {
127 *os << size.ToString();
130 void PrintTo(const SizeF& size, ::std::ostream* os) {
131 *os << size.ToString();
134 void PrintTo(const Transform& transform, ::std::ostream* os) {
135 *os << transform.ToString();
138 void PrintTo(const Vector2d& vector, ::std::ostream* os) {
139 *os << vector.ToString();
142 void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
143 *os << vector.ToString();
146 void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
147 *os << vector.ToString();
150 } // namespace gfx