Bug 1348223 - Part 1 - Add SiteDataTestUtils.jsm. r=mak

This commit adds a helper module for doing common tasks
related to site data, such as adding dummy data and getting usage.

There are many places that would potentially need to be cleaned
up to use this module instead, but I consider that work (and the
likely try failure fallout) out of scope for this bug.

MozReview-Commit-ID: 5eMDgHhClsO

--HG--
extra : rebase_source : e515a1a12f6a665f6a4b96deec73790540755189
This commit is contained in:
Johann Hofmann 2018-03-22 17:08:20 +01:00
Родитель 960a002f77
Коммит f34fe08cbd
4 изменённых файлов: 87 добавлений и 10 удалений

Просмотреть файл

@ -0,0 +1,78 @@
"use strict";
var EXPORTED_SYMBOLS = [
"SiteDataTestUtils",
];
ChromeUtils.import("resource://gre/modules/Services.jsm");
const {Sanitizer} = ChromeUtils.import("resource:///modules/Sanitizer.jsm", {});
/**
* 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
*
* @returns a Promise that resolves when the data was added successfully.
*/
addToIndexedDB(origin) {
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: performance.now().toString(), description: "IndexedDB Test"});
};
});
},
/**
* Adds a new cookie for the specified origin, with the specified contents.
* The cookie will be valid for one day.
*
* @param {String} name - the cookie name
* @param {String} value - the cookie value
*/
addToCookies(origin, name, value) {
let uri = Services.io.newURI(origin);
Services.cookies.add(uri.host, uri.pathQueryRef, name, value,
false, false, false, Date.now() + 24000 * 60 * 60);
},
/**
* 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"]);
},
};

Просмотреть файл

@ -21,6 +21,10 @@ MOCHITEST_CHROME_MANIFESTS += [
'content/test/chrome/chrome.ini',
]
TESTING_JS_MODULES += [
'content/test/sanitize/SiteDataTestUtils.jsm',
]
BROWSER_CHROME_MANIFESTS += [
'content/test/about/browser.ini',
'content/test/alerts/browser.ini',

Просмотреть файл

@ -30,7 +30,7 @@ async function testClearData(clearSiteData, clearCache) {
// Test the initial states.
let cacheUsage = await SiteDataManager.getCacheSize();
let quotaUsage = await getQuotaUsage(TEST_QUOTA_USAGE_ORIGIN);
let quotaUsage = await SiteDataTestUtils.getQuotaUsage(TEST_QUOTA_USAGE_ORIGIN);
let totalUsage = await SiteDataManager.getTotalUsage();
Assert.greater(cacheUsage, 0, "The cache usage should not be 0");
Assert.greater(quotaUsage, 0, "The quota usage should not be 0");
@ -127,7 +127,7 @@ async function testClearData(clearSiteData, clearCache) {
return usage == 0;
}, "The total usage should be removed");
} else {
quotaUsage = await getQuotaUsage(TEST_QUOTA_USAGE_ORIGIN);
quotaUsage = await SiteDataTestUtils.getQuotaUsage(TEST_QUOTA_USAGE_ORIGIN);
totalUsage = await SiteDataManager.getTotalUsage();
Assert.greater(quotaUsage, 0, "The quota usage should not be 0");
Assert.greater(totalUsage, 0, "The total usage should not be 0");

Просмотреть файл

@ -17,6 +17,9 @@ const { DownloadUtils } = ChromeUtils.import("resource://gre/modules/DownloadUti
const { SiteDataManager } = ChromeUtils.import("resource:///modules/SiteDataManager.jsm", {});
const { OfflineAppCacheHelper } = ChromeUtils.import("resource:///modules/offlineAppCache.jsm", {});
ChromeUtils.defineModuleGetter(this, "SiteDataTestUtils",
"resource://testing-common/SiteDataTestUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "serviceWorkerManager", "@mozilla.org/serviceworkers/manager;1", "nsIServiceWorkerManager");
function promiseSiteDataManagerSitesUpdated() {
@ -187,14 +190,6 @@ const mockSiteDataManager = {
}
};
function getQuotaUsage(origin) {
return new Promise(resolve => {
let uri = NetUtil.newURI(origin);
let principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
Services.qms.getUsageForPrincipal(principal, request => resolve(request.result.usage));
});
}
function promiseCookiesCleared() {
return TestUtils.topicObserved("cookie-changed", (subj, data) => {
return data === "cleared";