summaryrefslogtreecommitdiffstats
path: root/dom/smil/test/test_smilChangeAfterFrozen.xhtml
blob: 97fe13167816bdd00c5a66557c33e298c15e851a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for SMIL when things change after an animation is frozen</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="smilTestUtils.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=533291">Mozilla Bug 533291</a>
<p id="display"></p>
<!-- Bug 628848: The following should be display: none but we currently don't
     handle percentage lengths properly when the whole fragment is display: none
     -->
<div id="content" style="visibility: hidden">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="120px" height="120px"
     onload="this.pauseAnimations()">
  <g id="circleParent">
    <circle cx="0" cy="20" r="15" fill="blue" id="circle"/>
  </g>
</svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[
/** Test for SMIL values that are context-sensitive **/

/* See bugs 533291 and 562815.
   
   The format of each test is basically:
   1) create some animated and frozen state
   2) test the animated values
   3) change the context
   4) test that context-sensitive animation values have changed

   Ideally, after changing the context (3), the animated state would instantly
   update. However, this is not currently the case for many situations.

     For CSS properties we have bug 545282 - In animations involving 'inherit'
     / 'currentColor', changes to inherited value / 'color' don't show up in
     animated value immediately

     For SVG lengths we have bug 508206 - Relative units used in
     animation don't update immediately

     (There are a few of todo_is's in the following tests so that if those bugs
     are ever resolved we'll know to update this test case accordingly.)

   So in between (3) and (4) we force a sample. This is currently done by
   calling SVGSVGElement.setCurrentTime with the same current time which has the
   side effect of forcing a sample.

   What we *are* testing is that we're not too zealous with caching animation
   values whilst in the frozen state. Normally we'd say, "Hey, we're frozen,
   let's just use the same animation result as last time" but for some
   context-sensitive animation values that doesn't work.
*/

/* Global Variables */
const SVGNS = "http://www.w3.org/2000/svg";

// Animation parameters -- not used for <set> animation
const ANIM_DUR = "4s";
const TIME_ANIM_END = "4";
const TIME_AFTER_ANIM_END = "5";

const gSvg = document.getElementById("svg");
const gCircle = document.getElementById("circle");
const gCircleParent = document.getElementById("circleParent");

SimpleTest.waitForExplicitFinish();

// MAIN FUNCTION
// -------------

function main()
{
  ok(gSvg.animationsPaused(), "should be paused by <svg> load handler");
  is(gSvg.getCurrentTime(), 0, "should be paused at 0 in <svg> load handler");

  const tests =
    [ testBaseValueChange,
      testCurrentColorChange,
      testCurrentColorChangeUsingStyle,
      testCurrentColorChangeOnFallback,
      testInheritChange,
      testInheritChangeUsingStyle,
      testEmUnitChangeOnProp,
      testEmUnitChangeOnPropBase,
      testEmUnitChangeOnLength,
      testPercentUnitChangeOnProp,
      testPercentUnitChangeOnLength,
      testRelativeFontSize,
      testRelativeFontWeight,
      testRelativeFont,
      testCalcFontSize,
      testDashArray,
      testClip
     ];

  while (tests.length) {
    tests.shift()();
  }
  SimpleTest.finish();
}

// HELPER FUNCTIONS
// ----------------
function createAnimSetTo(attrName, toVal)
{
  var anim = document.createElementNS(SVGNS,"set");
  anim.setAttribute("attributeName", attrName);
  anim.setAttribute("to", toVal);
  return gCircle.appendChild(anim);
}

function createAnimBy(attrName, byVal)
{
  var anim = document.createElementNS(SVGNS,"animate");
  anim.setAttribute("attributeName", attrName);
  anim.setAttribute("dur", ANIM_DUR);
  anim.setAttribute("begin","0s");
  anim.setAttribute("by", byVal);
  anim.setAttribute("fill", "freeze");
  return gCircle.appendChild(anim);
}

function createAnimFromTo(attrName, fromVal, toVal)
{
  var anim = document.createElementNS(SVGNS,"animate");
  anim.setAttribute("attributeName", attrName);
  anim.setAttribute("dur", ANIM_DUR);
  anim.setAttribute("begin","0s");
  anim.setAttribute("from", fromVal);
  anim.setAttribute("to", toVal);
  anim.setAttribute("fill", "freeze");
  return gCircle.appendChild(anim);
}

// Common setup code for each test function: seek to 0, and make sure
// the previous test cleaned up its animations.
function setupTest() {
  gSvg.setCurrentTime(0);
  if (gCircle.firstChild) {
    ok(false, "Previous test didn't clean up after itself.");
  }
}

// THE TESTS
// ---------

function testBaseValueChange()
{
  setupTest();
  var anim = createAnimBy("cx", "50");
  gSvg.setCurrentTime(TIME_ANIM_END);
  is(gCircle.cx.animVal.value, 50,
     "Checking animated cx as anim ends");

  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(gCircle.cx.animVal.value, 50,
     "Checking animated cx after anim ends");

  gCircle.setAttribute("cx", 20);
  is(gCircle.cx.animVal.value, 70,
     "Checking animated cx after anim ends & after changing base val");

  anim.remove(); // clean up
}

function testCurrentColorChange()
{
  gCircle.setAttribute("color", "red"); // At first: currentColor=red
  var anim = createAnimSetTo("fill", "currentColor");

  gSvg.setCurrentTime(0); // trigger synchronous sample
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(255, 0, 0)",
     "Checking animated fill=currentColor after animating");

  gCircle.setAttribute("color", "lime"); // Change: currentColor=lime
  // Bug 545282: We should really detect this change and update immediately but
  // currently we don't until we get sampled again
  todo_is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(0, 255, 0)",
     "Checking animated fill=currentColor after updating context but before " +
     "sampling");
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(0, 255, 0)",
     "Checking animated fill=currentColor after updating context");

  // Clean up
  gCircle.removeAttribute("color");
  gCircle.firstChild.remove();
}

