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 namespace gfx {
12
13 namespace {
14
ColorAsString(SkColor color)15 std::string ColorAsString(SkColor color) {
16 std::ostringstream stream;
17 stream << std::hex << std::uppercase << "#" << std::setfill('0')
18 << std::setw(2) << SkColorGetA(color)
19 << std::setw(2) << SkColorGetR(color)
20 << std::setw(2) << SkColorGetG(color)
21 << std::setw(2) << SkColorGetB(color);
22 return stream.str();
23 }
24
FloatAlmostEqual(float a,float b)25 bool FloatAlmostEqual(float a, float b) {
26 // FloatLE is the gtest predicate for less than or almost equal to.
27 return ::testing::FloatLE("a", "b", a, b) &&
28 ::testing::FloatLE("b", "a", b, a);
29 }
30
31 } // namespace
32
AssertBoxFloatEqual(const char * lhs_expr,const char * rhs_expr,const BoxF & lhs,const BoxF & rhs)33 ::testing::AssertionResult AssertBoxFloatEqual(const char* lhs_expr,
34 const char* rhs_expr,
35 const BoxF& lhs,
36 const BoxF& rhs) {
37 if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
38 FloatAlmostEqual(lhs.y(), rhs.y()) &&
39 FloatAlmostEqual(lhs.z(), rhs.z()) &&
40 FloatAlmostEqual(lhs.width(), rhs.width()) &&
41 FloatAlmostEqual(lhs.height(), rhs.height()) &&
42 FloatAlmostEqual(lhs.depth(), rhs.depth())) {
43 return ::testing::AssertionSuccess();
44 }
45 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
46 << "\n Actual: " << rhs.ToString()
47 << "\nExpected: " << lhs_expr
48 << "\nWhich is: " << lhs.ToString();
49 }
50
AssertSkColorsEqual(const char * lhs_expr,const char * rhs_expr,SkColor lhs,SkColor rhs)51 ::testing::AssertionResult AssertSkColorsEqual(const char* lhs_expr,
52 const char* rhs_expr,
53 SkColor lhs,
54 SkColor rhs) {
55 if (lhs == rhs) {
56 return ::testing::AssertionSuccess();
57 }
58 return ::testing::AssertionFailure() << "Value of: " << rhs_expr
59 << "\n Actual: " << ColorAsString(rhs)
60 << "\nExpected: " << lhs_expr
61 << "\nWhich is: " << ColorAsString(lhs);
62 }
63
64 } // namespace gfx
65