gecko-dev/dom/tests/mochitest/localstorage/test_clear_browser_data.html

210 строки
7.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=795134
-->
<head>
<meta charset="utf-8">
<title>Tests that clearing mozbrowser private data removes the localStorage data</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=795134">Mozilla Bug 795134</a>
<p id="display"></p>
<div id="content">
<iframe src="http://example.com/tests/error404"></iframe>
</div>
<pre id="test">
<script type="application/javascript;version=1.7">
/** Test for Bug 795134 **/
/*
* This test will check that localStorage data are correctly deleted when it is
* requested that a mozbrowser private data are deleted.
*
* Here is the big picture of what the test does:
* 1. Setup permissions and preferences.
* 2. Install a dummy application and embed it in an iframe.
* 3. Load a mozbrowser iframe from this application.
* 4. Fill storages for the app and the mozbrowser iframe.
* 5. Uninstall the application.
*
* Expected result: all localStorage data from the mozbrowser have been deleted
* but sessionStorage stays untouched such as all non-browser data.
*
* This test is asynchronous and methods are called in a reading order.
* Each method has a deeper explanation of what it does
*/
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;
SimpleTest.waitForExplicitFinish();
var permManager = Cc["@mozilla.org/permissionmanager;1"]
.getService(Ci.nsIPermissionManager);
var appsService = Cc['@mozilla.org/AppsService;1']
.getService(Ci.nsIAppsService);
/**
* Initialize the |storage| that has been given with "foo" => "bar".
* Checks that the storage wasn't initialized and checks that the initialization
* was successful.
*/
function setupStorage(storage) {
is(storage.getItem("foo"), null, "no data");
storage.setItem("foo", "bar");
is(storage.getItem("foo"), "bar", "data written");
}
permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage",
Ci.nsIPermissionManager.ALLOW_ACTION);
permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
Ci.nsIPermissionManager.ALLOW_ACTION);
SimpleTest.registerCleanupFunction(() => {
gWitnessStorage.localStorage.clear();
gWitnessStorage.sessionStorage.clear();
permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage",
Ci.nsIPermissionManager.ALLOW_ACTION);
permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
Ci.nsIPermissionManager.ALLOW_ACTION);
});
// We want to simulate that all apps are launchable, for testing purpose.
SpecialPowers.setAllAppsLaunchable(true);
// URL of the manifest of the app we want to install.
const gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
// ID of the installed app.
var gTestAppId = 0;
// Cookies currently in the system.
var gCurrentCookiesCount = 0;
// Storages from a non-app to make sure we do not remove cookies from everywhere.
var gWitnessStorage = {};
// Storages for the app.
var gAppStorage = {};
// Storage for a mozbrowser inside the app.
var gBrowserStorage = {};
function runTest() {
/*
* We are setuping the witness storage (non-app) and will install the
* application.
* When the application is installed, we will insert it in an iframe and wait
* for the load event. to be fired.
*/
gWitnessStorage.localStorage = window.frames[0].localStorage;
gWitnessStorage.sessionStorage = window.frames[0].sessionStorage;
setupStorage(gWitnessStorage.localStorage);
setupStorage(gWitnessStorage.sessionStorage);
navigator.mozApps.install(gManifestURL, null).onsuccess = function() {
gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL);
var frame = document.createElement('iframe');
frame.setAttribute('mozbrowser', '');
frame.setAttribute('mozapp', gManifestURL);
frame.src = 'http://www.example.com/chrome/dom/tests/mochitest/localstorage/frame_clear_browser_data.html';
frame.name = 'app';
frame.addEventListener('load', appFrameLoadEvent);
document.body.appendChild(frame);
};
}
function appFrameLoadEvent() {
/*
* The app frame has been loaded. We can now add permissions for the app to
* create browsers and we will load a page in this browser and wait for the
* load event.
*/
permManager.addFromPrincipal(window.frames[1].document.nodePrincipal, "browser",
Ci.nsIPermissionManager.ALLOW_ACTION);
var frame = document.createElement('iframe');
frame.setAttribute('mozbrowser', '');
frame.src = 'http://example.com/tests/error404_2';
frame.addEventListener('load', browserLoadEvent);
document.getElementsByName('app')[0].contentDocument.body.appendChild(frame);
}
function browserLoadEvent() {
/*
* The browser inside the app has loaded.
* We can now setup the app and browser storages and request the browser data
* to be cleared.
*/
gAppStorage.localStorage = window.frames[1].localStorage;
gAppStorage.sessionStorage = window.frames[1].sessionStorage;
gBrowserStorage.localStorage = window.frames[1].frames[0].localStorage;
gBrowserStorage.sessionStorage = window.frames[1].frames[0].sessionStorage;
setupStorage(gAppStorage.localStorage);
setupStorage(gAppStorage.sessionStorage);
setupStorage(gBrowserStorage.localStorage);
setupStorage(gBrowserStorage.sessionStorage);
frames[1].postMessage("clear", "*");
waitForClearBrowserData();
};
function waitForClearBrowserData() {
SimpleTest.executeSoon(function() {
if (frames[1].document.getElementById('status').textContent != 'done') {
waitForClearBrowserData();
} else {
checks();
}
});
}
function checks() {
navigator.mozApps.mgmt.getAll().onsuccess = function() {
for (i in this.result) {
var app = this.result[i];
if (app.manifestURL == gManifestURL) {
is(gBrowserStorage.localStorage.getItem("foo"), null, "localstorage data have been deleted");
is(gBrowserStorage.sessionStorage.getItem("foo"), "bar", "sessionstorage data have not been deleted");
is(gAppStorage.localStorage.getItem("foo"), "bar", "data are still there");
is(gAppStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
is(gWitnessStorage.localStorage.getItem("foo"), "bar", "data are still there");
is(gWitnessStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
// Now we uninstall the app and make sure everything is clean.
navigator.mozApps.mgmt.uninstall(app).onsuccess = function() {
SimpleTest.finish();
};
}
}
};
}
addLoadEvent(() =>
SpecialPowers.pushPrefEnv({set: [['dom.mozBrowserFramesEnabled', true]]}, () =>
SpecialPowers.autoConfirmAppInstall(() =>
SpecialPowers.autoConfirmAppUninstall(runTest)
)
)
);
</script>
</pre>
</body>
</html>