Skip to content

Commit

Permalink
WPT: Allow window.onload to contain multiple test()s
Browse files Browse the repository at this point in the history
******************************************************************
*** SHERIFFS: please don't revert this CL if it causes web_tests
              to flake/fail. If that happens, the cause is a bad
              test. Please mark that test as flaky/fail in
              TestExpectations, with a new crbug. Please block the
              new bug against crbug.com/1395228. Thanks!
******************************************************************

Prior to this CL, a test like this:

```
<script>
window.onload = () => {
  test((t) => { ... }, 'test 1');
  test((t) => { ... }, 'test 2');
  test((t) => { ... }, 'test 3');
};
</script>
```

would not run anything after test #1. The issue is that the testharness
immediately adds a window load handler that marks `all_loaded = true`,
and that ends the tests as soon as the first result from the first test
is processed. (The test runner waits for the first test because
`Tests.prototype.all_done()` also waits until `this.tests.length > 0`.)
There were various mitigating corner cases, such as if you started
the list of tests with a promise_test(), that would increment a
counter that kept the rest of the tests alive. Etc.

With this CL, the testharness-added window.onload handler runs a
setTimeout(0), so that `all_loaded` is only set to true after all of
the tests are loaded by any window.onload handler.

This exposed a few tests that should have been failing but were
masked by the lack of test coverage - bugs have been filed for
those. Also, several tests that were working around this via various
means are also cleaned up in this CL. I'm sure there are more of
those.

Bug: 1395228,1395226,1307772
Change-Id: I6f12b5922186af4e1e06808ad23b47ceac68559c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4074305
Reviewed-by: Weizhong Xia <[email protected]>
Auto-Submit: Mason Freed <[email protected]>
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Mason Freed <[email protected]>
Reviewed-by: Xianzhu Wang <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1081558}
  • Loading branch information
mfreed7 authored and chromium-wpt-export-bot committed Dec 9, 2022
1 parent 7d8e7db commit e085ff1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,119 +8,108 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
setup({ explicit_done: true });

var t = async_test("Test that violation report event was fired");
window.addEventListener("securitypolicyviolation", t.step_func_done(function(e) {
assert_equals(e.violatedDirective, "style-src-attr");
}));
window.onload = function() {
try {
runTests();
} finally {
done();
}
window.nodes = document.getElementById('nodes');
window.node1 = document.getElementById('node1');
window.node1.style.background = "yellow";
window.node1.style.color = "red";
window.node2 = document.getElementById('node1').cloneNode(true);
window.node2.id = "node2";
window.node3 = document.getElementById('node3');
window.node3.style.background = "blue";
window.node3.style.color = "green";
window.node4 = document.getElementById('node3').cloneNode(false);
window.node4.id = "node4";
window.node4.innerHTML = "Node #4";
nodes.appendChild(node1);
nodes.appendChild(node2);
nodes.appendChild(node3);
nodes.appendChild(node4);
test(function() {
assert_equals(node1.style.background.match(/yellow/)[0], "yellow")
});
test(function() {
assert_equals(node2.style.background.match(/yellow/)[0], "yellow")
});
test(function() {
assert_equals(node3.style.background.match(/blue/)[0], "blue")
});
test(function() {
assert_equals(node4.style.background.match(/blue/)[0], "blue")
});
test(function() {
assert_equals(node1.style.color, "red")
});
test(function() {
assert_equals(node2.style.color, "red")
});
test(function() {
assert_equals(node3.style.color, "green")
});
test(function() {
assert_equals(node4.style.color, "green")
});
test(function() {
assert_equals(window.getComputedStyle(node1).background, window.getComputedStyle(node2).background)
});
test(function() {
assert_equals(window.getComputedStyle(node3).background, window.getComputedStyle(node4).background)
});
test(function() {
assert_equals(window.getComputedStyle(node1).color, window.getComputedStyle(node2).color)
});
test(function() {
assert_equals(window.getComputedStyle(node3).color, window.getComputedStyle(node4).color)
});
window.ops = document.getElementById('ops');
ops.style.color = 'red';
window.clonedOps = ops.cloneNode(true);
window.violetOps = document.getElementById('violetOps');
violetOps.style.background = 'rgb(238, 130, 238)';
document.getElementsByTagName('body')[0].appendChild(clonedOps);
test(function() {
assert_equals(ops.style.background, "")
});
test(function() {
assert_equals(ops.style.color, "red")
});
test(function() {
assert_equals(clonedOps.style.background, "")
});
test(function() {
assert_equals(violetOps.style.background.match(/rgb\(238, 130, 238\)/)[0], "rgb(238, 130, 238)")
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(ops).background)
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).color, window.getComputedStyle(ops).color)
});
test(function() {
assert_equals(window.getComputedStyle(ops).background, window.getComputedStyle(violetOps).background)
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(violetOps).background)
});
test(function() {
assert_equals(ops.id, "ops")
});
test(function() {
assert_equals(ops.id, clonedOps.id)
});
test(function() {
let el = document.getElementById("svg");
assert_equals(el.getAttribute("style"), "");
el.style.background = violetOps.style.background;
assert_not_equals(el.style.background, "");
let clone = el.cloneNode(true);
assert_equals(el.style.background, clone.style.background)
}, "non-HTML namespace");
};

