gecko/image/test/mochitest/test_synchronized_animation.html
Kyle Huey 0a62d4d460 Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:

1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).

2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).

3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.

The solve this, this patch does the following:

1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 06:10:46 -07:00

129 lines
3.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=867758
-->
<head>
<title>Test for Bug 867758</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>
<script type="application/javascript" src="imgutils.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=867758">Mozilla Bug 867758</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
/** Test for Bug 867758**/
SimpleTest.waitForExplicitFinish();
const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const gContent = document.getElementById("content");
var gDispatched = false;
var gRanEvent = false;
var gObserver;
var gImg1;
var gImg2;
var gFirstImageLoaded = false;
var gOuter;
var gFinished = false;
var gFirstRequest = null;
function cleanUpAndFinish() {
if (gFinished) {
return;
}
var imgLoadingContent = gImg1.QueryInterface(Ci.nsIImageLoadingContent);
imgLoadingContent.removeObserver(gOuter);
imgLoadingContent = gImg2.QueryInterface(Ci.nsIImageLoadingContent);
imgLoadingContent.removeObserver(gOuter);
SimpleTest.finish();
gFinished = true;
}
function frameUpdate(aRequest) {
if (!gDispatched) {
Promise.resolve().then(function() {
gRanEvent = true;
});
gDispatched = true;
gFirstRequest = aRequest;
} else if (aRequest != gFirstRequest) {
ok(!gRanEvent, "Should not have run event before all frame update events occurred!");
cleanUpAndFinish();
}
}
function failTest() {
ok(false, "timing out after " + FAILURE_TIMEOUT + "ms. ");
cleanUpAndFinish();
}
function waitForLoadAndTest(image) {
return () => {
// Draw the image into a canvas to ensure it's decoded.
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
// Attach the observer.
var imgLoadingContent = image.QueryInterface(Ci.nsIImageLoadingContent);
imgLoadingContent.addObserver(gOuter);
// If the other image already loaded, add both images to the document, which
// begins the real test.
if (gFirstImageLoaded) {
gContent.appendChild(gImg1);
gContent.appendChild(gImg2);
} else {
gFirstImageLoaded = true;
}
};
}
function main() {
gImg1 = new Image();
gImg2 = new Image();
// Create and customize decoder observer
var obs = new ImageDecoderObserverStub();
obs.frameUpdate = frameUpdate;
gOuter = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools).createScriptedObserver(obs);
// We want to test the cold loading behavior, so clear cache in case an
// earlier test got our image in there already.
clearAllImageCaches();
// These are two copies of the same image; hence, they have the same frame rate.
gImg1.src = "animated1.gif";
gImg2.src = "animated2.gif";
// Wait for each image to load.
gImg1.addEventListener('load', waitForLoadAndTest(gImg1));
gImg2.addEventListener('load', waitForLoadAndTest(gImg2));
// In case something goes wrong, fail earlier than mochitest timeout,
// and with more information.
setTimeout(failTest, FAILURE_TIMEOUT);
}
window.onload = main;
</script>
</pre>
</body>
</html>