Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make navigation-api/ tests resilient to starting with extra NavigationHistoryEntries #42232

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions navigation-api/commit-behavior/after-transition-traverse.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
<script src="resources/after-transition-commit-helpers.js"></script>
<body>
<script>
let start_index = navigation.currentEntry.index;

let tests = [
{ mode: "rejectBeforeCommit", destinationIndex: 0, description: "{ commit: 'after-transition' } for a traverse navigation, reject before commit" },
{ mode: "rejectAfterCommit", destinationIndex: 1, description: "{ commit: 'after-transition' } for a traverse navigation, reject after commit" },
{ mode: "successExplicitCommit", destinationIndex: 2, description: "{ commit: 'after-transition' } for a traverse navigation, explicit commit()" },
{ mode: "successNoExplicitCommit", destinationIndex: 3, description: "{ commit: 'after-transition' } for a traverse navigation, commit when handler resolves" }
{ 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.
Expand Down
8 changes: 5 additions & 3 deletions navigation-api/currententrychange-event/anchor-click.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<a id="a" href="#foo"></a>
<script>
test(t => {
let start_index = navigation.currentEntry.index;

let oncurrententrychange_called = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, navigation.entries()[0]);
assert_equals(e.from.index, 0);
assert_equals(e.from, navigation.entries()[start_index]);
assert_equals(e.from.index, start_index);
assert_equals(e.navigationType, "push");
assert_equals(navigation.currentEntry.index, 1);
assert_equals(navigation.currentEntry.index, start_index + 1);
});
a.click();
assert_true(oncurrententrychange_called);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
let start_length = navigation.entries().length;
let start_index = navigation.currentEntry.index;

// 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("#foo");
assert_equals(navigation.entries().length, 2);
assert_equals(navigation.entries().length, start_length + 1);

let oncurrententrychange_called = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, navigation.entries()[1]);
assert_equals(e.from, navigation.entries()[start_index + 1]);
assert_equals(e.navigationType, "traverse");
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.currentEntry.index, start_index);
});
history.back();
assert_false(oncurrententrychange_called);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let start_index = navigation.currentEntry.index;

let oncurrententrychange_called = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, navigation.entries()[0]);
assert_equals(e.from, navigation.entries()[start_index]);
assert_equals(e.navigationType, "push");
assert_equals(navigation.currentEntry.index, 1);
assert_equals(navigation.currentEntry.index, start_index + 1);
});
history.pushState(1, "", "#1");
assert_true(oncurrententrychange_called);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let start_index = navigation.currentEntry.index;

let oncurrententrychange_called = false;
let original_currentEntry = navigation.currentEntry;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, original_currentEntry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.currentEntry.index, start_index);
});
history.replaceState(1, "", "#1");
assert_true(oncurrententrychange_called);
Expand Down
4 changes: 3 additions & 1 deletion navigation-api/currententrychange-event/location-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let start_index = navigation.currentEntry.index;

let oncurrententrychange_called = false;
let original_entry = navigation.currentEntry;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.currentEntry.index, start_index);
});
location.hash = "#foo";
assert_true(oncurrententrychange_called);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
let start_length = navigation.entries().length;
let start_index = navigation.currentEntry.index;

// 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("#foo").committed;
assert_equals(navigation.entries().length, 2);
assert_equals(navigation.entries().length, start_length + 1);

let oncurrententrychange_back_called = false;
let back_committed = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_back_called = true;
assert_equals(e.from, navigation.entries()[1]);
assert_equals(e.from, navigation.entries()[start_index + 1]);
assert_equals(e.navigationType, "traverse");
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.currentEntry.index, start_index);
assert_false(back_committed);
});
let back_result = navigation.back();
Expand All @@ -27,9 +30,9 @@
let forward_committed = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_forward_called = true;
assert_equals(e.from, navigation.entries()[0]);
assert_equals(e.from, navigation.entries()[start_index]);
assert_equals(e.navigationType, "traverse");
assert_equals(navigation.currentEntry.index, 1);
assert_equals(navigation.currentEntry.index, start_index + 1);
assert_false(forward_committed);
});
let forward_result = navigation.forward();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
let start_index = navigation.currentEntry.index;

// 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));
Expand All @@ -14,7 +16,7 @@
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.currentEntry.index, start_index);
});
let result = navigation.navigate("#foo", { history: "replace" });
assert_true(oncurrententrychange_called);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
let start_index = navigation.currentEntry.index;

// 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));

