|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=826058 |
|
5 --> |
|
6 <head> |
|
7 <meta charset="utf-8"> |
|
8 <title>Test for Bug 826058</title> |
|
9 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
11 <script type="application/javascript;version=1.7"> |
|
12 |
|
13 /** Test for Bug 826058 **/ |
|
14 |
|
15 SimpleTest.waitForExplicitFinish(); |
|
16 |
|
17 var gBaseURL = 'http://test/tests/dom/apps/tests/'; |
|
18 var gHostedManifestURL = gBaseURL + 'file_app.sjs?apptype=hosted&getmanifest=true'; |
|
19 var gCachedManifestURL = gBaseURL + 'file_app.sjs?apptype=cached&getmanifest=true'; |
|
20 var gGenerator = runTest(); |
|
21 |
|
22 function go() { |
|
23 SpecialPowers.pushPermissions( |
|
24 [{ "type": "browser", "allow": 1, "context": document }, |
|
25 { "type": "embed-apps", "allow": 1, "context": document }, |
|
26 { "type": "webapps-manage", "allow": 1, "context": document }], |
|
27 function() { gGenerator.next() }); |
|
28 } |
|
29 |
|
30 function continueTest() { |
|
31 try { |
|
32 gGenerator.next(); |
|
33 } catch (e if e instanceof StopIteration) { |
|
34 finish(); |
|
35 } |
|
36 } |
|
37 |
|
38 function mozAppsError() { |
|
39 ok(false, "mozApps error: " + this.error.name); |
|
40 finish(); |
|
41 } |
|
42 |
|
43 function xhrError(event, url) { |
|
44 var xhr = event.target; |
|
45 ok(false, "XHR error loading " + url + ": " + xhr.status + " - " + |
|
46 xhr.statusText); |
|
47 finish(); |
|
48 } |
|
49 |
|
50 function xhrAbort(url) { |
|
51 ok(false, "XHR abort loading " + url); |
|
52 finish(); |
|
53 } |
|
54 |
|
55 function runTest() { |
|
56 // Set up. |
|
57 |
|
58 SpecialPowers.setAllAppsLaunchable(true); |
|
59 SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true); |
|
60 |
|
61 // Test Bug 927699 - navigator.mozApps.install(url) lets NS_ERROR_FAILURE |
|
62 // onto the web |
|
63 var request = navigator.mozApps.install(""); |
|
64 request.onerror = function() { |
|
65 ok(request.error.name == "INVALID_URL", "Got expected INVALID_URL"); |
|
66 continueTest(); |
|
67 }; |
|
68 request.onsuccess = mozAppsError; |
|
69 yield undefined; |
|
70 |
|
71 setAppVersion(1, continueTest); |
|
72 yield undefined; |
|
73 SpecialPowers.autoConfirmAppInstall(continueTest); |
|
74 yield undefined; |
|
75 |
|
76 // Load the app, uninstalled. |
|
77 checkAppState(null, false, 1, continueTest); |
|
78 yield undefined; |
|
79 |
|
80 // Bump the version and install the app. |
|
81 setAppVersion(2, continueTest); |
|
82 yield undefined; |
|
83 |
|
84 request = navigator.mozApps.install(gHostedManifestURL); |
|
85 request.onerror = mozAppsError; |
|
86 request.onsuccess = continueTest; |
|
87 yield undefined; |
|
88 var app = request.result; |
|
89 ok(app, "App is non-null"); |
|
90 ok(app.manifest.description == "Updated even faster than Firefox, just to annoy slashdotters.", |
|
91 "Manifest is HTML-sanitized"); |
|
92 |
|
93 // Check the app a few times. |
|
94 checkAppState(app, true, 2, continueTest); |
|
95 yield undefined; |
|
96 checkAppState(app, true, 2, continueTest); |
|
97 yield undefined; |
|
98 |
|
99 // Bump the version and check the app again. The app is not cached, so the |
|
100 // version bump takes effect. |
|
101 setAppVersion(3, continueTest); |
|
102 yield undefined; |
|
103 checkAppState(app, true, 3, continueTest); |
|
104 yield undefined; |
|
105 |
|
106 // check for update |
|
107 var icons = app.manifest.icons; |
|
108 var oldIcon = icons[Object.keys(icons)[0]]; |
|
109 var oldUpdateTime = app.updateTime; |
|
110 setAppIcon('new_icon', continueTest); |
|
111 yield undefined; |
|
112 |
|
113 app.ondownloadavailable = function() { |
|
114 ok(false, 'Got a downloadavailable event for non-cached hosted apps'); |
|
115 }; |
|
116 |
|
117 app.ondownloadapplied = function() { |
|
118 ok(true, 'Got a downloadapplied when checking for update'); |
|
119 app.ondownloadapplied = app.ondownloadavailable = null; |
|
120 continueTest(); |
|
121 }; |
|
122 request = app.checkForUpdate(); |
|
123 request.onerror = mozAppsError; |
|
124 request.onsuccess = function() { |
|
125 ok(true, "Got onsuccess"); |
|
126 continueTest(); |
|
127 }; |
|
128 yield undefined; |
|
129 yield undefined; |
|
130 |
|
131 icons = app.manifest.icons; |
|
132 var newIcon = icons[Object.keys(icons)[0]]; |
|
133 var newUpdateTime = app.updateTime; |
|
134 isnot(oldIcon, newIcon, 'The icon should be updated'); |
|
135 isnot(oldUpdateTime, newUpdateTime, 'The update time should be updated'); |
|
136 |
|
137 // Uninstall the app. |
|
138 request = navigator.mozApps.mgmt.uninstall(app); |
|
139 request.onerror = mozAppsError; |
|
140 request.onsuccess = continueTest; |
|
141 yield undefined; |
|
142 |
|
143 // Check the uninstalled app. |
|
144 checkAppState(app, false, 3, continueTest); |
|
145 yield undefined; |
|
146 |
|
147 // Install the cached app. |
|
148 setAppVersion(3, continueTest); |
|
149 yield undefined; |
|
150 ok(true, "Installing cached app"); |
|
151 var request = navigator.mozApps.install(gCachedManifestURL); |
|
152 request.onerror = mozAppsError; |
|
153 request.onsuccess = continueTest; |
|
154 yield undefined; |
|
155 var app = request.result; |
|
156 ok(app, "App is non-null"); |
|
157 if (app.installState == "pending") { |
|
158 ok(true, "App is pending. Waiting for progress"); |
|
159 app.onprogress = function() ok(true, "Got download progress"); |
|
160 app.ondownloadsuccess = continueTest; |
|
161 app.ondownloaderror = mozAppsError; |
|
162 yield undefined; |
|
163 } |
|
164 is(app.installState, "installed", "App is installed"); |
|
165 |
|
166 // Check the cached app. |
|
167 checkAppState(app, true, 3, continueTest); |
|
168 yield undefined; |
|
169 |
|
170 // Check for updates. The current infrastructure always returns a new appcache |
|
171 // manifest, so there should always be an update. |
|
172 var lastCheck = app.lastUpdateCheck; |
|
173 ok(true, "Setting callbacks"); |
|
174 app.ondownloadapplied = function() ok(true, "downloadapplied fired."); |
|
175 app.ondownloadavailable = function() ok(false, "downloadavailable fired"); |
|
176 ok(true, "Checking for updates"); |
|
177 var request = app.checkForUpdate(); |
|
178 request.onerror = mozAppsError; |
|
179 request.onsuccess = continueTest; |
|
180 yield undefined; |
|
181 todo(app.lastUpdateCheck > lastCheck, "lastUpdateCheck updated appropriately"); |
|
182 |
|
183 |
|
184 // Uninstall the app. |
|
185 request = navigator.mozApps.mgmt.uninstall(app); |
|
186 request.onerror = mozAppsError; |
|
187 request.onsuccess = continueTest; |
|
188 yield undefined; |
|
189 ok(true, "Uninstalled app"); |
|
190 |
|
191 // Check the uninstalled app. |
|
192 checkAppState(app, false, 3, continueTest); |
|
193 yield undefined; |
|
194 } |
|
195 |
|
196 function setAppVersion(version, cb) { |
|
197 var xhr = new XMLHttpRequest(); |
|
198 var url = gBaseURL + 'file_app.sjs?setVersion=' + version; |
|
199 xhr.addEventListener("load", function() { is(xhr.responseText, "OK", "setAppVersion OK"); cb(); }); |
|
200 xhr.addEventListener("error", event => xhrError(event, url)); |
|
201 xhr.addEventListener("abort", event => xhrAbort(url)); |
|
202 xhr.open('GET', url, true); |
|
203 xhr.send(); |
|
204 } |
|
205 |
|
206 function setAppIcon(icon, cb) { |
|
207 var xhr = new XMLHttpRequest(); |
|
208 var url = gBaseURL + 'file_app.sjs?setIcon=' + icon; |
|
209 xhr.addEventListener("load", function() { is(xhr.responseText, "OK", "setAppIcon OK"); cb(); }); |
|
210 xhr.addEventListener("error", event => xhrError(event, url)); |
|
211 xhr.addEventListener("abort", event => xhrAbort(url)); |
|
212 xhr.open('GET', url, true); |
|
213 xhr.send(); |
|
214 } |
|
215 |
|
216 // This function checks the state of an installed app. It does the following: |
|
217 // |
|
218 // * Check various state on the app object itself. |
|
219 // * Launch the app. |
|
220 // * Listen for messages from the app, verifying state. |
|
221 // * Close the app. |
|
222 // * Invoke the callback. |
|
223 function checkAppState(app, installed, version, cb) { |
|
224 // Check state on the app object. |
|
225 if (installed) |
|
226 is(app.installState, "installed", "Checking installed app"); |
|
227 else |
|
228 ok(true, "Checking uninstalled app"); |
|
229 |
|
230 // Set up the app. We need to set the attributes before the app is inserted |
|
231 // into the DOM. |
|
232 var ifr = document.createElement('iframe'); |
|
233 ifr.setAttribute('mozbrowser', 'true'); |
|
234 ifr.setAttribute('mozapp', app ? app.manifestURL : gHostedManifestURL); |
|
235 ifr.setAttribute('src', getAppURL(app)); |
|
236 var domParent = document.getElementById('container'); |
|
237 |
|
238 // Set us up to listen for messages from the app. |
|
239 var listener = function(e) { |
|
240 var message = e.detail.message; |
|
241 if (/OK/.exec(message)) { |
|
242 ok(true, "Message from app: " + message); |
|
243 } else if (/KO/.exec(message)) { |
|
244 ok(false, "Message from app: " + message); |
|
245 } else if (/IS_INSTALLED/.exec(message)) { |
|
246 ok(installed, "App is installed"); |
|
247 } else if (/NOT_INSTALLED/.exec(message)) { |
|
248 ok(!installed, "App is not installed"); |
|
249 } else if (/VERSION/.exec(message)) { |
|
250 is(message, "VERSION: MyWebApp v" + version, "Version should be correct"); |
|
251 } else if (/DONE/.exec(message)) { |
|
252 ok(true, "Messaging from app complete"); |
|
253 ifr.removeEventListener('mozbrowsershowmodalprompt', listener); |
|
254 domParent.removeChild(ifr); |
|
255 cb(); |
|
256 } |
|
257 } |
|
258 |
|
259 // This event is triggered when the app calls "alert". |
|
260 ifr.addEventListener('mozbrowsershowmodalprompt', listener, false); |
|
261 |
|
262 // Add the iframe to the DOM, triggering the launch. |
|
263 domParent.appendChild(ifr); |
|
264 } |
|
265 |
|
266 // Returns that appropriate path for the app associated with the manifest, |
|
267 // or the base sjs file if app is null. |
|
268 function getAppURL(app) { |
|
269 if (!app) |
|
270 return gBaseURL + "file_app.sjs?apptype=hosted"; |
|
271 return app.origin + app.manifest.launch_path; |
|
272 } |
|
273 |
|
274 function finish() { |
|
275 SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled"); |
|
276 SimpleTest.finish(); |
|
277 } |
|
278 |
|
279 function doReload() { |
|
280 window.location.reload(true); |
|
281 } |
|
282 |
|
283 </script> |
|
284 </head> |
|
285 <body onload="go()"> |
|
286 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=826058">Mozilla Bug 826058</a> |
|
287 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=863337">Mozilla Bug 863337</a> |
|
288 <p id="display"></p> |
|
289 <div id="content" style="display: none"> |
|
290 |
|
291 </div> |
|
292 <pre id="test"> |
|
293 </pre> |
|
294 <div id="container"></div> |
|
295 <button onclick="doReload()">Reload Page</button> |
|
296 </body> |
|
297 </html> |