dom/tests/mochitest/localstorage/test_app_uninstall.html

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

mercurial