dom/apps/tests/test_app_update.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/tests/test_app_update.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,297 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=826058
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug 826058</title>
    1.12 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +  <script type="application/javascript;version=1.7">
    1.15 +
    1.16 +  /** Test for Bug 826058 **/
    1.17 +
    1.18 +  SimpleTest.waitForExplicitFinish();
    1.19 +
    1.20 +  var gBaseURL = 'http://test/tests/dom/apps/tests/';
    1.21 +  var gHostedManifestURL = gBaseURL + 'file_app.sjs?apptype=hosted&getmanifest=true';
    1.22 +  var gCachedManifestURL = gBaseURL + 'file_app.sjs?apptype=cached&getmanifest=true';
    1.23 +  var gGenerator = runTest();
    1.24 +
    1.25 +  function go() {
    1.26 +    SpecialPowers.pushPermissions(
    1.27 +      [{ "type": "browser", "allow": 1, "context": document },
    1.28 +       { "type": "embed-apps", "allow": 1, "context": document },
    1.29 +       { "type": "webapps-manage", "allow": 1, "context": document }],
    1.30 +      function() { gGenerator.next() });
    1.31 +  }
    1.32 +
    1.33 +  function continueTest() {
    1.34 +    try {
    1.35 +      gGenerator.next();
    1.36 +    } catch (e if e instanceof StopIteration) {
    1.37 +      finish();
    1.38 +    }
    1.39 +  }
    1.40 +
    1.41 +  function mozAppsError() {
    1.42 +    ok(false, "mozApps error: " + this.error.name);
    1.43 +    finish();
    1.44 +  }
    1.45 +
    1.46 +  function xhrError(event, url) {
    1.47 +    var xhr = event.target;
    1.48 +    ok(false, "XHR error loading " + url + ": " + xhr.status + " - " +
    1.49 +              xhr.statusText);
    1.50 +    finish();
    1.51 +  }
    1.52 +
    1.53 +  function xhrAbort(url) {
    1.54 +    ok(false, "XHR abort loading " + url);
    1.55 +    finish();
    1.56 +  }
    1.57 +
    1.58 +  function runTest() {
    1.59 +    // Set up.
    1.60 +
    1.61 +    SpecialPowers.setAllAppsLaunchable(true);
    1.62 +    SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
    1.63 +
    1.64 +    // Test Bug 927699 - navigator.mozApps.install(url) lets NS_ERROR_FAILURE
    1.65 +    //                   onto the web
    1.66 +    var request = navigator.mozApps.install("");
    1.67 +    request.onerror = function() {
    1.68 +      ok(request.error.name == "INVALID_URL", "Got expected INVALID_URL");
    1.69 +      continueTest();
    1.70 +    };
    1.71 +    request.onsuccess = mozAppsError;
    1.72 +    yield undefined;
    1.73 +
    1.74 +    setAppVersion(1, continueTest);
    1.75 +    yield undefined;
    1.76 +    SpecialPowers.autoConfirmAppInstall(continueTest);
    1.77 +    yield undefined;
    1.78 +
    1.79 +    // Load the app, uninstalled.
    1.80 +    checkAppState(null, false, 1, continueTest);
    1.81 +    yield undefined;
    1.82 +
    1.83 +    // Bump the version and install the app.
    1.84 +    setAppVersion(2, continueTest);
    1.85 +    yield undefined;
    1.86 +
    1.87 +    request = navigator.mozApps.install(gHostedManifestURL);
    1.88 +    request.onerror = mozAppsError;
    1.89 +    request.onsuccess = continueTest;
    1.90 +    yield undefined;
    1.91 +    var app = request.result;
    1.92 +    ok(app, "App is non-null");
    1.93 +    ok(app.manifest.description == "Updated even faster than Firefox, just to annoy slashdotters.",
    1.94 +       "Manifest is HTML-sanitized");
    1.95 +
    1.96 +    // Check the app a few times.
    1.97 +    checkAppState(app, true, 2, continueTest);
    1.98 +    yield undefined;
    1.99 +    checkAppState(app, true, 2, continueTest);
   1.100 +    yield undefined;
   1.101 +
   1.102 +    // Bump the version and check the app again. The app is not cached, so the
   1.103 +    // version bump takes effect.
   1.104 +    setAppVersion(3, continueTest);
   1.105 +    yield undefined;
   1.106 +    checkAppState(app, true, 3, continueTest);
   1.107 +    yield undefined;
   1.108 +
   1.109 +    // check for update
   1.110 +    var icons = app.manifest.icons;
   1.111 +    var oldIcon = icons[Object.keys(icons)[0]];
   1.112 +    var oldUpdateTime = app.updateTime;
   1.113 +    setAppIcon('new_icon', continueTest);
   1.114 +    yield undefined;
   1.115 +
   1.116 +    app.ondownloadavailable = function() {
   1.117 +      ok(false, 'Got a downloadavailable event for non-cached hosted apps');
   1.118 +    };
   1.119 +
   1.120 +    app.ondownloadapplied = function() {
   1.121 +      ok(true, 'Got a downloadapplied when checking for update');
   1.122 +      app.ondownloadapplied = app.ondownloadavailable = null;
   1.123 +      continueTest();
   1.124 +    };
   1.125 +    request = app.checkForUpdate();
   1.126 +    request.onerror = mozAppsError;
   1.127 +    request.onsuccess = function() {
   1.128 +      ok(true, "Got onsuccess");
   1.129 +      continueTest();
   1.130 +    };
   1.131 +    yield undefined;
   1.132 +    yield undefined;
   1.133 +
   1.134 +    icons = app.manifest.icons;
   1.135 +    var newIcon = icons[Object.keys(icons)[0]];
   1.136 +    var newUpdateTime = app.updateTime;
   1.137 +    isnot(oldIcon, newIcon, 'The icon should be updated');
   1.138 +    isnot(oldUpdateTime, newUpdateTime, 'The update time should be updated');
   1.139 +
   1.140 +    // Uninstall the app.
   1.141 +    request = navigator.mozApps.mgmt.uninstall(app);
   1.142 +    request.onerror = mozAppsError;
   1.143 +    request.onsuccess = continueTest;
   1.144 +    yield undefined;
   1.145 +
   1.146 +    // Check the uninstalled app.
   1.147 +    checkAppState(app, false, 3, continueTest);
   1.148 +    yield undefined;
   1.149 +
   1.150 +    // Install the cached app.
   1.151 +    setAppVersion(3, continueTest);
   1.152 +    yield undefined;
   1.153 +    ok(true, "Installing cached app");
   1.154 +    var request = navigator.mozApps.install(gCachedManifestURL);
   1.155 +    request.onerror = mozAppsError;
   1.156 +    request.onsuccess = continueTest;
   1.157 +    yield undefined;
   1.158 +    var app = request.result;
   1.159 +    ok(app, "App is non-null");
   1.160 +    if (app.installState == "pending") {
   1.161 +      ok(true, "App is pending. Waiting for progress");
   1.162 +      app.onprogress = function() ok(true, "Got download progress");
   1.163 +      app.ondownloadsuccess = continueTest;
   1.164 +      app.ondownloaderror = mozAppsError;
   1.165 +      yield undefined;
   1.166 +    }
   1.167 +    is(app.installState, "installed", "App is installed");
   1.168 +
   1.169 +    // Check the cached app.
   1.170 +    checkAppState(app, true, 3, continueTest);
   1.171 +    yield undefined;
   1.172 +
   1.173 +    // Check for updates. The current infrastructure always returns a new appcache
   1.174 +    // manifest, so there should always be an update.
   1.175 +    var lastCheck = app.lastUpdateCheck;
   1.176 +    ok(true, "Setting callbacks");
   1.177 +    app.ondownloadapplied = function() ok(true, "downloadapplied fired.");
   1.178 +    app.ondownloadavailable = function() ok(false, "downloadavailable fired");
   1.179 +    ok(true, "Checking for updates");
   1.180 +    var request = app.checkForUpdate();
   1.181 +    request.onerror = mozAppsError;
   1.182 +    request.onsuccess = continueTest;
   1.183 +    yield undefined;
   1.184 +    todo(app.lastUpdateCheck > lastCheck, "lastUpdateCheck updated appropriately");
   1.185 +
   1.186 +
   1.187 +    // Uninstall the app.
   1.188 +    request = navigator.mozApps.mgmt.uninstall(app);
   1.189 +    request.onerror = mozAppsError;
   1.190 +    request.onsuccess = continueTest;
   1.191 +    yield undefined;
   1.192 +    ok(true, "Uninstalled app");
   1.193 +
   1.194 +    // Check the uninstalled app.
   1.195 +    checkAppState(app, false, 3, continueTest);
   1.196 +    yield undefined;
   1.197 +  }
   1.198 +
   1.199 +  function setAppVersion(version, cb) {
   1.200 +    var xhr = new XMLHttpRequest();
   1.201 +    var url = gBaseURL + 'file_app.sjs?setVersion=' + version;
   1.202 +    xhr.addEventListener("load", function() { is(xhr.responseText, "OK", "setAppVersion OK"); cb(); });
   1.203 +    xhr.addEventListener("error", event => xhrError(event, url));
   1.204 +    xhr.addEventListener("abort", event => xhrAbort(url));
   1.205 +    xhr.open('GET', url, true);
   1.206 +    xhr.send();
   1.207 +  }
   1.208 +
   1.209 +  function setAppIcon(icon, cb) {
   1.210 +    var xhr = new XMLHttpRequest();
   1.211 +    var url = gBaseURL + 'file_app.sjs?setIcon=' + icon;
   1.212 +    xhr.addEventListener("load", function() { is(xhr.responseText, "OK", "setAppIcon OK"); cb(); });
   1.213 +    xhr.addEventListener("error", event => xhrError(event, url));
   1.214 +    xhr.addEventListener("abort", event => xhrAbort(url));
   1.215 +    xhr.open('GET', url, true);
   1.216 +    xhr.send();
   1.217 +  }
   1.218 +
   1.219 +  // This function checks the state of an installed app. It does the following:
   1.220 +  //
   1.221 +  // * Check various state on the app object itself.
   1.222 +  // * Launch the app.
   1.223 +  // * Listen for messages from the app, verifying state.
   1.224 +  // * Close the app.
   1.225 +  // * Invoke the callback.
   1.226 +  function checkAppState(app, installed, version, cb) {
   1.227 +    // Check state on the app object.
   1.228 +    if (installed)
   1.229 +      is(app.installState, "installed", "Checking installed app");
   1.230 +    else
   1.231 +      ok(true, "Checking uninstalled app");
   1.232 +
   1.233 +    // Set up the app. We need to set the attributes before the app is inserted
   1.234 +    // into the DOM.
   1.235 +    var ifr = document.createElement('iframe');
   1.236 +    ifr.setAttribute('mozbrowser', 'true');
   1.237 +    ifr.setAttribute('mozapp', app ? app.manifestURL : gHostedManifestURL);
   1.238 +    ifr.setAttribute('src', getAppURL(app));
   1.239 +    var domParent = document.getElementById('container');
   1.240 +
   1.241 +    // Set us up to listen for messages from the app.
   1.242 +    var listener = function(e) {
   1.243 +      var message = e.detail.message;
   1.244 +      if (/OK/.exec(message)) {
   1.245 +        ok(true, "Message from app: " + message);
   1.246 +      } else if (/KO/.exec(message)) {
   1.247 +        ok(false, "Message from app: " + message);
   1.248 +      } else if (/IS_INSTALLED/.exec(message)) {
   1.249 +        ok(installed, "App is installed");
   1.250 +      } else if (/NOT_INSTALLED/.exec(message)) {
   1.251 +        ok(!installed, "App is not installed");
   1.252 +      } else if (/VERSION/.exec(message)) {
   1.253 +        is(message, "VERSION: MyWebApp v" + version, "Version should be correct");
   1.254 +      } else if (/DONE/.exec(message)) {
   1.255 +        ok(true, "Messaging from app complete");
   1.256 +        ifr.removeEventListener('mozbrowsershowmodalprompt', listener);
   1.257 +        domParent.removeChild(ifr);
   1.258 +        cb();
   1.259 +      }
   1.260 +    }
   1.261 +
   1.262 +    // This event is triggered when the app calls "alert".
   1.263 +    ifr.addEventListener('mozbrowsershowmodalprompt', listener, false);
   1.264 +
   1.265 +    // Add the iframe to the DOM, triggering the launch.
   1.266 +    domParent.appendChild(ifr);
   1.267 +  }
   1.268 +
   1.269 +  // Returns that appropriate path for the app associated with the manifest,
   1.270 +  // or the base sjs file if app is null.
   1.271 +  function getAppURL(app) {
   1.272 +    if (!app)
   1.273 +      return gBaseURL + "file_app.sjs?apptype=hosted";
   1.274 +    return app.origin + app.manifest.launch_path;
   1.275 +  }
   1.276 +
   1.277 +  function finish() {
   1.278 +    SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled");
   1.279 +    SimpleTest.finish();
   1.280 +  }
   1.281 +
   1.282 +  function doReload() {
   1.283 +    window.location.reload(true);
   1.284 +  }
   1.285 +
   1.286 +  </script>
   1.287 +</head>
   1.288 +<body onload="go()">
   1.289 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=826058">Mozilla Bug 826058</a>
   1.290 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=863337">Mozilla Bug 863337</a>
   1.291 +<p id="display"></p>
   1.292 +<div id="content" style="display: none">
   1.293 +
   1.294 +</div>
   1.295 +<pre id="test">
   1.296 +</pre>
   1.297 +<div id="container"></div>
   1.298 +<button onclick="doReload()">Reload Page</button>
   1.299 +</body>
   1.300 +</html>

mercurial