aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/graphics/paths_test.html
blob: e6e062b5b2d87a8676f250a86de52d0ea22ffed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.

Author: nicksantos@google.com (Nick Santos)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JsUnit tests for goog.graphics.paths</title>
<script src="../base.js"></script>
<script>

goog.require('goog.dom');
goog.require('goog.graphics');
goog.require('goog.graphics.paths');
goog.require('goog.testing.jsunit');


</script>
</head>
<body>

<div id="root"></div>

<script type='text/javascript'>

// The purpose of this test is less about the actual unit test, and
// more for drawing demos of shapes.
var regularNGon = goog.graphics.paths.createRegularNGon;
var arrow = goog.graphics.paths.createArrow;

function setUp() {
  goog.dom.removeChildren(goog.dom.getElement('root'));
}

function testSquare() {
  var square = regularNGon(
      $coord(10, 10), $coord(0, 10), 4);
  assertArrayRoughlyEquals(
      [0, 10, 10, 0, 20, 10, 10, 20], square.arguments_, 0.05);
}

function assertArrayRoughlyEquals(expected, actual, delta) {
  var message = 'Expected: ' + expected + ', Actual: ' + actual;
  assertEquals('Wrong length. ' + message, expected.length, actual.length);
  for (var i = 0; i < expected.length; i++) {
    assertRoughlyEquals(
        'Wrong item at ' + i + '. ' + message,
        expected[i], actual[i], delta);
  }
}

function tearDownPage() {
  var root = goog.dom.getElement('root');
  var graphics = goog.graphics.createGraphics(800, 600);

  var blueFill = new goog.graphics.SolidFill('blue');
  var blackStroke = new goog.graphics.Stroke(1, 'black');
  graphics.drawPath(
      regularNGon($coord(20, 50), $coord(0, 20), 3),
      blackStroke, blueFill);
  graphics.drawPath(
      regularNGon($coord(120, 50), $coord(100, 20), 4),
      blackStroke, blueFill);
  graphics.drawPath(
      regularNGon($coord(220, 50), $coord(200, 20), 5),
      blackStroke, blueFill);
  graphics.drawPath(
      regularNGon($coord(320, 50), $coord(300, 20), 6),
      blackStroke, blueFill);

  graphics.drawPath(
      arrow($coord(0, 300), $coord(100, 400), 0, 0),
      blackStroke, blueFill);
  graphics.drawPath(
      arrow($coord(120, 400), $coord(200, 300), 0, 10),
      blackStroke, blueFill);
  graphics.drawPath(
      arrow($coord(220, 300), $coord(300, 400), 10, 0),
      blackStroke, blueFill);
  graphics.drawPath(
      arrow($coord(320, 400), $coord(400, 300), 10, 10),
      blackStroke, blueFill);

  root.appendChild(graphics.getElement());
}

function $coord(x, y) {
  return new goog.math.Coordinate(x, y);
}

</script>
</body>
</html>