File: svggraphics_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (98 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (2)
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
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
// Use of this source code is governed by the Apache License, Version 2.0.

goog.provide('goog.graphics.SvgGraphicsTest');
goog.setTestOnly('goog.graphics.SvgGraphicsTest');

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

var graphics;

function setUp() {
  if (!document.createElementNS) {
    // Some browsers don't support document.createElementNS and this test
    // should not be run on those browsers (IE7,8).
    return;
  }
  graphics = new goog.graphics.SvgGraphics('100px', '100px');
  graphics.createDom();
  goog.dom.getElement('root').appendChild(graphics.getElement());
}

function testAddDef() {
  if (!graphics) {
    // setUp has failed (no browser support), we should not run this test.
    return;
  }
  var defElement1 = document.createElement('div');
  var defElement2 = document.createElement('div');
  var defKey1 = 'def1';
  var defKey2 = 'def2';
  var id = graphics.addDef(defKey1, defElement1);
  assertEquals('_svgdef_0', id);
  id = graphics.addDef(defKey1, defElement2);
  assertEquals('_svgdef_0', id);
  id = graphics.addDef(defKey2, defElement2);
  assertEquals('_svgdef_1', id);
}

function testGetDef() {
  if (!graphics) {
    // setUp has failed (no browser support), we should not run this test.
    return;
  }
  var defElement = document.createElement('div');
  var defKey = 'def';
  var id = graphics.addDef(defKey, defElement);
  assertEquals(id, graphics.getDef(defKey));
  assertNull(graphics.getDef('randomKey'));
}

function testRemoveDef() {
  if (!graphics) {
    // setUp has failed (no browser support), we should not run this test.
    return;
  }
  var defElement = document.createElement('div');
  var defKey = 'def';
  var addedId = graphics.addDef(defKey, defElement);
  graphics.removeDef('randomKey');
  assertEquals(addedId, graphics.getDef(defKey));
  graphics.removeDef(defKey);
  assertNull(graphics.getDef(defKey));
}

function testSetElementAffineTransform() {
  if (!graphics) {
    // setUp has failed (no browser support), we should not run this test.
    return;
  }
  var fill = new goog.graphics.SolidFill('blue');
  var stroke = null;
  var rad = -3.1415926 / 6;
  var costheta = Math.cos(rad);
  var sintheta = Math.sin(rad);
  var dx = 10;
  var dy = -20;
  var affine = new goog.graphics.AffineTransform(
    costheta, -sintheta + 1, sintheta, costheta, dx, dy);
  var rect = graphics.drawRect(10, 20, 30, 40, stroke, fill);
  rect.setTransform(affine);
  graphics.render();
  // getTransformToElement was deleted in Chrome48. See
  // https://code.google.com/p/chromium/issues/detail?id=524432.
  function getTransformToElement(element, target) {
    return target.getScreenCTM().inverse().multiply(element.getScreenCTM());
  }
  var svgMatrix =
      getTransformToElement(rect.getElement(), graphics.getElement());
  assertRoughlyEquals(svgMatrix.a, costheta, 0.001);
  assertRoughlyEquals(svgMatrix.b, -sintheta + 1, 0.001);
  assertRoughlyEquals(svgMatrix.c, sintheta, 0.001);
  assertRoughlyEquals(svgMatrix.d, costheta, 0.001);
  assertRoughlyEquals(svgMatrix.e, dx, 0.001);
  assertRoughlyEquals(svgMatrix.f, dy, 0.001);
}