1/*
2 * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include <WebCore/FloatSize.h>
29#include <WebCore/IntSize.h>
30
31#if USE(CG)
32#include <CoreGraphics/CoreGraphics.h>
33#endif
34
35#if PLATFORM(WIN)
36#include <d2d1.h>
37#endif
38
39namespace TestWebKitAPI {
40
41static void testGetAndSet(WebCore::IntSize rect)
42{
43 rect.setWidth(203);
44 EXPECT_EQ(203, rect.width());
45 rect.setHeight(73);
46 EXPECT_EQ(73, rect.height());
47}
48
49static void testEmptySize(const WebCore::IntSize& rect)
50{
51 EXPECT_EQ(0, rect.width());
52 EXPECT_EQ(0, rect.height());
53 EXPECT_TRUE(rect.isEmpty());
54 EXPECT_TRUE(rect.isZero());
55}
56
57TEST(IntSize, DefaultConstruction)
58{
59 WebCore::IntSize test;
60
61 testEmptySize(test);
62
63 testGetAndSet(test);
64}
65
66TEST(IntSize, ValueConstruction)
67{
68 WebCore::IntSize test(100, 200);
69
70 EXPECT_EQ(100, test.width());
71 EXPECT_EQ(200, test.height());
72 EXPECT_FALSE(test.isEmpty());
73 EXPECT_FALSE(test.isZero());
74
75 static const float epsilon = 0.0001f;
76 EXPECT_NEAR(0.5f, test.aspectRatio(), epsilon);
77}
78
79TEST(IntSize, FloatSizeConstruction)
80{
81 WebCore::FloatSize size(1024.2, 767.8);
82 WebCore::IntSize test(size);
83
84 EXPECT_EQ(1024, test.width());
85 EXPECT_EQ(767, test.height());
86
87 ASSERT_FALSE(test.isEmpty());
88 ASSERT_FALSE(test.isZero());
89
90 static const double epsilon = 0.001;
91 EXPECT_NEAR(1.335f, test.aspectRatio(), epsilon);
92
93 testGetAndSet(test);
94}
95
96TEST(IntSize, DiagonalLengthAndArea)
97{
98 WebCore::IntSize test(1024, 768);
99
100 EXPECT_EQ(1638400, test.diagonalLengthSquared());
101 EXPECT_EQ(786432U, test.area().unsafeGet());
102}
103
104TEST(IntSize, Scale)
105{
106 WebCore::IntSize test(1024, 768);
107
108 test.scale(2.0f);
109
110 EXPECT_EQ(2048, test.width());
111 EXPECT_EQ(1536, test.height());
112
113 test.scale(0.5f);
114
115 EXPECT_EQ(1024, test.width());
116 EXPECT_EQ(768, test.height());
117
118 test.scale(2.0f, 0.5f);
119
120 EXPECT_EQ(2048, test.width());
121 EXPECT_EQ(384, test.height());
122}
123
124TEST(IntSize, Expand)
125{
126 WebCore::IntSize test(1024, 768);
127
128 EXPECT_EQ(1024, test.width());
129 EXPECT_EQ(768, test.height());
130
131 test.expand(100, 50);
132
133 EXPECT_EQ(1124, test.width());
134 EXPECT_EQ(818, test.height());
135
136 WebCore::IntSize other(2048, 700);
137
138 auto expanded = test.expandedTo(other);
139
140 EXPECT_EQ(2048, expanded.width());
141 EXPECT_EQ(818, expanded.height());
142}
143
144TEST(IntSize, Shrink)
145{
146 WebCore::IntSize test(1024, 768);
147 WebCore::IntSize other(1000, 700);
148
149 auto shrunken = test.shrunkTo(other);
150
151 EXPECT_EQ(1000, shrunken.width());
152 EXPECT_EQ(700, shrunken.height());
153
154 WebCore::IntSize other2(2000.0f, 700.0f);
155
156 auto shrunken2 = test.shrunkTo(other2);
157
158 EXPECT_EQ(1024, shrunken2.width());
159 EXPECT_EQ(700, shrunken2.height());
160}
161
162TEST(IntSize, TransposedSize)
163{
164 WebCore::IntSize test(1024, 768);
165
166 auto transposedSize = test.transposedSize();
167
168 EXPECT_EQ(768, transposedSize.width());
169 EXPECT_EQ(1024, transposedSize.height());
170}
171
172TEST(IntSize, Casting)
173{
174 WebCore::IntSize test(1024, 768);
175
176#if USE(CG)
177 CGSize cgSize = test;
178
179 EXPECT_FLOAT_EQ(1024.0f, cgSize.width);
180 EXPECT_FLOAT_EQ(768.0f, cgSize.height);
181
182 CGSize cgSize2 = CGSizeMake(-22.3f, 14.6f);
183
184 WebCore::IntSize testCG(cgSize2);
185
186 EXPECT_EQ(-22, testCG.width());
187 EXPECT_EQ(14, testCG.height());
188#endif
189
190#if PLATFORM(WIN)
191 D2D1_SIZE_U d2dSizeU = test;
192 EXPECT_EQ(1024, d2dSizeU.width);
193 EXPECT_EQ(768, d2dSizeU.height);
194
195 D2D1_SIZE_F d2dSizeF = test;
196 EXPECT_FLOAT_EQ(1024.0f, d2dSizeF.width);
197 EXPECT_FLOAT_EQ(768.0f, d2dSizeF.height);
198
199 D2D1_SIZE_F d2dSizeF2 = D2D1::SizeF(-22.3f, 14.6f);
200
201 WebCore::IntSize testD2D(d2dSizeF2);
202
203 EXPECT_EQ(-22, testD2D.width());
204 EXPECT_EQ(14, testD2D.height());
205
206 D2D1_SIZE_F d2dSizeU2 = D2D1::SizeF(-23, 16);
207
208 WebCore::IntSize testD2D2(d2dSizeU2);
209
210 EXPECT_EQ(-23, testD2D2.width());
211 EXPECT_EQ(16, testD2D2.height());
212#endif
213}
214
215TEST(IntSize, AddSubtract)
216{
217 WebCore::IntSize a(512, 384);
218 WebCore::IntSize b(100, 100);
219
220 WebCore::IntSize c = a + b;
221
222 EXPECT_EQ(612, c.width());
223 EXPECT_EQ(484, c.height());
224
225 a += b;
226
227 EXPECT_EQ(612, a.width());
228 EXPECT_EQ(484, a.height());
229
230 WebCore::IntSize a2(512, 384);
231
232 WebCore::IntSize d = a2 - b;
233
234 EXPECT_EQ(412, d.width());
235 EXPECT_EQ(284, d.height());
236
237 a2 -= b;
238
239 EXPECT_EQ(412, a2.width());
240 EXPECT_EQ(284, a2.height());
241}
242
243TEST(IntSize, Negation)
244{
245 WebCore::IntSize a(512, 384);
246
247 WebCore::IntSize negated = -a;
248
249 EXPECT_EQ(-512, negated.width());
250 EXPECT_EQ(-384, negated.height());
251}
252
253TEST(IntSize, Equality)
254{
255 WebCore::IntSize a(1024, 768);
256 WebCore::IntSize b(1024, 768);
257 WebCore::IntSize c(768, 534);
258
259 ASSERT_TRUE(a == b);
260 ASSERT_FALSE(a != b);
261 ASSERT_FALSE(a == c);
262 ASSERT_TRUE(a != c);
263}
264
265TEST(IntSize, Contract)
266{
267 WebCore::IntSize a(1024, 768);
268
269 a.contract(100, 50);
270
271 EXPECT_EQ(924, a.width());
272 EXPECT_EQ(718, a.height());
273}
274
275TEST(IntSize, Clamp)
276{
277 WebCore::IntSize a(1024, 768);
278
279 a.clampNegativeToZero();
280
281 EXPECT_EQ(1024, a.width());
282 EXPECT_EQ(768, a.height());
283
284 WebCore::IntSize b(-1024, -768);
285
286 b.clampNegativeToZero();
287
288 EXPECT_EQ(0, b.width());
289 EXPECT_EQ(0, b.height());
290
291 WebCore::IntSize minimumSize(1024, 1000);
292
293 a.clampToMinimumSize(minimumSize);
294
295 EXPECT_EQ(1024, a.width());
296 EXPECT_EQ(1000, a.height());
297}
298
299TEST(IntSize, ConstrainedBetween)
300{
301 WebCore::IntSize minimumSize(384, 256);
302 WebCore::IntSize maximumSize(2048, 1536);
303
304 WebCore::IntSize a(1024, 768);
305
306 auto constrained1 = a.constrainedBetween(minimumSize, maximumSize);
307
308 EXPECT_EQ(1024, constrained1.width());
309 EXPECT_EQ(768, constrained1.height());
310
311 WebCore::IntSize b(200, 100);
312
313 auto constrained2 = b.constrainedBetween(minimumSize, maximumSize);
314
315 EXPECT_EQ(384, constrained2.width());
316 EXPECT_EQ(256, constrained2.height());
317
318 WebCore::IntSize c(5000, 2000);
319
320 auto constrained3 = c.constrainedBetween(minimumSize, maximumSize);
321
322 EXPECT_EQ(2048, constrained3.width());
323 EXPECT_EQ(1536, constrained3.height());
324}
325
326/*
327
328IntSize constrainedBetween(const IntSize& min, const IntSize& max) const;
329
330*/
331
332}
333