1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" 6 #include "ui/gfx/geometry/size.h" 7 #include "ui/gfx/geometry/size_base.h" 8 #include "ui/gfx/geometry/size_conversions.h" 9 #include "ui/gfx/geometry/size_f.h" 10 11 namespace gfx { 12 13 namespace { 14 15 int TestSizeF(const SizeF& s) { 16 return s.width(); 17 } 18 19 } // namespace 20 21 TEST(SizeTest, ToSizeF) { 22 // Check that implicit conversion from integer to float compiles. 23 Size a(10, 20); 24 float width = TestSizeF(a); 25 EXPECT_EQ(width, a.width()); 26 27 SizeF b(10, 20); 28 29 EXPECT_EQ(a, b); 30 EXPECT_EQ(b, a); 31 } 32 33 TEST(SizeTest, ToFlooredSize) { 34 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0, 0))); 35 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.0001f, 0.0001f))); 36 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.4999f, 0.4999f))); 37 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.5f, 0.5f))); 38 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.9999f, 0.9999f))); 39 40 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10, 10))); 41 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.0001f, 10.0001f))); 42 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.4999f, 10.4999f))); 43 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.5f, 10.5f))); 44 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.9999f, 10.9999f))); 45 } 46 47 TEST(SizeTest, ToCeiledSize) { 48 EXPECT_EQ(Size(0, 0), ToCeiledSize(SizeF(0, 0))); 49 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.0001f, 0.0001f))); 50 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.4999f, 0.4999f))); 51 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.5f, 0.5f))); 52 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.9999f, 0.9999f))); 53 54 EXPECT_EQ(Size(10, 10), ToCeiledSize(SizeF(10, 10))); 55 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.0001f, 10.0001f))); 56 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.4999f, 10.4999f))); 57 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.5f, 10.5f))); 58 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.9999f, 10.9999f))); 59 } 60 61 TEST(SizeTest, ToRoundedSize) { 62 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0, 0))); 63 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.0001f, 0.0001f))); 64 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.4999f, 0.4999f))); 65 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.5f, 0.5f))); 66 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.9999f, 0.9999f))); 67 68 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10, 10))); 69 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.0001f, 10.0001f))); 70 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.4999f, 10.4999f))); 71 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.5f, 10.5f))); 72 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.9999f, 10.9999f))); 73 } 74 75 TEST(SizeTest, ClampSize) { 76 Size a; 77 78 a = Size(3, 5); 79 EXPECT_EQ(Size(3, 5).ToString(), a.ToString()); 80 a.SetToMax(Size(2, 4)); 81 EXPECT_EQ(Size(3, 5).ToString(), a.ToString()); 82 a.SetToMax(Size(3, 5)); 83 EXPECT_EQ(Size(3, 5).ToString(), a.ToString()); 84 a.SetToMax(Size(4, 2)); 85 EXPECT_EQ(Size(4, 5).ToString(), a.ToString()); 86 a.SetToMax(Size(8, 10)); 87 EXPECT_EQ(Size(8, 10).ToString(), a.ToString()); 88 89 a.SetToMin(Size(9, 11)); 90 EXPECT_EQ(Size(8, 10).ToString(), a.ToString()); 91 a.SetToMin(Size(8, 10)); 92 EXPECT_EQ(Size(8, 10).ToString(), a.ToString()); 93 a.SetToMin(Size(11, 9)); 94 EXPECT_EQ(Size(8, 9).ToString(), a.ToString()); 95 a.SetToMin(Size(7, 11)); 96 EXPECT_EQ(Size(7, 9).ToString(), a.ToString()); 97 a.SetToMin(Size(3, 5)); 98 EXPECT_EQ(Size(3, 5).ToString(), a.ToString()); 99 } 100 101 TEST(SizeTest, ClampSizeF) { 102 SizeF a; 103 104 a = SizeF(3.5f, 5.5f); 105 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString()); 106 a.SetToMax(SizeF(2.5f, 4.5f)); 107 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString()); 108 a.SetToMax(SizeF(3.5f, 5.5f)); 109 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString()); 110 a.SetToMax(SizeF(4.5f, 2.5f)); 111 EXPECT_EQ(SizeF(4.5f, 5.5f).ToString(), a.ToString()); 112 a.SetToMax(SizeF(8.5f, 10.5f)); 113 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString()); 114 115 a.SetToMin(SizeF(9.5f, 11.5f)); 116 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString()); 117 a.SetToMin(SizeF(8.5f, 10.5f)); 118 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString()); 119 a.SetToMin(SizeF(11.5f, 9.5f)); 120 EXPECT_EQ(SizeF(8.5f, 9.5f).ToString(), a.ToString()); 121 a.SetToMin(SizeF(7.5f, 11.5f)); 122 EXPECT_EQ(SizeF(7.5f, 9.5f).ToString(), a.ToString()); 123 a.SetToMin(SizeF(3.5f, 5.5f)); 124 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString()); 125 } 126 127 } // namespace gfx 128