diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /testing/web-platform/tests/navigation-api/commit-behavior | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/navigation-api/commit-behavior')
11 files changed, 477 insertions, 0 deletions
diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-new-navigation-before-commit.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-new-navigation-before-commit.html new file mode 100644 index 0000000000..2d09d40dc9 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-new-navigation-before-commit.html @@ -0,0 +1,25 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<body> +<script> +promise_test(async t => { + navigation.addEventListener("navigate", e => e.intercept({ commit: "after-transition" }), { once: "true" }); + + let navigateerror_called = false; + navigation.onnavigateerror = t.step_func(() => { + navigateerror_called = true; + assert_equals(location.hash, ""); + }); + + let promises_should_not_commit = navigation.navigate("#ShouldNotCommit"); + let promises_fulfilled = navigation.navigate("#1"); + await assertBothRejectDOM(t, promises_should_not_commit, "AbortError"); + await assertBothFulfill(t, promises_fulfilled, navigation.currentEntry); + + assert_equals(location.hash, "#1"); + assert_true(navigateerror_called); +}, "Cancel a { commit: 'after-transition' } navigation before commit() by starting a new navigation"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-push.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-push.html new file mode 100644 index 0000000000..34bdcf7cac --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-push.html @@ -0,0 +1,23 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<script src="resources/after-transition-commit-helpers.js"></script> +<body> +<script> +let tests = [ + { mode: "rejectBeforeCommit", description: "{ commit: 'after-transition' } for a push navigation, reject before commit" }, + { mode: "rejectAfterCommit", description: "{ commit: 'after-transition' } for a push navigation, reject after commit" }, + { mode: "successExplicitCommit", description: "{ commit: 'after-transition' } for a push navigation, explicit commit()" }, + { mode: "successNoExplicitCommit", description: "{ commit: 'after-transition' } for a push navigation, commit when handler resolves" } +]; + +let onload_promise = new Promise(resolve => window.onload = resolve); +for (let test of tests) { + promise_test(async t => { + await onload_promise; + await testAfterTransitionCommit(t, "push", test.mode); + }, test.description); +} +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-reload.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-reload.html new file mode 100644 index 0000000000..203150eb05 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-reload.html @@ -0,0 +1,19 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<script src="resources/after-transition-commit-helpers.js"></script> +<body> +<script> +let tests = [ + { mode: "rejectBeforeCommit", description: "{ commit: 'after-transition' } for a reload navigation, reject before commit" }, + { mode: "rejectAfterCommit", description: "{ commit: 'after-transition' } for a reload navigation, reject after commit" }, + { mode: "successExplicitCommit", description: "{ commit: 'after-transition' } for a reload navigation, explicit commit()" }, + { mode: "successNoExplicitCommit", description: "{ commit: 'after-transition' } for a reload navigation, commit when handler resolves" } +]; + +for (let test of tests) { + promise_test(t => testAfterTransitionCommit(t, "reload", test.mode), test.description); +} +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-replace.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-replace.html new file mode 100644 index 0000000000..2fd48736a3 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-replace.html @@ -0,0 +1,19 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<script src="resources/after-transition-commit-helpers.js"></script> +<body> +<script> +let tests = [ + { mode: "rejectBeforeCommit", description: "{ commit: 'after-transition' } for a replace navigation, reject before commit" }, + { mode: "rejectAfterCommit", description: "{ commit: 'after-transition' } for a replace navigation, reject after commit" }, + { mode: "successExplicitCommit", description: "{ commit: 'after-transition' } for a replace navigation, explicit commit()" }, + { mode: "successNoExplicitCommit", description: "{ commit: 'after-transition' } for a replace navigation, commit when handler resolves" } +]; + +for (let test of tests) { + promise_test(t => testAfterTransitionCommit(t, "replace", test.mode), test.description); +} +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traversal-commit-new-navigation-before-commit.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traversal-commit-new-navigation-before-commit.html new file mode 100644 index 0000000000..9e74e10676 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traversal-commit-new-navigation-before-commit.html @@ -0,0 +1,36 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<body> +<script> +promise_test(async t => { + // Wait for after the load event so that the navigation doesn't get converted + // into a replace navigation. + await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0)); + + await navigation.navigate("#1").finished; + + let navigateerror_called = false; + navigation.onnavigateerror = t.step_func(() => { + navigateerror_called = true; + assert_equals(location.hash, "#1"); + }); + + // Go back and wait for the navigate event to fire. This traversal will be deferred. + let promises_should_not_commit = assertBothRejectDOM(t, navigation.back(), "AbortError"); + navigation.addEventListener("navigate", e => e.intercept({ commit: "after-transition", handler: () => new Promise(r => t.step_timeout(r, 1000)) }), { once: "true" }); + await new Promise(resolve => navigation.addEventListener("navigate", resolve, { once: "true" })); + + // While the traversal is deferred, start a new navigation and commit immediately. + navigation.addEventListener("navigate", e => e.intercept(), { once: "true" }); + let promises_fulfilled = navigation.navigate("#2"); + + await promises_should_not_commit; + await assertBothFulfill(t, promises_fulfilled, navigation.currentEntry); + + assert_equals(location.hash, "#2"); + assert_true(navigateerror_called); +}, "Cancel a { commit: 'after-transition' } traversal before commit() by starting a new navigation"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traverse.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traverse.html new file mode 100644 index 0000000000..d348a218ac --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-traverse.html @@ -0,0 +1,31 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<script src="resources/after-transition-commit-helpers.js"></script> +<body> +<script> +let start_index = navigation.currentEntry.index; + +let tests = [ + { mode: "rejectBeforeCommit", destinationIndex: start_index, description: "{ commit: 'after-transition' } for a traverse navigation, reject before commit" }, + { mode: "rejectAfterCommit", destinationIndex: start_index + 1, description: "{ commit: 'after-transition' } for a traverse navigation, reject after commit" }, + { mode: "successExplicitCommit", destinationIndex: start_index + 2, description: "{ commit: 'after-transition' } for a traverse navigation, explicit commit()" }, + { mode: "successNoExplicitCommit", destinationIndex: start_index + 3, description: "{ commit: 'after-transition' } for a traverse navigation, commit when handler resolves" } +]; + +// Push a bunch of history entries so each test case can target a unique entry. +history.pushState("", "", "#1"); +history.pushState("", "", "#2"); +history.pushState("", "", "#3"); +history.pushState("", "", "#4"); + +let onload_promise = new Promise(resolve => window.onload = resolve); +for (let test of tests) { + promise_test(async t => { + await onload_promise; + await testAfterTransitionCommit(t, "traverse", test.mode, test.destinationIndex); + }, test.description); +} +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-uncancelable.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-uncancelable.html new file mode 100644 index 0000000000..45bde9f090 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-uncancelable.html @@ -0,0 +1,23 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<iframe id="i" src="/common/blank.html"></iframe> +<body> +<script> +promise_test(async t => { + // Wait for after the load event so that the navigation doesn't get converted + // into a replace navigation. + await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0)); + + await i.contentWindow.navigation.navigate("#1").finished; + + i.contentWindow.navigation.onnavigate = t.step_func(e => { + assert_false(e.cancelable); + // intercept() should throw with commit: "after-transition" because e.cancelable is false. + let iframe_constructor = i.contentWindow.DOMException; + assert_throws_dom("InvalidStateError", iframe_constructor, () => e.intercept({ commit: "after-transition" })); + }); + await i.contentWindow.navigation.back().finished; +}, "{ commit: 'after-transition' } for an uncancelable traverse navigation"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-window-stop-before-commit.html b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-window-stop-before-commit.html new file mode 100644 index 0000000000..0f5e57d5b6 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/after-transition-window-stop-before-commit.html @@ -0,0 +1,33 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../navigation-methods/return-value/resources/helpers.js"></script> +<body> +<script> +promise_test(async t => { + // Wait for after the load event because window.stop() hangs the test harness + // if called before the load event. + await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0)); + + navigation.onnavigate = e => { + e.intercept({ commit: "after-transition" }); + }; + + navigation.onnavigatesuccess = t.unreached_func("navigatesuccess must not fire"); + let navigateerror_called = false; + navigation.onnavigateerror = t.step_func(() => { + navigateerror_called = true; + assert_equals(location.hash, ""); + }); + + let promises = navigation.navigate("#ShouldNotCommit"); + assert_equals(location.hash, ""); + + window.stop(); + await assertBothRejectDOM(t, promises, "AbortError"); + + assert_equals(location.hash, ""); + assert_true(navigateerror_called); +}, " { commit: 'after-transition' } with window.stop() before commit"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/commit-throws.html b/testing/web-platform/tests/navigation-api/commit-behavior/commit-throws.html new file mode 100644 index 0000000000..54abdbfd0f --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/commit-throws.html @@ -0,0 +1,95 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<body> +<script> +promise_test(async t => { + navigation.onnavigate = t.step_func(e => { + assert_throws_dom("InvalidStateError", () => e.commit()); + }); + await navigation.navigate("#").finished; +}, "commit() before intercept()"); + +promise_test(async t => { + navigation.onnavigate = t.step_func(e => { + e.intercept({ handler: t.step_func(() => { + assert_throws_dom("InvalidStateError", () => e.commit()); + }) }); + }); + await navigation.navigate("#").finished; +}, "commit() without commit behavior specified"); + +promise_test(async t => { + navigation.onnavigate = t.step_func(e => { + e.intercept({ + handler: t.step_func(() => { + assert_throws_dom("InvalidStateError", () => e.commit()); + }), + commit: "immediate" + }); + }); + await navigation.navigate("#").finished; +}, "commit() with { commit: immediate }"); + +promise_test(async t => { + navigation.onnavigate = t.step_func(e => { + e.intercept({ commit: "after-transition" }); + assert_throws_dom("InvalidStateError", () => e.commit()); + }); + await navigation.navigate("#").finished; +}, "commit() during event dispatch"); + +promise_test(async t => { + let navigate_event; + navigation.onnavigate = t.step_func(e => { + e.intercept({ commit: "after-transition" }); + navigate_event = e; + }); + await navigation.navigate("#").finished; + assert_throws_dom("InvalidStateError", () => navigate_event.commit()); +}, "commit() after finish"); + +promise_test(async t => { + navigation.onnavigate = t.step_func(e => { + e.intercept({ + handler: t.step_func(() => { + e.commit(); + assert_throws_dom("InvalidStateError", () => e.commit()); + }), + commit: "after-transition" + }); + }); + await navigation.navigate("#").finished; +}, "commit() twice"); + +promise_test(async t => { + // We need to grab an NavigationDestination to construct the event. + navigation.onnavigate = t.step_func(e => { + const event = new NavigateEvent("navigate", { + destination: e.destination, + signal: (new AbortController()).signal + }); + + assert_throws_dom("SecurityError", () => event.commit()); + }); + await navigation.navigate("#").finished; +}, "commit() on synthetic NavigateEvent"); + +promise_test(async t => { + let i = document.createElement("iframe"); + i.src = "about:blank"; + document.body.appendChild(i); + i.contentWindow.navigation.onnavigate = t.step_func(e => { + e.intercept({ + handler: t.step_func(() => { + let iframe_constructor = i.contentWindow.DOMException; + i.remove(); + assert_throws_dom("InvalidStateError", iframe_constructor, () => e.commit()); + }), + commit: "after-transition" + }); + }); + i.contentWindow.navigation.navigate("#"); +}, "commit() in detached iframe"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/multiple-intercept.html b/testing/web-platform/tests/navigation-api/commit-behavior/multiple-intercept.html new file mode 100644 index 0000000000..848af6a65d --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/multiple-intercept.html @@ -0,0 +1,83 @@ +<!doctype html> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<body> +<div style="height: 1000px; width: 1000px;"></div> +<div id="frag"></div> +<script> +let i = 0; +async function urlDidChangeImmediately(listener, expected) { + navigation.addEventListener("navigate", listener, { once: true }); + let expected_hash = "#" + ++i; + assert_not_equals(location.hash, expected_hash); + let finished = navigation.navigate(expected_hash).finished; + assert_equals(location.hash === expected_hash, expected); + await finished; + assert_equals(location.hash, expected_hash); +} + +async function testUrlDidChangeImmediately(listener) { + await urlDidChangeImmediately(listener, true); +} + +async function testUrlDidNotChangeImmediately(listener) { + await urlDidChangeImmediately(listener, false); +} + +promise_test(async t => { + await testUrlDidNotChangeImmediately(e => { + e.intercept({ commit: "after-transition" }); + e.intercept({ commit: "after-transition" }); + }); +}, "after-transition + after-transition"); + +promise_test(async t => { + await testUrlDidNotChangeImmediately(e => { + e.intercept({ commit: "after-transition" }); + e.intercept(); + }); +}, "after-transition + (not provided)"); + +promise_test(async t => { + await testUrlDidChangeImmediately(e => { + e.intercept({ commit: "after-transition" }); + e.intercept({ commit: "immediate" }); + }); +}, "after-transition + immediate"); + +promise_test(async t => { + await testUrlDidNotChangeImmediately(e => { + e.intercept({ commit: "immediate" }); + e.intercept({ commit: "after-transition" }); + }); +}, "immediate + after-transition"); + +promise_test(async t => { + await testUrlDidChangeImmediately(e => { + e.intercept({ commit: "immediate" }); + e.intercept(); + }); +}, "immediate + (not provided)"); + +promise_test(async t => { + await testUrlDidChangeImmediately(e => { + e.intercept({ commit: "immediate" }); + e.intercept({ commit: "immediate" }); + }); +}, "immediate + immediate"); + +promise_test(async t => { + await testUrlDidNotChangeImmediately(e => { + e.intercept(); + e.intercept({ commit: "after-transition" }); + }); +}, "(not provided) + after-transition"); + +promise_test(async t => { + await testUrlDidChangeImmediately(e => { + e.intercept(); + e.intercept({ commit: "immediate" }); + }); +}, "(not provided) + immediate"); +</script> +</body> diff --git a/testing/web-platform/tests/navigation-api/commit-behavior/resources/after-transition-commit-helpers.js b/testing/web-platform/tests/navigation-api/commit-behavior/resources/after-transition-commit-helpers.js new file mode 100644 index 0000000000..664e8d7280 --- /dev/null +++ b/testing/web-platform/tests/navigation-api/commit-behavior/resources/after-transition-commit-helpers.js @@ -0,0 +1,90 @@ +window.testAfterTransitionCommit = async (t, navigationType, mode, destinationIndex = 0) => { + let startHash = location.hash; + let destinationHash; + const err = new Error("boo!"); + + let popstate_fired = false; + window.addEventListener("popstate", () => popstate_fired = true, { once : true }); + let navigatesuccess_fired = false; + navigation.addEventListener("navigatesuccess", () => navigatesuccess_fired = true, { once : true }); + let navigateerror_fired = false; + navigation.addEventListener("navigateerror", () => navigateerror_fired = true, { once : true }); + + // mode-specific logic for the navigate event handler + let navigate_helpers = { + rejectBeforeCommit : async (e) => { + return Promise.reject(err); + }, + rejectAfterCommit : async (e) => { + e.commit(); + assert_equals(location.hash, destinationHash, "hash after commit"); + assert_true(popstate_fired, "popstate fired after commit"); + await new Promise(resolve => t.step_timeout(resolve, 0)); + return Promise.reject(err); + }, + successExplicitCommit : async (e) => { + e.commit(); + assert_equals(location.hash, destinationHash, "hash after commit"); + assert_true(popstate_fired, "popstate fired after commit"); + return new Promise(resolve => t.step_timeout(resolve, 0)); + }, + successNoExplicitCommit : async (e) => { + assert_equals(location.hash, startHash, "start has after first async step"); + assert_false(popstate_fired, "popstate fired after first async step"); + await new Promise(resolve => t.step_timeout(resolve, 0)); + assert_equals(location.hash, startHash, "start has after second async step"); + assert_false(popstate_fired, "popstate fired after second async step"); + return new Promise(resolve => t.step_timeout(resolve, 0)); + } + } + + navigation.addEventListener("navigate", e => { + e.intercept({ commit: "after-transition", + handler: t.step_func(async () => { + assert_equals(e.navigationType, navigationType); + assert_equals(location.hash, startHash, "start hash"); + assert_false(popstate_fired, "popstate fired at handler start"); + + await new Promise(resolve => t.step_timeout(resolve, 0)); + assert_equals(location.hash, startHash, "hash after first async step"); + assert_false(popstate_fired, "popstate fired after first async step"); + + return navigate_helpers[mode](e); + })}); + }, { once: true }); + + let promises; + if (navigationType === "push" || navigationType === "replace") { + destinationHash = (startHash === "" ? "#" : startHash) + "a"; + promises = navigation.navigate(destinationHash, { history: navigationType }); + } else if (navigationType === "reload") { + destinationHash = startHash; + promises = navigation.reload(); + } else if (navigationType === "traverse") { + let destinationEntry = navigation.entries()[destinationIndex]; + destinationHash = new URL(destinationEntry.url).hash; + promises = navigation.traverseTo(destinationEntry.key); + } + + if (mode === "rejectBeforeCommit") { + await assertBothRejectExactly(t, promises, err); + assert_equals(location.hash, startHash, "hash after promise resolution"); + assert_false(popstate_fired, "popstate fired after promise resolution"); + assert_false(navigatesuccess_fired, "navigatesuccess fired"); + assert_true(navigateerror_fired, "navigateerror fired"); + } else if (mode === "rejectAfterCommit") { + await promises.committed; + await assertCommittedFulfillsFinishedRejectsExactly(t, promises, navigation.currentEntry, err); + assert_equals(location.hash, destinationHash, "hash after promise resolution"); + assert_true(popstate_fired, "popstate fired after promise resolution"); + assert_false(navigatesuccess_fired, "navigatesuccess fired"); + assert_true(navigateerror_fired, "navigateerror fired"); + } else { + await promises.committed; + await assertBothFulfill(t, promises, navigation.currentEntry); + assert_equals(location.hash, destinationHash, "hash after promise resolution"); + assert_true(popstate_fired, "popstate fired after promise resolution"); + assert_true(navigatesuccess_fired, "navigatesuccess fired"); + assert_false(navigateerror_fired, "navigateerror fired"); + } +} |