This patch fixes a bunch of intermittent/perma failures in sanitize-offlineData.js by: - Ignoring localStorage for now. LocalStorage is cleared by sending an observer notification. The flush often happens after several seconds, heavily interfering with our own test or with subsequent tests. We can not reliably wait on the operation to finish. Waiting for "domstorage-test-flushed" after calling Sanitizer.sanitize() fixes the problem, but that notification is intermittently not triggered for other unknown reasons, which makes it not really viable to use. - Creating and checking indexedDB data in the chrome process (using SiteDataTestUtils). - Cleaning up after running the test. - Ignoring a stray NS_ERROR_ABORT that's hard to track down and doesn't seem to do any damage right now. I've also moved the ServiceWorker utility functions into SiteDataTestUtils, which we're planning to use in all other browser tests that handle site data.
134 lines
4.5 KiB
JavaScript
134 lines
4.5 KiB
JavaScript
"use strict";
|
|
|
|
var EXPORTED_SYMBOLS = [
|
|
"SiteDataTestUtils",
|
|
];
|
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
ChromeUtils.import("resource://testing-common/ContentTask.jsm");
|
|
ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
|
|
|
|
const {Sanitizer} = ChromeUtils.import("resource:///modules/Sanitizer.jsm", {});
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "swm",
|
|
"@mozilla.org/serviceworkers/manager;1",
|
|
"nsIServiceWorkerManager");
|
|
|
|
/**
|
|
* This module assists with tasks around testing functionality that shows
|
|
* or clears site data.
|
|
*
|
|
* Please note that you will have to clean up changes made manually, for
|
|
* example using SiteDataTestUtils.clear().
|
|
*/
|
|
var SiteDataTestUtils = {
|
|
|
|
/**
|
|
* Adds a new entry to a dummy indexedDB database for the specified origin.
|
|
*
|
|
* @param {String} origin - the origin of the site to add test data for
|
|
* @param {String} name [optional] - the entry key
|
|
* @param {String} value [optional] - the entry value
|
|
*
|
|
* @returns a Promise that resolves when the data was added successfully.
|
|
*/
|
|
addToIndexedDB(origin, key = "foo", value = "bar") {
|
|
return new Promise(resolve => {
|
|
let uri = Services.io.newURI(origin);
|
|
let principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
|
|
let request = indexedDB.openForPrincipal(principal, "TestDatabase", 1);
|
|
request.onupgradeneeded = function(e) {
|
|
let db = e.target.result;
|
|
db.createObjectStore("TestStore", { keyPath: "id" });
|
|
};
|
|
request.onsuccess = function(e) {
|
|
let db = e.target.result;
|
|
let tx = db.transaction("TestStore", "readwrite");
|
|
let store = tx.objectStore("TestStore");
|
|
tx.oncomplete = resolve;
|
|
store.put({ id: key, description: value});
|
|
};
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Adds a new cookie for the specified origin, with the specified contents.
|
|
* The cookie will be valid for one day.
|
|
*
|
|
* @param {String} origin - the origin of the site to add test data for
|
|
* @param {String} name [optional] - the cookie name
|
|
* @param {String} value [optional] - the cookie value
|
|
*/
|
|
addToCookies(origin, name = "foo", value = "bar") {
|
|
let uri = Services.io.newURI(origin);
|
|
Services.cookies.add(uri.host, uri.pathQueryRef, name, value,
|
|
false, false, false, Date.now() + 24000 * 60 * 60);
|
|
},
|
|
|
|
/**
|
|
* Adds a new serviceworker with the specified path. Note that this
|
|
* method will open a new tab at the domain of the SW path to that effect.
|
|
*
|
|
* @param {String} path - the path to the service worker to add.
|
|
*
|
|
* @returns a Promise that resolves when the service worker was registered
|
|
*/
|
|
addServiceWorker(path) {
|
|
let uri = Services.io.newURI(path);
|
|
// Register a dummy ServiceWorker.
|
|
return BrowserTestUtils.withNewTab(uri.prePath, async function(browser) {
|
|
return ContentTask.spawn(browser, {path}, async ({path: p}) => {
|
|
let r = await content.navigator.serviceWorker.register(p);
|
|
return new Promise(resolve => {
|
|
let worker = r.installing;
|
|
worker.addEventListener("statechange", () => {
|
|
if (worker.state === "installed") {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Checks whether the specified origin has registered ServiceWorkers.
|
|
*
|
|
* @param {String} origin - the origin of the site to check
|
|
*
|
|
* @returns {Boolean} whether or not the site has ServiceWorkers.
|
|
*/
|
|
hasServiceWorkers(origin) {
|
|
let serviceWorkers = swm.getAllRegistrations();
|
|
for (let i = 0; i < serviceWorkers.length; i++) {
|
|
let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
|
|
if (sw.principal.origin == origin) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
},
|
|
|
|
/**
|
|
* Gets the current quota usage for the specified origin.
|
|
*
|
|
* @returns a Promise that resolves to an integer with the total
|
|
* amount of disk usage by a given origin.
|
|
*/
|
|
getQuotaUsage(origin) {
|
|
return new Promise(resolve => {
|
|
let uri = Services.io.newURI(origin);
|
|
let principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
|
|
Services.qms.getUsageForPrincipal(principal, request => resolve(request.result.usage));
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Cleans up all site data.
|
|
*/
|
|
clear() {
|
|
return Sanitizer.sanitize(["cookies", "cache", "siteSettings", "offlineApps"]);
|
|
},
|
|
};
|