summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/SVGAnimationTestCase-testharness.js
blob: 9ebaf6803922e940b2d3cc5fed5eb2a5fe2e74bd (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
// NOTE(edvardt):
// This file is a slimmed down wrapper for the old SVGAnimationTestCase.js,
// it has some convenience functions and should not be used for new tests.
// New tests should not build on this API as it's just meant to keep things
// working.

// Helper functions
const xlinkNS = "http://www.w3.org/1999/xlink"

function expectFillColor(element, red, green, blue, message) {
    let color = window.getComputedStyle(element, null).fill;
    var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
    rgb = re.exec(color);

    assert_approx_equals(Number(rgb[1]), red, 2.0, message);
    assert_approx_equals(Number(rgb[2]), green, 2.0, message);
    assert_approx_equals(Number(rgb[3]), blue, 2.0, message);
}

function expectColor(element, red, green, blue, property) {
    if (typeof property != "string")
      color = getComputedStyle(element).getPropertyValue("color");
    else
      color = getComputedStyle(element).getPropertyValue(property);
    var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
    rgb = re.exec(color);
    assert_approx_equals(Number(rgb[1]), red, 2.0);
    assert_approx_equals(Number(rgb[2]), green, 2.0);
    assert_approx_equals(Number(rgb[3]), blue, 2.0);
}

function createSVGElement(type) {
  return document.createElementNS("http://www.w3.org/2000/svg", type);
}

// Inspired by Layoutests/animations/animation-test-helpers.js
function moveAnimationTimelineAndSample(index) {
    var animationId = expectedResults[index][0];
    var time = expectedResults[index][1];
    var sampleCallback = expectedResults[index][2];
    var animation = rootSVGElement.ownerDocument.getElementById(animationId);

    // If we want to sample the animation end, add a small delta, to reliable point past the end of the animation.
    newTime = time;

    // The sample time is relative to the start time of the animation, take that into account.
    rootSVGElement.setCurrentTime(newTime);

    // NOTE(edvardt):
    // This is a dumb hack, some of the old tests sampled before the animation start, this
    // isn't technically part of the animation tests and is "impossible" to translate since
    // tests start automatically. Thus I solved it by skipping it.
    if (time != 0.0)
        sampleCallback();
}

var currentTest = 0;
var expectedResults;

function sampleAnimation(t) {
    if (currentTest == expectedResults.length) {
        t.done();
        return;
    }

    moveAnimationTimelineAndSample(currentTest);
    ++currentTest;

    step_timeout(t.step_func(function () { sampleAnimation(t); }), 0);
}

function runAnimationTest(t, expected) {
    if (!expected)
        throw("Expected results are missing!");
    if (currentTest > 0)
        throw("Not allowed to call runAnimationTest() twice");

    expectedResults = expected;
    testCount = expectedResults.length;
    currentTest = 0;

    step_timeout(t.step_func(function () { sampleAnimation(this); }), 50);
}

function smil_async_test(func) {
  async_test(t => {
    window.onload = t.step_func(function () {
      // Pause animations, we'll drive them manually.
      // This also ensures that the timeline is paused before
      // it starts. This should make the instance time of the below
      // 'click' (for instance) 0, and hence minimize rounding
      // errors for the addition in moveAnimationTimelineAndSample.
      rootSVGElement.pauseAnimations();

      // If eg. an animation is running with begin="0s", and
      // we want to sample the first time, before the animation
      // starts, then we can't delay the testing by using an
      // onclick event, as the animation would be past start time.
      func(t);
    });
  });
}