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