Skip to content

Commit

Permalink
Bug 1733160 [wpt PR 30951] - Implement AppHistoryCurrentChangeEvent, …
Browse files Browse the repository at this point in the history
…a=testonly

Automatic update from web-platform-tests
Implement AppHistoryCurrentChangeEvent

Spec/explainer: WICG/navigation-api#171

Bug: 1252532
Change-Id: Ibd8a856fc36b37d5f4095627808a9a0be15de283
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3182849
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/main@{#924964}

--

wpt-commits: 161a88adff2a582eb6a00fb556a8dbb33801df8b
wpt-pr: 30951
  • Loading branch information
natechapin authored and moz-wptsync-bot committed Oct 4, 2021
1 parent caa1777 commit 4d20d48
Show file tree
Hide file tree
Showing 22 changed files with 423 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<a id="a" href="#foo"></a>
<script>
test(t => {
let oncurrentchange_called = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, appHistory.entries()[0]);
assert_equals(e.from.index, 0);
assert_equals(e.navigationType, "push");
assert_equals(appHistory.current.index, 1);
});
a.click();
assert_true(oncurrentchange_called);
}, "AppHistoryCurrentChangeEvent fires for link click");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);
i.contentWindow.appHistory.navigate("/common/blank.html?1");
await new Promise(resolve => i.onload = resolve);
assert_equals(i.contentWindow.appHistory.entries().length, 2);

i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire for cross-document navigations");
i.contentWindow.appHistory.back();
await new Promise(resolve => i.onload = resolve);

i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire for cross-document navigations");
i.contentWindow.appHistory.forward();
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire for cross-document appHistory.back() and appHistory.forward()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<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 appHistory.navigate("#foo");
assert_equals(appHistory.entries().length, 2);

let oncurrentchange_back_called = false;
let back_committed = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_back_called = true;
assert_equals(e.from, appHistory.entries()[1]);
assert_equals(e.navigationType, "traverse");
assert_equals(appHistory.current.index, 0);
assert_false(back_committed);
});
let back_result = appHistory.back();
assert_false(oncurrentchange_back_called);
await back_result.committed.then(() => back_committed = true);
assert_true(oncurrentchange_back_called);

let oncurrentchange_forward_called = false;
let forward_committed = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_forward_called = true;
assert_equals(e.from, appHistory.entries()[0]);
assert_equals(e.navigationType, "traverse");
assert_equals(appHistory.current.index, 1);
assert_false(forward_committed);
});
let forward_result = appHistory.forward();
assert_false(oncurrentchange_forward_called);
await forward_result.committed.then(() => forward_committed = true);
assert_true(oncurrentchange_forward_called);
}, "AppHistoryCurrentChangeEvent fires for appHistory.back() and appHistory.forward()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);
i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire for cross-document navigations");
i.contentWindow.appHistory.navigate("/common/blank.html?1");
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire for cross-document appHistory.navigate()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
appHistory.oncurrentchange = t.unreached_func("currentchange should not fire");
appHistory.onnavigate = e => e.preventDefault();
await promise_rejects_dom(t, "AbortError", appHistory.navigate("#foo").committed);
}, "AppHistoryCurrentChangeEvent does not fire when onnavigate preventDefault() is called");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);
i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire for cross-document navigations");
i.contentWindow.appHistory.navigate("/common/blank.html?1", { replace: true });
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire for cross-document appHistory.navigate() with replace");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<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));

let oncurrentchange_called = false;
let original_entry = appHistory.current;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(appHistory.current.index, 0);
});
let result = appHistory.navigate("#foo", { replace: true });
assert_true(oncurrentchange_called);
await result.committed;
}, "AppHistoryCurrentChangeEvent fires for appHistory.navigate() with replace");
</script>
Original file line number Diff line number Diff line change
@@ -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>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);

let oncurrentchange_called = false;
let original_entry = i.contentWindow.appHistory.current;
i.contentWindow.appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(i.contentWindow.appHistory.current.index, 0);
});
i.contentWindow.appHistory.onnavigate = e => e.transitionWhile(Promise.resolve());
let result = i.contentWindow.appHistory.navigate("/common/blank.html?1", { replace: true });
assert_true(oncurrentchange_called);
await result.committed;
}, "AppHistoryCurrentChangeEvent fires for appHistory.navigate() with replace intercepted by transitionWhile");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<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));

let oncurrentchange_called = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, appHistory.entries()[0]);
assert_equals(e.navigationType, "push");
assert_equals(appHistory.current.index, 1);
});
let result = appHistory.navigate("#foo");
assert_true(oncurrentchange_called);
await result.committed;
}, "AppHistoryCurrentChangeEvent fires for appHistory.navigate()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);

