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_conversions.h"
8 #include "ui/gfx/geometry/size_f.h"
9
10 namespace gfx {
11
12 namespace {
13
TestSizeF(const SizeF & s)14 int TestSizeF(const SizeF& s) {
15 return s.width();
16 }
17
18 } // namespace
19
TEST(SizeTest,ToSizeF)20 TEST(SizeTest, ToSizeF) {
21 // Check that explicit conversion from integer to float compiles.
22 Size a(10, 20);
23 float width = TestSizeF(gfx::SizeF(a));
24 EXPECT_EQ(width, a.width());
25
26 SizeF b(10, 20);
27
28 EXPECT_EQ(b, gfx::SizeF(a));
29 }
30
TEST(SizeTest,ToFlooredSize)31 TEST(SizeTest, ToFlooredSize) {
32 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0, 0)));
33 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.0001f, 0.0001f)));
34 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.4999f, 0.4999f)));
35 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.5f, 0.5f)));
36 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.9999f, 0.9999f)));
37
38 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10, 10)));
39 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.0001f, 10.0001f)));
40 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.4999f, 10.4999f)));
41 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.5f, 10.5f)));
42 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.9999f, 10.9999f)));
43 }
44
TEST(SizeTest,ToCeiledSize)45 TEST(SizeTest, ToCeiledSize) {
46 EXPECT_EQ(Size(0, 0), ToCeiledSize(SizeF(0, 0)));
47 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.0001f, 0.0001f)));
48 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.4999f, 0.4999f)));
49 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.5f, 0.5f)));
50 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.9999f, 0.9999f)));
51
52 EXPECT_EQ(Size(10, 10), ToCeiledSize(SizeF(10, 10)));
53 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.0001f, 10.0001f)));
54 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.4999f, 10.4999f)));
55 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.5f, 10.5f)));
56 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.9999f, 10.9999f)));
57 }
58
TEST(SizeTest,ToRoundedSize)59 TEST(SizeTest, ToRoundedSize) {
60 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0, 0)));
61 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.0001f, 0.0001f)));
62 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.4999f, 0.4999f)));
63 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.5f, 0.5f)));
64 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.9999f, 0.9999f)));
65
66 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10, 10)));
67 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.0001f, 10.0001f)));
68 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.4999f, 10.4999f)));
69 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.5f, 10.5f)));
70 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.9999f, 10.9999f)));
71 }
72
TEST(SizeTest,ClampSize)73 TEST(SizeTest, ClampSize) {
74 Size a;
75
76 a = Size(3, 5);
77 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
78 a.SetToMax(Size(2, 4));
79 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
80 a.SetToMax(Size(3, 5));
81 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
82 a.SetToMax(Size(4, 2));
83 EXPECT_EQ(Size(4, 5).ToString(), a.ToString());
84 a.SetToMax(Size(8, 10));
85 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
86
87 a.SetToMin(Size(9, 11));
88 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
89 a.SetToMin(Size(8, 10));
90 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
91 a.SetToMin(Size(11, 9));
92 EXPECT_EQ(Size(8, 9).ToString(), a.ToString());
93 a.SetToMin(Size(7, 11));
94 EXPECT_EQ(Size(7, 9).ToString(), a.ToString());
95 a.SetToMin(Size(3, 5));
96 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
97 }
98
TEST(SizeTest,ClampSizeF)99 TEST(SizeTest, ClampSizeF) {
100 SizeF a;
101
102 a = SizeF(3.5f, 5.5f);
103 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
104 a.SetToMax(SizeF(2.5f, 4.5f));
105 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
106 a.SetToMax(SizeF(3.5f, 5.5f));
107 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
108 a.SetToMax(SizeF(4.5f, 2.5f));
109 EXPECT_EQ(SizeF(4.5f, 5.5f).ToString(), a.ToString());
110 a.SetToMax(SizeF(8.5f, 10.5f));
111 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
112
113 a.SetToMin(SizeF(9.5f, 11.5f));
114 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
115 a.SetToMin(SizeF(8.5f, 10.5f));
116 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
117 a.SetToMin(SizeF(11.5f, 9.5f));
118 EXPECT_EQ(SizeF(8.5f, 9.5f).ToString(), a.ToString());
119 a.SetToMin(SizeF(7.5f, 11.5f));
120 EXPECT_EQ(SizeF(7.5f, 9.5f).ToString(), a.ToString());
121 a.SetToMin(SizeF(3.5f, 5.5f));
122 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
123 }
124
TEST(SizeTest,Enlarge)125 TEST(SizeTest, Enlarge) {
126 Size test(3, 4);
127 test.Enlarge(5, -8);
128 EXPECT_EQ(test, Size(8, -4));
129 }
130
TEST(SizeTest,IntegerOverflow)131 TEST(SizeTest, IntegerOverflow) {
132 int int_max = std::numeric_limits<int>::max();
133 int int_min = std::numeric_limits<int>::min();
134
135 Size max_size(int_max, int_max);
136 Size min_size(int_min, int_min);
137 Size test;
138
139 test = Size();
140 test.Enlarge(int_max, int_max);
141 EXPECT_EQ(test, max_size);
142
143 test = Size();
144 test.Enlarge(int_min, int_min);
145 EXPECT_EQ(test, min_size);
146
147 test = Size(10, 20);
148 test.Enlarge(int_max, int_max);
149 EXPECT_EQ(test, max_size);
150
151 test = Size(-10, -20);
152 test.Enlarge(int_min, int_min);
153 EXPECT_EQ(test, min_size);
154 }
155
156 // This checks that we set IsEmpty appropriately.
TEST(SizeTest,TrivialDimensionTests)157 TEST(SizeTest, TrivialDimensionTests) {
158 const float clearly_trivial = SizeF::kTrivial / 2.f;
159 const float massize_dimension = 4e13f;
160
161 // First, using the constructor.
162 EXPECT_TRUE(SizeF(clearly_trivial, 1.f).IsEmpty());
163 EXPECT_TRUE(SizeF(.01f, clearly_trivial).IsEmpty());
164 EXPECT_TRUE(SizeF(0.f, 0.f).IsEmpty());
165 EXPECT_FALSE(SizeF(.01f, .01f).IsEmpty());
166
167 // Then use the setter.
168 SizeF test(2.f, 1.f);
169 EXPECT_FALSE(test.IsEmpty());
170
171 test.SetSize(clearly_trivial, 1.f);
172 EXPECT_TRUE(test.IsEmpty());
173
174 test.SetSize(.01f, clearly_trivial);
175 EXPECT_TRUE(test.IsEmpty());
176
177 test.SetSize(0.f, 0.f);
178 EXPECT_TRUE(test.IsEmpty());
179
180 test.SetSize(.01f, .01f);
181 EXPECT_FALSE(test.IsEmpty());
182
183 // Now just one dimension at a time.
184 test.set_width(clearly_trivial);
185 EXPECT_TRUE(test.IsEmpty());
186
187 test.set_width(massize_dimension);
188 test.set_height(clearly_trivial);
189 EXPECT_TRUE(test.IsEmpty());
190
191 test.set_width(clearly_trivial);
192 test.set_height(massize_dimension);
193 EXPECT_TRUE(test.IsEmpty());
194
195 test.set_width(2.f);
196 EXPECT_FALSE(test.IsEmpty());
197 }
198
199 // These are the ramifications of the decision to keep the recorded size
200 // at zero for trivial sizes.
TEST(SizeTest,ClampsToZero)201 TEST(SizeTest, ClampsToZero) {
202 const float clearly_trivial = SizeF::kTrivial / 2.f;
203 const float nearly_trivial = SizeF::kTrivial * 1.5f;
204
205 SizeF test(clearly_trivial, 1.f);
206
207 EXPECT_FLOAT_EQ(0.f, test.width());
208 EXPECT_FLOAT_EQ(1.f, test.height());
209
210 test.SetSize(.01f, clearly_trivial);
211
212 EXPECT_FLOAT_EQ(.01f, test.width());
213 EXPECT_FLOAT_EQ(0.f, test.height());
214
215 test.SetSize(nearly_trivial, nearly_trivial);
216
217 EXPECT_FLOAT_EQ(nearly_trivial, test.width());
218 EXPECT_FLOAT_EQ(nearly_trivial, test.height());
219
220 test.Scale(0.5f);
221
222 EXPECT_FLOAT_EQ(0.f, test.width());
223 EXPECT_FLOAT_EQ(0.f, test.height());
224
225 test.SetSize(0.f, 0.f);
226 test.Enlarge(clearly_trivial, clearly_trivial);
227 test.Enlarge(clearly_trivial, clearly_trivial);
228 test.Enlarge(clearly_trivial, clearly_trivial);
229
230 EXPECT_EQ(SizeF(0.f, 0.f), test);
231 }
232
233 // These make sure the constructor and setter have the same effect on the
234 // boundary case. This claims to know the boundary, but not which way it goes.
TEST(SizeTest,ConsistentClamping)235 TEST(SizeTest, ConsistentClamping) {
236 SizeF resized;
237
238 resized.SetSize(SizeF::kTrivial, 0.f);
239 EXPECT_EQ(SizeF(SizeF::kTrivial, 0.f), resized);
240
241 resized.SetSize(0.f, SizeF::kTrivial);
242 EXPECT_EQ(SizeF(0.f, SizeF::kTrivial), resized);
243 }
244
245 // Let's make sure we don't unexpectedly grow the struct by adding constants.
246 // Also, if some platform packs floats inefficiently, it would be worth noting.
TEST(SizeTest,StaysSmall)247 TEST(SizeTest, StaysSmall) {
248 EXPECT_EQ(2 * sizeof(float), sizeof(SizeF));
249 }
250
251 } // namespace gfx
252