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.
4
5#include "ui/gfx/test/gfx_util.h"
6
7#include <iomanip>
8#include <sstream>
9#include <string>
10
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"
24
25namespace gfx {
26
27namespace {
28
29std::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);
36  return stream.str();
37}
38
39bool 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);
43}
44
45}  // namespace
46
47::testing::AssertionResult AssertBoxFloatEqual(const char* lhs_expr,
48                                               const char* rhs_expr,
49                                               const BoxF& lhs,
50                                               const BoxF& rhs) {
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();
58  }
59  return ::testing::AssertionFailure() << "Value of: " << rhs_expr
60                                       << "\n  Actual: " << rhs.ToString()
61                                       << "\nExpected: " << lhs_expr
62                                       << "\nWhich is: " << lhs.ToString();
63}
64
65::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
66                                                const char* rhs_expr,
67                                                const RectF& lhs,
68                                                const RectF& rhs) {
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();
74  }
75  return ::testing::AssertionFailure()
76         << "Value of: " << rhs_expr << "\n  Actual: " << rhs.ToString()
77         << "\nExpected: " << lhs_expr << "\nWhich is: " << lhs.ToString();
78}
79
80::testing::AssertionResult AssertSkColorsEqual(const char* lhs_expr,
81                                               const char* rhs_expr,
82                                               SkColor lhs,
83                                               SkColor rhs) {
84  if (lhs == rhs) {
85    return ::testing::AssertionSuccess();
86  }
87  return ::testing::AssertionFailure() << "Value of: " << rhs_expr
88                                       << "\n  Actual: " << ColorAsString(rhs)
89                                       << "\nExpected: " << lhs_expr
90                                       << "\nWhich is: " << ColorAsString(lhs);
91}
92
93void PrintTo(const BoxF& box, ::std::ostream* os) {
94  *os << box.ToString();
95}
96
97void PrintTo(const Point& point, ::std::ostream* os) {
98  *os << point.ToString();
99}
100
101void PrintTo(const Point3F& point, ::std::ostream* os) {
102  *os << point.ToString();
103}
104
105void PrintTo(const PointF& point, ::std::ostream* os) {
106  *os << point.ToString();
107}
108
109void PrintTo(const QuadF& quad, ::std::ostream* os) {
110  *os << quad.ToString();
111}
112
113void PrintTo(const Rect& rect, ::std::ostream* os) {
114  *os << rect.ToString();
115}
116
117void PrintTo(const RectF& rect, ::std::ostream* os) {
118  *os << rect.ToString();
119}
120
121void PrintTo(const Size& size, ::std::ostream* os) {
122  *os << size.ToString();
123}
124
125void PrintTo(const SizeF& size, ::std::ostream* os) {
126  *os << size.ToString();
127}
128
129void PrintTo(const Transform& transform, ::std::ostream* os) {
130  *os << transform.ToString();
131}
132
133void PrintTo(const Vector2d& vector, ::std::ostream* os) {
134  *os << vector.ToString();
135}
136
137void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
138  *os << vector.ToString();
139}
140
141void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
142  *os << vector.ToString();
143}
144
145}  // namespace gfx
146