Skip to content

Commit

Permalink
Clean up Client Hint WPT failures
Browse files Browse the repository at this point in the history
The wpt.fyi test runner does not restart the browser on a failure like
the trybot runners do, so any state left by the tests makes for
flakiness.

Also, accept-ch-change.https.html *actually* wasn't working properly, in
that nothing was running except the initial check

Leaving the cache-related failures for a separate patch

Bug: 1257191
Change-Id: I6a51ad020a6dde748afa9fa1efeeaf4c1ccf8b0d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3213006
Reviewed-by: Mike Taylor <[email protected]>
Commit-Queue: Aaron Tagliaboschi <[email protected]>
Cr-Commit-Position: refs/heads/main@{#932969}
  • Loading branch information
amtunlimited authored and chromium-wpt-export-bot committed Oct 19, 2021
1 parent 691416c commit 207e223
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 52 deletions.
48 changes: 19 additions & 29 deletions client-hints/accept-ch-change.https.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html>
<title>Accept-CH test</title>
<body>
Expand Down Expand Up @@ -25,50 +26,39 @@
// After this, the same is done but with a differet Accept-CH header, to test
// that the preferences change after receiving a different header.

// First, verify the initial state to make sure that the browser does not have
// client hints preferences cached from a previous run of the test.
promise_test(t => {
return fetch("resources/echo-client-hints-received.py").then(r => {
assert_equals(r.status, 200)
// Verify that the browser did not include client hints in the request
// headers when fetching echo_client_hints_received.py.
assert_false(r.headers.has("device-memory-received"), "device-memory-received");
assert_false(r.headers.has("viewport-width-received"), "viewport-width-received");
});
}, "Precondition: Test that the browser does not have client hints preferences cached");
promise_test(async () => {
r = await fetch("resources/echo-client-hints-received.py");

assert_equals(r.status, 200);
// Verify that the browser did not include client hints in the request
// headers when fetching echo_client_hints_received.py.
assert_false(r.headers.has("device-memory-received"), "device-memory-received");
assert_false(r.headers.has("viewport-width-received"), "viewport-width-received");

// Fetching this webpage should cause user-agent to persist client hint
// preferences for the origin.
await open_and_add_load_event("resources/accept-ch.html");

function acceptChLoaded() {
// Open a new window. Verify that the user agent does attach the client hints.
open_and_add_load_event("resources/expect-client-hints-headers.html", changeCh);
}
await open_and_expect_headers("resources/expect-client-hints-headers.html");

function changeCh() {
// Use new window to overwrite client hint preferences, then verify new
// settings have been saved
open_and_add_load_event("resources/accept-ch-different.html", verifyChangeCh);
}
await open_and_add_load_event("resources/accept-ch-different.html");

function verifyChangeCh() {
// Use new window to overwrite client hint preferences, then verify new
// settings have been saved
open_and_add_load_event("resources/expect-client-hints-headers-different.html", clearCh);
}
await open_and_expect_headers("resources/expect-different-client-hints-headers.html");

function clearCh() {
// Use new window to overwrite client hint preferences, then verify new
// settings have been saved
open_and_add_load_event("resources/accept-ch-empty.html", verifyClearCh);
}
await open_and_add_load_event("resources/accept-ch-empty.html");

function verifyClearCh() {
// Use new window to overwrite client hint preferences, then verify new
// settings have been saved
open_and_add_load_event("resources/do-not-expect-client-hints.html", null);
}
await open_and_expect_headers("resources/expect-no-client-hints-headers.html");

// Fetching this webpage should cause user-agent to persist client hint
// preferences for the origin.
open_and_add_load_event("resources/accept-ch.html", acceptChLoaded);
}, "Accept-CH changes based on header");

</script>
</body>
Expand Down
26 changes: 12 additions & 14 deletions client-hints/accept-ch-malformed-header.https.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<html>
<!DOCTYPE html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/open-and-add-load-event.js"></script>

<script>

promise_test(t => {
return fetch("/client-hints/resources/echo-client-hints-received.py").then(r => {
assert_equals(r.status, 200)
// Verify that the browser does not include client hints in the headers
// since Accept-CH is malformed (includes whitespace between attributes
// instead of comma).
assert_false(r.headers.has("device-memory-received"), "device-memory-received");
assert_false(r.headers.has("dpr-received"), "dpr-received");
assert_false(r.headers.has("viewport-width-received"), "viewport-width-received");
assert_false(r.headers.has("rtt-received"), "rtt-received");
assert_false(r.headers.has("downlink-received"), "downlink-received");
assert_false(r.headers.has("ect-received"), "ect-received");
promise_test(async t => {
t.add_cleanup(() => {
return open_and_add_load_event("resources/accept-ch-empty.html");
});
}, "Accept-CH header test");

await open_and_add_load_event("resources/accept-ch-malformed.html");
// Verify that the browser does not include client hints in the headers
// since Accept-CH is malformed (includes whitespace between attributes
// instead of comma).
await open_and_expect_headers("resources/expect-no-client-hints-headers.html");
}, "Accept-CH malformed header test");

</script>

Expand Down
1 change: 0 additions & 1 deletion client-hints/accept-ch-malformed-header.https.html.headers

This file was deleted.

11 changes: 11 additions & 0 deletions client-hints/resources/accept-ch-malformed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<body>

<!-- An empty webpage whose response headers include only the
Accept-CH header. Fetching this webpage should cause
user-agent to persist origin preferences for the client hints
specified in the Accept-CH header.-->

</body>
</html>
2 changes: 2 additions & 0 deletions client-hints/resources/accept-ch-malformed.html.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Accept-CH: device memory
Access-Control-Allow-Origin: *
24 changes: 24 additions & 0 deletions client-hints/resources/expect-no-client-hints-headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<body>
<script>

// This test checks if browser attaches the viewport-width client hint in the
// HTTP request headers.

// resources/echo-client-hints-received.py sets the response headers depending on the set
// of client hints it receives in the request headers.

fetch("../resources/echo-client-hints-received.py").then(r => {
if(r.status == 200 &&
!r.headers.has("viewport-width-received") &&
!r.headers.has("viewport-width-received")) {
window.top.opener.postMessage('PASS', '*');
} else {
window.top.opener.postMessage('FAIL', '*');
}
});

</script>
</body>
</html>
30 changes: 22 additions & 8 deletions client-hints/resources/open-and-add-load-event.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
function open_and_add_load_event(href, func) {
// While not practically possible, opening "blank" first and setting the
// href after allows for the theoretical possibility of registering the event
// after the window is loaded.
let popup_window = window.open("about:blank");
assert_not_equals(popup_window, null, "Popup windows not allowed?");
popup_window.addEventListener('load', func, false);
popup_window.location.href=href;
function open_and_add_load_event(href) {
return new Promise((resolve) => {
// While not practically possible, opening "blank" first and setting the
// href after allows for the theoretical possibility of registering the event
// after the window is loaded.
let popup_window = window.open("/resources/blank.html");
assert_not_equals(popup_window, null, "Popup windows not allowed?");
popup_window.addEventListener('load', resolve, {once: true});
popup_window.location.href = href;
});
}

async function open_and_expect_headers(href) {
let e = await new Promise(resolve => {
let popup_window = window.open("/resources/blank.html");
assert_not_equals(popup_window, null, "Popup windows not allowed?");
window.addEventListener('message', resolve, false);
popup_window.location.href = href;
});

assert_equals(e.data, "PASS");
return e;
}

0 comments on commit 207e223

Please sign in to comment.