single-detection-helpers.js (2205B)
1 function imageLoadedPromise(image) { 2 return new Promise(function(resolve, reject) { 3 if (image.complete) 4 resolve(); 5 image.addEventListener("load", resolve, { once: true }); 6 }); 7 } 8 9 function videoLoadedPromise(video) { 10 return new Promise(function(resolve, reject) { 11 if (video.readyState == 4) 12 resolve(); 13 else { 14 video.addEventListener("loadeddata", resolve, { once: true }); 15 video.addEventListener("error", reject, { once: true }); 16 } 17 }); 18 } 19 20 function waitForNFrames(count) { 21 if (count <= 0) 22 return Promise.reject(new TypeError("count should be greater than 0!")); 23 24 return new Promise(resolve => { 25 function tick() { 26 (--count) ? requestAnimationFrame(tick) : resolve(); 27 } 28 requestAnimationFrame(tick); 29 }); 30 } 31 32 function seekTo(video, time) { 33 return new Promise(function(resolve, reject) { 34 video.addEventListener("seeked", async function() { 35 /* Work around flakiness in video players... */ 36 await waitForNFrames(3); 37 resolve(); 38 }, { once: true }); 39 video.currentTime = time; 40 }); 41 } 42 43 function checkBoundingBox(actual, expected, fuzziness) { 44 assert_equals(actual.constructor.name, "DOMRectReadOnly"); 45 assert_approx_equals(actual.left, expected.left, fuzziness); 46 assert_approx_equals(actual.right, expected.right, fuzziness); 47 assert_approx_equals(actual.top, expected.top, fuzziness); 48 assert_approx_equals(actual.bottom, expected.bottom, fuzziness); 49 } 50 51 function checkPointsLieWithinBoundingBox(points, boundingBox) { 52 for (point of points) { 53 assert_between_inclusive(point.x, boundingBox.left, boundingBox.right); 54 assert_between_inclusive(point.y, boundingBox.top, boundingBox.bottom); 55 } 56 } 57 58 function checkPointIsNear(actual, expected, fuzzinessX, fuzzinessY) { 59 assert_approx_equals(actual.x, expected.x, fuzzinessX); 60 assert_approx_equals(actual.y, expected.y, fuzzinessY); 61 } 62 63 function checkPointsAreNear(actual, expected, fuzzinessX, fuzzinessY) { 64 for (point of actual) 65 checkPointIsNear(point, expected, fuzzinessX, fuzzinessY); 66 }