function runTests() {
window.nodes = document.getElementById('nodes');
window.node1 = document.getElementById('node1');
window.node1.style.background = "yellow";
window.node1.style.color = "red";
window.node2 = document.getElementById('node1').cloneNode(true);
window.node2.id = "node2";
window.node3 = document.getElementById('node3');
window.node3.style.background = "blue";
window.node3.style.color = "green";
window.node4 = document.getElementById('node3').cloneNode(false);
window.node4.id = "node4";
window.node4.innerHTML = "Node #4";
nodes.appendChild(node1);
nodes.appendChild(node2);
nodes.appendChild(node3);
nodes.appendChild(node4);
test(function() {
assert_equals(node1.style.background.match(/yellow/)[0], "yellow")
});
test(function() {
assert_equals(node2.style.background.match(/yellow/)[0], "yellow")
});
test(function() {
assert_equals(node3.style.background.match(/blue/)[0], "blue")
});
test(function() {
assert_equals(node4.style.background.match(/blue/)[0], "blue")
});
test(function() {
assert_equals(node1.style.color, "red")
});
test(function() {
assert_equals(node2.style.color, "red")
});
test(function() {
assert_equals(node3.style.color, "green")
});
test(function() {
assert_equals(node4.style.color, "green")
});
test(function() {
assert_equals(window.getComputedStyle(node1).background, window.getComputedStyle(node2).background)
});
test(function() {
assert_equals(window.getComputedStyle(node3).background, window.getComputedStyle(node4).background)
});
test(function() {
assert_equals(window.getComputedStyle(node1).color, window.getComputedStyle(node2).color)
});
test(function() {
assert_equals(window.getComputedStyle(node3).color, window.getComputedStyle(node4).color)
});
window.ops = document.getElementById('ops');
ops.style.color = 'red';
window.clonedOps = ops.cloneNode(true);
window.violetOps = document.getElementById('violetOps');
violetOps.style.background = 'rgb(238, 130, 238)';
document.getElementsByTagName('body')[0].appendChild(clonedOps);
test(function() {
assert_equals(ops.style.background, "")
});
test(function() {
assert_equals(ops.style.color, "red")
});
test(function() {
assert_equals(clonedOps.style.background, "")
});
test(function() {
assert_equals(violetOps.style.background.match(/rgb\(238, 130, 238\)/)[0], "rgb(238, 130, 238)")
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(ops).background)
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).color, window.getComputedStyle(ops).color)
});
test(function() {
assert_equals(window.getComputedStyle(ops).background, window.getComputedStyle(violetOps).background)
});
test(function() {
assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(violetOps).background)
});
test(function() {
assert_equals(ops.id, "ops")
});
test(function() {
assert_equals(ops.id, clonedOps.id)
});
test(function() {
let el = document.getElementById("svg");
assert_equals(el.getAttribute("style"), "");
el.style.background = violetOps.style.background;
assert_not_equals(el.style.background, "");
let clone = el.cloneNode(true);
assert_equals(el.style.background, clone.style.background)
}, "non-HTML namespace");
}

</script>
</head>

Expand Down
2 changes: 0 additions & 2 deletions css/cssom-view/negativeMargins.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Hello
</div>
<script>
setup({explicit_done:true});
window.onload = function () {
var outer = document.getElementById('outer');
var inner = document.getElementById('inner');
Expand All @@ -26,7 +25,6 @@
[inner, outer, document.body, document.querySelector('html')],
"elementsFromPoint should get sequence [inner, outer, body, html]");
});
done();
};
</script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

<script>
"use strict";
setup({ explicit_done: true });

window.onload = () => {
const target = frames[0];
const origProto = Object.getPrototypeOf(target);
Expand All @@ -33,7 +31,5 @@
testSettingImmutablePrototypeToNewValueOnly(
"Became cross-origin via document.domain", target, origProto,
"the original value from before going cross-origin", { isSameOriginDomain: false });

done();
};
</script>
2 changes: 1 addition & 1 deletion infrastructure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ This directory contains a number of tests to ensure test running
infrastructure is operating correctly:

* The tests in assumptions/ are designed to test UA assumptions
documented in [assumptions.md](/docs/_writing-tests/assumptions.md).
documented in [assumptions.md](/docs/writing-tests/assumptions.md).

* The tests in server/ are designed to test the WPT server configuration

Expand Down
28 changes: 28 additions & 0 deletions infrastructure/expected-fail/window-onload-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
window.onload = () => {
test((t) => {
assert_true(true,'This should pass');
}, 'test 1');
test((t) => {
assert_true(false,'This should FAIL');
}, 'test 2');
test((t) => {
assert_true(false,'This should FAIL');
}, 'test 3');

promise_test(async t => {
assert_true(false,'This should FAIL');
},'promise 1');
promise_test(async t => {
assert_true(false,'This should FAIL');
},'promise 2');
promise_test(async t => {
assert_true(false,'This should FAIL');
},'promise 3');
};
</script>
5 changes: 5 additions & 0 deletions resources/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@
}

on_event(window, 'load', function() {
setTimeout(() => {
this_obj.all_loaded = true;
if (tests.all_done()) {
tests.complete();
}
},0);
});

on_event(window, 'message', function(event) {
Expand Down

0 comments on commit e085ff1

Please sign in to comment.