-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BFCache] Basic event tests + helpers
Change-Id: I034f9f5376dc3f9f32ca0b936dbd06e458c9160b
- Loading branch information
1 parent
3d3e869
commit ffbc29e
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
html/browsers/browsing-the-web/back-forward-cache/events.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<script src="/common/PrefixedLocalStorage.js"></script> | ||
<title>Events fired during BFCached back navigation (cross-site)</title> | ||
<script> | ||
const prefixedLocalStorage = new PrefixedLocalStorageTest(); | ||
fetch_tests_from_prefixed_local_storage(prefixedLocalStorage); | ||
window.open(prefixedLocalStorage.url('resources/events.html'), | ||
'_blank', | ||
'noopener'); | ||
</script> |
4 changes: 4 additions & 0 deletions
4
html/browsers/browsing-the-web/back-forward-cache/resources/back.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!DOCTYPE HTML> | ||
<script> | ||
window.onload = () => history.back(); | ||
</script> |
26 changes: 26 additions & 0 deletions
26
html/browsers/browsing-the-web/back-forward-cache/resources/events.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE HTML> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/common/PrefixedLocalStorage.js"></script> | ||
<script src="helper.sub.js"></script> | ||
<script> | ||
startRecordingEvents(['visibilitychange', 'pagehide', 'pageshow', 'load']); | ||
|
||
const t = async_test('Events'); | ||
runTest( | ||
t, | ||
() => location.href = backUrl, | ||
(isBFCached, observedEvents) => { | ||
assert_implements_optional(isBFCached, 'Should be BFCached'); | ||
assert_array_equals(observedEvents, [ | ||
'window.load', | ||
'window.pageshow', | ||
'window.pagehide.persisted', | ||
'document.visibilitychange.hidden', | ||
'window.visibilitychange.hidden', | ||
'document.visibilitychange.visible', | ||
'window.visibilitychange.visible', | ||
'window.pageshow.persisted']); | ||
t.done(); | ||
} | ||
); | ||
</script> |
89 changes: 89 additions & 0 deletions
89
html/browsers/browsing-the-web/back-forward-cache/resources/helper.sub.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// A helper script for simple A->B->A navigation scenarios like: | ||
// 1. Initial navigation to `A.html`. | ||
// 2. Navigation to `B.html`. | ||
// 3. Back navigation to `A.html`, assuming `A.html` is (or is not) in BFCache. | ||
|
||
// This script is loaded from `A.html`. | ||
|
||
// `A.html` should be opened using `PrefixedLocalStorage.url()`, because | ||
// `/common/PrefixedLocalStorage.js` is used to save states across navigations. | ||
|
||
window.prefixedLocalStorage = new PrefixedLocalStorageResource({ | ||
close_on_cleanup: true | ||
}); | ||
|
||
// Starts an A->B->A navigation test. This should be called on `A.html`. | ||
// `onStart()` is called on the initial navigation, which is expected to | ||
// initiate a navigation to a page (`B.html`) that will eventually back | ||
// navigate to `A.html`. | ||
// `onBackNavigated(isBFCached, observedEvents)` is called on back navigation. | ||
// - `isBFCached` indicates whether the back navigation is from BFCache or not, | ||
// based on events fired. | ||
// - `observedEvents` is an array of event labels fired on `A.html`, | ||
// if `startRecordingEvents()` is called. | ||
function runTest(test, onStart, onBackNavigated) { | ||
window.addEventListener('load', () => { | ||
if (prefixedLocalStorage.getItem('state') === null) { | ||
// Initial navigation. | ||
prefixedLocalStorage.setItem('state', 'started'); | ||
|
||
// Call `onStart()` (and thus starting navigation) after this document | ||
// is fully loaded. | ||
// `step_timeout()` is used here because starting the navigation | ||
// synchronously inside the window load event handler seems to | ||
// cause back navigation to this page to fail on Firefox. | ||
test.step_timeout(() => { | ||
window.addEventListener('pageshow', (() => { | ||
// Back navigation, from BFCache. | ||
test.step( | ||
onBackNavigated, | ||
undefined, | ||
true, | ||
prefixedLocalStorage.getPushedItems('observedEvents')); | ||
})); | ||
test.step(onStart); | ||
}, 0); | ||
} else { | ||
// Back navigation, not from BFCache. | ||
test.step( | ||
onBackNavigated, | ||
undefined, | ||
false, | ||
prefixedLocalStorage.getPushedItems('observedEvents')); | ||
} | ||
}); | ||
} | ||
|
||
// Records events fired on `window` and `document`, with names listed in | ||
// `eventNames`. | ||
// The recorded events are stored in localStorage and used later in the | ||
// runTest() callback. | ||
function startRecordingEvents(eventNames) { | ||
window.testObservedEvents = []; | ||
for (const eventName of eventNames) { | ||
window.addEventListener(eventName, event => { | ||
let result = eventName; | ||
if (event.persisted) { | ||
result += '.persisted'; | ||
} | ||
if (eventName === 'visibilitychange') { | ||
result += '.' + document.visibilityState; | ||
} | ||
prefixedLocalStorage.pushItem('observedEvents', 'window.' + result); | ||
}); | ||
document.addEventListener(eventName, () => { | ||
let result = eventName; | ||
if (eventName === 'visibilitychange') { | ||
result += '.' + document.visibilityState; | ||
} | ||
prefixedLocalStorage.pushItem('observedEvents', 'document.' + result); | ||
}); | ||
} | ||
} | ||
|
||
const origin = | ||
'http://{{hosts[alt][www]}}:{{ports[http][0]}}'; // cross-site | ||
|
||
const backUrl = | ||
origin + | ||
'/html/browsers/browsing-the-web/back-forward-cache/resources/back.html'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters