summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_SVGTransformListAddition.xhtml
blob: 4ba3d4773dbf9259f0c282f9808aeeaa37b12da3 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=602759
-->
<head>
  <title>Tests specific to SVGLengthList addition</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=602759">
  Mozilla Bug 602759</a>
<p id="display"></p>
<div id="content">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="100"
     onload="this.pauseAnimations();">
  <g id="g"/>
</svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

/*
This file runs a series of tests specific to addition of SVGTransformList in
animation.
*/

function AdditionTestCase(desc, baseVal, animSpecs, expectedTransformList)
{
  this.desc = desc;
  this.baseVal = baseVal;
  this.animSpecs = animSpecs;
  this.expectedTransformList = expectedTransformList;
}

function Transform(type, angle)
{
  this.type = type;
  this.angle = angle;
}

function main(g)
{
  var cases = [
    new AdditionTestCase("Not additive",
          "translate(150 50)",
          {type: 'rotate', from: '0', to: '90'},
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90)]
    ),
    new AdditionTestCase("To animation",
          "rotate(-90)",
          {type: 'rotate', to: '90'},
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90)]
    ),
    new AdditionTestCase("By animation",
          "rotate(-90)",
          {type: 'rotate', by: '180'},
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, -90),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 180)]
    ),
    new AdditionTestCase("Normal additive: same type",
          "rotate(45)",
          {type: 'rotate', from: '0', to: '45', additive: 'sum'},
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45)]
    ),
    new AdditionTestCase("Normal additive: different type",
          "translate(50)",
          {type: 'rotate', from: '0', to: '90', additive: 'sum'},
          [new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90)]
    ),
    new AdditionTestCase("Stacked additive: same type",
          "rotate(-90)",
          [{type: 'rotate', from: '0', to: '90', additive: 'sum'},
           {type: 'rotate', from: '0', to: '90', additive: 'sum'}],
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, -90),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90)]
    ),
    new AdditionTestCase("Stacked additive: different types #1",
          "translate(50)",
          [{type: 'rotate', from: '0', to: '45', additive: 'sum'},
           {type: 'rotate', from: '0', to: '45', additive: 'sum'}],
          [new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45)]
    ),
    new AdditionTestCase("Stacked additive: different types #2",
          "skewX(20) translate(50)",
          [{type: 'rotate', from: '0', to: '45', additive: 'sum'},
           {type: 'rotate', from: '0', to: '45', additive: 'sum'}],
          [new Transform(SVGTransform.SVG_TRANSFORM_SKEWX, 20),
           new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45)]
    ),
    new AdditionTestCase("Stacked additive: different types #3",
          "skewX(20) translate(50)",
          [{type: 'rotate', from: '0', to: '45', additive: 'sum'},
           {type: 'translate', from: '0', to: '30', additive: 'sum'},
           {type: 'translate', from: '0', to: '-30', additive: 'sum'},
           {type: 'rotate', from: '0', to: '45', additive: 'sum'}],
          [new Transform(SVGTransform.SVG_TRANSFORM_SKEWX, 20),
           new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45),
           new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0),
           new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 45)]
    ),
    new AdditionTestCase("Base value with rotation around a centre",
          "rotate(90 50 50)",
          {type: 'translate', from: '0 0', to: '0 -50', additive: 'sum'},
          [new Transform(SVGTransform.SVG_TRANSFORM_ROTATE, 90),
           new Transform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 0)]
    ),
  ];

  for (var i = 0; i < cases.length; i++) {
    runAdditionTestCase(cases[i], $('g'), $('svg'));
  }

  SimpleTest.finish();
}

function runAdditionTestCase(test, elem, svg)
{
  var anims = createAnims(test.animSpecs);

  elem.setAttribute('transform', test.baseVal);
  elem.appendChild(anims);

  svg.setCurrentTime(1);
  var expected = test.expectedTransformList; // Array of Transform objects
  var actual   = elem.transform.animVal;     // SVGTransformList
  is(actual.numberOfItems, expected.length,
     "Unexpected number of transforms");

  if (actual.numberOfItems == expected.length) {
    for (var i = 0; i < actual.numberOfItems; i++) {
      var transform = actual.getItem(i);
      var testDesc = " for transform " + i + " in '" + test.desc + "' test";
      is(transform.type,  expected[i].type,
         "Unexpected transform type" + testDesc);
      is(transform.angle, expected[i].angle,
         "Unexpected transform angle" + testDesc);
    }
  }
  // We assume the only children of elem are the animation elements we've just
  // added.
  while (elem.firstChild) {
    elem.removeChild(elem.firstChild);
  }
}

function createAnims(specs)
{
  if (specs.constructor == Array) {
    var frag = document.createDocumentFragment();
    for (var i = 0; i < specs.length; ++i) {
      frag.appendChild(createAnim(specs[i]));
    }
    return frag;
  }

  return createAnim(specs);
}

function createAnim(attrs)
{
  var SVG_NS = 'http://www.w3.org/2000/svg';
  var anim = document.createElementNS(SVG_NS, 'animateTransform');
  anim.setAttribute('attributeName', 'transform');
  anim.setAttribute('dur', '1s');
  anim.setAttribute('fill', 'freeze');
  for (attr in attrs) {
    anim.setAttribute(attr, attrs[attr]);
  }
  return anim;
}

window.addEventListener("load", main, false);

]]>
</script>
</pre>
</body>
</html>