Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / test / gfx_util.cc
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/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"
25
26 namespace gfx {
27
28 namespace {
29
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();
38 }
39
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);
44 }
45
46 }  // namespace
47
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();
59   }
60   return ::testing::AssertionFailure() << "Value of: " << rhs_expr
61                                        << "\n  Actual: " << rhs.ToString()
62                                        << "\nExpected: " << lhs_expr
63                                        << "\nWhich is: " << lhs.ToString();
64 }
65
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();
75   }
76   return ::testing::AssertionFailure()
77          << "Value of: " << rhs_expr << "\n  Actual: " << rhs.ToString()
78          << "\nExpected: " << lhs_expr << "\nWhich is: " << lhs.ToString();
79 }
80
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();
87   }
88   return ::testing::AssertionFailure() << "Value of: " << rhs_expr
89                                        << "\n  Actual: " << ColorAsString(rhs)
90                                        << "\nExpected: " << lhs_expr
91                                        << "\nWhich is: " << ColorAsString(lhs);
92 }
93
94 void PrintTo(const BoxF& box, ::std::ostream* os) {
95   *os << box.ToString();
96 }
97
98 void PrintTo(const Point& point, ::std::ostream* os) {
99   *os << point.ToString();
100 }
101
102 void PrintTo(const Point3F& point, ::std::ostream* os) {
103   *os << point.ToString();
104 }
105
106 void PrintTo(const PointF& point, ::std::ostream* os) {
107   *os << point.ToString();
108 }
109
110 void PrintTo(const QuadF& quad, ::std::ostream* os) {
111   *os << quad.ToString();
112 }
113
114 void PrintTo(const Rect& rect, ::std::ostream* os) {
115   *os << rect.ToString();
116 }
117
118 void PrintTo(const RectF& rect, ::std::ostream* os) {
119   *os << rect.ToString();
120 }
121
122 void PrintTo(const ScrollOffset& offset, ::std::ostream* os) {
123   *os << offset.ToString();
124 }
125
126 void PrintTo(const Size& size, ::std::ostream* os) {
127   *os << size.ToString();
128 }
129
130 void PrintTo(const SizeF& size, ::std::ostream* os) {
131   *os << size.ToString();
132 }
133
134 void PrintTo(const Transform& transform, ::std::ostream* os) {
135   *os << transform.ToString();
136 }
137
138 void PrintTo(const Vector2d& vector, ::std::ostream* os) {
139   *os << vector.ToString();
140 }
141
142 void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
143   *os << vector.ToString();
144 }
145
146 void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
147   *os << vector.ToString();
148 }
149
150 }  // namespace gfx