| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// Use of this source code is governed by the Apache License, Version 2.0. |
| 3 |
|
| 4 |
goog.provide('goog.graphics.PathTest'); |
| 5 |
goog.setTestOnly('goog.graphics.PathTest'); |
| 6 |
|
| 7 |
goog.require('goog.array'); |
| 8 |
goog.require('goog.math'); |
| 9 |
goog.require('goog.graphics.Path'); |
| 10 |
goog.require('goog.graphics.AffineTransform'); |
| 11 |
goog.require('goog.testing.graphics'); |
| 12 |
goog.require('goog.testing.jsunit'); |
| 13 |
|
| 14 |
function testConstructor() { |
| 15 |
var path = new goog.graphics.Path(); |
| 16 |
assertTrue(path.isSimple()); |
| 17 |
assertNull(path.getCurrentPoint()); |
| 18 |
goog.testing.graphics.assertPathEquals([], path); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
function testGetSegmentCount() { |
| 23 |
assertArrayEquals([2, 2, 6, 6, 0], goog.array.map([ |
| 24 |
goog.graphics.Path.Segment.MOVETO, |
| 25 |
goog.graphics.Path.Segment.LINETO, |
| 26 |
goog.graphics.Path.Segment.CURVETO, |
| 27 |
goog.graphics.Path.Segment.ARCTO, |
| 28 |
goog.graphics.Path.Segment.CLOSE |
| 29 |
], goog.graphics.Path.getSegmentCount)); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
function testSimpleMoveTo() { |
| 34 |
var path = new goog.graphics.Path(); |
| 35 |
path.moveTo(30, 50); |
| 36 |
assertTrue(path.isSimple()); |
| 37 |
assertObjectEquals([30, 50], path.getCurrentPoint()); |
| 38 |
goog.testing.graphics.assertPathEquals(['M', 30, 50], path); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
function testRepeatedMoveTo() { |
| 43 |
var path = new goog.graphics.Path(); |
| 44 |
path.moveTo(30, 50); |
| 45 |
path.moveTo(40, 60); |
| 46 |
assertTrue(path.isSimple()); |
| 47 |
assertObjectEquals([40, 60], path.getCurrentPoint()); |
| 48 |
goog.testing.graphics.assertPathEquals(['M', 40, 60], path); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function testSimpleLineTo() { |
| 53 |
var path = new goog.graphics.Path(); |
| 54 |
var e = assertThrows(function() { |
| 55 |
path.lineTo(30, 50); |
| 56 |
}); |
| 57 |
assertEquals('Path cannot start with lineTo', e.message); |
| 58 |
path.moveTo(0, 0); |
| 59 |
path.lineTo(30, 50); |
| 60 |
assertTrue(path.isSimple()); |
| 61 |
assertObjectEquals([30, 50], path.getCurrentPoint()); |
| 62 |
goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50], path); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
function testMultiArgLineTo() { |
| 67 |
var path = new goog.graphics.Path(); |
| 68 |
path.moveTo(0, 0); |
| 69 |
path.lineTo(30, 50, 40 , 60); |
| 70 |
assertTrue(path.isSimple()); |
| 71 |
assertObjectEquals([40, 60], path.getCurrentPoint()); |
| 72 |
goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], |
| 73 |
path); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
function testRepeatedLineTo() { |
| 78 |
var path = new goog.graphics.Path(); |
| 79 |
path.moveTo(0, 0); |
| 80 |
path.lineTo(30, 50); |
| 81 |
path.lineTo(40, 60); |
| 82 |
assertTrue(path.isSimple()); |
| 83 |
assertObjectEquals([40, 60], path.getCurrentPoint()); |
| 84 |
goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60], |
| 85 |
path); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
function testSimpleCurveTo() { |
| 90 |
var path = new goog.graphics.Path(); |
| 91 |
var e = assertThrows(function() { |
| 92 |
path.curveTo(10, 20, 30, 40, 50, 60); |
| 93 |
}); |
| 94 |
assertEquals('Path cannot start with curve', e.message); |
| 95 |
path.moveTo(0, 0); |
| 96 |
path.curveTo(10, 20, 30, 40, 50, 60); |
| 97 |
assertTrue(path.isSimple()); |
| 98 |
assertObjectEquals([50, 60], path.getCurrentPoint()); |
| 99 |
goog.testing.graphics.assertPathEquals( |
| 100 |
['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60], path); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testMultiCurveTo() { |
| 105 |
var path = new goog.graphics.Path(); |
| 106 |
path.moveTo(0, 0); |
| 107 |
path.curveTo(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120); |
| 108 |
assertTrue(path.isSimple()); |
| 109 |
assertObjectEquals([110, 120], path.getCurrentPoint()); |
| 110 |
goog.testing.graphics.assertPathEquals( |
| 111 |
['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], |
| 112 |
path); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function testRepeatedCurveTo() { |
| 117 |
var path = new goog.graphics.Path(); |
| 118 |
path.moveTo(0, 0); |
| 119 |
path.curveTo(10, 20, 30, 40, 50, 60); |
| 120 |
path.curveTo(70, 80, 90, 100, 110, 120); |
| 121 |
assertTrue(path.isSimple()); |
| 122 |
assertObjectEquals([110, 120], path.getCurrentPoint()); |
| 123 |
goog.testing.graphics.assertPathEquals( |
| 124 |
['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], |
| 125 |
path); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function testSimpleArc() { |
| 130 |
var path = new goog.graphics.Path(); |
| 131 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 132 |
assertFalse(path.isSimple()); |
| 133 |
var p = path.getCurrentPoint(); |
| 134 |
assertEquals(55, p[0]); |
| 135 |
assertRoughlyEquals(77.32, p[1], 0.01); |
| 136 |
goog.testing.graphics.assertPathEquals( |
| 137 |
['M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32], path); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
function testArcNonConnectClose() { |
| 142 |
var path = new goog.graphics.Path(); |
| 143 |
path.moveTo(0, 0); |
| 144 |
path.arc(10, 10, 10, 10, -90, 180); |
| 145 |
assertObjectEquals([10, 20], path.getCurrentPoint()); |
| 146 |
path.close(); |
| 147 |
assertObjectEquals([10, 0], path.getCurrentPoint()); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
function testRepeatedArc() { |
| 152 |
var path = new goog.graphics.Path(); |
| 153 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 154 |
path.arc(50, 60, 10, 20, 60, 30, false); |
| 155 |
assertFalse(path.isSimple()); |
| 156 |
assertObjectEquals([50, 80], path.getCurrentPoint()); |
| 157 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 158 |
'A', 10, 20, 30, 30, 55, 77.32, |
| 159 |
'M', 55, 77.32, |
| 160 |
'A', 10, 20, 60, 30, 50, 80], path); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
function testRepeatedArc2() { |
| 165 |
var path = new goog.graphics.Path(); |
| 166 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 167 |
path.arc(50, 60, 10, 20, 60, 30, true); |
| 168 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 169 |
'A', 10, 20, 30, 30, 55, 77.32, |
| 170 |
'A', 10, 20, 60, 30, 50, 80], path); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
function testCompleteCircle() { |
| 175 |
var path = new goog.graphics.Path(); |
| 176 |
path.arc(0, 0, 10, 10, 0, 360, false); |
| 177 |
assertFalse(path.isSimple()); |
| 178 |
var p = path.getCurrentPoint(); |
| 179 |
assertRoughlyEquals(10, p[0], 0.01); |
| 180 |
assertRoughlyEquals(0, p[1], 0.01); |
| 181 |
goog.testing.graphics.assertPathEquals( |
| 182 |
['M', 10, 0, 'A', 10, 10, 0, 360, 10, 0], path); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
function testClose() { |
| 187 |
var path = new goog.graphics.Path(); |
| 188 |
try { |
| 189 |
path.close(); |
| 190 |
fail(); |
| 191 |
} catch (e) { |
| 192 |
// Expected |
| 193 |
assertEquals('Path cannot start with close', e.message); |
| 194 |
} |
| 195 |
path.moveTo(0, 0); |
| 196 |
path.lineTo(10, 20, 30, 40, 50, 60); |
| 197 |
path.close() |
| 198 |
assertTrue(path.isSimple()); |
| 199 |
assertObjectEquals([0, 0], path.getCurrentPoint()); |
| 200 |
goog.testing.graphics.assertPathEquals( |
| 201 |
['M', 0, 0, 'L', 10, 20, 30, 40, 50, 60, 'X'], path); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
function testClear() { |
| 206 |
var path = new goog.graphics.Path(); |
| 207 |
path.moveTo(0, 0); |
| 208 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 209 |
path.clear(); |
| 210 |
assertTrue(path.isSimple()); |
| 211 |
assertNull(path.getCurrentPoint()); |
| 212 |
goog.testing.graphics.assertPathEquals([], path); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
function testCreateSimplifiedPath() { |
| 217 |
var path = new goog.graphics.Path(); |
| 218 |
path.moveTo(0, 0); |
| 219 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 220 |
assertFalse(path.isSimple()); |
| 221 |
path = goog.graphics.Path.createSimplifiedPath(path); |
| 222 |
assertTrue(path.isSimple()); |
| 223 |
var p = path.getCurrentPoint(); |
| 224 |
assertEquals(55, p[0]); |
| 225 |
assertRoughlyEquals(77.32, p[1], 0.01); |
| 226 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 227 |
'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
function testCreateSimplifiedPath2() { |
| 232 |
var path = new goog.graphics.Path(); |
| 233 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 234 |
path.arc(50, 60, 10, 20, 60, 30, false); |
| 235 |
assertFalse(path.isSimple()); |
| 236 |
path = goog.graphics.Path.createSimplifiedPath(path); |
| 237 |
assertTrue(path.isSimple()); |
| 238 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 239 |
'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32, |
| 240 |
'M', 55, 77.32, |
| 241 |
'C', 53.48, 79.08, 51.76, 80, 50, 80], path); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
function testCreateSimplifiedPath3() { |
| 246 |
var path = new goog.graphics.Path(); |
| 247 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 248 |
path.arc(50, 60, 10, 20, 60, 30, true); |
| 249 |
path.close(); |
| 250 |
path = goog.graphics.Path.createSimplifiedPath(path); |
| 251 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 252 |
'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32, |
| 253 |
53.48, 79.08, 51.76, 80, 50, 80, 'X'], path); |
| 254 |
var p = path.getCurrentPoint(); |
| 255 |
assertRoughlyEquals(58.66, p[0], 0.01); |
| 256 |
assertRoughlyEquals(70, p[1], 0.01); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
function testArcToAsCurves() { |
| 261 |
var path = new goog.graphics.Path(); |
| 262 |
path.moveTo(58.66, 70); |
| 263 |
path.arcToAsCurves(10, 20, 30, 30, false); |
| 264 |
goog.testing.graphics.assertPathEquals(['M', 58.66, 70, |
| 265 |
'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
function testCreateTransformedPath() { |
| 270 |
var path = new goog.graphics.Path(); |
| 271 |
path.moveTo(0, 0); |
| 272 |
path.lineTo(0, 10, 10, 10, 10, 0); |
| 273 |
path.close(); |
| 274 |
var tx = new goog.graphics.AffineTransform(2, 0, 0, 3, 10, 20); |
| 275 |
var path2 = path.createTransformedPath(tx); |
| 276 |
goog.testing.graphics.assertPathEquals( |
| 277 |
['M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X'], path); |
| 278 |
goog.testing.graphics.assertPathEquals( |
| 279 |
['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
function testTransform() { |
| 284 |
var path = new goog.graphics.Path(); |
| 285 |
path.moveTo(0, 0); |
| 286 |
path.lineTo(0, 10, 10, 10, 10, 0); |
| 287 |
path.close(); |
| 288 |
var tx = new goog.graphics.AffineTransform(2, 0, 0, 3, 10, 20); |
| 289 |
var path2 = path.transform(tx); |
| 290 |
assertTrue(path === path2); |
| 291 |
goog.testing.graphics.assertPathEquals( |
| 292 |
['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
function testTransformCurrentAndClosePoints() { |
| 297 |
var path = new goog.graphics.Path(); |
| 298 |
path.moveTo(0, 0); |
| 299 |
assertObjectEquals([0, 0], path.getCurrentPoint()); |
| 300 |
path.transform(new goog.graphics.AffineTransform(1, 0, 0, 1, 10, 20)); |
| 301 |
assertObjectEquals([10, 20], path.getCurrentPoint()); |
| 302 |
path.lineTo(50, 50); |
| 303 |
path.close(); |
| 304 |
assertObjectEquals([10, 20], path.getCurrentPoint()); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
function testTransformNonSimple() { |
| 309 |
var path = new goog.graphics.Path(); |
| 310 |
path.arc(50, 60, 10, 20, 30, 30, false); |
| 311 |
assertThrows(function() { |
| 312 |
path.transform(new goog.graphics.AffineTransform(1, 0, 0, 1, 10, 20)); |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
function testAppendPath() { |
| 318 |
var path1 = new goog.graphics.Path(); |
| 319 |
path1.moveTo(0, 0); |
| 320 |
path1.lineTo(0, 10, 10, 10, 10, 0); |
| 321 |
path1.close(); |
| 322 |
|
| 323 |
var path2 = new goog.graphics.Path(); |
| 324 |
path2.arc(50, 60, 10, 20, 30, 30, false); |
| 325 |
|
| 326 |
assertTrue(path1.isSimple()); |
| 327 |
path1.appendPath(path2); |
| 328 |
assertFalse(path1.isSimple()); |
| 329 |
goog.testing.graphics.assertPathEquals([ |
| 330 |
'M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X', |
| 331 |
'M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32 |
| 332 |
], path1); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
function testIsEmpty() { |
| 337 |
var path = new goog.graphics.Path(); |
| 338 |
assertTrue('Initially path is empty', path.isEmpty()); |
| 339 |
|
| 340 |
path.moveTo(0, 0); |
| 341 |
assertFalse('After command addition, path is not empty', path.isEmpty()); |
| 342 |
|
| 343 |
path.clear(); |
| 344 |
assertTrue('After clear, path is empty again', path.isEmpty()); |
| 345 |
} |