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

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

mercurial