dom/tests/mochitest/localstorage/test_clear_browser_data.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=795134
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <meta charset="utf-8">
michael@0 8 <title>Tests that clearing mozbrowser private data removes the localStorage data</title>
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 11 </head>
michael@0 12 <body>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=795134">Mozilla Bug 795134</a>
michael@0 14 <p id="display"></p>
michael@0 15 <div id="content">
michael@0 16 <iframe src="http://example.com/tests/error404"></iframe>
michael@0 17 </div>
michael@0 18 <pre id="test">
michael@0 19 <script type="application/javascript;version=1.7">
michael@0 20
michael@0 21 /** Test for Bug 795134 **/
michael@0 22
michael@0 23 /*
michael@0 24 * This test will check that localStorage data are correctly deleted when it is
michael@0 25 * requested that a mozbrowser private data are deleted.
michael@0 26 *
michael@0 27 * Here is the big picture of what the test does:
michael@0 28 * 1. Setup permissions and preferences.
michael@0 29 * 2. Install a dummy application and embed it in an iframe.
michael@0 30 * 3. Load a mozbrowser iframe from this application.
michael@0 31 * 4. Fill storages for the app and the mozbrowser iframe.
michael@0 32 * 5. Uninstall the application.
michael@0 33 *
michael@0 34 * Expected result: all localStorage data from the mozbrowser have been deleted
michael@0 35 * but sessionStorage stays untouched such as all non-browser data.
michael@0 36 *
michael@0 37 * This test is asynchronous and methods are called in a reading order.
michael@0 38 * Each method has a deeper explanation of what it does
michael@0 39 */
michael@0 40
michael@0 41 const Ci = Components.interfaces;
michael@0 42 const Cc = Components.classes;
michael@0 43 const Cu = Components.utils;
michael@0 44
michael@0 45 SimpleTest.waitForExplicitFinish();
michael@0 46
michael@0 47 var permManager = Cc["@mozilla.org/permissionmanager;1"]
michael@0 48 .getService(Ci.nsIPermissionManager);
michael@0 49 var appsService = Cc['@mozilla.org/AppsService;1']
michael@0 50 .getService(Ci.nsIAppsService);
michael@0 51
michael@0 52 /**
michael@0 53 * Initialize the |storage| that has been given with "foo" => "bar".
michael@0 54 * Checks that the storage wasn't initialized and checks that the initialization
michael@0 55 * was successful.
michael@0 56 */
michael@0 57 function setupStorage(storage) {
michael@0 58 is(storage.getItem("foo"), null, "no data");
michael@0 59
michael@0 60 storage.setItem("foo", "bar");
michael@0 61 is(storage.getItem("foo"), "bar", "data written");
michael@0 62 }
michael@0 63
michael@0 64 permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage",
michael@0 65 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 66 permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
michael@0 67 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 68
michael@0 69 SimpleTest.registerCleanupFunction(() => {
michael@0 70 gWitnessStorage.localStorage.clear();
michael@0 71 gWitnessStorage.sessionStorage.clear();
michael@0 72
michael@0 73 permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage",
michael@0 74 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 75 permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
michael@0 76 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 77 });
michael@0 78
michael@0 79 // We want to simulate that all apps are launchable, for testing purpose.
michael@0 80 SpecialPowers.setAllAppsLaunchable(true);
michael@0 81
michael@0 82 // URL of the manifest of the app we want to install.
michael@0 83 const gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
michael@0 84 // ID of the installed app.
michael@0 85 var gTestAppId = 0;
michael@0 86 // Cookies currently in the system.
michael@0 87 var gCurrentCookiesCount = 0;
michael@0 88 // Storages from a non-app to make sure we do not remove cookies from everywhere.
michael@0 89 var gWitnessStorage = {};
michael@0 90 // Storages for the app.
michael@0 91 var gAppStorage = {};
michael@0 92 // Storage for a mozbrowser inside the app.
michael@0 93 var gBrowserStorage = {};
michael@0 94
michael@0 95 function runTest() {
michael@0 96 /*
michael@0 97 * We are setuping the witness storage (non-app) and will install the
michael@0 98 * application.
michael@0 99 * When the application is installed, we will insert it in an iframe and wait
michael@0 100 * for the load event. to be fired.
michael@0 101 */
michael@0 102
michael@0 103 gWitnessStorage.localStorage = window.frames[0].localStorage;
michael@0 104 gWitnessStorage.sessionStorage = window.frames[0].sessionStorage;
michael@0 105
michael@0 106 setupStorage(gWitnessStorage.localStorage);
michael@0 107 setupStorage(gWitnessStorage.sessionStorage);
michael@0 108
michael@0 109 navigator.mozApps.install(gManifestURL, null).onsuccess = function() {
michael@0 110 gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL);
michael@0 111
michael@0 112 var frame = document.createElement('iframe');
michael@0 113 frame.setAttribute('mozbrowser', '');
michael@0 114 frame.setAttribute('mozapp', gManifestURL);
michael@0 115 frame.src = 'http://www.example.com/chrome/dom/tests/mochitest/localstorage/frame_clear_browser_data.html';
michael@0 116 frame.name = 'app';
michael@0 117
michael@0 118 frame.addEventListener('load', appFrameLoadEvent);
michael@0 119
michael@0 120 document.body.appendChild(frame);
michael@0 121 };
michael@0 122 }
michael@0 123
michael@0 124 function appFrameLoadEvent() {
michael@0 125 /*
michael@0 126 * The app frame has been loaded. We can now add permissions for the app to
michael@0 127 * create browsers and we will load a page in this browser and wait for the
michael@0 128 * load event.
michael@0 129 */
michael@0 130 permManager.addFromPrincipal(window.frames[1].document.nodePrincipal, "browser",
michael@0 131 Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 132
michael@0 133 var frame = document.createElement('iframe');
michael@0 134 frame.setAttribute('mozbrowser', '');
michael@0 135 frame.src = 'http://example.com/tests/error404_2';
michael@0 136
michael@0 137 frame.addEventListener('load', browserLoadEvent);
michael@0 138
michael@0 139 document.getElementsByName('app')[0].contentDocument.body.appendChild(frame);
michael@0 140 }
michael@0 141
michael@0 142 function browserLoadEvent() {
michael@0 143 /*
michael@0 144 * The browser inside the app has loaded.
michael@0 145 * We can now setup the app and browser storages and request the browser data
michael@0 146 * to be cleared.
michael@0 147 */
michael@0 148
michael@0 149 gAppStorage.localStorage = window.frames[1].localStorage;
michael@0 150 gAppStorage.sessionStorage = window.frames[1].sessionStorage;
michael@0 151
michael@0 152 gBrowserStorage.localStorage = window.frames[1].frames[0].localStorage;
michael@0 153 gBrowserStorage.sessionStorage = window.frames[1].frames[0].sessionStorage;
michael@0 154
michael@0 155 setupStorage(gAppStorage.localStorage);
michael@0 156 setupStorage(gAppStorage.sessionStorage);
michael@0 157 setupStorage(gBrowserStorage.localStorage);
michael@0 158 setupStorage(gBrowserStorage.sessionStorage);
michael@0 159
michael@0 160 frames[1].postMessage("clear", "*");
michael@0 161
michael@0 162 waitForClearBrowserData();
michael@0 163 };
michael@0 164
michael@0 165 function waitForClearBrowserData() {
michael@0 166 SimpleTest.executeSoon(function() {
michael@0 167 if (frames[1].document.getElementsByTagName('done').length == 0) {
michael@0 168 waitForClearBrowserData();
michael@0 169 } else {
michael@0 170 checks();
michael@0 171 }
michael@0 172 });
michael@0 173 }
michael@0 174 function checks() {
michael@0 175 navigator.mozApps.mgmt.getAll().onsuccess = function() {
michael@0 176 for (i in this.result) {
michael@0 177 var app = this.result[i];
michael@0 178 if (app.manifestURL == gManifestURL) {
michael@0 179 is(gBrowserStorage.localStorage.getItem("foo"), null, "localstorage data have been deleted");
michael@0 180 is(gBrowserStorage.sessionStorage.getItem("foo"), "bar", "sessionstorage data have not been deleted");
michael@0 181
michael@0 182 is(gAppStorage.localStorage.getItem("foo"), "bar", "data are still there");
michael@0 183 is(gAppStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
michael@0 184
michael@0 185 is(gWitnessStorage.localStorage.getItem("foo"), "bar", "data are still there");
michael@0 186 is(gWitnessStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
michael@0 187
michael@0 188 // Now we uninstall the app and make sure everything is clean.
michael@0 189 navigator.mozApps.mgmt.uninstall(app).onsuccess = function() {
michael@0 190 SimpleTest.finish();
michael@0 191 };
michael@0 192 }
michael@0 193 }
michael@0 194 };
michael@0 195 }
michael@0 196
michael@0 197 addLoadEvent(() =>
michael@0 198 SpecialPowers.pushPrefEnv({set: [['dom.mozBrowserFramesEnabled', true]]}, () =>
michael@0 199 SpecialPowers.autoConfirmAppInstall(runTest)
michael@0 200 )
michael@0 201 );
michael@0 202
michael@0 203 </script>
michael@0 204 </pre>
michael@0 205 </body>
michael@0 206 </html>

mercurial