paths_test.js 2.4 KB

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