Debian Codegraph @

testing/web-platform/tests/resources/SVGAnimationTestCase-testharness.js

raw
1 // NOTE(edvardt):
2 // This file is a slimmed down wrapper for the old SVGAnimationTestCase.js,
3 // it has some convenience functions and should not be used for new tests.
4 // New tests should not build on this API as it's just meant to keep things
5 // working.
6
7 // Helper functions
8 const xlinkNS = "http://www.w3.org/1999/xlink"
9
10 function expectFillColor(element, red, green, blue, message) {
11 let color = window.getComputedStyle(element, null).fill;
12 var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
13 rgb = re.exec(color);
14
15 assert_approx_equals(Number(rgb[1]), red, 2.0, message);
16 assert_approx_equals(Number(rgb[2]), green, 2.0, message);
17 assert_approx_equals(Number(rgb[3]), blue, 2.0, message);
18 }
19
20 function expectColor(element, red, green, blue, property) {
21 if (typeof property != "string")
22 color = getComputedStyle(element).getPropertyValue("color");
23 else
24 color = getComputedStyle(element).getPropertyValue(property);
25 var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
26 rgb = re.exec(color);
27 assert_approx_equals(Number(rgb[1]), red, 2.0);
28 assert_approx_equals(Number(rgb[2]), green, 2.0);
29 assert_approx_equals(Number(rgb[3]), blue, 2.0);
30 }
31
32 function createSVGElement(type) {
33 return document.createElementNS("http://www.w3.org/2000/svg", type);
34 }
35
36 // Inspired by Layoutests/animations/animation-test-helpers.js
37 function moveAnimationTimelineAndSample(index) {
38 var animationId = expectedResults[index][0];
39 var time = expectedResults[index][1];
40 var sampleCallback = expectedResults[index][2];
41 var animation = rootSVGElement.ownerDocument.getElementById(animationId);
42
43 // If we want to sample the animation end, add a small delta, to reliable point past the end of the animation.
44 newTime = time;
45
46 // The sample time is relative to the start time of the animation, take that into account.
47 rootSVGElement.setCurrentTime(newTime);
48
49 // NOTE(edvardt):
50 // This is a dumb hack, some of the old tests sampled before the animation start, this
51 // isn't technically part of the animation tests and is "impossible" to translate since
52 // tests start automatically. Thus I solved it by skipping it.
53 if (time != 0.0)
54 sampleCallback();
55 }
56
57 var currentTest = 0;
58 var expectedResults;
59
60 function sampleAnimation(t) {
61 if (currentTest == expectedResults.length) {
62 t.done();
63 return;
64 }
65
66 moveAnimationTimelineAndSample(currentTest);
67 ++currentTest;
68
69 step_timeout(t.step_func(function () { sampleAnimation(t); }), 0);
70 }
71
72 function runAnimationTest(t, expected) {
73 if (!expected)
74 throw("Expected results are missing!");
75 if (currentTest > 0)
76 throw("Not allowed to call runAnimationTest() twice");
77
78 expectedResults = expected;
79 testCount = expectedResults.length;
80 currentTest = 0;
81
82 step_timeout(t.step_func(function () { sampleAnimation(this); }), 50);
83 }
84
85 function smil_async_test(func) {
86 async_test(t => {
87 window.onload = t.step_func(function () {
88 // Pause animations, we'll drive them manually.
89 // This also ensures that the timeline is paused before
90 // it starts. This should make the instance time of the below
91 // 'click' (for instance) 0, and hence minimize rounding
92 // errors for the addition in moveAnimationTimelineAndSample.
93 rootSVGElement.pauseAnimations();
94
95 // If eg. an animation is running with begin="0s", and
96 // we want to sample the first time, before the animation
97 // starts, then we can't delay the testing by using an
98 // onclick event, as the animation would be past start time.
99 func(t);
100 });
101 });
102 }