Debian Codegraph @

closure-library/closure/goog/graphics/svggraphics_test.js

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