1/*
2 * Copyright (C) 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/FloatPoint.h>
29#include <WebCore/IntPoint.h>
30#include <WebCore/IntSize.h>
31
32#if USE(CG)
33#include <CoreGraphics/CoreGraphics.h>
34#endif
35
36#if PLATFORM(WIN)
37#include <d2d1.h>
38#endif
39
40namespace TestWebKitAPI {
41
42static void testGetAndSet(WebCore::IntPoint point)
43{
44 point.setX(1);
45 EXPECT_EQ(1, point.x());
46 point.setY(2);
47 EXPECT_EQ(2, point.y());
48}
49
50TEST(IntPoint, DefaultConstruction)
51{
52 WebCore::IntPoint test;
53
54 EXPECT_EQ(0, test.x());
55 EXPECT_EQ(0, test.y());
56 ASSERT_TRUE(test.isZero());
57
58 testGetAndSet(test);
59}
60
61TEST(IntPoint, ValueConstruction)
62{
63 WebCore::IntPoint test(10, 9);
64
65 EXPECT_EQ(10, test.x());
66 EXPECT_EQ(9, test.y());
67
68 testGetAndSet(test);
69}
70
71TEST(IntPoint, ZeroConstruction)
72{
73 WebCore::IntPoint test = WebCore::IntPoint::zero();
74
75 EXPECT_EQ(0, test.x());
76 EXPECT_EQ(0, test.y());
77 ASSERT_TRUE(test.isZero());
78}
79
80TEST(IntPoint, IntSizeConstruction)
81{
82 WebCore::IntSize testInput(2003, 1997);
83 WebCore::IntPoint test(testInput);
84
85 EXPECT_EQ(2003, test.x());
86 EXPECT_EQ(1997, test.y());
87 ASSERT_FALSE(test.isZero());
88}
89
90TEST(IntPoint, FloatPointConstruction)
91{
92 WebCore::FloatPoint testInput(2003.2f, 1997.3f);
93 WebCore::IntPoint test(testInput);
94
95 EXPECT_EQ(2003, test.x());
96 EXPECT_EQ(1997, test.y());
97 ASSERT_FALSE(test.isZero());
98}
99
100TEST(IntPoint, Move)
101{
102 WebCore::IntPoint test(10, 20);
103 WebCore::IntSize size(30, 50);
104
105 test.move(size);
106
107 EXPECT_EQ(40, test.x());
108 EXPECT_EQ(70, test.y());
109
110 test.move(-2, 8);
111
112 EXPECT_EQ(38, test.x());
113 EXPECT_EQ(78, test.y());
114
115 WebCore::IntPoint offset(100, 100);
116
117 test.moveBy(offset);
118
119 EXPECT_EQ(138, test.x());
120 EXPECT_EQ(178, test.y());
121}
122
123TEST(IntPoint, Scale)
124{
125 WebCore::IntPoint test(10, 20);
126
127 test.scale(2.0);
128
129 EXPECT_EQ(20, test.x());
130 EXPECT_EQ(40, test.y());
131
132 test.scale(3.0, 1.5);
133
134 EXPECT_EQ(60, test.x());
135 EXPECT_EQ(60, test.y());
136}
137
138TEST(IntPoint, Expand)
139{
140 WebCore::IntPoint a(10, 20);
141 WebCore::IntPoint b(20, 40);
142
143 auto c = a.expandedTo(b);
144
145 EXPECT_EQ(20, c.x());
146 EXPECT_EQ(40, c.y());
147}
148
149TEST(IntPoint, Shrink)
150{
151 WebCore::IntPoint a(10, 20);
152 WebCore::IntPoint b(20, 40);
153
154 auto c = b.shrunkTo(a);
155
156 EXPECT_EQ(10, c.x());
157 EXPECT_EQ(20, c.y());
158}
159
160TEST(IntPoint, Transpose)
161{
162 WebCore::IntPoint a(10, 20);
163
164 auto b = a.transposedPoint();
165
166 EXPECT_EQ(20, b.x());
167 EXPECT_EQ(10, b.y());
168}
169
170TEST(IntPoint, Cast)
171{
172 WebCore::IntPoint a(10, 20);
173
174 WebCore::IntSize as = WebCore::toIntSize(a);
175 EXPECT_EQ(10, as.width());
176 EXPECT_EQ(20, as.height());
177
178#if USE(CG)
179 CGPoint cgPoint = a;
180
181 ASSERT_FLOAT_EQ(10.0f, cgPoint.x);
182 ASSERT_FLOAT_EQ(20.0f, cgPoint.y);
183
184 WebCore::IntPoint fromCGPoint(cgPoint);
185 EXPECT_EQ(10, fromCGPoint.x());
186 EXPECT_EQ(20, fromCGPoint.y());
187 ASSERT_TRUE(a == fromCGPoint);
188#endif
189
190#if PLATFORM(WIN)
191 POINT gdiPoint = a;
192
193 ASSERT_FLOAT_EQ(10.0f, gdiPoint.x);
194 ASSERT_FLOAT_EQ(20.0f, gdiPoint.y);
195
196 WebCore::IntPoint fromGDIPoint(gdiPoint);
197 EXPECT_EQ(10, fromGDIPoint.x());
198 EXPECT_EQ(20, fromGDIPoint.y());
199 ASSERT_TRUE(a == fromGDIPoint);
200
201 D2D1_POINT_2F d2dPointF = a;
202
203 ASSERT_FLOAT_EQ(10.0f, d2dPointF.x);
204 ASSERT_FLOAT_EQ(20.0f, d2dPointF.y);
205
206 WebCore::IntPoint fromD2DPointF(d2dPointF);
207 EXPECT_EQ(10, fromD2DPointF.x());
208 EXPECT_EQ(20, fromD2DPointF.y());
209 ASSERT_TRUE(a == fromD2DPointF);
210
211 D2D1_POINT_2U d2dPointU = a;
212
213 ASSERT_FLOAT_EQ(10.0f, d2dPointU.x);
214 ASSERT_FLOAT_EQ(20.0f, d2dPointU.y);
215
216 WebCore::IntPoint fromD2DPointU(d2dPointU);
217 EXPECT_EQ(10, fromD2DPointU.x());
218 EXPECT_EQ(20, fromD2DPointU.y());
219 ASSERT_TRUE(a == fromD2DPointU);
220#endif
221}
222
223TEST(IntPoint, Addition)
224{
225 WebCore::IntPoint a(10, 20);
226 WebCore::IntPoint b(50, 60);
227 WebCore::IntSize bs(50, 60);
228
229 auto c = a + b;
230
231 EXPECT_EQ(60, c.x());
232 EXPECT_EQ(80, c.y());
233
234 a += bs;
235
236 EXPECT_EQ(60, a.x());
237 EXPECT_EQ(80, a.y());
238}
239
240TEST(IntPoint, Subtraction)
241{
242 WebCore::IntPoint a(100, 80);
243 WebCore::IntPoint b(50, 60);
244 WebCore::IntSize bs(50, 60);
245
246 WebCore::IntSize c = a - b;
247
248 EXPECT_EQ(50, c.width());
249 EXPECT_EQ(20, c.height());
250
251 WebCore::IntPoint d = a - bs;
252
253 EXPECT_EQ(50, d.x());
254 EXPECT_EQ(20, d.y());
255
256 a -= bs;
257
258 EXPECT_EQ(50, a.x());
259 EXPECT_EQ(20, a.y());
260}
261
262TEST(IntPoint, Negation)
263{
264 WebCore::IntPoint a(100, 80);
265 auto b = -a;
266
267 EXPECT_EQ(-100, b.x());
268 EXPECT_EQ(-80, b.y());
269}
270
271TEST(IntPoint, Equality)
272{
273 WebCore::IntPoint a(100, 80);
274 WebCore::IntPoint b(70, 50);
275 WebCore::IntPoint c(100, 80);
276
277 ASSERT_TRUE(a == c);
278 ASSERT_FALSE(a == b);
279 ASSERT_FALSE(a != c);
280 ASSERT_TRUE(a != b);
281}
282
283}
284