let oncurrentchange_called = false;
i.contentWindow.appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, i.contentWindow.appHistory.entries()[0]);
assert_equals(e.navigationType, "push");
assert_equals(i.contentWindow.appHistory.current.index, 1);
});
i.contentWindow.appHistory.onnavigate = e => e.transitionWhile(Promise.resolve());
let result = i.contentWindow.appHistory.navigate("/common/blank.html?1");
assert_true(oncurrentchange_called);
await result.committed;
}, "AppHistoryCurrentChangeEvent fires for appHistory.navigate() intercepted by transitionWhile");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);
i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire for cross-document navigations");
i.contentWindow.appHistory.reload();
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire for cross-document appHistory.reload()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
await new Promise(resolve => window.onload = resolve);

let oncurrentchange_called = false;
i.contentWindow.appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, i.contentWindow.appHistory.current);
assert_equals(e.navigationType, "reload");
assert_equals(i.contentWindow.appHistory.current.index, 0);
});
i.contentWindow.appHistory.onnavigate = e => e.transitionWhile(Promise.resolve());
let result = i.contentWindow.appHistory.reload();
assert_true(oncurrentchange_called);
await result.committed;
}, "AppHistoryCurrentChangeEvent fires for appHistory.reload() intercepted by transitionWhile");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let oncurrentchange_count = 0;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_count++;
assert_equals(e.from, appHistory.current);
assert_equals(e.navigationType, null);
assert_equals(appHistory.current.getState(), "newState");
});
appHistory.updateCurrent({ state: "newState" });
assert_equals(oncurrentchange_count, 1);

// "Updating" the state to the current state should still fire currentchange.
appHistory.updateCurrent({ state: appHistory.current.getState() });
assert_equals(oncurrentchange_count, 2);
}, "AppHistoryCurrentChangeEvent fires for appHistory.updateCurrent()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let oncurrentchange_called = false;
let ondispose_called = false;

let original_entry = appHistory.current;
original_entry.ondispose = t.step_func(() => {
assert_true(oncurrentchange_called);
ondispose_called = true;
});

appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(appHistory.current.index, 0);
});
appHistory.navigate("#foo", { replace: true });
assert_true(oncurrentchange_called);
assert_true(ondispose_called);
}, "Ordering between AppHistoryCurrentChangeEvent and AppHistoryEntry dispose events");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<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 appHistory.navigate("#foo");
assert_equals(appHistory.entries().length, 2);

let oncurrentchange_called = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, appHistory.entries()[1]);
assert_equals(e.navigationType, "traverse");
assert_equals(appHistory.current.index, 0);
});
history.back();
assert_false(oncurrentchange_called);
await new Promise(resolve => window.onpopstate = resolve);
assert_true(oncurrentchange_called);
}, "AppHistoryCurrentChangeEvent fires for history.back()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let oncurrentchange_called = false;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, appHistory.entries()[0]);
assert_equals(e.navigationType, "push");
assert_equals(appHistory.current.index, 1);
});
history.pushState(1, "", "#1");
assert_true(oncurrentchange_called);
}, "AppHistoryCurrentChangeEvent fires for history.pushState()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let oncurrentchange_called = false;
let original_current = appHistory.current;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, original_current);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(appHistory.current.index, 0);
});
history.replaceState(1, "", "#1");
assert_true(oncurrentchange_called);
}, "AppHistoryCurrentChangeEvent fires for history.replaceState()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(t => {
let oncurrentchange_called = false;
let original_entry = appHistory.current;
appHistory.oncurrentchange = t.step_func(e => {
oncurrentchange_called = true;
assert_equals(e.from, original_entry);
assert_equals(e.from.index, -1);
assert_equals(e.navigationType, "replace");
assert_equals(appHistory.current.index, 0);
});
location.hash = "#foo";
assert_true(oncurrentchange_called);
}, "AppHistoryCurrentChangeEvent fires for location API navigations");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire");
history.pushState(1, "", "#1");
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire when navigating away from the initial about:blank");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="i" src="/common/blank.html"></iframe>
<script>
promise_test(async t => {
i.contentWindow.appHistory.oncurrentchange = t.unreached_func("currentchange should not fire");
i.contentWindow.appHistory.navigate("/common/blank.html#1");
await new Promise(resolve => i.onload = resolve);
}, "AppHistoryCurrentChangeEvent does not fire when navigating away from the initial about:blank");
</script>
Loading

0 comments on commit 4d20d48

Please sign in to comment.