11cb0ef41Sopenharmony_ci// NOTE(edvardt):
21cb0ef41Sopenharmony_ci// This file is a slimmed down wrapper for the old SVGAnimationTestCase.js,
31cb0ef41Sopenharmony_ci// it has some convenience functions and should not be used for new tests.
41cb0ef41Sopenharmony_ci// New tests should not build on this API as it's just meant to keep things
51cb0ef41Sopenharmony_ci// working.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Helper functions
81cb0ef41Sopenharmony_ciconst xlinkNS = "http://www.w3.org/1999/xlink"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction expectFillColor(element, red, green, blue, message) {
111cb0ef41Sopenharmony_ci    let color = window.getComputedStyle(element, null).fill;
121cb0ef41Sopenharmony_ci    var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
131cb0ef41Sopenharmony_ci    rgb = re.exec(color);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[1]), red, 2.0, message);
161cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[2]), green, 2.0, message);
171cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[3]), blue, 2.0, message);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction expectColor(element, red, green, blue, property) {
211cb0ef41Sopenharmony_ci    if (typeof property != "string")
221cb0ef41Sopenharmony_ci      color = getComputedStyle(element).getPropertyValue("color");
231cb0ef41Sopenharmony_ci    else
241cb0ef41Sopenharmony_ci      color = getComputedStyle(element).getPropertyValue(property);
251cb0ef41Sopenharmony_ci    var re = new RegExp("rgba?\\(([^, ]*), ([^, ]*), ([^, ]*)(?:, )?([^, ]*)\\)");
261cb0ef41Sopenharmony_ci    rgb = re.exec(color);
271cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[1]), red, 2.0);
281cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[2]), green, 2.0);
291cb0ef41Sopenharmony_ci    assert_approx_equals(Number(rgb[3]), blue, 2.0);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction createSVGElement(type) {
331cb0ef41Sopenharmony_ci  return document.createElementNS("http://www.w3.org/2000/svg", type);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Inspired by Layoutests/animations/animation-test-helpers.js
371cb0ef41Sopenharmony_cifunction moveAnimationTimelineAndSample(index) {
381cb0ef41Sopenharmony_ci    var animationId = expectedResults[index][0];
391cb0ef41Sopenharmony_ci    var time = expectedResults[index][1];
401cb0ef41Sopenharmony_ci    var sampleCallback = expectedResults[index][2];
411cb0ef41Sopenharmony_ci    var animation = rootSVGElement.ownerDocument.getElementById(animationId);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    // If we want to sample the animation end, add a small delta, to reliable point past the end of the animation.
441cb0ef41Sopenharmony_ci    newTime = time;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    // The sample time is relative to the start time of the animation, take that into account.
471cb0ef41Sopenharmony_ci    rootSVGElement.setCurrentTime(newTime);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    // NOTE(edvardt):
501cb0ef41Sopenharmony_ci    // This is a dumb hack, some of the old tests sampled before the animation start, this
511cb0ef41Sopenharmony_ci    // isn't technically part of the animation tests and is "impossible" to translate since
521cb0ef41Sopenharmony_ci    // tests start automatically. Thus I solved it by skipping it.
531cb0ef41Sopenharmony_ci    if (time != 0.0)
541cb0ef41Sopenharmony_ci        sampleCallback();
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_civar currentTest = 0;
581cb0ef41Sopenharmony_civar expectedResults;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cifunction sampleAnimation(t) {
611cb0ef41Sopenharmony_ci    if (currentTest == expectedResults.length) {
621cb0ef41Sopenharmony_ci        t.done();
631cb0ef41Sopenharmony_ci        return;
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    moveAnimationTimelineAndSample(currentTest);
671cb0ef41Sopenharmony_ci    ++currentTest;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    step_timeout(t.step_func(function () { sampleAnimation(t); }), 0);
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cifunction runAnimationTest(t, expected) {
731cb0ef41Sopenharmony_ci    if (!expected)
741cb0ef41Sopenharmony_ci        throw("Expected results are missing!");
751cb0ef41Sopenharmony_ci    if (currentTest > 0)
761cb0ef41Sopenharmony_ci        throw("Not allowed to call runAnimationTest() twice");
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    expectedResults = expected;
791cb0ef41Sopenharmony_ci    testCount = expectedResults.length;
801cb0ef41Sopenharmony_ci    currentTest = 0;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    step_timeout(t.step_func(function () { sampleAnimation(this); }), 50);
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_cifunction smil_async_test(func) {
861cb0ef41Sopenharmony_ci  async_test(t => {
871cb0ef41Sopenharmony_ci    window.onload = t.step_func(function () {
881cb0ef41Sopenharmony_ci      // Pause animations, we'll drive them manually.
891cb0ef41Sopenharmony_ci      // This also ensures that the timeline is paused before
901cb0ef41Sopenharmony_ci      // it starts. This should make the instance time of the below
911cb0ef41Sopenharmony_ci      // 'click' (for instance) 0, and hence minimize rounding
921cb0ef41Sopenharmony_ci      // errors for the addition in moveAnimationTimelineAndSample.
931cb0ef41Sopenharmony_ci      rootSVGElement.pauseAnimations();
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci      // If eg. an animation is running with begin="0s", and
961cb0ef41Sopenharmony_ci      // we want to sample the first time, before the animation
971cb0ef41Sopenharmony_ci      // starts, then we can't delay the testing by using an
981cb0ef41Sopenharmony_ci      // onclick event, as the animation would be past start time.
991cb0ef41Sopenharmony_ci      func(t);
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci}
103