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
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=997886
5 -->
6 <head>
7 <meta charset="utf-8">
8 <title>Test for Bug 997886 - Test installing and updating apps with asm.js pre-compiling</title>
9 <script type="application/javascript"
10 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
11 <script type="application/javascript"
12 src="chrome://mochikit/content/chrome-harness.js"></script>
13 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
14 <script type="application/javascript;version=1.7">
16 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
18 SimpleTest.waitForExplicitFinish();
20 const gBaseURL = 'http://test/chrome/dom/apps/tests/asmjs/';
21 const gSJS = gBaseURL + 'asmjs_app.sjs';
22 const gManifestURL = gSJS + '?getManifest=true';
23 let gGenerator = runTest();
25 // Mock WebappOSUtils
26 Cu.import("resource://gre/modules/WebappOSUtils.jsm");
27 let oldWebappOSUtils = WebappOSUtils;
28 WebappOSUtils.getPackagePath = function(aApp) {
29 return aApp.basePath + "/" + aApp.id;
30 }
32 // Enable the ScriptPreloader module
33 Cu.import("resource://gre/modules/ScriptPreloader.jsm");
34 let oldScriptPreloaderEnabled = ScriptPreloader._enabled;
35 ScriptPreloader._enabled = true;
37 SimpleTest.registerCleanupFunction(() => {
38 WebappOSUtils = oldWebappOSUtils;
39 ScriptPreloader._enabled = oldScriptPreloaderEnabled;
40 });
42 function go() {
43 gGenerator.next();
44 }
46 function continueTest() {
47 try {
48 gGenerator.next();
49 } catch (e if e instanceof StopIteration) {
50 SimpleTest.finish();
51 }
52 }
54 function mozAppsError() {
55 ok(false, "mozApps error: " + this.error.name);
56 SimpleTest.finish();
57 }
59 function xhrError(aEvent, aURL) {
60 var xhr = aEvent.target;
61 ok(false, "XHR error loading " + aURL + ": " + xhr.status + " - " +
62 xhr.statusText);
63 SimpleTest.finish();
64 }
66 function xhrAbort(aURL) {
67 ok(false, "XHR abort loading " + aURL);
68 SimpleTest.finish();
69 }
71 function setState(aQuery, aVersion, aCallback) {
72 var xhr = new XMLHttpRequest();
73 var url = gSJS + '?' + aQuery + '=' + aVersion;
74 xhr.addEventListener("load", function() {
75 is(xhr.responseText, "OK", aQuery + " OK");
76 aCallback();
77 });
78 xhr.addEventListener("error", event => xhrError(event, url));
79 xhr.addEventListener("abort", event => xhrAbort(url));
80 xhr.open('GET', url, true);
81 xhr.send();
82 }
84 function runApp(aApp, aCallback) {
85 let domParent = document.getElementById('container');
87 let ifr = document.createElement('iframe');
88 ifr.setAttribute('mozbrowser', 'true');
89 ifr.setAttribute('mozapp', gManifestURL);
90 ifr.src = aApp.origin + aApp.manifest.launch_path;
92 ifr.addEventListener('mozbrowsershowmodalprompt', function onAlert(e) {
93 var message = e.detail.message;
95 if (message.startsWith("OK: ")) {
96 ok(true, message.substring(4, message.length));
97 } else if (message.startsWith("ERROR: ")) {
98 ok(false, message.substring(7, message.length));
99 } else if (message == "DONE") {
100 ifr.removeEventListener('mozbrowsershowmodalprompt', onAlert, false);
101 domParent.removeChild(ifr);
102 aCallback();
103 }
104 }, false);
106 domParent.appendChild(ifr);
107 }
109 function testNoPrecompile(aPrecompile, aCallback) {
110 setState("setPrecompile", aPrecompile, function() {
111 let request = navigator.mozApps.installPackage(gManifestURL);
112 request.onerror = mozAppsError;
113 request.onsuccess = function() {
114 let app = request.result;
115 ok(app, "App is non-null");
117 app.ondownloaderror = mozAppsError;
118 app.ondownloadapplied = function() {
119 runApp(app, function() {
120 request = navigator.mozApps.mgmt.uninstall(app);
121 request.onerror = mozAppsError;
122 request.onsuccess = function() {
123 app.ondownloadapplied = null;
124 aCallback();
125 };
126 });
127 };
128 };
129 });
130 }
132 function runTest() {
133 // Set up.
135 SpecialPowers.setAllAppsLaunchable(true);
136 SpecialPowers.pushPrefEnv({'set': [["dom.mozBrowserFramesEnabled", true]]},
137 continueTest);
138 yield undefined;
140 SpecialPowers.autoConfirmAppInstall(continueTest);
141 yield undefined;
143 setState("setVersion", 1, continueTest);
144 yield undefined;
146 // Test apps with wrong precompile fields
147 info("Test with an empty array");
148 testNoPrecompile('[]', continueTest);
149 yield undefined;
150 info("Test with an object");
151 testNoPrecompile('{}', continueTest);
152 yield undefined;
153 info("Test with a string");
154 testNoPrecompile('"asmjsmodule"', continueTest);
155 yield undefined;
156 info("Test with a file that doesn't exist");
157 testNoPrecompile('["file.js"]', continueTest);
158 yield undefined;
160 setState("setPrecompile", '["asmjsmodule.js"]', continueTest);
161 yield undefined;
163 let request = navigator.mozApps.installPackage(gManifestURL);
164 request.onerror = mozAppsError;
165 request.onsuccess = continueTest;
166 yield undefined;
167 let app = request.result;
168 ok(app, "App is non-null");
169 app.ondownloaderror = mozAppsError;
170 app.ondownloadsuccess = continueTest;
171 app.ondownloadapplied = continueTest;
172 yield undefined;
173 yield undefined;
175 runApp(app, continueTest);
176 yield undefined;
178 // Update app version
179 setState("setVersion", 2, continueTest);
180 yield undefined;
182 // Check for updates
183 lastCheck = app.lastUpdateCheck;
184 app.ondownloadavailable = function() {
185 ok(true, "downloadavailable fired");
186 continueTest();
187 }
188 request = app.checkForUpdate();
189 request.onerror = mozAppsError;
190 request.onsuccess = function() {
191 ok(true, "checkForUpdate success");
192 continueTest();
193 }
194 yield undefined;
195 yield undefined;
197 // Check that there's a download available and download it
198 ok(app.downloadAvailable, "Download available");
199 app.ondownloaderror = mozAppsError;
200 app.ondownloadsuccess = function() {
201 ok(true, "downloadsuccess fired");
202 continueTest();
203 }
204 app.download();
205 yield undefined;
207 // Apply downloaded update
208 ok(app.readyToApplyDownload, "App ready to apply download");
209 app.ondownloaderror = mozAppsError;
210 app.ondownloadapplied = function() {
211 ok(true, "downloadapplied fired");
212 continueTest();
213 }
214 navigator.mozApps.mgmt.applyDownload(app);
215 yield undefined;
217 runApp(app, continueTest);
218 yield undefined;
220 // Uninstall the app.
221 request = navigator.mozApps.mgmt.uninstall(app);
222 request.onerror = mozAppsError;
223 request.onsuccess = continueTest;
224 yield undefined;
225 }
227 </script>
228 </head>
229 <body onload="go()">
230 <p id="display"></p>
231 <div id="content" style="display: none">
232 </div>
233 <pre id="test">
234 </pre>
235 <div id="container"></div>
236 </body>
237 </html>