dom/apps/tests/test_packaged_app_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/tests/test_packaged_app_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +var PackagedTestHelper = (function PackagedTestHelper() {
     1.9 +  "use strict";
    1.10 +
    1.11 +  var steps;
    1.12 +  var index = -1;
    1.13 +  var gSJSPath = "tests/dom/apps/tests/file_packaged_app.sjs";
    1.14 +  var gSJS = "http://test/" + gSJSPath;
    1.15 +  var gAppName = "appname";
    1.16 +  var gApp = null;
    1.17 +  var gInstallOrigin = "http://mochi.test:8888";
    1.18 +
    1.19 +  function debug(aMsg) {
    1.20 +    //dump("== PackageTestHelper debug == " + aMsg + "\n");
    1.21 +  }
    1.22 +
    1.23 +  function next() {
    1.24 +    index += 1;
    1.25 +    if (index >= steps.length) {
    1.26 +      ok(false, "Shouldn't get here!");
    1.27 +      return;
    1.28 +    }
    1.29 +    try {
    1.30 +      steps[index]();
    1.31 +    } catch(ex) {
    1.32 +      ok(false, "Caught exception", ex);
    1.33 +    }
    1.34 +  }
    1.35 +
    1.36 +  function start() {
    1.37 +    next();
    1.38 +  }
    1.39 +
    1.40 +  function finish() {
    1.41 +    SpecialPowers.removePermission("webapps-manage", document);
    1.42 +    SpecialPowers.removePermission("browser", document);
    1.43 +    SimpleTest.finish();
    1.44 +  }
    1.45 +
    1.46 +  function mozAppsError() {
    1.47 +    ok(false, "mozApps error: " + this.error.name);
    1.48 +    finish();
    1.49 +  }
    1.50 +
    1.51 +  function xhrError(event, url) {
    1.52 +    var xhr = event.target;
    1.53 +    ok(false, "XHR error loading " + url + ": " + xhr.status + " - " +
    1.54 +       xhr.statusText);
    1.55 +    finish();
    1.56 +  }
    1.57 +
    1.58 +  function xhrAbort(url) {
    1.59 +    ok(false, "XHR abort loading " + url);
    1.60 +    finish();
    1.61 +  }
    1.62 +
    1.63 +  function setAppVersion(aVersion, aCb, aDontUpdatePackage) {
    1.64 +    var xhr = new XMLHttpRequest();
    1.65 +    var dontUpdate = "";
    1.66 +    if (aDontUpdatePackage) {
    1.67 +      dontUpdate = "&dontUpdatePackage=1";
    1.68 +    }
    1.69 +    var url = gSJS + "?setVersion=" + aVersion + dontUpdate;
    1.70 +    xhr.addEventListener("load", function() {
    1.71 +                           is(xhr.responseText, "OK", "setAppVersion OK");
    1.72 +                           aCb();
    1.73 +                         });
    1.74 +    xhr.addEventListener("error", event => xhrError(event, url));
    1.75 +    xhr.addEventListener("abort", event => xhrAbort(url));
    1.76 +    xhr.open("GET", url, true);
    1.77 +    xhr.send();
    1.78 +  }
    1.79 +
    1.80 +  function checkAppDownloadError(aMiniManifestURL,
    1.81 +                                 aExpectedError,
    1.82 +                                 aVersion,
    1.83 +                                 aUninstall,
    1.84 +                                 aDownloadAvailable,
    1.85 +                                 aName,
    1.86 +                                 aCb) {
    1.87 +    var req = navigator.mozApps.installPackage(aMiniManifestURL);
    1.88 +    req.onsuccess = function(evt) {
    1.89 +      ok(true, "App installed");
    1.90 +      if (!aUninstall) {
    1.91 +        // Save it for later.
    1.92 +        gApp = req.result;
    1.93 +      }
    1.94 +    };
    1.95 +    req.onerror = function(evt) {
    1.96 +      ok(false, "Got unexpected " + evt.target.error.name);
    1.97 +      finish();
    1.98 +    };
    1.99 +
   1.100 +    navigator.mozApps.mgmt.oninstall = function(evt) {
   1.101 +      var aApp = evt.application;
   1.102 +      aApp.ondownloaderror = function(evt) {
   1.103 +        var error = aApp.downloadError.name;
   1.104 +        if (error == aExpectedError) {
   1.105 +          ok(true, "Got expected " + aExpectedError);
   1.106 +          var expected = {
   1.107 +            name: aName,
   1.108 +            manifestURL: aMiniManifestURL,
   1.109 +            installOrigin: gInstallOrigin,
   1.110 +            progress: 0,
   1.111 +            installState: "pending",
   1.112 +            downloadAvailable: aDownloadAvailable,
   1.113 +            downloading: false,
   1.114 +            downloadSize: 0,
   1.115 +            size: 0,
   1.116 +            readyToApplyDownload: false
   1.117 +          };
   1.118 +          checkAppState(aApp, aVersion, expected, false, aUninstall,
   1.119 +                        aCb || next);
   1.120 +        } else {
   1.121 +          ok(false, "Got unexpected " + error);
   1.122 +          finish();
   1.123 +        }
   1.124 +      };
   1.125 +      aApp.ondownloadsuccess = function(evt) {
   1.126 +        ok(false, "We are supposed to throw " + aExpectedError);
   1.127 +        finish();
   1.128 +      };
   1.129 +    };
   1.130 +  }
   1.131 +
   1.132 +  function checkAppState(aApp,
   1.133 +                         aVersion,
   1.134 +                         aExpectedApp,
   1.135 +                         aLaunchable,
   1.136 +                         aUninstall,
   1.137 +                         aCb) {
   1.138 +    debug(JSON.stringify(aApp, null, 2));
   1.139 +    if (aApp.manifest) {
   1.140 +      debug(JSON.stringify(aApp.manifest, null, 2));
   1.141 +    }
   1.142 +
   1.143 +    if (aExpectedApp.name) {
   1.144 +      if (aApp.manifest) {
   1.145 +        is(aApp.manifest.name, aExpectedApp.name, "Check name");
   1.146 +      }
   1.147 +      is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
   1.148 +    }
   1.149 +    if (aApp.manifest) {
   1.150 +      is(aApp.manifest.version, aVersion, "Check version");
   1.151 +    }
   1.152 +    if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
   1.153 +      is(aApp.manifest.size, aExpectedApp.size, "Check size");
   1.154 +    }
   1.155 +    if (aApp.manifest) {
   1.156 +      is(aApp.manifest.launch_path, aExpectedApp.launch_path || gSJSPath, "Check launch path");
   1.157 +    }
   1.158 +    if (aExpectedApp.manifestURL) {
   1.159 +      is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
   1.160 +    }
   1.161 +    if (aExpectedApp.installOrigin) {
   1.162 +      is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
   1.163 +    }
   1.164 +    ok(aApp.removable, "Removable app");
   1.165 +    if (typeof aExpectedApp.progress !== "undefined") {
   1.166 +      todo(aApp.progress == aExpectedApp.progress, "Check progress");
   1.167 +    }
   1.168 +    if (aExpectedApp.installState) {
   1.169 +      is(aApp.installState, aExpectedApp.installState, "Check installState");
   1.170 +    }
   1.171 +    if (typeof aExpectedApp.downloadAvailable !== "undefined") {
   1.172 +      is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
   1.173 +         "Check download available");
   1.174 +    }
   1.175 +    if (typeof aExpectedApp.downloading !== "undefined") {
   1.176 +      is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
   1.177 +    }
   1.178 +    if (typeof aExpectedApp.downloadSize !== "undefined") {
   1.179 +      is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
   1.180 +    }
   1.181 +    if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
   1.182 +      is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
   1.183 +         "Check readyToApplyDownload");
   1.184 +    }
   1.185 +    if (typeof aExpectedApp.origin !== "undefined") {
   1.186 +      is(aApp.origin, aExpectedApp.origin, "Check origin");
   1.187 +    }
   1.188 +    if (aLaunchable) {
   1.189 +      if (aUninstall) {
   1.190 +        checkUninstallApp(aApp);
   1.191 +      } else if (aCb && typeof aCb === "function") {
   1.192 +        aCb();
   1.193 +      }
   1.194 +      return;
   1.195 +    }
   1.196 +
   1.197 +    // Check if app is not launchable.
   1.198 +    var req = aApp.launch();
   1.199 +    req.onsuccess = function () {
   1.200 +      ok(false, "We shouldn't be here");
   1.201 +      finish();
   1.202 +    };
   1.203 +    req.onerror = function() {
   1.204 +      ok(true, "App is not launchable");
   1.205 +      if (aUninstall) {
   1.206 +        checkUninstallApp(aApp);
   1.207 +      } else if (aCb && typeof aCb === "function") {
   1.208 +        aCb();
   1.209 +      }
   1.210 +      return;
   1.211 +    };
   1.212 +  }
   1.213 +
   1.214 +  return {
   1.215 +    setSteps: function (aSteps) {
   1.216 +      steps = aSteps;
   1.217 +    },
   1.218 +    next: next,
   1.219 +    start: start,
   1.220 +    finish: finish,
   1.221 +    mozAppsError: mozAppsError,
   1.222 +    setAppVersion: setAppVersion,
   1.223 +    checkAppState: checkAppState,
   1.224 +    checkAppDownloadError: checkAppDownloadError,
   1.225 +    get gSJSPath() { return gSJSPath; },
   1.226 +    set gSJSPath(aValue) { gSJSPath = aValue },
   1.227 +    get gSJS() { return gSJS; },
   1.228 +    get gAppName() { return gAppName;},
   1.229 +    get gApp() { return gApp; },
   1.230 +    set gApp(aValue) { gApp = aValue; },
   1.231 +    gInstallOrigin: gInstallOrigin
   1.232 +  };
   1.233 +
   1.234 +})();

mercurial