dom/apps/tests/test_packaged_app_common.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 var PackagedTestHelper = (function PackagedTestHelper() {
     6   "use strict";
     8   var steps;
     9   var index = -1;
    10   var gSJSPath = "tests/dom/apps/tests/file_packaged_app.sjs";
    11   var gSJS = "http://test/" + gSJSPath;
    12   var gAppName = "appname";
    13   var gApp = null;
    14   var gInstallOrigin = "http://mochi.test:8888";
    16   function debug(aMsg) {
    17     //dump("== PackageTestHelper debug == " + aMsg + "\n");
    18   }
    20   function next() {
    21     index += 1;
    22     if (index >= steps.length) {
    23       ok(false, "Shouldn't get here!");
    24       return;
    25     }
    26     try {
    27       steps[index]();
    28     } catch(ex) {
    29       ok(false, "Caught exception", ex);
    30     }
    31   }
    33   function start() {
    34     next();
    35   }
    37   function finish() {
    38     SpecialPowers.removePermission("webapps-manage", document);
    39     SpecialPowers.removePermission("browser", document);
    40     SimpleTest.finish();
    41   }
    43   function mozAppsError() {
    44     ok(false, "mozApps error: " + this.error.name);
    45     finish();
    46   }
    48   function xhrError(event, url) {
    49     var xhr = event.target;
    50     ok(false, "XHR error loading " + url + ": " + xhr.status + " - " +
    51        xhr.statusText);
    52     finish();
    53   }
    55   function xhrAbort(url) {
    56     ok(false, "XHR abort loading " + url);
    57     finish();
    58   }
    60   function setAppVersion(aVersion, aCb, aDontUpdatePackage) {
    61     var xhr = new XMLHttpRequest();
    62     var dontUpdate = "";
    63     if (aDontUpdatePackage) {
    64       dontUpdate = "&dontUpdatePackage=1";
    65     }
    66     var url = gSJS + "?setVersion=" + aVersion + dontUpdate;
    67     xhr.addEventListener("load", function() {
    68                            is(xhr.responseText, "OK", "setAppVersion OK");
    69                            aCb();
    70                          });
    71     xhr.addEventListener("error", event => xhrError(event, url));
    72     xhr.addEventListener("abort", event => xhrAbort(url));
    73     xhr.open("GET", url, true);
    74     xhr.send();
    75   }
    77   function checkAppDownloadError(aMiniManifestURL,
    78                                  aExpectedError,
    79                                  aVersion,
    80                                  aUninstall,
    81                                  aDownloadAvailable,
    82                                  aName,
    83                                  aCb) {
    84     var req = navigator.mozApps.installPackage(aMiniManifestURL);
    85     req.onsuccess = function(evt) {
    86       ok(true, "App installed");
    87       if (!aUninstall) {
    88         // Save it for later.
    89         gApp = req.result;
    90       }
    91     };
    92     req.onerror = function(evt) {
    93       ok(false, "Got unexpected " + evt.target.error.name);
    94       finish();
    95     };
    97     navigator.mozApps.mgmt.oninstall = function(evt) {
    98       var aApp = evt.application;
    99       aApp.ondownloaderror = function(evt) {
   100         var error = aApp.downloadError.name;
   101         if (error == aExpectedError) {
   102           ok(true, "Got expected " + aExpectedError);
   103           var expected = {
   104             name: aName,
   105             manifestURL: aMiniManifestURL,
   106             installOrigin: gInstallOrigin,
   107             progress: 0,
   108             installState: "pending",
   109             downloadAvailable: aDownloadAvailable,
   110             downloading: false,
   111             downloadSize: 0,
   112             size: 0,
   113             readyToApplyDownload: false
   114           };
   115           checkAppState(aApp, aVersion, expected, false, aUninstall,
   116                         aCb || next);
   117         } else {
   118           ok(false, "Got unexpected " + error);
   119           finish();
   120         }
   121       };
   122       aApp.ondownloadsuccess = function(evt) {
   123         ok(false, "We are supposed to throw " + aExpectedError);
   124         finish();
   125       };
   126     };
   127   }
   129   function checkAppState(aApp,
   130                          aVersion,
   131                          aExpectedApp,
   132                          aLaunchable,
   133                          aUninstall,
   134                          aCb) {
   135     debug(JSON.stringify(aApp, null, 2));
   136     if (aApp.manifest) {
   137       debug(JSON.stringify(aApp.manifest, null, 2));
   138     }
   140     if (aExpectedApp.name) {
   141       if (aApp.manifest) {
   142         is(aApp.manifest.name, aExpectedApp.name, "Check name");
   143       }
   144       is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
   145     }
   146     if (aApp.manifest) {
   147       is(aApp.manifest.version, aVersion, "Check version");
   148     }
   149     if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
   150       is(aApp.manifest.size, aExpectedApp.size, "Check size");
   151     }
   152     if (aApp.manifest) {
   153       is(aApp.manifest.launch_path, aExpectedApp.launch_path || gSJSPath, "Check launch path");
   154     }
   155     if (aExpectedApp.manifestURL) {
   156       is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
   157     }
   158     if (aExpectedApp.installOrigin) {
   159       is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
   160     }
   161     ok(aApp.removable, "Removable app");
   162     if (typeof aExpectedApp.progress !== "undefined") {
   163       todo(aApp.progress == aExpectedApp.progress, "Check progress");
   164     }
   165     if (aExpectedApp.installState) {
   166       is(aApp.installState, aExpectedApp.installState, "Check installState");
   167     }
   168     if (typeof aExpectedApp.downloadAvailable !== "undefined") {
   169       is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
   170          "Check download available");
   171     }
   172     if (typeof aExpectedApp.downloading !== "undefined") {
   173       is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
   174     }
   175     if (typeof aExpectedApp.downloadSize !== "undefined") {
   176       is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
   177     }
   178     if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
   179       is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
   180          "Check readyToApplyDownload");
   181     }
   182     if (typeof aExpectedApp.origin !== "undefined") {
   183       is(aApp.origin, aExpectedApp.origin, "Check origin");
   184     }
   185     if (aLaunchable) {
   186       if (aUninstall) {
   187         checkUninstallApp(aApp);
   188       } else if (aCb && typeof aCb === "function") {
   189         aCb();
   190       }
   191       return;
   192     }
   194     // Check if app is not launchable.
   195     var req = aApp.launch();
   196     req.onsuccess = function () {
   197       ok(false, "We shouldn't be here");
   198       finish();
   199     };
   200     req.onerror = function() {
   201       ok(true, "App is not launchable");
   202       if (aUninstall) {
   203         checkUninstallApp(aApp);
   204       } else if (aCb && typeof aCb === "function") {
   205         aCb();
   206       }
   207       return;
   208     };
   209   }
   211   return {
   212     setSteps: function (aSteps) {
   213       steps = aSteps;
   214     },
   215     next: next,
   216     start: start,
   217     finish: finish,
   218     mozAppsError: mozAppsError,
   219     setAppVersion: setAppVersion,
   220     checkAppState: checkAppState,
   221     checkAppDownloadError: checkAppDownloadError,
   222     get gSJSPath() { return gSJSPath; },
   223     set gSJSPath(aValue) { gSJSPath = aValue },
   224     get gSJS() { return gSJS; },
   225     get gAppName() { return gAppName;},
   226     get gApp() { return gApp; },
   227     set gApp(aValue) { gApp = aValue; },
   228     gInstallOrigin: gInstallOrigin
   229   };
   231 })();

mercurial