let oncurrententrychange_called = false;
navigation.oncurrententrychange = t.step_func(e => {
oncurrententrychange_called = true;
assert_equals(e.from, navigation.entries()[0]);
assert_equals(e.from, navigation.entries()[start_index]);
assert_equals(e.navigationType, "push");
assert_equals(navigation.currentEntry.index, 1);
assert_equals(navigation.currentEntry.index, start_index + 1);
});
let result = navigation.navigate("#foo");
assert_true(oncurrententrychange_called);
Expand Down
34 changes: 18 additions & 16 deletions navigation-api/navigation-history-entry/current-basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
<script src="resources/is_uuid.js"></script>
<script>
test(() => {
let start_length = navigation.entries().length;
let first_entry = navigation.currentEntry;
let start_index = first_entry.index;
assert_not_equals(first_entry, null);
assert_not_equals(first_entry.key, null);
assert_true(isUUID(first_entry.key));
assert_not_equals(first_entry.id, null);
assert_true(isUUID(first_entry.id));
assert_equals(first_entry.url, location.href);
assert_true(first_entry.sameDocument);
assert_equals(navigation.entries().length, 1);
assert_equals(first_entry, navigation.entries()[0]);
assert_equals(navigation.entries().length, start_length);
assert_equals(first_entry, navigation.entries()[start_index]);

history.replaceState(2, "", "#2");
let second_entry = navigation.currentEntry;
Expand All @@ -24,8 +26,8 @@
assert_true(isUUID(second_entry.id));
assert_equals(second_entry.url, location.href);
assert_true(second_entry.sameDocument);
assert_equals(navigation.entries().length, 1);
assert_equals(second_entry, navigation.entries()[0]);
assert_equals(navigation.entries().length, start_length);
assert_equals(second_entry, navigation.entries()[start_index]);

history.pushState(3, "", "#3");
let third_entry = navigation.currentEntry;
Expand All @@ -36,8 +38,8 @@
assert_true(isUUID(third_entry.id));
assert_equals(third_entry.url, location.href);
assert_true(third_entry.sameDocument);
assert_equals(navigation.entries().length, 2);
assert_equals(third_entry, navigation.entries()[1]);
assert_equals(navigation.entries().length, start_length + 1);
assert_equals(third_entry, navigation.entries()[start_index + 1]);

history.pushState(4, "");
let fourth_entry = navigation.currentEntry;
Expand All @@ -48,8 +50,8 @@
assert_true(isUUID(fourth_entry.id));
assert_equals(fourth_entry.url, third_entry.url);
assert_true(fourth_entry.sameDocument);
assert_equals(navigation.entries().length, 3);
assert_equals(fourth_entry, navigation.entries()[2]);
assert_equals(navigation.entries().length, start_length + 2);
assert_equals(fourth_entry, navigation.entries()[start_index + 2]);

history.replaceState(5, "");
let fifth_entry = navigation.currentEntry;
Expand All @@ -60,8 +62,8 @@
assert_true(isUUID(fifth_entry.id));
assert_equals(fifth_entry.url, fourth_entry.url);
assert_true(fifth_entry.sameDocument);
assert_equals(navigation.entries().length, 3);
assert_equals(fifth_entry, navigation.entries()[2]);
assert_equals(navigation.entries().length, start_length + 2);
assert_equals(fifth_entry, navigation.entries()[start_index + 2]);

history.pushState(5, "");
let fifth_entry_after_push = navigation.currentEntry;
Expand All @@ -72,8 +74,8 @@
assert_true(isUUID(fifth_entry_after_push.id));
assert_equals(fifth_entry_after_push.url, fifth_entry.url);
assert_true(fifth_entry_after_push.sameDocument);
assert_equals(navigation.entries().length, 4);
assert_equals(fifth_entry_after_push, navigation.entries()[3]);
assert_equals(navigation.entries().length, start_length + 3);
assert_equals(fifth_entry_after_push, navigation.entries()[start_index + 3]);

history.replaceState(5, "");
let fifth_entry_after_replace = navigation.currentEntry;
Expand All @@ -84,8 +86,8 @@
assert_true(isUUID(fifth_entry_after_replace.id));
assert_equals(fifth_entry_after_replace.url, fifth_entry_after_push.url);
assert_true(fifth_entry_after_replace.sameDocument);
assert_equals(navigation.entries().length, 4);
assert_equals(fifth_entry_after_replace, navigation.entries()[3]);
assert_equals(navigation.entries().length, start_length + 3);
assert_equals(fifth_entry_after_replace, navigation.entries()[start_index + 3]);

location.hash = "6";
let sixth_entry = navigation.currentEntry;
Expand All @@ -96,8 +98,8 @@
assert_true(isUUID(sixth_entry.id));
assert_not_equals(sixth_entry.url, fifth_entry_after_replace.url);
assert_true(sixth_entry.sameDocument);
assert_equals(navigation.entries().length, 4);
assert_equals(sixth_entry, navigation.entries()[3]);
assert_equals(navigation.entries().length, start_length + 3);
assert_equals(sixth_entry, navigation.entries()[start_index + 3]);

navigation.entries().forEach(entry => {
assert_true(isUUID(entry.id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<iframe id="i" src="/common/blank.html"></iframe>
<script>
async_test(t => {
let start_index = navigation.currentEntry.index;
// The navigations in each window should have only added an navigation to
// their own window.
function assertExpectedEntries(entries, expected_url) {
Expand All @@ -25,7 +26,7 @@
window.onload = () => t.step_timeout(t.step_func(() => {
location.hash = "#1";
i.onload = t.step_func_done(() => {
assertExpectedEntries(navigation.entries(), location.href);
assertExpectedEntries(navigation.entries().slice(start_index), location.href);
assertExpectedEntries(i.contentWindow.navigation.entries(), i.contentWindow.location.href);
});
i.contentWindow.location = "/common/blank.html?2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
<script src="/resources/testharnessreport.js"></script>
<script>
async_test(t => {
const first_entry = navigation.entries()[0];
let start_index = navigation.currentEntry.index;
const first_entry = navigation.entries()[start_index];
history.pushState(1, "", "#1");
assert_equals(navigation.entries()[0], first_entry);
assert_equals(navigation.entries()[start_index], first_entry);
history.back();
window.onpopstate = t.step_func_done(() => {
const second_entry = navigation.entries()[1];
const second_entry = navigation.entries()[start_index + 1];
history.replaceState(0, "", "#0");
assert_equals(navigation.entries()[1], second_entry);
assert_equals(navigation.entries()[start_index + 1], second_entry);
});
}, "A non-active entry in navigation.entries() should not be modified when a different entry is modified");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
<iframe id="i" src="/common/blank.html"></iframe>
<script>
async_test(t => {
let start_index = navigation.currentEntry.index;

// Wait for after the load event so that the navigation doesn't get converted
// into a replace navigation.
window.onload = () => t.step_timeout(t.step_func_done(() => {
// Remove the entry by replacing it.
let replaced_entry = navigation.currentEntry;
assert_equals(replaced_entry.index, 0);
assert_equals(replaced_entry.index, start_index);
navigation.navigate("#0", { history: "replace" });
assert_equals(replaced_entry.index, -1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
<iframe id="i" src="resources/key-navigate-back-cross-document-helper.html"></iframe>
<script>
async_test(t => {
let start_length = navigation.entries().length;
window.finish = t.step_func_done((end_key, end_id) => {
assert_equals(window.start_key, end_key);
assert_equals(window.start_id, end_id);
// The new history entry in the iframe should not add any entries to
// this window's navigation.
assert_equals(navigation.entries().length, 1);
assert_equals(navigation.entries().length, start_length);
});
}, "NavigationHistoryEntry's key and id on cross-document back navigation");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
<script src="/resources/testharnessreport.js"></script>
<script>
async_test(t => {
let start_length = navigation.entries().length;
let start_index = navigation.currentEntry.index;
let key = navigation.currentEntry.key;
let id = navigation.currentEntry.id;
assert_equals(navigation.entries().length, 1);

history.pushState("hash", "", "#hash");
assert_not_equals(key, navigation.currentEntry.key);
assert_not_equals(id, navigation.currentEntry.id);
assert_equals(navigation.entries().length, 2);
assert_equals(navigation.currentEntry.index, 1);
assert_equals(navigation.entries().length, start_length + 1);
assert_equals(navigation.currentEntry.index, start_index + 1);

window.onpopstate = t.step_func_done(() => {
assert_equals(key, navigation.currentEntry.key);
assert_equals(id, navigation.currentEntry.id);
assert_equals(navigation.entries().length, 2);
assert_equals(navigation.currentEntry.index, 0);
assert_equals(navigation.entries().length, start_length + 1);
assert_equals(navigation.currentEntry.index, start_index);
});
history.back();
}, "NavigationHistoryEntry's key and id on same-document back navigation");
Expand Down
Loading