Skip to content

Commit

Permalink
Update tests for subtle ordering issues
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Nov 7, 2016
1 parent 8cffc77 commit 55ec2da
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>document.interactive, document.contentLoaded, and document.loaded when document.open() happens inside DOMContentLoaded</title>
<link rel="author" title="Domenic Denicola" href="mailto:[email protected]">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-document-interactive">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-document-contentloaded">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-document-loaded">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<!-- This is testing https://github.com/whatwg/html/pull/1936#issuecomment-258952129 -->

<iframe src="document-promises-with-document-open-support.html"></iframe>

<script>
"use strict";

async_test(t => {
let contentDoc, interactiveBefore, contentLoadedBefore, loadedBefore;
const fulfilled1 = [];
const fulfilled2 = [];

window.onload = t.step_func(() => {
contentDoc = frames[0].document;

interactiveBefore = contentDoc.interactive;
contentLoadedBefore = contentDoc.contentLoaded;
loadedBefore = contentDoc.loaded;

contentDoc.interactive.then(() => {
fulfilled1.push("interactive");
});
contentDoc.contentLoaded.then(() => {
fulfilled1.push("contentLoaded");
});
contentDoc.loaded.then(() => {
fulfilled1.push("loaded");
});

contentDoc.addEventListener("DOMContentLoaded", t.step_func(() => {
assert_equals(contentDoc.interactive, interactiveBefore,
"document.interactive must not have changed by the time a DOMContentLoaded handler fires");
assert_equals(contentDoc.contentLoaded, contentLoadedBefore,
"document.contentLoaded must not have changed by the time a DOMContentLoaded handler fires");
assert_equals(contentDoc.loaded, loadedBefore,
"document.loaded must not have changed by the time a DOMContentLoaded handler fires");

assert_array_equals(fulfilled1, ["interactive"],
"Only interactive must have been fulfilled by the time the DOMContentLoaded handler fires");

contentDoc.open();

assert_equals(contentDoc.readyState, "loading", "After document.open(), readyState must be loading");

assert_not_equals(contentDoc.interactive, interactiveBefore, "document.open() must reset document.interactive");
assert_not_equals(contentDoc.contentLoaded, contentLoadedBefore,
"document.open() must reset document.contentLoaded");
assert_not_equals(contentDoc.loaded, loadedBefore, "document.open() must reset document.loaded");

contentDoc.interactive.then(() => {
fulfilled2.push("interactive");
});
contentDoc.contentLoaded.then(() => {
fulfilled2.push("contentLoaded");
});
contentDoc.loaded.then(() => {
fulfilled2.push("loaded");
});

t.step_timeout(() => {
assert_array_equals(fulfilled1, ["interactive", "contentLoaded"],
"Of the original promises, 10 ms after document.open(), interactive and contentLoaded must have fulfilled");
assert_array_equals(fulfilled2, [], "None of the new promises should be fulfilled 10 ms after document.open()");

contentDoc.close();
}, 10);
}));
});
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

let onDomContentLoadedFired = false;
let onLoadFired = false;
const readyStateChanges = [];

let interactiveFulfilled = false;
let contentLoadedFulfilled = false;
Expand All @@ -21,6 +22,10 @@
let interactiveAssertions, contentLoadedAssertions, loadedAssertions;

test(() => {
document.addEventListener("readystatechange", () => {
readyStateChanges.push(document.readyState);
});

document.addEventListener("DOMContentLoaded", () => {
onDomContentLoadedFired = true;
});
Expand All @@ -30,7 +35,8 @@
});

// A note on timing:
// The event handler function must execute before the promise fulfillment callback does, because the sequence goes:
// The readystatechange event handler function must execute before the promise fulfillment callback does, because the
// sequence goes:
// - UA code resolves the promise, thus enqueuing a microtask to call the onFulfilled
// - UA code fires an event
// - Eventually this uses Web IDL to invoke the callback
Expand All @@ -42,6 +48,8 @@
interactiveFulfilled = true;

assert_equals(value, undefined, "The document.interactive promise must fulfill with undefined");
assert_array_equals(readyStateChanges, ["interactive"],
"Inside the document.interactive fulfillment handler, the readystatechange event must have fired once already");
assert_equals(document.readyState, "interactive",
"Inside the document.interactive fulfillment handler, readyState must be interactive");
});
Expand All @@ -56,6 +64,8 @@
"Inside the document.contentLoaded fulfillment handler, document.interactive must have already fulfilled");
assert_equals(onDomContentLoadedFired, true,
"Inside the document.contentLoaded fulfillment handler, DOMContentLoaded must have already fired");
assert_array_equals(readyStateChanges, ["interactive"],
"Inside the document.interactive fulfillment handler, the readystatechange event must have fired exactly once");
});

loadedAssertions = document.loaded.then(value => {
Expand All @@ -70,8 +80,10 @@
"Inside the document.loaded fulfillment handler, document.contentLoaded must have already fulfilled");
assert_equals(onDomContentLoadedFired, true,
"Inside the document.loaded fulfillment handler, DOMContentLoaded must have already fired");
assert_equals(onLoadFired, true,
"Inside the document.loaded fulfillment handler, load must have already fired");
assert_equals(onLoadFired, false,
"Inside the document.loaded fulfillment handler, load must not have already fired");
assert_array_equals(readyStateChanges, ["interactive", "complete"],
"Inside the document.interactive fulfillment handler, the readystatechange event must have fired exactly twice");
});
}, "Setup code");

Expand Down

0 comments on commit 55ec2da

Please sign in to comment.