summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shape-detection/resources/single-detection-helpers.js
blob: bbd2bda96b20a29f58d1360f71d68647556b5934 (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
function imageLoadedPromise(image) {
    return new Promise(function(resolve, reject) {
        if (image.complete)
            resolve();
        image.addEventListener("load", resolve, { once: true });
    });
}

function videoLoadedPromise(video) {
    return new Promise(function(resolve, reject) {
        if (video.readyState == 4)
            resolve();
        else {
            video.addEventListener("loadeddata", resolve, { once: true });
            video.addEventListener("error", reject, { once: true });
        }
    });
}

function waitForNFrames(count) {
    if (count <= 0)
         return Promise.reject(new TypeError("count should be greater than 0!"));

    return new Promise(resolve => {
        function tick() {
            (--count) ? requestAnimationFrame(tick) : resolve();
        }
        requestAnimationFrame(tick);
    });
}

function seekTo(video, time) {
    return new Promise(function(resolve, reject) {
        video.addEventListener("seeked", async function() {
            /* Work around flakiness in video players... */
            await waitForNFrames(3);
            resolve();
        }, { once: true });
        video.currentTime = time;
    });
}

function checkBoundingBox(actual, expected, fuzziness) {
    assert_equals(actual.constructor.name, "DOMRectReadOnly");
    assert_approx_equals(actual.left, expected.left, fuzziness);
    assert_approx_equals(actual.right, expected.right, fuzziness);
    assert_approx_equals(actual.top, expected.top, fuzziness);
    assert_approx_equals(actual.bottom, expected.bottom, fuzziness);
}

function checkPointsLieWithinBoundingBox(points, boundingBox) {
    for (point of points) {
        assert_between_inclusive(point.x, boundingBox.left, boundingBox.right);
        assert_between_inclusive(point.y, boundingBox.top, boundingBox.bottom);
    }
}

function checkPointIsNear(actual, expected, fuzzinessX, fuzzinessY) {
    assert_approx_equals(actual.x, expected.x, fuzzinessX);
    assert_approx_equals(actual.y, expected.y, fuzzinessY);
}

function checkPointsAreNear(actual, expected, fuzzinessX, fuzzinessY) {
    for (point of actual)
        checkPointIsNear(point, expected, fuzzinessX, fuzzinessY);
}