Skip to content

Commit

Permalink
Implement AppHistoryCurrentChangeEvent
Browse files Browse the repository at this point in the history
Spec/explainer: WICG/navigation-api#171

Bug: 1252532
Change-Id: Ibd8a856fc36b37d5f4095627808a9a0be15de283
  • Loading branch information
natechapin authored and chromium-wpt-export-bot committed Sep 24, 2021
1 parent 567dd16 commit 86b3db7
Show file tree
Hide file tree
Showing 21 changed files with 401 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app-history/current-change-event/currentchange-anchor-href.html
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,34 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
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;
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);
});
let back_result = appHistory.back();
assert_false(oncurrentchange_back_called);
await back_result.committed;
assert_true(oncurrentchange_back_called);

let oncurrentchange_forward_called = 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);
});
let forward_result = appHistory.forward();
assert_false(oncurrentchange_forward_called);
await forward_result.committed;
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,21 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
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,19 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
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,22 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async t => {
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>
18 changes: 18 additions & 0 deletions app-history/current-change-event/currentchange-location-api.html
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");
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>
14 changes: 14 additions & 0 deletions app-history/current-change-event/currentchange-properties.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
async_test(t => {
appHistory.oncurrentchange = t.step_func_done(e => {
assert_equals(e.constructor, AppHistoryCurrentChangeEvent);
assert_false(e.bubbles);
assert_false(e.cancelable);
assert_true(e.isTrusted);
});
location.href = "#1";
}, "AppHistoryCurrentChangeEvent's properties");
</script>
Loading

0 comments on commit 86b3db7

Please sign in to comment.