function testCurrentColorChangeUsingStyle()
{
  setupTest();
  gCircle.setAttribute("style", "color: red"); // At first: currentColor=red
  var anim = createAnimSetTo("fill", "currentColor");

  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(255, 0, 0)",
     "Checking animated fill=currentColor after animating (using style attr)");

  gCircle.setAttribute("style", "color: lime"); // Change: currentColor=lime
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(0, 255, 0)",
     "Checking animated fill=currentColor after updating context "
     + "(using style attr)");

  // Clean up
  gCircle.removeAttribute("style");
  gCircle.firstChild.remove();
}

function getFallbackColor(pServerStr)
{
  return pServerStr.substr(pServerStr.indexOf(" ")+1);
}

function testCurrentColorChangeOnFallback()
{
  setupTest();
  gCircle.setAttribute("color", "red"); // At first: currentColor=red
  var anim = createAnimSetTo("fill", "url(#missingGrad) currentColor");

  gSvg.setCurrentTime(0);
  var fallback =
    getFallbackColor(SMILUtil.getComputedStyleSimple(gCircle, "fill"));
  is(fallback, "rgb(255, 0, 0)",
     "Checking animated fallback fill=currentColor after animating");

  gCircle.setAttribute("color", "lime"); // Change: currentColor=lime
  gSvg.setCurrentTime(0);
  fallback = getFallbackColor(SMILUtil.getComputedStyleSimple(gCircle, "fill"));
  is(fallback, "rgb(0, 255, 0)",
     "Checking animated fallback fill=currentColor after updating context");

  gCircle.removeAttribute("style");
  gCircle.firstChild.remove();
}

function testInheritChange()
{
  setupTest();
  gCircleParent.setAttribute("fill", "red"); // At first: inherit=red
  var anim = createAnimSetTo("fill", "inherit");

  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(255, 0, 0)",
     "Checking animated fill=inherit after animating");

  gCircleParent.setAttribute("fill", "lime"); // Change: inherit=lime
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(0, 255, 0)",
     "Checking animated fill=inherit after updating context");

  gCircleParent.removeAttribute("fill");
  gCircle.firstChild.remove();
}

function testInheritChangeUsingStyle()
{
  setupTest();
  gCircleParent.setAttribute("style", "fill: red"); // At first: inherit=red
  var anim = createAnimSetTo("fill", "inherit");

  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(255, 0, 0)",
     "Checking animated fill=inherit after animating (using style attr)");

  gCircleParent.setAttribute("style", "fill: lime"); // Change: inherit=lime
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "fill"), "rgb(0, 255, 0)",
     "Checking animated fill=inherit after updating context "
     + "(using style attr)");

  gCircleParent.removeAttribute("style");
  gCircle.firstChild.remove();
}

