toolkit/webapps/tests/test_packaged_launch_no_registry.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/webapps/tests/test_packaged_launch_no_registry.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,259 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=981249
     1.9 +-->
    1.10 +<window title="Mozilla Bug 981249"
    1.11 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    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 +  <script type="application/javascript" src="head.js"/>
    1.17 +
    1.18 +  <!-- test results are displayed in the html:body -->
    1.19 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.20 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=981249"
    1.21 +     target="_blank">Mozilla Bug 981249</a>
    1.22 +  </body>
    1.23 +
    1.24 +<script type="application/javascript">
    1.25 +<![CDATA[
    1.26 +
    1.27 +/** Test for Bug 981249 **/
    1.28 +
    1.29 +"use strict";
    1.30 +
    1.31 +SimpleTest.waitForExplicitFinish();
    1.32 +
    1.33 +Cu.import("resource://gre/modules/Services.jsm");
    1.34 +Cu.import("resource://gre/modules/NativeApp.jsm");
    1.35 +Cu.import("resource://gre/modules/WebappOSUtils.jsm");
    1.36 +Cu.import("resource://gre/modules/Promise.jsm");
    1.37 +
    1.38 +const PR_RDWR        = 0x04;
    1.39 +const PR_CREATE_FILE = 0x08;
    1.40 +const PR_TRUNCATE    = 0x20;
    1.41 +
    1.42 +let manifest = {
    1.43 +  name: "test_desktop_packaged_launch_no_registry",
    1.44 +  version: "0.1a",
    1.45 +  size: 777,
    1.46 +  package_path: "/data/app.zip",
    1.47 +  launch_path: "/index.html",
    1.48 +};
    1.49 +
    1.50 +let app = {
    1.51 +  name: "test_desktop_packaged_launch_no_registry",
    1.52 +  manifestURL: "http://127.0.0.1:8888/sample_no_registry.manifest",
    1.53 +  manifest: manifest,
    1.54 +  updateManifest: manifest,
    1.55 +  origin: "app://test_desktop_packaged_launch_no_registry/",
    1.56 +  categories: [],
    1.57 +  installOrigin: "http://127.0.0.1:8888/",
    1.58 +  receipts: [],
    1.59 +  installTime: Date.now(),
    1.60 +};
    1.61 +
    1.62 +let profileDir;
    1.63 +let installPath;
    1.64 +let exePath;
    1.65 +let appProcess = Cc["@mozilla.org/process/util;1"].
    1.66 +                 createInstance(Ci.nsIProcess);
    1.67 +
    1.68 +let cleanup;
    1.69 +
    1.70 +if (LINUX) {
    1.71 +  installPath = OS.Path.join(OS.Constants.Path.homeDir, "." + WebappOSUtils.getUniqueName(app));
    1.72 +  exePath = OS.Path.join(installPath, "webapprt-stub");
    1.73 +
    1.74 +  let xdg_data_home = Cc["@mozilla.org/process/environment;1"].
    1.75 +                      getService(Ci.nsIEnvironment).
    1.76 +                      get("XDG_DATA_HOME");
    1.77 +  if (!xdg_data_home) {
    1.78 +    xdg_data_home = OS.Path.join(OS.Constants.Path.homeDir, ".local", "share");
    1.79 +  }
    1.80 +
    1.81 +  let desktopINI = OS.Path.join(xdg_data_home, "applications",
    1.82 +                                "owa-" + WebappOSUtils.getUniqueName(app) + ".desktop");
    1.83 +
    1.84 +  cleanup = function() {
    1.85 +    return Task.spawn(function*() {
    1.86 +      if (appProcess.isRunning) {
    1.87 +        appProcess.kill();
    1.88 +      }
    1.89 +
    1.90 +      if (profileDir) {
    1.91 +        yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true });
    1.92 +      }
    1.93 +
    1.94 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
    1.95 +
    1.96 +      yield OS.File.remove(desktopINI, { ignoreAbsent: true });
    1.97 +    });
    1.98 +  };
    1.99 +} else if (WIN) {
   1.100 +  installPath = OS.Path.join(OS.Constants.Path.winAppDataDir, WebappOSUtils.getUniqueName(app));
   1.101 +  exePath = OS.Path.join(installPath, "test_desktop_packaged_launch_no_registry.exe");
   1.102 +
   1.103 +  let desktopShortcut = OS.Path.join(OS.Constants.Path.desktopDir, "test_desktop_packaged_launch_no_registry.lnk");
   1.104 +  let startMenuShortcut = OS.Path.join(OS.Constants.Path.winStartMenuProgsDir, "test_desktop_packaged_launch_no_registry.lnk");
   1.105 +
   1.106 +  cleanup = function() {
   1.107 +    return Task.spawn(function*() {
   1.108 +      if (appProcess.isRunning) {
   1.109 +        appProcess.kill();
   1.110 +      }
   1.111 +
   1.112 +      let uninstallKey;
   1.113 +      try {
   1.114 +        uninstallKey = Cc["@mozilla.org/windows-registry-key;1"].
   1.115 +                       createInstance(Ci.nsIWindowsRegKey);
   1.116 +        uninstallKey.open(uninstallKey.ROOT_KEY_CURRENT_USER,
   1.117 +                          "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
   1.118 +                          uninstallKey.ACCESS_WRITE);
   1.119 +        if (uninstallKey.hasChild(WebappOSUtils.getUniqueName(app))) {
   1.120 +          uninstallKey.removeChild(WebappOSUtils.getUniqueName(app));
   1.121 +        }
   1.122 +      } catch (e) {
   1.123 +      } finally {
   1.124 +        if (uninstallKey) {
   1.125 +          uninstallKey.close();
   1.126 +        }
   1.127 +      }
   1.128 +
   1.129 +      if (profileDir) {
   1.130 +        yield OS.File.removeDir(profileDir.parent.parent.path, { ignoreAbsent: true });
   1.131 +      }
   1.132 +
   1.133 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
   1.134 +
   1.135 +      yield OS.File.remove(desktopShortcut, { ignoreAbsent: true });
   1.136 +      yield OS.File.remove(startMenuShortcut, { ignoreAbsent: true });
   1.137 +    });
   1.138 +  };
   1.139 +} else if (MAC) {
   1.140 +  installPath = OS.Path.join(OS.Constants.Path.homeDir, "Applications", "test_desktop_packaged_launch_no_registry.app");
   1.141 +  exePath = OS.Path.join(installPath, "Contents", "MacOS", "webapprt");
   1.142 +
   1.143 +  let appProfileDir = OS.Path.join(OS.Constants.Path.macUserLibDir, "Application Support",
   1.144 +                                   WebappOSUtils.getUniqueName(app));
   1.145 +
   1.146 +  cleanup = function() {
   1.147 +    return Task.spawn(function*() {
   1.148 +      if (appProcess.isRunning) {
   1.149 +        appProcess.kill();
   1.150 +      }
   1.151 +
   1.152 +      if (profileDir) {
   1.153 +        yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true });
   1.154 +      }
   1.155 +
   1.156 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
   1.157 +
   1.158 +      yield OS.File.removeDir(appProfileDir, { ignoreAbsent: true });
   1.159 +    });
   1.160 +  };
   1.161 +}
   1.162 +
   1.163 +function buildAppPackage() {
   1.164 +  let zipFile = getFile(OS.Constants.Path.profileDir, "sample.zip");
   1.165 +
   1.166 +  let zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
   1.167 +  zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
   1.168 +  zipWriter.addEntryFile("index.html",
   1.169 +                         Ci.nsIZipWriter.COMPRESSION_NONE,
   1.170 +                         getFile(getTestFilePath("data/app/index.html")),
   1.171 +                         false);
   1.172 +  zipWriter.addEntryFile("manifest.webapp",
   1.173 +                         Ci.nsIZipWriter.COMPRESSION_NONE,
   1.174 +                         getFile(getTestFilePath("data/app/manifest.webapp")),
   1.175 +                         false);
   1.176 +  zipWriter.close();
   1.177 +
   1.178 +  return zipFile.path;
   1.179 +}
   1.180 +
   1.181 +function wasAppSJSAccessed() {
   1.182 +  let deferred = Promise.defer();
   1.183 +
   1.184 +  var xhr = new XMLHttpRequest();
   1.185 +
   1.186 +  xhr.addEventListener("load", function() {
   1.187 +    let ret = (xhr.responseText == "done") ? true : false;
   1.188 +    deferred.resolve(ret);
   1.189 +  });
   1.190 +
   1.191 +  xhr.addEventListener("error", aError => deferred.reject(aError));
   1.192 +  xhr.addEventListener("abort", aError => deferred.reject(aError));
   1.193 +
   1.194 +  xhr.open('GET', 'http://test/chrome/toolkit/webapps/tests/app.sjs?testreq', true);
   1.195 +  xhr.send();
   1.196 +
   1.197 +  return deferred.promise;
   1.198 +}
   1.199 +
   1.200 +let runTest = Task.async(function*() {
   1.201 +  // Get to a clean state before the test
   1.202 +  yield cleanup();
   1.203 +
   1.204 +  SimpleTest.registerCleanupFunction(cleanup);
   1.205 +
   1.206 +  setDryRunPref();
   1.207 +
   1.208 +  let zipPath = buildAppPackage();
   1.209 +
   1.210 +  let nativeApp = new NativeApp(app, manifest, app.categories);
   1.211 +  ok(nativeApp, "NativeApp object created");
   1.212 +
   1.213 +  profileDir = nativeApp.createProfile();
   1.214 +  ok(profileDir && profileDir.exists(), "Profile directory created");
   1.215 +
   1.216 +  // On Mac build servers, we don't have enough privileges to write to /Applications,
   1.217 +  // so we install apps in a user-owned directory.
   1.218 +  if (MAC) {
   1.219 +    nativeApp._rootInstallDir = OS.Path.join(OS.Constants.Path.homeDir, "Applications");
   1.220 +    yield OS.File.makeDir(nativeApp._rootInstallDir, { ignoreExisting: true });
   1.221 +  }
   1.222 +
   1.223 +  // Install application
   1.224 +  info("Test installation");
   1.225 +  yield nativeApp.install(manifest, zipPath);
   1.226 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.227 +    yield wait(1000);
   1.228 +  }
   1.229 +  ok(true, "App launchable");
   1.230 +
   1.231 +  let exeFile = getFile(exePath);
   1.232 +
   1.233 +  ok(exeFile.isExecutable(), "webapprt executable is executable");
   1.234 +
   1.235 +  let appClosed = false;
   1.236 +
   1.237 +  appProcess.init(exeFile)
   1.238 +  appProcess.runAsync([], 0, () => appClosed = true);
   1.239 +
   1.240 +  while (!(yield wasAppSJSAccessed()) && !appClosed) {
   1.241 +    yield wait(1000);
   1.242 +  }
   1.243 +  ok(!appClosed, "App was launched and is still running");
   1.244 +
   1.245 +  SimpleTest.finish();
   1.246 +});
   1.247 +
   1.248 +// The test doesn't work yet on Mac OS X 10.6 machines.
   1.249 +// See bug 993690.
   1.250 +if (MAC_106) {
   1.251 +  todo(false, "The test doesn't work on Mac OS X 10.6 machines");
   1.252 +  SimpleTest.finish();
   1.253 +} else {
   1.254 +  runTest().then(null, function(e) {
   1.255 +    ok(false, "Error during test: " + e);
   1.256 +    SimpleTest.finish();
   1.257 +  });
   1.258 +}
   1.259 +
   1.260 +]]>
   1.261 +</script>
   1.262 +</window>

mercurial