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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/gfx/geometry/size.h"
7 #include "ui/gfx/geometry/size_conversions.h"
8 #include "ui/gfx/geometry/size_f.h"
14 int TestSizeF(const SizeF
& s
) {
20 TEST(SizeTest
, ToSizeF
) {
21 // Check that implicit conversion from integer to float compiles.
23 float width
= TestSizeF(a
);
24 EXPECT_EQ(width
, a
.width());
32 TEST(SizeTest
, ToFlooredSize
) {
33 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0, 0)));
34 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.0001f
, 0.0001f
)));
35 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.4999f
, 0.4999f
)));
36 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.5f
, 0.5f
)));
37 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.9999f
, 0.9999f
)));
39 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10, 10)));
40 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.0001f
, 10.0001f
)));
41 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.4999f
, 10.4999f
)));
42 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.5f
, 10.5f
)));
43 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.9999f
, 10.9999f
)));
46 TEST(SizeTest
, ToCeiledSize
) {
47 EXPECT_EQ(Size(0, 0), ToCeiledSize(SizeF(0, 0)));
48 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.0001f
, 0.0001f
)));
49 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.4999f
, 0.4999f
)));
50 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.5f
, 0.5f
)));
51 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.9999f
, 0.9999f
)));
53 EXPECT_EQ(Size(10, 10), ToCeiledSize(SizeF(10, 10)));
54 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.0001f
, 10.0001f
)));
55 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.4999f
, 10.4999f
)));
56 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.5f
, 10.5f
)));
57 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.9999f
, 10.9999f
)));
60 TEST(SizeTest
, ToRoundedSize
) {
61 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0, 0)));
62 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.0001f
, 0.0001f
)));
63 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.4999f
, 0.4999f
)));
64 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.5f
, 0.5f
)));
65 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.9999f
, 0.9999f
)));
67 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10, 10)));
68 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.0001f
, 10.0001f
)));
69 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.4999f
, 10.4999f
)));
70 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.5f
, 10.5f
)));
71 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.9999f
, 10.9999f
)));
74 TEST(SizeTest
, ClampSize
) {
78 EXPECT_EQ(Size(3, 5).ToString(), a
.ToString());
79 a
.SetToMax(Size(2, 4));
80 EXPECT_EQ(Size(3, 5).ToString(), a
.ToString());
81 a
.SetToMax(Size(3, 5));
82 EXPECT_EQ(Size(3, 5).ToString(), a
.ToString());
83 a
.SetToMax(Size(4, 2));
84 EXPECT_EQ(Size(4, 5).ToString(), a
.ToString());
85 a
.SetToMax(Size(8, 10));
86 EXPECT_EQ(Size(8, 10).ToString(), a
.ToString());
88 a
.SetToMin(Size(9, 11));
89 EXPECT_EQ(Size(8, 10).ToString(), a
.ToString());
90 a
.SetToMin(Size(8, 10));
91 EXPECT_EQ(Size(8, 10).ToString(), a
.ToString());
92 a
.SetToMin(Size(11, 9));
93 EXPECT_EQ(Size(8, 9).ToString(), a
.ToString());
94 a
.SetToMin(Size(7, 11));
95 EXPECT_EQ(Size(7, 9).ToString(), a
.ToString());
96 a
.SetToMin(Size(3, 5));
97 EXPECT_EQ(Size(3, 5).ToString(), a
.ToString());
100 TEST(SizeTest
, ClampSizeF
) {
103 a
= SizeF(3.5f
, 5.5f
);
104 EXPECT_EQ(SizeF(3.5f
, 5.5f
).ToString(), a
.ToString());
105 a
.SetToMax(SizeF(2.5f
, 4.5f
));
106 EXPECT_EQ(SizeF(3.5f
, 5.5f
).ToString(), a
.ToString());
107 a
.SetToMax(SizeF(3.5f
, 5.5f
));
108 EXPECT_EQ(SizeF(3.5f
, 5.5f
).ToString(), a
.ToString());
109 a
.SetToMax(SizeF(4.5f
, 2.5f
));
110 EXPECT_EQ(SizeF(4.5f
, 5.5f
).ToString(), a
.ToString());
111 a
.SetToMax(SizeF(8.5f
, 10.5f
));
112 EXPECT_EQ(SizeF(8.5f
, 10.5f
).ToString(), a
.ToString());
114 a
.SetToMin(SizeF(9.5f
, 11.5f
));
115 EXPECT_EQ(SizeF(8.5f
, 10.5f
).ToString(), a
.ToString());
116 a
.SetToMin(SizeF(8.5f
, 10.5f
));
117 EXPECT_EQ(SizeF(8.5f
, 10.5f
).ToString(), a
.ToString());
118 a
.SetToMin(SizeF(11.5f
, 9.5f
));
119 EXPECT_EQ(SizeF(8.5f
, 9.5f
).ToString(), a
.ToString());
120 a
.SetToMin(SizeF(7.5f
, 11.5f
));
121 EXPECT_EQ(SizeF(7.5f
, 9.5f
).ToString(), a
.ToString());
122 a
.SetToMin(SizeF(3.5f
, 5.5f
));
123 EXPECT_EQ(SizeF(3.5f
, 5.5f
).ToString(), a
.ToString());