svggraphics_test.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2011 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.SvgGraphicsTest');
  4. goog.setTestOnly('goog.graphics.SvgGraphicsTest');
  5. goog.require('goog.dom');
  6. goog.require('goog.graphics.SvgGraphics');
  7. goog.require('goog.testing.graphics');
  8. goog.require('goog.testing.jsunit');
  9. var graphics;
  10. function setUp() {
  11. if (!document.createElementNS) {
  12. // Some browsers don't support document.createElementNS and this test
  13. // should not be run on those browsers (IE7,8).
  14. return;
  15. }
  16. graphics = new goog.graphics.SvgGraphics('100px', '100px');
  17. graphics.createDom();
  18. goog.dom.getElement('root').appendChild(graphics.getElement());
  19. }
  20. function testAddDef() {
  21. if (!graphics) {
  22. // setUp has failed (no browser support), we should not run this test.
  23. return;
  24. }
  25. var defElement1 = document.createElement('div');
  26. var defElement2 = document.createElement('div');
  27. var defKey1 = 'def1';
  28. var defKey2 = 'def2';
  29. var id = graphics.addDef(defKey1, defElement1);
  30. assertEquals('_svgdef_0', id);
  31. id = graphics.addDef(defKey1, defElement2);
  32. assertEquals('_svgdef_0', id);
  33. id = graphics.addDef(defKey2, defElement2);
  34. assertEquals('_svgdef_1', id);
  35. }
  36. function testGetDef() {
  37. if (!graphics) {
  38. // setUp has failed (no browser support), we should not run this test.
  39. return;
  40. }
  41. var defElement = document.createElement('div');
  42. var defKey = 'def';
  43. var id = graphics.addDef(defKey, defElement);
  44. assertEquals(id, graphics.getDef(defKey));
  45. assertNull(graphics.getDef('randomKey'));
  46. }
  47. function testRemoveDef() {
  48. if (!graphics) {
  49. // setUp has failed (no browser support), we should not run this test.
  50. return;
  51. }
  52. var defElement = document.createElement('div');
  53. var defKey = 'def';
  54. var addedId = graphics.addDef(defKey, defElement);
  55. graphics.removeDef('randomKey');
  56. assertEquals(addedId, graphics.getDef(defKey));
  57. graphics.removeDef(defKey);
  58. assertNull(graphics.getDef(defKey));
  59. }
  60. function testSetElementAffineTransform() {
  61. if (!graphics) {
  62. // setUp has failed (no browser support), we should not run this test.
  63. return;
  64. }
  65. var fill = new goog.graphics.SolidFill('blue');
  66. var stroke = null;
  67. var rad = -3.1415926 / 6;
  68. var costheta = Math.cos(rad);
  69. var sintheta = Math.sin(rad);
  70. var dx = 10;
  71. var dy = -20;
  72. var affine = new goog.graphics.AffineTransform(
  73. costheta, -sintheta + 1, sintheta, costheta, dx, dy);
  74. var rect = graphics.drawRect(10, 20, 30, 40, stroke, fill);
  75. rect.setTransform(affine);
  76. graphics.render();
  77. // getTransformToElement was deleted in Chrome48. See
  78. // https://code.google.com/p/chromium/issues/detail?id=524432.
  79. function getTransformToElement(element, target) {
  80. return target.getScreenCTM().inverse().multiply(element.getScreenCTM());
  81. }
  82. var svgMatrix =
  83. getTransformToElement(rect.getElement(), graphics.getElement());
  84. assertRoughlyEquals(svgMatrix.a, costheta, 0.001);
  85. assertRoughlyEquals(svgMatrix.b, -sintheta + 1, 0.001);
  86. assertRoughlyEquals(svgMatrix.c, sintheta, 0.001);
  87. assertRoughlyEquals(svgMatrix.d, costheta, 0.001);
  88. assertRoughlyEquals(svgMatrix.e, dx, 0.001);
  89. assertRoughlyEquals(svgMatrix.f, dy, 0.001);
  90. }