Skip to content

Commit

Permalink
Bug 1773369 [wpt PR 34353] - Implement navigateEvent.intercept() and …
Browse files Browse the repository at this point in the history
…navigateEvent.canIntercept, a=testonly

Automatic update from web-platform-tests
Implement navigateEvent.intercept() and navigateEvent.canIntercept

Follows WICG/navigation-api#235

intercept() works very similarly to transitionWhile(), except that
instead of taking a mandatory Promise, it takes an optional handler
function. If a function is provided and it returns a promise,
navigation finish will be delayed until the Promise resolve, just as
transitionWhile() delays navigation finish for its Promise.

canIntercept is identical to canTransition.

Intent to ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/jyWqjAEv5LU

Bug: 1336000
Change-Id: I94edd7fdc727080594f16fe4511cb7c302d88941
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3688177
Reviewed-by: Chris Harrelson <chrishtr@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1026325}

--

wpt-commits: 5f0779f40dbb30349778db64db04754df8c4a4ef
wpt-pr: 34353
  • Loading branch information
natechapin authored and moz-wptsync-bot committed Jul 22, 2022
1 parent 662aa16 commit fc73c28
Show file tree
Hide file tree
Showing 136 changed files with 492 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
assert_equals(e.navigationType, "push");
assert_equals(i.contentWindow.navigation.currentEntry.index, 1);
});
i.contentWindow.navigation.onnavigate = e => e.transitionWhile(Promise.resolve());
i.contentWindow.navigation.onnavigate = e => e.intercept();
let result = i.contentWindow.navigation.navigate("/common/blank.html?1");
assert_true(oncurrententrychange_called);
await result.committed;
}, "currententrychange fires for navigation.navigate() intercepted by transitionWhile()");
}, "currententrychange fires for navigation.navigate() intercepted by intercept()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
assert_equals(e.navigationType, "replace");
assert_equals(i.contentWindow.navigation.currentEntry.index, 0);
});
i.contentWindow.navigation.onnavigate = e => e.transitionWhile(Promise.resolve());
i.contentWindow.navigation.onnavigate = e => e.intercept();
let result = i.contentWindow.navigation.navigate("/common/blank.html?1", { history: "replace" });
assert_true(oncurrententrychange_called);
await result.committed;
}, "currententrychange fires for navigation.navigate() with replace intercepted by transitionWhile()");
}, "currententrychange fires for navigation.navigate() with replace intercepted by intercept()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
assert_equals(e.navigationType, "reload");
assert_equals(i.contentWindow.navigation.currentEntry.index, 0);
});
i.contentWindow.navigation.onnavigate = e => e.transitionWhile(Promise.resolve());
i.contentWindow.navigation.onnavigate = e => e.intercept();
let result = i.contentWindow.navigation.reload();
assert_true(oncurrententrychange_called);
await result.committed;
}, "currententrychange fires for navigation.reload() intercepted by transitionWhile()");
}, "currententrychange fires for navigation.reload() intercepted by intercept()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -44,7 +44,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -65,7 +65,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -88,7 +88,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -111,7 +111,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -134,7 +134,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand All @@ -156,7 +156,7 @@
assert_equals(document.activeElement, decoy, "focus() worked");

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

const { committed, finished } = navigation.navigate("#1");
Expand Down
17 changes: 7 additions & 10 deletions testing/web-platform/tests/navigation-api/focus-reset/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

navigation.addEventListener("navigate", e => {
assert_throws_js(TypeError, () => {
e.transitionWhile(Promise.resolve(), { focusReset: "invalid" });
e.intercept({ focusReset: "invalid" });
});
throwAssertionHappened = true;
}, { once: true });
Expand All @@ -27,40 +27,37 @@

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });
}, "Resets the focus when no focusReset option is provided");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });
}, "Resets the focus when focusReset is explicitly set to undefined");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(new Promise(r => t.step_timeout(r, 5)));
e.intercept({ handler: () => new Promise(r => t.step_timeout(r, 5)) });
}, { once: true });
}, "Resets the focus when no focusReset option is provided (nontrivial fulfilled promise)");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.reject());
e.intercept({ handler: () => Promise.reject() });
}, { once: true });
}, "Resets the focus when no focusReset option is provided (rejected promise)");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(
Promise.resolve(),
{ focusReset: "after-transition" }
);
e.intercept({ focusReset: "after-transition" });
}, { once: true });
}, "Resets the focus when focusReset is explicitly set to 'after-transition'");

testFocusWasNotReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "manual" });
e.intercept({ focusReset: "manual" });
});
}, "Does not reset the focus when focusReset is set to 'manual'");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<body>
<script>
promise_test(async t => {
let transitionWhile_resolve;
let intercept_resolve;
navigation.addEventListener("navigate", e => {
e.transitionWhile(new Promise(resolve => transitionWhile_resolve = resolve),
{ focusReset: "after-transition" });
e.intercept({ handler: () => new Promise(resolve => intercept_resolve = resolve),
focusReset: "after-transition" });
}, { once: true });

const button = document.body.appendChild(document.createElement("button"));
Expand All @@ -27,7 +27,7 @@
button.blur();
assert_equals(document.activeElement, button2, "focus() in blur worked");

transitionWhile_resolve();
intercept_resolve();
await finished;
assert_equals(document.activeElement, button2, "Focus was not reset after the transition");
}, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<body>
<script>
promise_test(async t => {
let transitionWhile_resolve;
let intercept_resolve;
navigation.addEventListener("navigate", e => {
e.transitionWhile(new Promise(resolve => transitionWhile_resolve = resolve),
{ focusReset: "after-transition" });
e.intercept({ handler: () => new Promise(resolve => intercept_resolve = resolve),
focusReset: "after-transition" });
}, { once: true });

const button = document.body.appendChild(document.createElement("button"));
Expand All @@ -28,7 +28,7 @@
button.focus();
assert_equals(document.activeElement, button, "focus() worked");

transitionWhile_resolve();
intercept_resolve();
await finished;
assert_equals(document.activeElement, button, "Focus was not reset after the transition");
}, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<body>
<script>
promise_test(async t => {
let transitionWhile_resolve;
let intercept_resolve;
navigation.addEventListener("navigate", e => {
e.transitionWhile(new Promise(resolve => transitionWhile_resolve = resolve),
{ focusReset: "after-transition" });
e.intercept({ handler: () => new Promise(resolve => intercept_resolve = resolve),
focusReset: "after-transition" });
}, { once: true });

const button = document.body.appendChild(document.createElement("button"));
Expand All @@ -26,7 +26,7 @@
button2.focus();
assert_equals(document.activeElement, button2, "focus() worked");

transitionWhile_resolve();
intercept_resolve();
await finished;
assert_equals(document.activeElement, button2, "Focus was not reset after the transition");
}, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
<body>
<script>
promise_test(async t => {
let transitionWhile_resolve;
let intercept_resolve;
navigation.addEventListener("navigate", e => {
e.transitionWhile(new Promise(resolve => transitionWhile_resolve = resolve), { focusReset: "after-transition" });
e.intercept({ handler: () => new Promise(resolve => intercept_resolve = resolve),
focusReset: "after-transition" });
}, { once: true });

const button = document.body.appendChild(document.createElement("button"));
Expand All @@ -30,7 +31,7 @@
assert_true(onfocus_called);

document.body.onfocus = t.unreached_func("onfocus shouldn't fire a second time due to focus reset");
transitionWhile_resolve();
intercept_resolve();
await finished;
assert_equals(document.activeElement, document.body, "Focus remains on document.body after promise fulfills");
await new Promise(resolve => t.step_timeout(resolve, 10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,61 @@

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "after-transition" });
e.intercept({ focusReset: "after-transition" });
}, { once: true });
}, "(not provided) + after-transition");

testFocusWasNotReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "manual" });
e.intercept({ focusReset: "manual" });
}, { once: true });
}, "(not provided) + manual");

testFocusWasNotReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "after-transition" });
e.intercept({ focusReset: "after-transition" });
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "manual" });
e.intercept({ focusReset: "manual" });
}, { once: true });
}, "after-transition + manual");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "after-transition" });
e.intercept({ focusReset: "after-transition" });
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });
}, "after-transition + (not provided)");

testFocusWasReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "manual" });
e.intercept({ focusReset: "manual" });
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "after-transition" });
e.intercept({ focusReset: "after-transition" });
}, { once: true });
}, "manual + after-transition");

testFocusWasNotReset(t => {
navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve(), { focusReset: "manual" });
e.intercept({ focusReset: "manual" });
}, { once: true });

navigation.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
e.intercept();
}, { once: true });
}, "manual + (not provided)");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_false(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
i.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_false(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_true(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
window.onmessage = t.step_func_done(e => {
assert_equals(e.data.navigationType, "push", "navigationType");
assert_true(e.data.cancelable, "cancelable");
assert_true(e.data.canTransition, "canTransition");
assert_true(e.data.canIntercept, "canIntercept");
assert_false(e.data.userInitiated, "userInitiated");
assert_true(e.data.hashChange, "hashChange");
assert_equals(e.data.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
i.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_true(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_false(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
i.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_false(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "push", "navigationType");
assert_true(e.cancelable, "cancelable");
assert_true(e.canTransition, "canTransition");
assert_true(e.canIntercept, "canIntercept");
assert_false(e.userInitiated, "userInitiated");
assert_true(e.hashChange, "hashChange");
assert_equals(e.formData, null, "formData");
Expand Down
Loading

0 comments on commit fc73c28

Please sign in to comment.