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