Debian Codegraph @

closure-library/closure/goog/math/path_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.PathTest');
16
17 goog.require('goog.array');
18 goog.require('goog.math.AffineTransform');
19 goog.require('goog.math.Path');
20 goog.require('goog.testing.jsunit');
21
22 goog.setTestOnly('goog.math.PathTest');
23
24
25 /**
26 * Array mapping numeric segment constant to a descriptive character.
27 * @type {!Array<string>}
28 * @private
29 */
30 var SEGMENT_NAMES_ = function() {
31 var arr = [];
32 arr[goog.math.Path.Segment.MOVETO] = 'M';
33 arr[goog.math.Path.Segment.LINETO] = 'L';
34 arr[goog.math.Path.Segment.CURVETO] = 'C';
35 arr[goog.math.Path.Segment.ARCTO] = 'A';
36 arr[goog.math.Path.Segment.CLOSE] = 'X';
37 return arr;
38 }();
39
40
41 /**
42 * Test if the given path matches the expected array of commands and parameters.
43 * @param {Array<string|number>} expected The expected array of commands and
44 * parameters.
45 * @param {goog.math.Path} path The path to test against.
46 */
47 var assertPathEquals = function(expected, path) {
48 var actual = [];
49 path.forEachSegment(function(seg, args) {
50 actual.push(SEGMENT_NAMES_[seg]);
51 Array.prototype.push.apply(actual, args);
52 });
53 assertEquals(expected.length, actual.length);
54 for (var i = 0; i < expected.length; i++) {
55 if (goog.isNumber(expected[i])) {
56 assertTrue(goog.isNumber(actual[i]));
57 assertRoughlyEquals(expected[i], actual[i], 0.01);
58 } else {
59 assertEquals(expected[i], actual[i]);
60 }
61 }
62 };
63
64
65 function testConstructor() {
66 var path = new goog.math.Path();
67 assertTrue(path.isSimple());
68 assertNull(path.getCurrentPoint());
69 assertPathEquals([], path);
70 }
71
72
73 function testGetSegmentCount() {
74 assertArrayEquals(
75 [2, 2, 6, 6, 0],
76 goog.array.map(
77 [
78 goog.math.Path.Segment.MOVETO, goog.math.Path.Segment.LINETO,
79 goog.math.Path.Segment.CURVETO, goog.math.Path.Segment.ARCTO,
80 goog.math.Path.Segment.CLOSE
81 ],
82 goog.math.Path.getSegmentCount));
83 }
84
85
86 function testSimpleMoveTo() {
87 var path = new goog.math.Path();
88 path.moveTo(30, 50);
89 assertTrue(path.isSimple());
90 assertObjectEquals([30, 50], path.getCurrentPoint());
91 assertPathEquals(['M', 30, 50], path);
92 }
93
94
95 function testRepeatedMoveTo() {
96 var path = new goog.math.Path();
97 path.moveTo(30, 50);
98 path.moveTo(40, 60);
99 assertTrue(path.isSimple());
100 assertObjectEquals([40, 60], path.getCurrentPoint());
101 assertPathEquals(['M', 40, 60], path);
102 }
103
104
105 function testSimpleLineTo_fromArgs() {
106 var path = new goog.math.Path();
107 var e = assertThrows(function() { path.lineTo(30, 50); });
108 assertEquals('Path cannot start with lineTo', e.message);
109 path.moveTo(0, 0);
110 path.lineTo(30, 50);
111 assertTrue(path.isSimple());
112 assertObjectEquals([30, 50], path.getCurrentPoint());
113 assertPathEquals(['M', 0, 0, 'L', 30, 50], path);
114 }
115
116
117 function testSimpleLineTo_fromArray() {
118 var path = new goog.math.Path();
119 var e = assertThrows(function() { path.lineToFromArray([30, 50]); });
120 assertEquals('Path cannot start with lineTo', e.message);
121 path.moveTo(0, 0);
122 path.lineToFromArray([30, 50]);
123 assertTrue(path.isSimple());
124 assertObjectEquals([30, 50], path.getCurrentPoint());
125 assertPathEquals(['M', 0, 0, 'L', 30, 50], path);
126 }
127
128
129 function testMultiArgLineTo_fromArgs() {
130 var path = new goog.math.Path();
131 path.moveTo(0, 0);
132 path.lineTo(30, 50, 40, 60);
133 assertTrue(path.isSimple());
134 assertObjectEquals([40, 60], path.getCurrentPoint());
135 assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], path);
136 }
137
138
139 function testMultiArgLineTo_fromArray() {
140 var path = new goog.math.Path();
141 path.moveTo(0, 0);
142 path.lineToFromArray([30, 50, 40, 60]);
143 assertTrue(path.isSimple());
144 assertObjectEquals([40, 60], path.getCurrentPoint());
145 assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], path);
146 }
147
148
149 function testRepeatedLineTo_fromArgs() {
150 var path = new goog.math.Path();
151 path.moveTo(0, 0);
152 path.lineTo(30, 50);
153 path.lineTo(40, 60);
154 assertTrue(path.isSimple());
155 assertObjectEquals([40, 60], path.getCurrentPoint());
156 assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], path);
157 }
158
159
160 function testRepeatedLineTo_fromArray() {
161 var path = new goog.math.Path();
162 path.moveTo(0, 0);
163 path.lineToFromArray([30, 50]);
164 path.lineToFromArray([40, 60]);
165 assertTrue(path.isSimple());
166 assertObjectEquals([40, 60], path.getCurrentPoint());
167 assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], path);
168 }
169
170
171 function testSimpleCurveTo_fromArgs() {
172 var path = new goog.math.Path();
173 var e = assertThrows(function() { path.curveTo(10, 20, 30, 40, 50, 60); });
174 assertEquals('Path cannot start with curve', e.message);
175 path.moveTo(0, 0);
176 path.curveTo(10, 20, 30, 40, 50, 60);
177 assertTrue(path.isSimple());
178 assertObjectEquals([50, 60], path.getCurrentPoint());
179 assertPathEquals(['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60], path);
180 }
181
182
183 function testSimpleCurveTo_fromArray() {
184 var path = new goog.math.Path();
185 var e = assertThrows(function() {
186 path.curveToFromArray([10, 20, 30, 40, 50, 60]);
187 });
188 assertEquals('Path cannot start with curve', e.message);
189 path.moveTo(0, 0);
190 path.curveToFromArray([10, 20, 30, 40, 50, 60]);
191 assertTrue(path.isSimple());
192 assertObjectEquals([50, 60], path.getCurrentPoint());
193 assertPathEquals(['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60], path);
194 }
195
196
197 function testMultiCurveTo_fromArgs() {
198 var path = new goog.math.Path();
199 path.moveTo(0, 0);
200 path.curveTo(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120);
201 assertTrue(path.isSimple());
202 assertObjectEquals([110, 120], path.getCurrentPoint());
203 assertPathEquals(
204 ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
205 path);
206 }
207
208
209 function testMultiCurveTo_fromArray() {
210 var path = new goog.math.Path();
211 path.moveTo(0, 0);
212 path.curveToFromArray([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]);
213 assertTrue(path.isSimple());
214 assertObjectEquals([110, 120], path.getCurrentPoint());
215 assertPathEquals(
216 ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
217 path);
218 }
219
220
221 function testRepeatedCurveTo_fromArgs() {
222 var path = new goog.math.Path();
223 path.moveTo(0, 0);
224 path.curveTo(10, 20, 30, 40, 50, 60);
225 path.curveTo(70, 80, 90, 100, 110, 120);
226 assertTrue(path.isSimple());
227 assertObjectEquals([110, 120], path.getCurrentPoint());
228 assertPathEquals(
229 ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
230 path);
231 }
232
233
234 function testRepeatedCurveTo_fromArray() {
235 var path = new goog.math.Path();
236 path.moveTo(0, 0);
237 path.curveToFromArray([10, 20, 30, 40, 50, 60]);
238 path.curveToFromArray([70, 80, 90, 100, 110, 120]);
239 assertTrue(path.isSimple());
240 assertObjectEquals([110, 120], path.getCurrentPoint());
241 assertPathEquals(
242 ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
243 path);
244 }
245
246
247 function testSimpleArc() {
248 var path = new goog.math.Path();
249 path.arc(50, 60, 10, 20, 30, 30, false);
250 assertFalse(path.isSimple());
251 var p = path.getCurrentPoint();
252 assertEquals(55, p[0]);
253 assertRoughlyEquals(77.32, p[1], 0.01);
254 assertPathEquals(['M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32], path);
255 }
256
257
258 function testArcNonConnectClose() {
259 var path = new goog.math.Path();
260 path.moveTo(0, 0);
261 path.arc(10, 10, 10, 10, -90, 180, false);
262 assertObjectEquals([10, 20], path.getCurrentPoint());
263 path.close();
264 assertObjectEquals([10, 0], path.getCurrentPoint());
265 }
266
267
268 function testRepeatedArc() {
269 var path = new goog.math.Path();
270 path.arc(50, 60, 10, 20, 30, 30, false);
271 path.arc(50, 60, 10, 20, 60, 30, false);
272 assertFalse(path.isSimple());
273 assertObjectEquals([50, 80], path.getCurrentPoint());
274 assertPathEquals(
275 [
276 'M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32,
277 'M', 55, 77.32, 'A', 10, 20, 60, 30, 50, 80
278 ],
279 path);
280 }
281
282
283 function testRepeatedArc2() {
284 var path = new goog.math.Path();
285 path.arc(50, 60, 10, 20, 30, 30, false);
286 path.arc(50, 60, 10, 20, 60, 30, true);
287 assertPathEquals(
288 [
289 'M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32, 'A', 10, 20, 60, 30, 50,
290 80
291 ],
292 path);
293 }
294
295
296 function testCompleteCircle() {
297 var path = new goog.math.Path();
298 path.arc(0, 0, 10, 10, 0, 360, false);
299 assertFalse(path.isSimple());
300 var p = path.getCurrentPoint();
301 assertRoughlyEquals(10, p[0], 0.01);
302 assertRoughlyEquals(0, p[1], 0.01);
303 assertPathEquals(['M', 10, 0, 'A', 10, 10, 0, 360, 10, 0], path);
304 }
305
306
307 function testClose() {
308 var path = new goog.math.Path();
309 assertThrows('Path cannot start with close', function() { path.close(); });
310
311 path.moveTo(0, 0);
312 path.lineTo(10, 20, 30, 40, 50, 60);
313 path.close();
314 assertTrue(path.isSimple());
315 assertObjectEquals([0, 0], path.getCurrentPoint());
316 assertPathEquals(['M', 0, 0, 'L', 10, 20, 30, 40, 50, 60, 'X'], path);
317 }
318
319
320 function testClear() {
321 var path = new goog.math.Path();
322 path.moveTo(0, 0);
323 path.arc(50, 60, 10, 20, 30, 30, false);
324 path.clear();
325 assertTrue(path.isSimple());
326 assertNull(path.getCurrentPoint());
327 assertPathEquals([], path);
328 }
329
330
331 function testCreateSimplifiedPath() {
332 var path = new goog.math.Path();
333 path.moveTo(0, 0);
334 path.arc(50, 60, 10, 20, 30, 30, false);
335 assertFalse(path.isSimple());
336 path = goog.math.Path.createSimplifiedPath(path);
337 assertTrue(path.isSimple());
338 var p = path.getCurrentPoint();
339 assertEquals(55, p[0]);
340 assertRoughlyEquals(77.32, p[1], 0.01);
341 assertPathEquals(
342 ['M', 58.66, 70, 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path);
343 }
344
345
346 function testCreateSimplifiedPath2() {
347 var path = new goog.math.Path();
348 path.arc(50, 60, 10, 20, 30, 30, false);
349 path.arc(50, 60, 10, 20, 60, 30, false);
350 assertFalse(path.isSimple());
351 path = goog.math.Path.createSimplifiedPath(path);
352 assertTrue(path.isSimple());
353 assertPathEquals(
354 [
355 'M', 58.66, 70, 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32,
356 'M', 55, 77.32, 'C', 53.48, 79.08, 51.76, 80, 50, 80
357 ],
358 path);
359 }
360
361
362 function testCreateSimplifiedPath3() {
363 var path = new goog.math.Path();
364 path.arc(50, 60, 10, 20, 30, 30, false);
365 path.arc(50, 60, 10, 20, 60, 30, true);
366 path.close();
367 path = goog.math.Path.createSimplifiedPath(path);
368 assertPathEquals(
369 [
370 'M', 58.66, 70, 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32, 53.48,
371 79.08, 51.76, 80, 50, 80, 'X'
372 ],
373 path);
374 var p = path.getCurrentPoint();
375 assertRoughlyEquals(58.66, p[0], 0.01);
376 assertRoughlyEquals(70, p[1], 0.01);
377 }
378
379
380 function testArcToAsCurves() {
381 var path = new goog.math.Path();
382 path.moveTo(58.66, 70);
383 path.arcToAsCurves(10, 20, 30, 30);
384 assertPathEquals(
385 ['M', 58.66, 70, 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path);
386 }
387
388
389 function testCreateTransformedPath() {
390 var path = new goog.math.Path();
391 path.moveTo(0, 0);
392 path.lineTo(0, 10, 10, 10, 10, 0);
393 path.close();
394 var tx = new goog.math.AffineTransform(2, 0, 0, 3, 10, 20);
395 var path2 = path.createTransformedPath(tx);
396 assertPathEquals(['M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X'], path);
397 assertPathEquals(['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2);
398 }
399
400
401 function testTransform() {
402 var path = new goog.math.Path();
403 path.moveTo(0, 0);
404 path.lineTo(0, 10, 10, 10, 10, 0);
405 path.close();
406 var tx = new goog.math.AffineTransform(2, 0, 0, 3, 10, 20);
407 var path2 = path.transform(tx);
408 assertTrue(path === path2);
409 assertPathEquals(['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2);
410 }
411
412
413 function testTransformCurrentAndClosePoints() {
414 var path = new goog.math.Path();
415 path.moveTo(0, 0);
416 assertObjectEquals([0, 0], path.getCurrentPoint());
417 path.transform(new goog.math.AffineTransform(1, 0, 0, 1, 10, 20));
418 assertObjectEquals([10, 20], path.getCurrentPoint());
419 path.lineTo(50, 50);
420 path.close();
421 assertObjectEquals([10, 20], path.getCurrentPoint());
422 }
423
424
425 function testTransformNonSimple() {
426 var path = new goog.math.Path();
427 path.arc(50, 60, 10, 20, 30, 30, false);
428 assertThrows(function() {
429 path.transform(new goog.math.AffineTransform(1, 0, 0, 1, 10, 20));
430 });
431 }
432
433
434 function testAppendPath() {
435 var path1 = new goog.math.Path();
436 path1.moveTo(0, 0);
437 path1.lineTo(0, 10, 10, 10, 10, 0);
438 path1.close();
439
440 var path2 = new goog.math.Path();
441 path2.arc(50, 60, 10, 20, 30, 30, false);
442
443 assertTrue(path1.isSimple());
444 path1.appendPath(path2);
445 assertFalse(path1.isSimple());
446 assertPathEquals(
447 [
448 'M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X',
449 'M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32
450 ],
451 path1);
452 }
453
454
455 function testIsEmpty() {
456 var path = new goog.math.Path();
457 assertTrue('Initially path is empty', path.isEmpty());
458
459 path.moveTo(0, 0);
460 assertFalse('After command addition, path is not empty', path.isEmpty());
461
462 path.clear();
463 assertTrue('After clear, path is empty again', path.isEmpty());
464 }
465
466
467 function testGetSegmentTypes() {
468 var path = new goog.math.Path();
469 path.moveTo(0, 0);
470 path.lineTo(10, 20, 30, 40);
471 path.close();
472
473 var Segment = goog.math.Path.Segment;
474 var segmentTypes = path.getSegmentTypes();
475 assertArrayEquals(
476 'The returned segment types do not match the expected values',
477 [Segment.MOVETO, Segment.LINETO, Segment.CLOSE], segmentTypes);
478
479 segmentTypes[2] = Segment.LINETO;
480 assertArrayEquals(
481 'Modifying the returned segment types changed the path',
482 [Segment.MOVETO, Segment.LINETO, Segment.CLOSE], path.getSegmentTypes());
483 }
484
485
486 function testGetSegmentCounts() {
487 var path = new goog.math.Path();
488 path.moveTo(0, 0);
489 path.lineTo(10, 20, 30, 40);
490 path.close();
491
492 var segmentTypes = path.getSegmentCounts();
493 assertArrayEquals(
494 'The returned segment counts do not match the expected values', [1, 2, 1],
495 segmentTypes);
496
497 segmentTypes[1] = 3;
498 assertArrayEquals(
499 'Modifying the returned segment counts changed the path', [1, 2, 1],
500 path.getSegmentCounts());
501 }
502
503
504 function testGetSegmentArgs() {
505 var path = new goog.math.Path();
506 path.moveTo(0, 0);
507 path.lineTo(10, 20, 30, 40);
508 path.close();
509
510 var segmentTypes = path.getSegmentArgs();
511 assertArrayEquals(
512 'The returned segment args do not match the expected values',
513 [0, 0, 10, 20, 30, 40], segmentTypes);
514
515 segmentTypes[1] = -10;
516 assertArrayEquals(
517 'Modifying the returned segment args changed the path',
518 [0, 0, 10, 20, 30, 40], path.getSegmentArgs());
519 }