| 1 |
// Copyright 2010 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.pathsTest'); |
| 5 |
goog.setTestOnly('goog.graphics.pathsTest'); |
| 6 |
|
| 7 |
goog.require('goog.dom'); |
| 8 |
goog.require('goog.graphics'); |
| 9 |
goog.require('goog.graphics.paths'); |
| 10 |
goog.require('goog.testing.jsunit'); |
| 11 |
|
| 12 |
// The purpose of this test is less about the actual unit test, and |
| 13 |
// more for drawing demos of shapes. |
| 14 |
var regularNGon = goog.graphics.paths.createRegularNGon; |
| 15 |
var arrow = goog.graphics.paths.createArrow; |
| 16 |
|
| 17 |
function setUp() { |
| 18 |
goog.dom.removeChildren(goog.dom.getElement('root')); |
| 19 |
} |
| 20 |
|
| 21 |
function testSquare() { |
| 22 |
var square = regularNGon( |
| 23 |
$coord(10, 10), $coord(0, 10), 4); |
| 24 |
assertArrayRoughlyEquals( |
| 25 |
[0, 10, 10, 0, 20, 10, 10, 20], square.arguments_, 0.05); |
| 26 |
} |
| 27 |
|
| 28 |
function assertArrayRoughlyEquals(expected, actual, delta) { |
| 29 |
var message = 'Expected: ' + expected + ', Actual: ' + actual; |
| 30 |
assertEquals('Wrong length. ' + message, expected.length, actual.length); |
| 31 |
for (var i = 0; i < expected.length; i++) { |
| 32 |
assertRoughlyEquals( |
| 33 |
'Wrong item at ' + i + '. ' + message, |
| 34 |
expected[i], actual[i], delta); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
function tearDownPage() { |
| 39 |
var root = goog.dom.getElement('root'); |
| 40 |
var graphics = goog.graphics.createGraphics(800, 600); |
| 41 |
|
| 42 |
var blueFill = new goog.graphics.SolidFill('blue'); |
| 43 |
var blackStroke = new goog.graphics.Stroke(1, 'black'); |
| 44 |
graphics.drawPath( |
| 45 |
regularNGon($coord(20, 50), $coord(0, 20), 3), |
| 46 |
blackStroke, blueFill); |
| 47 |
graphics.drawPath( |
| 48 |
regularNGon($coord(120, 50), $coord(100, 20), 4), |
| 49 |
blackStroke, blueFill); |
| 50 |
graphics.drawPath( |
| 51 |
regularNGon($coord(220, 50), $coord(200, 20), 5), |
| 52 |
blackStroke, blueFill); |
| 53 |
graphics.drawPath( |
| 54 |
regularNGon($coord(320, 50), $coord(300, 20), 6), |
| 55 |
blackStroke, blueFill); |
| 56 |
|
| 57 |
graphics.drawPath( |
| 58 |
arrow($coord(0, 300), $coord(100, 400), 0, 0), |
| 59 |
blackStroke, blueFill); |
| 60 |
graphics.drawPath( |
| 61 |
arrow($coord(120, 400), $coord(200, 300), 0, 10), |
| 62 |
blackStroke, blueFill); |
| 63 |
graphics.drawPath( |
| 64 |
arrow($coord(220, 300), $coord(300, 400), 10, 0), |
| 65 |
blackStroke, blueFill); |
| 66 |
graphics.drawPath( |
| 67 |
arrow($coord(320, 400), $coord(400, 300), 10, 10), |
| 68 |
blackStroke, blueFill); |
| 69 |
|
| 70 |
root.appendChild(graphics.getElement()); |
| 71 |
} |
| 72 |
|
| 73 |
function $coord(x, y) { |
| 74 |
return new goog.math.Coordinate(x, y); |
| 75 |
} |