dom/tests/mochitest/localstorage/test_clear_browser_data.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/localstorage/test_clear_browser_data.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,206 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=795134
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Tests that clearing mozbrowser private data removes the localStorage data</title>
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=795134">Mozilla Bug 795134</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content">
    1.19 +  <iframe src="http://example.com/tests/error404"></iframe>
    1.20 +</div>
    1.21 +<pre id="test">
    1.22 +<script type="application/javascript;version=1.7">
    1.23 +
    1.24 +/** Test for Bug 795134 **/
    1.25 +
    1.26 +/*
    1.27 + * This test will check that localStorage data are correctly deleted when it is
    1.28 + * requested that a mozbrowser private data are deleted.
    1.29 + *
    1.30 + * Here is the big picture of what the test does:
    1.31 + * 1. Setup permissions and preferences.
    1.32 + * 2. Install a dummy application and embed it in an iframe.
    1.33 + * 3. Load a mozbrowser iframe from this application.
    1.34 + * 4. Fill storages for the app and the mozbrowser iframe.
    1.35 + * 5. Uninstall the application.
    1.36 + *
    1.37 + * Expected result: all localStorage data from the mozbrowser have been deleted
    1.38 + * but sessionStorage stays untouched such as all non-browser data.
    1.39 + *
    1.40 + * This test is asynchronous and methods are called in a reading order.
    1.41 + * Each method has a deeper explanation of what it does
    1.42 + */
    1.43 +
    1.44 +const Ci = Components.interfaces;
    1.45 +const Cc = Components.classes;
    1.46 +const Cu = Components.utils;
    1.47 +
    1.48 +SimpleTest.waitForExplicitFinish();
    1.49 +
    1.50 +var permManager = Cc["@mozilla.org/permissionmanager;1"]
    1.51 +                    .getService(Ci.nsIPermissionManager);
    1.52 +var appsService = Cc['@mozilla.org/AppsService;1']
    1.53 +                    .getService(Ci.nsIAppsService);
    1.54 +
    1.55 +/**
    1.56 + * Initialize the |storage| that has been given with "foo" => "bar".
    1.57 + * Checks that the storage wasn't initialized and checks that the initialization
    1.58 + * was successful.
    1.59 + */
    1.60 +function setupStorage(storage) {
    1.61 +  is(storage.getItem("foo"), null, "no data");
    1.62 +
    1.63 +  storage.setItem("foo", "bar");
    1.64 +  is(storage.getItem("foo"), "bar", "data written");
    1.65 +}
    1.66 +
    1.67 +permManager.addFromPrincipal(window.document.nodePrincipal, "webapps-manage",
    1.68 +                             Ci.nsIPermissionManager.ALLOW_ACTION);
    1.69 +permManager.addFromPrincipal(window.document.nodePrincipal, "browser",
    1.70 +                             Ci.nsIPermissionManager.ALLOW_ACTION);
    1.71 +
    1.72 +SimpleTest.registerCleanupFunction(() => {
    1.73 +  gWitnessStorage.localStorage.clear();
    1.74 +  gWitnessStorage.sessionStorage.clear();
    1.75 +
    1.76 +  permManager.removeFromPrincipal(window.document.nodePrincipal, "webapps-manage",
    1.77 +                                  Ci.nsIPermissionManager.ALLOW_ACTION);
    1.78 +  permManager.removeFromPrincipal(window.document.nodePrincipal, "browser",
    1.79 +                                  Ci.nsIPermissionManager.ALLOW_ACTION);
    1.80 +});
    1.81 +
    1.82 +// We want to simulate that all apps are launchable, for testing purpose.
    1.83 +SpecialPowers.setAllAppsLaunchable(true);
    1.84 +
    1.85 +// URL of the manifest of the app we want to install.
    1.86 +const gManifestURL = "http://www.example.com/chrome/dom/tests/mochitest/webapps/apps/basic.webapp";
    1.87 +// ID of the installed app.
    1.88 +var gTestAppId = 0;
    1.89 +// Cookies currently in the system.
    1.90 +var gCurrentCookiesCount = 0;
    1.91 +// Storages from a non-app to make sure we do not remove cookies from everywhere.
    1.92 +var gWitnessStorage = {};
    1.93 +// Storages for the app.
    1.94 +var gAppStorage = {};
    1.95 +// Storage for a mozbrowser inside the app.
    1.96 +var gBrowserStorage = {};
    1.97 +
    1.98 +function runTest() {
    1.99 +  /*
   1.100 +   * We are setuping the witness storage (non-app) and will install the
   1.101 +   * application.
   1.102 +   * When the application is installed, we will insert it in an iframe and wait
   1.103 +   * for the load event. to be fired.
   1.104 +   */
   1.105 +
   1.106 +  gWitnessStorage.localStorage = window.frames[0].localStorage;
   1.107 +  gWitnessStorage.sessionStorage = window.frames[0].sessionStorage;
   1.108 +
   1.109 +  setupStorage(gWitnessStorage.localStorage);
   1.110 +  setupStorage(gWitnessStorage.sessionStorage);
   1.111 +
   1.112 +  navigator.mozApps.install(gManifestURL, null).onsuccess = function() {
   1.113 +    gTestAppId = appsService.getAppLocalIdByManifestURL(gManifestURL);
   1.114 +
   1.115 +    var frame = document.createElement('iframe');
   1.116 +    frame.setAttribute('mozbrowser', '');
   1.117 +    frame.setAttribute('mozapp', gManifestURL);
   1.118 +    frame.src = 'http://www.example.com/chrome/dom/tests/mochitest/localstorage/frame_clear_browser_data.html';
   1.119 +    frame.name = 'app';
   1.120 +
   1.121 +    frame.addEventListener('load', appFrameLoadEvent);
   1.122 +
   1.123 +    document.body.appendChild(frame);
   1.124 +  };
   1.125 +}
   1.126 +
   1.127 +function appFrameLoadEvent() {
   1.128 +  /*
   1.129 +   * The app frame has been loaded. We can now add permissions for the app to
   1.130 +   * create browsers and we will load a page in this browser and wait for the
   1.131 +   * load event.
   1.132 +   */
   1.133 +  permManager.addFromPrincipal(window.frames[1].document.nodePrincipal, "browser",
   1.134 +                               Ci.nsIPermissionManager.ALLOW_ACTION);
   1.135 +
   1.136 +  var frame = document.createElement('iframe');
   1.137 +  frame.setAttribute('mozbrowser', '');
   1.138 +  frame.src = 'http://example.com/tests/error404_2';
   1.139 +
   1.140 +  frame.addEventListener('load', browserLoadEvent);
   1.141 +
   1.142 +  document.getElementsByName('app')[0].contentDocument.body.appendChild(frame);
   1.143 +}
   1.144 +
   1.145 +function browserLoadEvent() {
   1.146 +  /*
   1.147 +   * The browser inside the app has loaded.
   1.148 +   * We can now setup the app and browser storages and request the browser data
   1.149 +   * to be cleared.
   1.150 +   */
   1.151 +
   1.152 +  gAppStorage.localStorage = window.frames[1].localStorage;
   1.153 +  gAppStorage.sessionStorage = window.frames[1].sessionStorage;
   1.154 +
   1.155 +  gBrowserStorage.localStorage = window.frames[1].frames[0].localStorage;
   1.156 +  gBrowserStorage.sessionStorage = window.frames[1].frames[0].sessionStorage;
   1.157 +
   1.158 +  setupStorage(gAppStorage.localStorage);
   1.159 +  setupStorage(gAppStorage.sessionStorage);
   1.160 +  setupStorage(gBrowserStorage.localStorage);
   1.161 +  setupStorage(gBrowserStorage.sessionStorage);
   1.162 +
   1.163 +  frames[1].postMessage("clear", "*");
   1.164 +
   1.165 +  waitForClearBrowserData();
   1.166 +};
   1.167 +
   1.168 +function waitForClearBrowserData() {
   1.169 +  SimpleTest.executeSoon(function() {
   1.170 +    if (frames[1].document.getElementsByTagName('done').length == 0) {
   1.171 +      waitForClearBrowserData();
   1.172 +    } else {
   1.173 +      checks();
   1.174 +    }
   1.175 +  });
   1.176 +}
   1.177 +function checks() {
   1.178 +  navigator.mozApps.mgmt.getAll().onsuccess = function() {
   1.179 +    for (i in this.result) {
   1.180 +      var app = this.result[i];
   1.181 +      if (app.manifestURL == gManifestURL) {
   1.182 +        is(gBrowserStorage.localStorage.getItem("foo"), null, "localstorage data have been deleted");
   1.183 +        is(gBrowserStorage.sessionStorage.getItem("foo"), "bar", "sessionstorage data have not been deleted");
   1.184 +
   1.185 +        is(gAppStorage.localStorage.getItem("foo"), "bar", "data are still there");
   1.186 +        is(gAppStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
   1.187 +
   1.188 +        is(gWitnessStorage.localStorage.getItem("foo"), "bar", "data are still there");
   1.189 +        is(gWitnessStorage.sessionStorage.getItem("foo"), "bar", "data are still there");
   1.190 +
   1.191 +        // Now we uninstall the app and make sure everything is clean.
   1.192 +        navigator.mozApps.mgmt.uninstall(app).onsuccess = function() {
   1.193 +          SimpleTest.finish();
   1.194 +        };
   1.195 +      }
   1.196 +    }
   1.197 +  };
   1.198 +}
   1.199 +
   1.200 +addLoadEvent(() =>
   1.201 +  SpecialPowers.pushPrefEnv({set: [['dom.mozBrowserFramesEnabled', true]]}, () =>
   1.202 +    SpecialPowers.autoConfirmAppInstall(runTest)
   1.203 +  )
   1.204 +);
   1.205 +
   1.206 +</script>
   1.207 +</pre>
   1.208 +</body>
   1.209 +</html>

mercurial