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

WPT: Create function to test BFCache not restored reason #38038

Merged
merged 1 commit into from
Jan 31, 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
7 changes: 5 additions & 2 deletions IndexedDB/back-forward-cache-open-connection.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ promise_test(async t => {
// Create an IndexedDB database with higher version.
await createIndexedDBForTesting(rc2, 'test_idb_2', 2);
await rc2.historyBack();
// The previous page receiving versionchange event should be evicted.
await assert_not_bfcached(rc1);
// The previous page receiving versionchange event should be evicted with the
// correct reason.
// `kIgnoreEventAndEvict` will be reported as "Internal error".
// See `NotRestoredReasonToReportString()`.
await assert_not_bfcached(rc1, ['Internal error']);
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,62 @@ async function assert_implements_bfcache(remoteContextHelper) {
assert_implements_optional(beforeBFCache == true, 'BFCache not supported.');
}

// If the value in window is undefined, this means that the page was reloaded,
// i.e., the page was not restored from BFCache.
// A helper function to assert that the page is not restored from BFCache by
// checking whether the `beforeBFCache` value from `window` is undefined
// due to page reload.
// This function also takes an optional `notRestoredReasons` list which
// indicates the set of expected reasons that make the page not restored.
// If the reasons list is undefined, the check will be skipped. Otherwise
// this check will use the `notRestoredReasons` API, to obtain the reasons
// in a tree structure, and flatten the reasons before making the order-
// insensitive comparison.
// If the API is not available, the function will terminate instead of marking
// the assertion failed.
// Call `prepareForBFCache()` before navigating away to call this function.
async function assert_not_bfcached(remoteContextHelper) {
async function assert_not_bfcached(
remoteContextHelper, notRestoredReasons) {
var beforeBFCache = await getBeforeBFCache(remoteContextHelper);
assert_equals(beforeBFCache, undefined);

// The reason is optional, so skip the remaining test if the
// `notRestoredReasons` is not set.
if (notRestoredReasons === undefined) {
return;
}

let isFeatureEnabled = await remoteContextHelper.executeScript(() => {
return 'notRestoredReasons' in performance.getEntriesByType('navigation')[0];
});

// Return if the `notRestoredReasons` API is not available.
if (!isFeatureEnabled) {
return;
}

let result = await remoteContextHelper.executeScript(() => {
return performance.getEntriesByType('navigation')[0].notRestoredReasons;
});

let expectedNotRestoredReasonsSet = new Set(notRestoredReasons);
let notRestoredReasonsSet = new Set();

// Flatten the reasons from the main frame and all the child frames.
const collectReason = (node) => {
for (let reason of node.reasons) {
notRestoredReasonsSet.add(reason);
}
for (let child of node.children) {
collectReason(child);
}
}
collectReason(result);

assert_equals(notRestoredReasonsSet.length,
expectedNotRestoredReasonsSet.length);

for (let reason of expectedNotRestoredReasonsSet) {
assert_true(notRestoredReasonsSet.has(reason));
}
}

// A helper function that combines the steps of setting window property,
Expand Down