dom/apps/tests/test_operator_app_install.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/tests/test_operator_app_install.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,267 @@
     1.4 +"use strict";
     1.5 +
     1.6 +const Cu = Components.utils;
     1.7 +const Cc = Components.classes;
     1.8 +const Ci = Components.interfaces;
     1.9 +
    1.10 +Cu.import("resource://gre/modules/OperatorApps.jsm");
    1.11 +Cu.import("resource://gre/modules/FileUtils.jsm");
    1.12 +Cu.import("resource://gre/modules/NetUtil.jsm");
    1.13 +
    1.14 +// From prio.h
    1.15 +const PR_RDWR        = 0x04;
    1.16 +const PR_CREATE_FILE = 0x08;
    1.17 +const PR_TRUNCATE    = 0x20;
    1.18 +
    1.19 +SimpleTest.waitForExplicitFinish();
    1.20 +
    1.21 +var gApp = null;
    1.22 +
    1.23 +var index = -1;
    1.24 +var singlevariantDir = undefined;
    1.25 +
    1.26 +function debug(aMsg) {
    1.27 +  //dump("== Tests debug == " + aMsg + "\n");
    1.28 +}
    1.29 +
    1.30 +
    1.31 +var updateData = {
    1.32 +  name : "testOperatorApp1",
    1.33 +  version : 2,
    1.34 +  size : 767,
    1.35 +  package_path: "http://test/tests/dom/apps/tests/file_packaged_app.sjs",
    1.36 +  description: "Updated even faster than Firefox, just to annoy slashdotters",
    1.37 +  developer: {
    1.38 +    name: "Tester Operator App",
    1.39 +    url: "http://mochi.test:8888"
    1.40 +  }
    1.41 +};
    1.42 +
    1.43 +var manifestData = {
    1.44 +  name : "testOperatorApp1",
    1.45 +  version : 2,
    1.46 +  description: "Updated even faster than Firefox, just to annoy slashdotters",
    1.47 +  launch_path: "index.html",
    1.48 +  developer: {
    1.49 +    name: "Tester Operator App",
    1.50 +    url: "http://mochi.test:8888"
    1.51 +  },
    1.52 +  default_locale: "en-US"
    1.53 +};
    1.54 +
    1.55 +var metadataData = {
    1.56 +  id: "testOperatorApp1",
    1.57 +  installOrigin: "http://mochi.test:8888",
    1.58 +  manifestURL: "http://test/tests/dom/apps/tests/file_packaged_app.sjs",
    1.59 +  origin: "http://test"
    1.60 +};
    1.61 +
    1.62 +function writeFile(aFile, aData, aCb) {
    1.63 +  debug("Saving " + aFile.path);
    1.64 +  // Initialize the file output stream.
    1.65 +  let ostream = FileUtils.openSafeFileOutputStream(aFile);
    1.66 +
    1.67 +  // Obtain a converter to convert our data to a UTF-8 encoded input stream.
    1.68 +  let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
    1.69 +                  .createInstance(Ci.nsIScriptableUnicodeConverter);
    1.70 +  converter.charset = "UTF-8";
    1.71 +
    1.72 +  // Asynchronously copy the data to the file.
    1.73 +  let istream = converter.convertToInputStream(aData);
    1.74 +  NetUtil.asyncCopy(istream, ostream, function(rc) {
    1.75 +    FileUtils.closeSafeFileOutputStream(ostream);
    1.76 +    if (aCb)
    1.77 +      aCb();
    1.78 +  });
    1.79 +}
    1.80 +
    1.81 +// File and resources helpers
    1.82 +function addZipEntry(zipWriter, entry, entryName) {
    1.83 +  var stream = Cc["@mozilla.org/io/string-input-stream;1"]
    1.84 +               .createInstance(Ci.nsIStringInputStream);
    1.85 +  stream.setData(entry, entry.length);
    1.86 +  zipWriter.addEntryStream(entryName, Date.now(),
    1.87 +                           Ci.nsIZipWriter.COMPRESSION_BEST, stream, false);
    1.88 +}
    1.89 +
    1.90 +function setupDataDirs(aCb) {
    1.91 +  let dirNum = "tmp_" + Math.floor(Math.random() * 10000000 + 1);
    1.92 +  let tmpDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps"], true,
    1.93 +                                true);
    1.94 +  let appDir = FileUtils.getDir("TmpD", [dirNum, "singlevariantapps",
    1.95 +                                "testOperatorApp1"], true, true);
    1.96 +
    1.97 +  singlevariantDir = tmpDir.path;
    1.98 +  let singlevariantFile = tmpDir.clone();
    1.99 +  singlevariantFile.append("singlevariantconf.json");
   1.100 +
   1.101 +
   1.102 +  writeFile(singlevariantFile, JSON.stringify({"214-007":["testOperatorApp1"]}),
   1.103 +            function() {
   1.104 +    let indexhtml = "<html></html>";
   1.105 +    let manifest = JSON.stringify(manifestData);
   1.106 +    // Create the application package.
   1.107 +    var zipWriter = Cc["@mozilla.org/zipwriter;1"]
   1.108 +                    .createInstance(Ci.nsIZipWriter);
   1.109 +    var zipFile = FileUtils.getFile("TmpD", [
   1.110 +                                      dirNum,
   1.111 +                                      "singlevariantapps",
   1.112 +                                      "testOperatorApp1",
   1.113 +                                      "application.zip"]);
   1.114 +    zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
   1.115 +    addZipEntry(zipWriter, indexhtml, "index.html");
   1.116 +    addZipEntry(zipWriter, manifest, "manifest.webapp");
   1.117 +    zipWriter.close();
   1.118 +
   1.119 +    var metadataFile = appDir.clone();
   1.120 +    metadataFile.append("metadata.json");
   1.121 +    writeFile(metadataFile, JSON.stringify(metadataData), function() {
   1.122 +      var updateFile = appDir.clone();
   1.123 +      updateFile.append("update.webapp");
   1.124 +      writeFile(updateFile, JSON.stringify(updateData), aCb);
   1.125 +    });
   1.126 +  });
   1.127 +}
   1.128 +
   1.129 +function next() {
   1.130 +  index += 1;
   1.131 +  if (index >= steps.length) {
   1.132 +    ok(false, "Shouldn't get here!");
   1.133 +    return;
   1.134 +  }
   1.135 +  try {
   1.136 +    steps[index]();
   1.137 +  } catch(ex) {
   1.138 +    ok(false, "Caught exception", ex);
   1.139 +  }
   1.140 +}
   1.141 +
   1.142 +function go() {
   1.143 +  next();
   1.144 +}
   1.145 +
   1.146 +function finish() {
   1.147 +  SimpleTest.finish();
   1.148 +}
   1.149 +
   1.150 +function mozAppsError() {
   1.151 +  ok(false, "mozApps error: " + this.error.name);
   1.152 +  finish();
   1.153 +}
   1.154 +
   1.155 +function installOperatorApp(aMcc, aMnc) {
   1.156 +  OperatorAppsRegistry.appsDir = singlevariantDir;
   1.157 +  OperatorAppsRegistry._installOperatorApps(aMcc, aMnc);
   1.158 +}
   1.159 +
   1.160 +function checkAppState(aApp,
   1.161 +                       aVersion,
   1.162 +                       aExpectedApp,
   1.163 +                       aCb) {
   1.164 +  debug(JSON.stringify(aApp, null, 2));
   1.165 +  if (aApp.manifest) {
   1.166 +    debug(JSON.stringify(aApp.manifest, null, 2));
   1.167 +  }
   1.168 +
   1.169 +  if (aExpectedApp.name) {
   1.170 +    if (aApp.manifest) {
   1.171 +      is(aApp.manifest.name, aExpectedApp.name, "Check name");
   1.172 +    }
   1.173 +    is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
   1.174 +  }
   1.175 +  if (aApp.manifest) {
   1.176 +    is(aApp.manifest.version, aVersion, "Check version");
   1.177 +  }
   1.178 +  if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
   1.179 +    is(aApp.manifest.size, aExpectedApp.size, "Check size");
   1.180 +  }
   1.181 +  if (aApp.manifest) {
   1.182 +    is(aApp.manifest.launch_path, "index.html", "Check launch path");
   1.183 +  }
   1.184 +  if (aExpectedApp.manifestURL) {
   1.185 +    is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
   1.186 +  }
   1.187 +  if (aExpectedApp.installOrigin) {
   1.188 +    is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
   1.189 +  }
   1.190 +  ok(aApp.removable, "Removable app");
   1.191 +  if (typeof aExpectedApp.progress !== "undefined") {
   1.192 +    todo(aApp.progress == aExpectedApp.progress, "Check progress");
   1.193 +  }
   1.194 +  if (aExpectedApp.installState) {
   1.195 +    is(aApp.installState, aExpectedApp.installState, "Check installState");
   1.196 +  }
   1.197 +  if (typeof aExpectedApp.downloadAvailable !== "undefined") {
   1.198 +    is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
   1.199 +       "Check download available");
   1.200 +  }
   1.201 +  if (typeof aExpectedApp.downloading !== "undefined") {
   1.202 +    is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
   1.203 +  }
   1.204 +  if (typeof aExpectedApp.downloadSize !== "undefined") {
   1.205 +    is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
   1.206 +  }
   1.207 +  if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
   1.208 +    is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
   1.209 +       "Check readyToApplyDownload");
   1.210 +  }
   1.211 +  if (aCb && typeof aCb === 'function') {
   1.212 +    aCb();
   1.213 +  }
   1.214 +  return;
   1.215 +}
   1.216 +
   1.217 +var steps = [
   1.218 +  function() {
   1.219 +    setupDataDirs(next);
   1.220 +    ok(true, "Data directory set up to " + singlevariantDir);
   1.221 +  },
   1.222 +  function() {
   1.223 +    ok(true, "autoConfirmAppInstall");
   1.224 +    SpecialPowers.autoConfirmAppInstall(next);
   1.225 +  },
   1.226 +  function() {
   1.227 +    ok(true, "== TEST == Install operator app");
   1.228 +
   1.229 +    navigator.mozApps.mgmt.oninstall = function(evt) {
   1.230 +      ok(true, "Got oninstall event");
   1.231 +      gApp = evt.application;
   1.232 +      gApp.ondownloaderror = function() {
   1.233 +        ok(false, "Download error " + gApp.downloadError.name);
   1.234 +        finish();
   1.235 +      };
   1.236 +      let downloadsuccessHandler = function() {
   1.237 +        gApp.ondownloadsuccess = null;
   1.238 +        ok(true, "App downloaded");
   1.239 +
   1.240 +        var expected = {
   1.241 +          name: manifestData.name,
   1.242 +          manifestURL: metadataData.manifestURL,
   1.243 +          installOrigin: metadataData.installOrigin,
   1.244 +          progress: 0,
   1.245 +          installState: "installed",
   1.246 +          downloadAvailable: false,
   1.247 +          downloading: false,
   1.248 +          downloadSize: 767,
   1.249 +          readyToApplyDownload: false
   1.250 +        };
   1.251 +        checkAppState(gApp, manifestData.version, expected, next);
   1.252 +      };
   1.253 +      gApp.ondownloadsuccess = downloadsuccessHandler;
   1.254 +      if (!gApp.downloading && gApp.ondownloadsuccess) {
   1.255 +        ok(true, "Got an earlier event");
   1.256 +        // Seems we set the handler too late.
   1.257 +        gApp.ondownloadsuccess = null;
   1.258 +        downloadsuccessHandler();
   1.259 +      }
   1.260 +    };
   1.261 +    installOperatorApp("214", "007");
   1.262 +  },
   1.263 +  function() {
   1.264 +    ok(true, "all done!\n");
   1.265 +    finish();
   1.266 +  }
   1.267 +];
   1.268 +
   1.269 +go();
   1.270 +

mercurial