function testEmUnitChangeOnProp()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  var anim = createAnimSetTo("font-size", "2em");

  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "20px",
     "Checking animated font-size=2em after animating ends");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "40px",
     "Checking animated font-size=2em after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testEmUnitChangeOnPropBase()
{
  // Test the case where the base value for our animation sandwich is
  // context-sensitive.
  // Currently, this is taken care of by the compositor which keeps a cached
  // base value and compares it with the current base value. This test then just
  // serves as a regression test in case the compositor's behaviour changes.
  setupTest();
  gSvg.setAttribute("font-size", "10px"); // At first: font-size: 10px
  gCircleParent.setAttribute("font-size", "1em"); // Base: 10px
  var anim = createAnimBy("font-size", "10px");

  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "20px",
     "Checking animated font-size=20px after anim ends");

  gSvg.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "30px",
     "Checking animated font-size=30px after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testEmUnitChangeOnLength()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  var anim = createAnimSetTo("cx", "2em");

  gSvg.setCurrentTime(0);
  is(gCircle.cx.animVal.value, 20,
     "Checking animated length=2em after animating");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  // Bug 508206: We should really detect this change and update immediately but
  // currently we don't until we get sampled again
  todo_is(gCircle.cx.animVal.value, 40,
     "Checking animated length=2em after updating context but before sampling");

  gSvg.setCurrentTime(0);
  is(gCircle.cx.animVal.value, 40,
     "Checking animated length=2em after updating context and after " +
     "resampling");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testPercentUnitChangeOnProp()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  var anim = createAnimSetTo("font-size", "150%");

  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "15px",
     "Checking animated font-size=150% after animating");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(0);
  is(SMILUtil.getComputedStyleSimple(gCircle, "font-size"), "30px",
     "Checking animated font-size=150% after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testPercentUnitChangeOnLength()
{
  setupTest();
  var oldHeight = gSvg.getAttribute("height");
  gSvg.setAttribute("height", "100px"); // At first: viewport height: 100px
  var anim = createAnimSetTo("cy", "100%");

  gSvg.setCurrentTime(0); // Force synchronous sample so animation takes effect
  // Due to bug 627594 (SVGLength.value for percent value lengths doesn't
  // reflect updated viewport until reflow) the following will fail.
  // Check that it does indeed fail so that when that bug is fixed this test
  // can be updated.
  todo_is(gCircle.cy.animVal.value, 100,
     "Checking animated length=100% after animating but before reflow");
  // force a layout flush (Bug 627594)
  gSvg.getCTM();
  // Even after doing a reflow though we'll still fail due to bug 508206
  // (Relative units used in animation don't update immediately)
  todo_is(gCircle.cy.animVal.value, 100,
     "Checking animated length=100% after animating but before resampling");
  gSvg.setCurrentTime(0);
  // Now we should be up to date
  is(gCircle.cy.animVal.value, 100,
     "Checking animated length=100% after animating");

  gSvg.setAttribute("height", "50px"); // Change: height: 50px
  // force a layout flush (Bug 627594)
  gSvg.getCTM();
  gSvg.setCurrentTime(0); // Bug 508206
  is(gCircle.cy.animVal.value, 50,
     "Checking animated length=100% after updating context");

  gSvg.setAttribute("height", oldHeight);
  gCircle.firstChild.remove();
}

function testRelativeFontSize()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  var anim = createAnimSetTo("font-size", "larger");

  gSvg.setCurrentTime(0);
  var fsize = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  // CSS 2 suggests a scaling factor of 1.2 so we should be looking at something
  // around about 12 or so
  ok(fsize > 10 && fsize < 20,
    "Checking animated font-size > 10px after animating");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(0);
  fsize = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  ok(fsize > 20, "Checking animated font-size > 20px after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testRelativeFontWeight()
{
  setupTest();
  gCircleParent.setAttribute("font-weight", "100"); // At first: font-weight 100
  var anim = createAnimSetTo("font-weight", "bolder");
  // CSS 2: 'bolder': Specifies the next weight that is assigned to a font
  // that is darker than the inherited one. If there is no such weight, it
  // simply results in the next darker numerical value (and the font remains
  // unchanged), unless the inherited value was '900', in which case the
  // resulting weight is also '900'.

  gSvg.setCurrentTime(0);
  var weight =
    parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-weight"));
  ok(weight > 100, "Checking animated font-weight > 100 after animating");

  gCircleParent.setAttribute("font-weight", "800"); // Change: font-weight 800
  gSvg.setCurrentTime(0);
  weight = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-weight"));
  is(weight, 900,
     "Checking animated font-weight = 900 after updating context");

  gCircleParent.removeAttribute("font-weight");
  gCircle.firstChild.remove();
}

function testRelativeFont()
{
  // Test a relative font-size as part of a 'font' spec since the code path
  // is different in this case
  // It turns out that, due to the way we store shorthand font properties, we
  // don't need to worry about marking such values as context-sensitive since we
  // seem to store them in their relative form. If, however, we change the way
  // we store shorthand font properties in the future, this will serve as
  // a useful regression test.
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  // We must be sure to set every part of the shorthand property to some
  // non-context sensitive value because we want to test that even if only the
  // font-size is relative we will update it appropriately.
  var anim =
    createAnimSetTo("font", "normal normal bold larger/normal sans-serif");

  gSvg.setCurrentTime(0);
  var fsize = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  ok(fsize > 10 && fsize < 20,
    "Checking size of shorthand 'font' > 10px after animating");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(0);
  fsize  = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  ok(fsize > 20,
     "Checking size of shorthand 'font' > 20px after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testCalcFontSize()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "10px"); // At first: font-size: 10px
  var anim = createAnimSetTo("font-size", "calc(110% + 0.1em)");

  gSvg.setCurrentTime(0);
  var fsize = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  // Font size should be 1.1 * 10px + 0.1 * 10px = 12
  is(fsize, 12, "Checking animated calc font-size == 12px after animating");

  gCircleParent.setAttribute("font-size", "20px"); // Change: font-size: 20px
  gSvg.setCurrentTime(0);
  fsize = parseInt(SMILUtil.getComputedStyleSimple(gCircle, "font-size"));
  is(fsize, 24, "Checking animated calc font-size == 24px after updating " +
                "context");

  gCircleParent.removeAttribute("font-size");
  gCircle.firstChild.remove();
}

function testDashArray()
{
  // stroke dasharrays don't currently convert units--but if someone ever fixes
  // that, hopefully this test will fail and remind us not to cache percentage
  // values in that case
  setupTest();
  var oldHeight = gSvg.getAttribute("height");
  var oldWidth  = gSvg.getAttribute("width");
  gSvg.setAttribute("height", "100px"); // At first: viewport: 100x100px
  gSvg.setAttribute("width",  "100px");
  var anim = createAnimFromTo("stroke-dasharray", "0 5", "0 50%");

  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);

  // Now we should be up to date
  is(SMILUtil.getComputedStyleSimple(gCircle, "stroke-dasharray"), "0, 50%",
     "Checking animated stroke-dasharray after animating");

  gSvg.setAttribute("height", "50px"); // Change viewport: 50x50px
  gSvg.setAttribute("width",  "50px");
  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(SMILUtil.getComputedStyleSimple(gCircle, "stroke-dasharray"), "0, 50%",
     "Checking animated stroke-dasharray after updating context");

  gSvg.setAttribute("height", oldHeight);
  gSvg.setAttribute("width",  oldWidth);
  gCircle.firstChild.remove();
}

function testClip()
{
  setupTest();
  gCircleParent.setAttribute("font-size", "20px"); // At first: font-size: 20px

  // The clip property only applies to elements that establish a new
  // viewport so we need to create a nested svg and add animation to that
  var nestedSVG = document.createElementNS(SVGNS, "svg");
  nestedSVG.setAttribute("clip", "rect(0px 0px 0px 0px)");
  gCircleParent.appendChild(nestedSVG);

  var anim = createAnimSetTo("clip", "rect(1em 1em 1em 1em)");
  // createAnimSetTo will make the animation a child of gCircle so we need to
  // move it so it targets nestedSVG instead
  nestedSVG.appendChild(anim);

  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(SMILUtil.getComputedStyleSimple(nestedSVG, "clip"),
     "rect(20px, 20px, 20px, 20px)",
     "Checking animated clip rect after animating");

  gCircleParent.setAttribute("font-size", "10px"); // Change: font-size: 10px
  gSvg.setCurrentTime(TIME_AFTER_ANIM_END);
  is(SMILUtil.getComputedStyleSimple(nestedSVG, "clip"),
     "rect(10px, 10px, 10px, 10px)",
     "Checking animated clip rect after updating context");

  gCircleParent.removeAttribute("font-size");
  gCircleParent.removeChild(nestedSVG);
}

window.addEventListener("load", main);
]]>
</script>
</pre>
</body>
</html>