-
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
1c4e5c8
commit a9ac8e5
Showing
6 changed files
with
171 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 @@ | ||
<script> | ||
// TODO: assert that the previous page is in BFCache. | ||
window.onload = () => setTimeout(() => history.back(), 100); | ||
</script> |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
<!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( | ||
() => location.href = backUrl, | ||
t.step_func_done((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']); | ||
}) | ||
); | ||
</script> |
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
// This Document is opened using `window.open()` with 'noopener' option by | ||
// the main testharness Window and | ||
// writes the result back to the main Window via `localStorage`. | ||
// This is because the current test runners expect the top-level testharness | ||
// Document is never unloaded in the middle of a test. | ||
|
||
window.prefixedLocalStorage = new PrefixedLocalStorageResource({ | ||
close_on_cleanup: true | ||
}); | ||
|
||
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); | ||
}); | ||
} | ||
} | ||
|
||
function runTest(onStart, onBackNavigated) { | ||
window.addEventListener('load', () => { | ||
if (prefixedLocalStorage.getItem('state') === null) { | ||
prefixedLocalStorage.setItem('state', 'started'); | ||
|
||
// Navigate after this document is fully loaded. | ||
// Calling | ||
// location.href = 'resources/back.html'; | ||
// synchronously here seems to cause `history.back()` on `back.html` to go | ||
// back to the previous page of this page, not this page, on Firefox. | ||
setTimeout(() => { | ||
window.addEventListener('pageshow', (() => { | ||
onBackNavigated( | ||
true, | ||
prefixedLocalStorage.getPushedItems('observedEvents')); | ||
})); | ||
onStart(); | ||
}, 100); | ||
} else { | ||
onBackNavigated( | ||
false, | ||
prefixedLocalStorage.getPushedItems('observedEvents')); | ||
} | ||
}); | ||
} | ||
|
||
const originParam = new URL(location.href).searchParams.get('origin'); | ||
|
||
const origin = | ||
originParam === 'same-origin' ? 'http://{{host}}:{{ports[http][0]}}' : | ||
originParam === 'same-site' ? 'http://{{host}}:{{ports[http][1]}}' : | ||
'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