dom/apps/tests/test_packaged_app_asmjs.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/tests/test_packaged_app_asmjs.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,237 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=997886
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug 997886 - Test installing and updating apps with asm.js pre-compiling</title>
    1.12 +  <script type="application/javascript"
    1.13 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.14 +  <script type="application/javascript"
    1.15 +          src="chrome://mochikit/content/chrome-harness.js"></script>
    1.16 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    1.17 +  <script type="application/javascript;version=1.7">
    1.18 +
    1.19 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.20 +
    1.21 +SimpleTest.waitForExplicitFinish();
    1.22 +
    1.23 +const gBaseURL = 'http://test/chrome/dom/apps/tests/asmjs/';
    1.24 +const gSJS = gBaseURL + 'asmjs_app.sjs';
    1.25 +const gManifestURL = gSJS + '?getManifest=true';
    1.26 +let gGenerator = runTest();
    1.27 +
    1.28 +// Mock WebappOSUtils
    1.29 +Cu.import("resource://gre/modules/WebappOSUtils.jsm");
    1.30 +let oldWebappOSUtils = WebappOSUtils;
    1.31 +WebappOSUtils.getPackagePath = function(aApp) {
    1.32 +  return aApp.basePath + "/" + aApp.id;
    1.33 +}
    1.34 +
    1.35 +// Enable the ScriptPreloader module
    1.36 +Cu.import("resource://gre/modules/ScriptPreloader.jsm");
    1.37 +let oldScriptPreloaderEnabled = ScriptPreloader._enabled;
    1.38 +ScriptPreloader._enabled = true;
    1.39 +
    1.40 +SimpleTest.registerCleanupFunction(() => {
    1.41 +  WebappOSUtils = oldWebappOSUtils;
    1.42 +  ScriptPreloader._enabled = oldScriptPreloaderEnabled;
    1.43 +});
    1.44 +
    1.45 +function go() {
    1.46 +  gGenerator.next();
    1.47 +}
    1.48 +
    1.49 +function continueTest() {
    1.50 +  try {
    1.51 +    gGenerator.next();
    1.52 +  } catch (e if e instanceof StopIteration) {
    1.53 +    SimpleTest.finish();
    1.54 +  }
    1.55 +}
    1.56 +
    1.57 +function mozAppsError() {
    1.58 +  ok(false, "mozApps error: " + this.error.name);
    1.59 +  SimpleTest.finish();
    1.60 +}
    1.61 +
    1.62 +function xhrError(aEvent, aURL) {
    1.63 +  var xhr = aEvent.target;
    1.64 +  ok(false, "XHR error loading " + aURL + ": " + xhr.status + " - " +
    1.65 +            xhr.statusText);
    1.66 +  SimpleTest.finish();
    1.67 +}
    1.68 +
    1.69 +function xhrAbort(aURL) {
    1.70 +  ok(false, "XHR abort loading " + aURL);
    1.71 +  SimpleTest.finish();
    1.72 +}
    1.73 +
    1.74 +function setState(aQuery, aVersion, aCallback) {
    1.75 +  var xhr = new XMLHttpRequest();
    1.76 +  var url = gSJS + '?' + aQuery + '=' + aVersion;
    1.77 +  xhr.addEventListener("load", function() {
    1.78 +    is(xhr.responseText, "OK", aQuery + " OK");
    1.79 +    aCallback();
    1.80 +  });
    1.81 +  xhr.addEventListener("error", event => xhrError(event, url));
    1.82 +  xhr.addEventListener("abort", event => xhrAbort(url));
    1.83 +  xhr.open('GET', url, true);
    1.84 +  xhr.send();
    1.85 +}
    1.86 +
    1.87 +function runApp(aApp, aCallback) {
    1.88 +  let domParent = document.getElementById('container');
    1.89 +
    1.90 +  let ifr = document.createElement('iframe');
    1.91 +  ifr.setAttribute('mozbrowser', 'true');
    1.92 +  ifr.setAttribute('mozapp', gManifestURL);
    1.93 +  ifr.src = aApp.origin + aApp.manifest.launch_path;
    1.94 +
    1.95 +  ifr.addEventListener('mozbrowsershowmodalprompt', function onAlert(e) {
    1.96 +    var message = e.detail.message;
    1.97 +
    1.98 +    if (message.startsWith("OK: ")) {
    1.99 +      ok(true, message.substring(4, message.length));
   1.100 +    } else if (message.startsWith("ERROR: ")) {
   1.101 +      ok(false, message.substring(7, message.length));
   1.102 +    } else if (message == "DONE") {
   1.103 +      ifr.removeEventListener('mozbrowsershowmodalprompt', onAlert, false);
   1.104 +      domParent.removeChild(ifr);
   1.105 +      aCallback();
   1.106 +    }
   1.107 +  }, false);
   1.108 +
   1.109 +  domParent.appendChild(ifr);
   1.110 +}
   1.111 +
   1.112 +function testNoPrecompile(aPrecompile, aCallback) {
   1.113 +  setState("setPrecompile", aPrecompile, function() {
   1.114 +    let request = navigator.mozApps.installPackage(gManifestURL);
   1.115 +    request.onerror = mozAppsError;
   1.116 +    request.onsuccess = function() {
   1.117 +      let app = request.result;
   1.118 +      ok(app, "App is non-null");
   1.119 +
   1.120 +      app.ondownloaderror = mozAppsError;
   1.121 +      app.ondownloadapplied = function() {
   1.122 +        runApp(app, function() {
   1.123 +          request = navigator.mozApps.mgmt.uninstall(app);
   1.124 +          request.onerror = mozAppsError;
   1.125 +          request.onsuccess = function() {
   1.126 +            app.ondownloadapplied = null;
   1.127 +            aCallback();
   1.128 +          };
   1.129 +        });
   1.130 +      };
   1.131 +    };
   1.132 +  });
   1.133 +}
   1.134 +
   1.135 +function runTest() {
   1.136 +  // Set up.
   1.137 +
   1.138 +  SpecialPowers.setAllAppsLaunchable(true);
   1.139 +  SpecialPowers.pushPrefEnv({'set': [["dom.mozBrowserFramesEnabled", true]]},
   1.140 +                            continueTest);
   1.141 +  yield undefined;
   1.142 +
   1.143 +  SpecialPowers.autoConfirmAppInstall(continueTest);
   1.144 +  yield undefined;
   1.145 +
   1.146 +  setState("setVersion", 1, continueTest);
   1.147 +  yield undefined;
   1.148 +
   1.149 +  // Test apps with wrong precompile fields
   1.150 +  info("Test with an empty array");
   1.151 +  testNoPrecompile('[]', continueTest);
   1.152 +  yield undefined;
   1.153 +  info("Test with an object");
   1.154 +  testNoPrecompile('{}', continueTest);
   1.155 +  yield undefined;
   1.156 +  info("Test with a string");
   1.157 +  testNoPrecompile('"asmjsmodule"', continueTest);
   1.158 +  yield undefined;
   1.159 +  info("Test with a file that doesn't exist");
   1.160 +  testNoPrecompile('["file.js"]', continueTest);
   1.161 +  yield undefined;
   1.162 +
   1.163 +  setState("setPrecompile", '["asmjsmodule.js"]', continueTest);
   1.164 +  yield undefined;
   1.165 +
   1.166 +  let request = navigator.mozApps.installPackage(gManifestURL);
   1.167 +  request.onerror = mozAppsError;
   1.168 +  request.onsuccess = continueTest;
   1.169 +  yield undefined;
   1.170 +  let app = request.result;
   1.171 +  ok(app, "App is non-null");
   1.172 +  app.ondownloaderror = mozAppsError;
   1.173 +  app.ondownloadsuccess = continueTest;
   1.174 +  app.ondownloadapplied = continueTest;
   1.175 +  yield undefined;
   1.176 +  yield undefined;
   1.177 +
   1.178 +  runApp(app, continueTest);
   1.179 +  yield undefined;
   1.180 +
   1.181 +  // Update app version
   1.182 +  setState("setVersion", 2, continueTest);
   1.183 +  yield undefined;
   1.184 +
   1.185 +  // Check for updates
   1.186 +  lastCheck = app.lastUpdateCheck;
   1.187 +  app.ondownloadavailable = function() {
   1.188 +    ok(true, "downloadavailable fired");
   1.189 +    continueTest();
   1.190 +  }
   1.191 +  request = app.checkForUpdate();
   1.192 +  request.onerror = mozAppsError;
   1.193 +  request.onsuccess = function() {
   1.194 +    ok(true, "checkForUpdate success");
   1.195 +    continueTest();
   1.196 +  }
   1.197 +  yield undefined;
   1.198 +  yield undefined;
   1.199 +
   1.200 +  // Check that there's a download available and download it
   1.201 +  ok(app.downloadAvailable, "Download available");
   1.202 +  app.ondownloaderror = mozAppsError;
   1.203 +  app.ondownloadsuccess = function() {
   1.204 +    ok(true, "downloadsuccess fired");
   1.205 +    continueTest();
   1.206 +  }
   1.207 +  app.download();
   1.208 +  yield undefined;
   1.209 +
   1.210 +  // Apply downloaded update
   1.211 +  ok(app.readyToApplyDownload, "App ready to apply download");
   1.212 +  app.ondownloaderror = mozAppsError;
   1.213 +  app.ondownloadapplied = function() {
   1.214 +    ok(true, "downloadapplied fired");
   1.215 +    continueTest();
   1.216 +  }
   1.217 +  navigator.mozApps.mgmt.applyDownload(app);
   1.218 +  yield undefined;
   1.219 +
   1.220 +  runApp(app, continueTest);
   1.221 +  yield undefined;
   1.222 +
   1.223 +  // Uninstall the app.
   1.224 +  request = navigator.mozApps.mgmt.uninstall(app);
   1.225 +  request.onerror = mozAppsError;
   1.226 +  request.onsuccess = continueTest;
   1.227 +  yield undefined;
   1.228 +}
   1.229 +
   1.230 +  </script>
   1.231 +</head>
   1.232 +<body onload="go()">
   1.233 +<p id="display"></p>
   1.234 +<div id="content" style="display: none">
   1.235 +</div>
   1.236 +<pre id="test">
   1.237 +</pre>
   1.238 +<div id="container"></div>
   1.239 +</body>
   1.240 +</html>

mercurial