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