1 //----------------------------------------------------------------------
3 // Common testing functions
5 //----------------------------------------------------------------------
7 function advance_clock(milliseconds) {
8 SpecialPowers.DOMWindowUtils.advanceTimeAndRefresh(milliseconds);
11 // Test-element creation/destruction and event checking
14 var gEventsReceived = [];
16 function new_div(style) {
17 return new_element("div", style);
20 // Creates a new |tagname| element with inline style |style| and appends
21 // it as a child of the element with ID 'display'.
22 // The element will also be given the class 'target' which can be used
23 // for additional styling.
24 function new_element(tagname, style) {
26 ok(false, "test author forgot to call done_div/done_elem");
28 if (typeof style != "string") {
29 ok(false, "test author forgot to pass argument");
31 if (!document.getElementById("display")) {
32 ok(false, "no 'display' element to append to");
34 gElem = document.createElement(tagname);
35 gElem.setAttribute("style", style);
36 gElem.classList.add("target");
37 document.getElementById("display").appendChild(gElem);
38 return [gElem, getComputedStyle(gElem, "")];
43 ok(false, "test author forgot to call new_div before listen");
46 function listener(event) {
47 gEventsReceived.push(event);
49 gElem.addEventListener("animationstart", listener);
50 gElem.addEventListener("animationiteration", listener);
51 gElem.addEventListener("animationend", listener);
54 function check_events(eventsExpected, desc) {
55 // This function checks that the list of eventsExpected matches
56 // the received events -- but it only checks the properties that
57 // are present on eventsExpected.
59 gEventsReceived.length,
60 eventsExpected.length,
61 "number of events received for " + desc
65 i_end = Math.min(eventsExpected.length, gEventsReceived.length);
69 var exp = eventsExpected[i];
70 var rec = gEventsReceived[i];
71 for (var prop in exp) {
72 if (prop == "elapsedTime") {
73 // Allow floating point error.
75 Math.abs(rec.elapsedTime - exp.elapsedTime) < 0.000002,
91 "events[" + i + "]." + prop + " for " + desc
96 for (var i = eventsExpected.length; i < gEventsReceived.length; ++i) {
97 ok(false, "unexpected " + gEventsReceived[i].type + " event for " + desc);
102 function done_element() {
106 "test author called done_element/done_div without matching" +
107 " call to new_element/new_div"
112 if (gEventsReceived.length) {
113 ok(false, "caller should have called check_events");
117 [new_div, new_element, listen, check_events, done_element].forEach(function (
120 window[fn.name] = fn;
122 window.done_div = done_element;
125 function px_to_num(str) {
126 return Number(String(str).match(/^([\d.]+)px$/)[1]);
129 function bezier(x1, y1, x2, y2) {
130 // Cubic bezier with control points (0, 0), (x1, y1), (x2, y2), and (1, 1).
131 function x_for_t(t) {
133 return 3 * omt * omt * t * x1 + 3 * omt * t * t * x2 + t * t * t;
135 function y_for_t(t) {
137 return 3 * omt * omt * t * y1 + 3 * omt * t * t * y2 + t * t * t;
139 function t_for_x(x) {
140 // Binary subdivision.
143 for (var i = 0; i < 30; ++i) {
144 var guesst = (mint + maxt) / 2;
145 var guessx = x_for_t(guesst);
152 return (mint + maxt) / 2;
154 return function bezier_closure(x) {
161 return y_for_t(t_for_x(x));
165 function step_end(nsteps) {
166 return function step_end_closure(x) {
167 return Math.floor(x * nsteps) / nsteps;
171 function step_start(nsteps) {
172 var stepend = step_end(nsteps);
173 return function step_start_closure(x) {
174 return 1.0 - stepend(1.0 - x);
179 ease: bezier(0.25, 0.1, 0.25, 1),
180 linear: function (x) {
183 ease_in: bezier(0.42, 0, 1, 1),
184 ease_out: bezier(0, 0, 0.58, 1),
185 ease_in_out: bezier(0.42, 0, 0.58, 1),
186 step_start: step_start(1),
187 step_end: step_end(1),
190 function is_approx(float1, float2, error, desc) {
192 Math.abs(float1 - float2) < error,
193 desc + ": " + float1 + " and " + float2 + " should be within " + error
197 function findKeyframesRule(name) {
198 for (var i = 0; i < document.styleSheets.length; i++) {
199 var match = [].find.call(document.styleSheets[i].cssRules, function (rule) {
200 return rule.type == CSSRule.KEYFRAMES_RULE && rule.name == name;
209 // Checks if off-main thread animation (OMTA) is available, and if it is, runs
210 // the provided callback function. If OMTA is not available or is not
211 // functioning correctly, the second callback, aOnSkip, is run instead.
213 // This function also does an internal test to verify that OMTA is working at
214 // all so that if OMTA is not functioning correctly when it is expected to
215 // function only a single failure is produced.
217 // Since this function relies on various asynchronous operations, the caller is
218 // responsible for calling SimpleTest.waitForExplicitFinish() before calling
219 // this and SimpleTest.finish() within aTestFunction and aOnSkip.
221 // specialPowersForPrefs exists because some SpecialPowers objects apparently
222 // can get prefs and some can't; callers that would normally have one of the
223 // latter but can get their hands on one of the former can pass it in
225 function runOMTATest(aTestFunction, aOnSkip, specialPowersForPrefs) {
226 const OMTAPrefKey = "layers.offmainthreadcomposition.async-animations";
227 var utils = SpecialPowers.DOMWindowUtils;
228 if (!specialPowersForPrefs) {
229 specialPowersForPrefs = SpecialPowers;
232 utils.layerManagerRemote &&
233 // ^ Off-main thread animation cannot be used if off-main
234 // thread composition (OMTC) is not available
235 specialPowersForPrefs.getBoolPref(OMTAPrefKey);
238 .then(function (isWorking) {
243 // We only call this when we know it will fail as otherwise in the
244 // regular success case we will end up inflating the "passed tests"
246 ok(isWorking, "OMTA should work");
252 "OMTA should ideally work, though we don't expect it to work on " +
253 "this platform/configuration"
258 .catch(function (err) {
263 function isOMTAWorking() {
264 // Create keyframes rule
265 const animationName = "a6ce3091ed85"; // Random name to avoid clashes
269 " { from { opacity: 0.5 } to { opacity: 0.5 } }";
270 var style = document.createElement("style");
271 style.appendChild(document.createTextNode(ruleText));
272 document.head.appendChild(style);
274 // Create animation target
275 var div = document.createElement("div");
276 document.body.appendChild(div);
278 // Give the target geometry so it is eligible for layerization
279 div.style.width = "100px";
280 div.style.height = "100px";
281 div.style.backgroundColor = "white";
283 // Common clean up code
284 var cleanUp = function () {
287 if (utils.isTestControllingRefreshes) {
288 utils.restoreNormalRefresh();
292 return waitForDocumentLoad()
293 .then(loadPaintListener)
295 // Put refresh driver under test control and flush all pending style,
296 // layout and paint to avoid the situation that waitForPaintsFlush()
297 // receives unexpected MozAfterpaint event for those pending
299 utils.advanceTimeAndRefresh(0);
300 return waitForPaintsFlushed();
303 div.style.animation = animationName + " 10s";
305 return waitForPaintsFlushed();
308 var opacity = utils.getOMTAStyle(div, "opacity");
310 return Promise.resolve(opacity == 0.5);
312 .catch(function (err) {
314 return Promise.reject(err);
318 function waitForDocumentLoad() {
319 return new Promise(function (resolve, reject) {
320 if (document.readyState === "complete") {
323 window.addEventListener("load", resolve);
328 function loadPaintListener() {
329 return new Promise(function (resolve, reject) {
330 if (typeof window.waitForAllPaints !== "function") {
331 var script = document.createElement("script");
332 script.onload = resolve;
333 script.onerror = function () {
334 reject(new Error("Failed to load paint listener"));
336 script.src = "/tests/SimpleTest/paint_listener.js";
337 var firstScript = document.scripts[0];
338 firstScript.parentNode.insertBefore(script, firstScript);
346 // Common architecture for setting up a series of asynchronous animation tests
350 // addAsyncAnimTest(function *() {
352 // yield functionThatReturnsAPromise();
355 // runAllAsyncAnimTests().then(SimpleTest.finish());
360 window.addAsyncAnimTest = function (generator) {
361 tests.push(generator);
364 // Returns a promise when all tests have run
365 window.runAllAsyncAnimTests = function (aOnAbort) {
366 // runAsyncAnimTest returns a Promise that is resolved when the
367 // test is finished so we can chain them together
368 return tests.reduce(function (sequence, test) {
369 return sequence.then(function () {
370 return runAsyncAnimTest(test, aOnAbort);
372 }, Promise.resolve() /* the start of the sequence */);
375 // Takes a generator function that represents a test case. Each point in the
376 // test case that waits asynchronously for some result yields a Promise that
377 // is resolved when the asynchronous action has completed. By chaining these
378 // intermediate results together we run the test to completion.
380 // This method itself returns a Promise that is resolved when the generator
381 // function has completed.
383 // This arrangement is based on add_task() which is currently only available
384 // in mochitest-chrome (bug 872229). If add_task becomes available in
385 // mochitest-plain, we can remove this function and use add_task instead.
386 function runAsyncAnimTest(aTestFunc, aOnAbort) {
392 next = generator.next(arg);
394 return Promise.reject(e);
397 return Promise.resolve(next.value);
399 return Promise.resolve(next.value).then(step, function (err) {
404 // Put refresh driver under test control
405 SpecialPowers.DOMWindowUtils.advanceTimeAndRefresh(0);
408 var promise = aTestFunc();
414 .catch(function (err) {
415 ok(false, err.message);
416 if (typeof aOnAbort == "function") {
422 SpecialPowers.DOMWindowUtils.restoreNormalRefresh();
427 //----------------------------------------------------------------------
429 // Helper functions for testing animated values on the compositor
431 //----------------------------------------------------------------------
441 const ExpectComparisonTo = {
447 window.omta_todo_is = function (
455 return omta_is_approx(
462 ExpectComparisonTo.Fail,
467 window.omta_is = function (
475 return omta_is_approx(
482 ExpectComparisonTo.Pass,
487 // Many callers of this method will pass 'undefined' for
488 // expectedComparisonResult.
489 window.omta_is_approx = function (
496 expectedComparisonResult,
500 // FIXME: Auto generate this array.
501 const omtaProperties = [
514 if (!omtaProperties.includes(property)) {
515 ok(false, property + " is not an OMTA property");
520 var normalizedToString = JSON.stringify;
523 case "offset-distance":
524 case "offset-rotate":
525 case "offset-anchor":
526 case "offset-position":
530 if (runningOn == RunningOn.MainThread) {
531 normalize = value => value;
532 compare = function (a, b, error) {
539 normalize = convertTo3dMatrix;
540 compare = matricesRoughlyEqual;
541 normalizedToString = convert3dMatrixToString;
544 normalize = parseFloat;
545 compare = function (a, b, error) {
546 return Math.abs(a - b) <= error;
550 normalize = value => value;
551 compare = function (a, b, error) {
557 if (!!expected.compositorValue) {
558 const originalNormalize = normalize;
560 !!value.compositorValue
561 ? originalNormalize(value.compositorValue)
562 : originalNormalize(value);
566 var compositorStr = SpecialPowers.DOMWindowUtils.getOMTAStyle(
571 var computedStr = window.getComputedStyle(elem, pseudo)[property];
573 // Prepare expected value
574 var expectedValue = normalize(expected);
575 if (expectedValue === null) {
579 ": test author should provide a valid 'expected' value" +
586 // Check expected value appears in the right place
589 case RunningOn.Either:
591 compositorStr !== "" ? RunningOn.Compositor : RunningOn.MainThread;
592 actualStr = compositorStr !== "" ? compositorStr : computedStr;
595 case RunningOn.Compositor:
596 if (compositorStr === "") {
597 ok(false, desc + ": should be animating on compositor");
600 actualStr = compositorStr;
603 case RunningOn.TodoMainThread:
605 compositorStr === "",
606 desc + ": should NOT be animating on compositor"
608 actualStr = compositorStr === "" ? computedStr : compositorStr;
611 case RunningOn.TodoCompositor:
613 compositorStr !== "",
614 desc + ": should be animating on compositor"
616 actualStr = compositorStr !== "" ? computedStr : compositorStr;
620 if (compositorStr !== "") {
621 ok(false, desc + ": should NOT be animating on compositor");
624 actualStr = computedStr;
629 expectedComparisonResult == ExpectComparisonTo.Fail ? todo : ok;
631 // Compare animated value with expected
632 var actualValue = normalize(actualStr);
633 // Note: the actualStr should be empty string when using todoCompositor, so
634 // actualValue is null in this case. However, compare() should handle null
637 compare(expectedValue, actualValue, tolerance),
642 normalizedToString(expectedValue)
645 // For transform-like properties, if we have multiple transform-like
646 // properties, the OMTA value and getComputedStyle() must be different,
647 // so use this flag to skip the following tests.
648 // FIXME: Putting this property on the expected value is a little bit odd.
649 // It's not really a product of the expected value, but rather the kind of
650 // test we're running. That said, the omta_is, omta_todo_is etc. methods are
651 // already pretty complex and adding another parameter would probably
652 // complicate things too much so this is fine for now. If we extend these
653 // functions any more, though, we should probably reconsider this API.
654 if (expected.usesMultipleProperties) {
658 if (typeof expected.computed !== "undefined") {
659 // For some tests we specify a separate computed value for comparing
660 // with getComputedStyle.
662 // In particular, we do this for the individual transform functions since
663 // the form returned from getComputedStyle() reflects the individual
664 // properties (e.g. 'translate: 100px') while the form we read back from
665 // the compositor represents the combined result of all the transform
666 // properties as a single transform matrix (e.g. [0, 0, 0, 0, 100, 0]).
668 // Despite the fact that we can't directly compare the OMTA value against
669 // the getComputedStyle value in this case, it is still worth checking the
670 // result of getComputedStyle since it will help to alert us if some
671 // discrepancy arises between the way we calculate values on the main
672 // thread and compositor.
674 computedStr == expected.computed,
675 desc + ": Computed style should be equal to " + expected.computed
677 } else if (actualStr === compositorStr) {
678 // For compositor animations do an additional check that they match
679 // the value calculated on the main thread
680 var computedValue = normalize(computedStr);
681 if (computedValue === null) {
685 ": test framework should parse computed style" +
692 compare(computedValue, actualValue, 0.0),
694 ": OMTA style and computed style should be equal" +
703 window.matricesRoughlyEqual = function (a, b, tolerance) {
704 // Error handle if a or b is invalid.
709 tolerance = tolerance || 0.00011;
710 for (var i = 0; i < 4; i++) {
711 for (var j = 0; j < 4; j++) {
712 var diff = Math.abs(a[i][j] - b[i][j]);
713 if (diff > tolerance || isNaN(diff)) {
721 // Converts something representing an transform into a 3d matrix in
722 // column-major order.
723 // The following are supported:
727 // { a: 1, ty: 23 } etc.
728 window.convertTo3dMatrix = function (matrixLike) {
729 if (typeof matrixLike == "string") {
730 return convertStringTo3dMatrix(matrixLike);
731 } else if (Array.isArray(matrixLike)) {
732 return convertArrayTo3dMatrix(matrixLike);
733 } else if (typeof matrixLike == "object") {
734 return convertObjectTo3dMatrix(matrixLike);
739 // In future most of these methods should be able to be replaced
741 window.isInvertible = function (matrix) {
742 return getDeterminant(matrix) != 0;
745 // Converts strings of the format "matrix(...)" and "matrix3d(...)" to a 3d
747 function convertStringTo3dMatrix(str) {
749 return convertArrayTo3dMatrix([1, 0, 0, 1, 0, 0]);
751 var result = str.match("^matrix(3d)?\\(");
752 if (result === null) {
756 return convertArrayTo3dMatrix(
758 .substring(result[0].length, str.length - 1)
760 .map(function (component) {
761 return Number(component);
766 // Takes an array of numbers of length 6 (2d matrix) or 16 (3d matrix)
767 // representing a matrix specified in column-major order and returns a 3d
768 // matrix represented as an array of arrays
769 function convertArrayTo3dMatrix(array) {
770 if (array.length == 6) {
771 return convertObjectTo3dMatrix({
779 } else if (array.length == 16) {
790 // Return the first defined value in args.
791 function defined(...args) {
792 return args.find(arg => typeof arg !== "undefined");
795 // Takes an object of the form { a: 1.1, e: 23 } and builds up a 3d matrix
796 // with unspecified values filled in with identity values.
797 function convertObjectTo3dMatrix(obj) {
800 defined(obj.a, obj.sx, obj.m11, 1),
801 obj.b || obj.m12 || 0,
806 obj.c || obj.m21 || 0,
807 defined(obj.d, obj.sy, obj.m22, 1),
811 [obj.m31 || 0, obj.m32 || 0, defined(obj.sz, obj.m33, 1), obj.m34 || 0],
813 obj.e || obj.tx || obj.m41 || 0,
814 obj.f || obj.ty || obj.m42 || 0,
815 obj.tz || obj.m43 || 0,
821 function convert3dMatrixToString(matrix) {
839 .reduce(function (outer, inner) {
840 return outer.concat(inner);
847 function is2d(matrix) {
849 matrix[0][2] === 0 &&
850 matrix[0][3] === 0 &&
851 matrix[1][2] === 0 &&
852 matrix[1][3] === 0 &&
853 matrix[2][0] === 0 &&
854 matrix[2][1] === 0 &&
855 matrix[2][2] === 1 &&
856 matrix[2][3] === 0 &&
857 matrix[3][2] === 0 &&
862 function getDeterminant(matrix) {
864 return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
868 matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] -
869 matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] -
870 matrix[0][3] * matrix[1][1] * matrix[2][2] * matrix[3][0] +
871 matrix[0][1] * matrix[1][3] * matrix[2][2] * matrix[3][0] +
872 matrix[0][2] * matrix[1][1] * matrix[2][3] * matrix[3][0] -
873 matrix[0][1] * matrix[1][2] * matrix[2][3] * matrix[3][0] -
874 matrix[0][3] * matrix[1][2] * matrix[2][0] * matrix[3][1] +
875 matrix[0][2] * matrix[1][3] * matrix[2][0] * matrix[3][1] +
876 matrix[0][3] * matrix[1][0] * matrix[2][2] * matrix[3][1] -
877 matrix[0][0] * matrix[1][3] * matrix[2][2] * matrix[3][1] -
878 matrix[0][2] * matrix[1][0] * matrix[2][3] * matrix[3][1] +
879 matrix[0][0] * matrix[1][2] * matrix[2][3] * matrix[3][1] +
880 matrix[0][3] * matrix[1][1] * matrix[2][0] * matrix[3][2] -
881 matrix[0][1] * matrix[1][3] * matrix[2][0] * matrix[3][2] -
882 matrix[0][3] * matrix[1][0] * matrix[2][1] * matrix[3][2] +
883 matrix[0][0] * matrix[1][3] * matrix[2][1] * matrix[3][2] +
884 matrix[0][1] * matrix[1][0] * matrix[2][3] * matrix[3][2] -
885 matrix[0][0] * matrix[1][1] * matrix[2][3] * matrix[3][2] -
886 matrix[0][2] * matrix[1][1] * matrix[2][0] * matrix[3][3] +
887 matrix[0][1] * matrix[1][2] * matrix[2][0] * matrix[3][3] +
888 matrix[0][2] * matrix[1][0] * matrix[2][1] * matrix[3][3] -
889 matrix[0][0] * matrix[1][2] * matrix[2][1] * matrix[3][3] -
890 matrix[0][1] * matrix[1][0] * matrix[2][2] * matrix[3][3] +
891 matrix[0][0] * matrix[1][1] * matrix[2][2] * matrix[3][3]
896 //----------------------------------------------------------------------
898 // Promise wrappers for paint_listener.js
900 //----------------------------------------------------------------------
902 // Returns a Promise that resolves once all paints have completed
903 function waitForPaints() {
904 return new Promise(function (resolve, reject) {
905 waitForAllPaints(resolve);
909 // As with waitForPaints but also flushes pending style changes before waiting
910 function waitForPaintsFlushed() {
911 return new Promise(function (resolve, reject) {
912 waitForAllPaintsFlushed(resolve);
916 function waitForVisitedLinkColoring(visitedLink, waitProperty, waitValue) {
917 function checkLink(resolve) {
919 SpecialPowers.DOMWindowUtils.getVisitedDependentComputedStyle(
925 // Our link has been styled as visited. Resolve.
928 // Our link is not yet styled as visited. Poll for completion.
929 setTimeout(checkLink, 0, resolve);
932 return new Promise(function (resolve, reject) {