toolkit/webapps/tests/test_packaged.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/webapps/tests/test_packaged.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,341 @@
     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=898647
     1.9 +-->
    1.10 +<window title="Mozilla Bug 898647"
    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" src="head.js"/>
    1.15 +
    1.16 +  <!-- test results are displayed in the html:body -->
    1.17 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.18 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=898647"
    1.19 +     target="_blank">Mozilla Bug 898647</a>
    1.20 +  </body>
    1.21 +
    1.22 +<script type="application/javascript">
    1.23 +<![CDATA[
    1.24 +
    1.25 +/** Test for Bug 898647 **/
    1.26 +
    1.27 +"use strict";
    1.28 +
    1.29 +SimpleTest.waitForExplicitFinish();
    1.30 +
    1.31 +Cu.import("resource://gre/modules/Services.jsm");
    1.32 +Cu.import("resource://gre/modules/NativeApp.jsm");
    1.33 +Cu.import("resource://gre/modules/WebappOSUtils.jsm");
    1.34 +
    1.35 +let zipPath = OS.Path.join(OS.Constants.Path.profileDir, "sample.zip");
    1.36 +
    1.37 +let manifest = {
    1.38 +  name: "Sample packaged app",
    1.39 +  version: "0.1a",
    1.40 +  size: 777,
    1.41 +  package_path: "/sample.zip",
    1.42 +};
    1.43 +
    1.44 +let app = {
    1.45 +  name: "Sample packaged app",
    1.46 +  manifestURL: "http://example.com/sample.manifest",
    1.47 +  manifest: manifest,
    1.48 +  updateManifest: manifest,
    1.49 +  origin: "http://example.com/",
    1.50 +  categories: [],
    1.51 +  installOrigin: "http://example.com/",
    1.52 +  receipts: [],
    1.53 +  installTime: Date.now(),
    1.54 +};
    1.55 +
    1.56 +let profileDir;
    1.57 +let profilesIni;
    1.58 +let installPath;
    1.59 +
    1.60 +let installedFiles;
    1.61 +let tempUpdatedFiles;
    1.62 +let updatedFiles;
    1.63 +
    1.64 +let cleanup;
    1.65 +
    1.66 +if (LINUX) {
    1.67 +  installPath = OS.Path.join(OS.Constants.Path.homeDir, "." + WebappOSUtils.getUniqueName(app));
    1.68 +
    1.69 +  let xdg_data_home = Cc["@mozilla.org/process/environment;1"].
    1.70 +                      getService(Ci.nsIEnvironment).
    1.71 +                      get("XDG_DATA_HOME");
    1.72 +  if (!xdg_data_home) {
    1.73 +    xdg_data_home = OS.Path.join(OS.Constants.Path.homeDir, ".local", "share");
    1.74 +  }
    1.75 +
    1.76 +  let desktopINI = OS.Path.join(xdg_data_home, "applications",
    1.77 +                                "owa-" + WebappOSUtils.getUniqueName(app) + ".desktop");
    1.78 +
    1.79 +  installedFiles = [
    1.80 +    OS.Path.join(installPath, "icon.png"),
    1.81 +    OS.Path.join(installPath, "webapprt-stub"),
    1.82 +    OS.Path.join(installPath, "webapp.json"),
    1.83 +    OS.Path.join(installPath, "webapp.ini"),
    1.84 +    OS.Path.join(installPath, "application.zip"),
    1.85 +    desktopINI,
    1.86 +  ];
    1.87 +  tempUpdatedFiles = [
    1.88 +    OS.Path.join(installPath, "update", "icon.png"),
    1.89 +    OS.Path.join(installPath, "update", "webapp.json"),
    1.90 +    OS.Path.join(installPath, "update", "webapp.ini"),
    1.91 +    OS.Path.join(installPath, "update", "application.zip"),
    1.92 +  ];
    1.93 +  updatedFiles = [
    1.94 +    OS.Path.join(installPath, "icon.png"),
    1.95 +    OS.Path.join(installPath, "webapp.json"),
    1.96 +    OS.Path.join(installPath, "webapp.ini"),
    1.97 +    OS.Path.join(installPath, "application.zip"),
    1.98 +    desktopINI,
    1.99 +  ];
   1.100 +
   1.101 +  profilesIni = OS.Path.join(installPath, "profiles.ini");
   1.102 +
   1.103 +  cleanup = function() {
   1.104 +    return Task.spawn(function*() {
   1.105 +      if (profileDir) {
   1.106 +        yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true });
   1.107 +      }
   1.108 +
   1.109 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
   1.110 +
   1.111 +      yield OS.File.remove(desktopINI, { ignoreAbsent: true });
   1.112 +    });
   1.113 +  };
   1.114 +} else if (WIN) {
   1.115 +  installPath = OS.Path.join(OS.Constants.Path.winAppDataDir, WebappOSUtils.getUniqueName(app));
   1.116 +
   1.117 +  let desktopShortcut = OS.Path.join(OS.Constants.Path.desktopDir, "Sample packaged app.lnk");
   1.118 +  let startMenuShortcut = OS.Path.join(OS.Constants.Path.winStartMenuProgsDir, "Sample packaged app.lnk");
   1.119 +
   1.120 +  installedFiles = [
   1.121 +    OS.Path.join(installPath, "Sample packaged app.exe"),
   1.122 +    OS.Path.join(installPath, "chrome", "icons", "default", "default.ico"),
   1.123 +    OS.Path.join(installPath, "webapp.json"),
   1.124 +    OS.Path.join(installPath, "webapp.ini"),
   1.125 +    OS.Path.join(installPath, "application.zip"),
   1.126 +    OS.Path.join(installPath, "uninstall", "shortcuts_log.ini"),
   1.127 +    OS.Path.join(installPath, "uninstall", "uninstall.log"),
   1.128 +    OS.Path.join(installPath, "uninstall", "webapp-uninstaller.exe"),
   1.129 +    desktopShortcut,
   1.130 +    startMenuShortcut,
   1.131 +  ];
   1.132 +  tempUpdatedFiles = [
   1.133 +    OS.Path.join(installPath, "update", "chrome", "icons", "default", "default.ico"),
   1.134 +    OS.Path.join(installPath, "update", "webapp.json"),
   1.135 +    OS.Path.join(installPath, "update", "webapp.ini"),
   1.136 +    OS.Path.join(installPath, "update", "application.zip"),
   1.137 +    OS.Path.join(installPath, "update", "uninstall", "shortcuts_log.ini"),
   1.138 +    OS.Path.join(installPath, "update", "uninstall", "uninstall.log"),
   1.139 +    OS.Path.join(installPath, "update", "uninstall", "webapp-uninstaller.exe"),
   1.140 +  ];
   1.141 +  updatedFiles = [
   1.142 +    OS.Path.join(installPath, "chrome", "icons", "default", "default.ico"),
   1.143 +    OS.Path.join(installPath, "webapp.json"),
   1.144 +    OS.Path.join(installPath, "webapp.ini"),
   1.145 +    OS.Path.join(installPath, "application.zip"),
   1.146 +    OS.Path.join(installPath, "uninstall", "shortcuts_log.ini"),
   1.147 +    OS.Path.join(installPath, "uninstall", "uninstall.log"),
   1.148 +    desktopShortcut,
   1.149 +    startMenuShortcut,
   1.150 +  ];
   1.151 +
   1.152 +  profilesIni = OS.Path.join(installPath, "profiles.ini");
   1.153 +
   1.154 +  cleanup = function() {
   1.155 +    return Task.spawn(function*() {
   1.156 +      let uninstallKey;
   1.157 +      try {
   1.158 +        uninstallKey = Cc["@mozilla.org/windows-registry-key;1"].
   1.159 +                       createInstance(Ci.nsIWindowsRegKey);
   1.160 +        uninstallKey.open(uninstallKey.ROOT_KEY_CURRENT_USER,
   1.161 +                          "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
   1.162 +                          uninstallKey.ACCESS_WRITE);
   1.163 +        if (uninstallKey.hasChild(WebappOSUtils.getUniqueName(app))) {
   1.164 +          uninstallKey.removeChild(WebappOSUtils.getUniqueName(app));
   1.165 +        }
   1.166 +      } catch (e) {
   1.167 +      } finally {
   1.168 +        if (uninstallKey) {
   1.169 +          uninstallKey.close();
   1.170 +        }
   1.171 +      }
   1.172 +
   1.173 +      if (profileDir) {
   1.174 +        yield OS.File.removeDir(profileDir.parent.parent.path, { ignoreAbsent: true });
   1.175 +      }
   1.176 +
   1.177 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
   1.178 +
   1.179 +      yield OS.File.remove(desktopShortcut, { ignoreAbsent: true });
   1.180 +      yield OS.File.remove(startMenuShortcut, { ignoreAbsent: true });
   1.181 +    });
   1.182 +  };
   1.183 +} else if (MAC) {
   1.184 +  installPath = OS.Path.join(OS.Constants.Path.homeDir, "Applications", "Sample packaged app.app");
   1.185 +  let appProfileDir = OS.Path.join(OS.Constants.Path.macUserLibDir, "Application Support",
   1.186 +                                   WebappOSUtils.getUniqueName(app));
   1.187 +
   1.188 +  installedFiles = [
   1.189 +    OS.Path.join(installPath, "Contents", "Info.plist"),
   1.190 +    OS.Path.join(installPath, "Contents", "MacOS", "webapprt"),
   1.191 +    OS.Path.join(installPath, "Contents", "MacOS", "webapp.ini"),
   1.192 +    OS.Path.join(installPath, "Contents", "Resources", "appicon.icns"),
   1.193 +    OS.Path.join(installPath, "Contents", "Resources", "application.zip"),
   1.194 +    OS.Path.join(appProfileDir, "webapp.json"),
   1.195 +  ];
   1.196 +  tempUpdatedFiles = [
   1.197 +    OS.Path.join(installPath, "update", "Contents", "Info.plist"),
   1.198 +    OS.Path.join(installPath, "update", "Contents", "MacOS", "webapp.ini"),
   1.199 +    OS.Path.join(installPath, "update", "Contents", "Resources", "appicon.icns"),
   1.200 +    OS.Path.join(installPath, "update", "Contents", "Resources", "application.zip"),
   1.201 +    OS.Path.join(installPath, "update", "webapp.json"),
   1.202 +  ];
   1.203 +  updatedFiles = [
   1.204 +    OS.Path.join(installPath, "Contents", "Info.plist"),
   1.205 +    OS.Path.join(installPath, "Contents", "MacOS", "webapp.ini"),
   1.206 +    OS.Path.join(installPath, "Contents", "Resources", "appicon.icns"),
   1.207 +    OS.Path.join(installPath, "Contents", "Resources", "application.zip"),
   1.208 +    OS.Path.join(appProfileDir, "webapp.json"),
   1.209 +  ];
   1.210 +
   1.211 +  profilesIni = OS.Path.join(appProfileDir, "profiles.ini");
   1.212 +
   1.213 +  cleanup = function() {
   1.214 +    return Task.spawn(function*() {
   1.215 +      if (profileDir) {
   1.216 +        yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true });
   1.217 +      }
   1.218 +
   1.219 +      yield OS.File.removeDir(installPath, { ignoreAbsent: true });
   1.220 +
   1.221 +      yield OS.File.removeDir(appProfileDir, { ignoreAbsent: true });
   1.222 +    });
   1.223 +  };
   1.224 +}
   1.225 +
   1.226 +let runTest = Task.async(function*() {
   1.227 +  // Get to a clean state before the test
   1.228 +  yield cleanup();
   1.229 +
   1.230 +  SimpleTest.registerCleanupFunction(cleanup);
   1.231 +
   1.232 +  setDryRunPref();
   1.233 +
   1.234 +  let zipFile = yield OS.File.open(zipPath, { create: true });
   1.235 +  yield zipFile.close();
   1.236 +
   1.237 +  let nativeApp = new NativeApp(app, manifest, app.categories);
   1.238 +  ok(nativeApp, "NativeApp object created");
   1.239 +
   1.240 +  info("Test update for an application that isn't installed");
   1.241 +  try {
   1.242 +    yield nativeApp.prepareUpdate(manifest, zipPath);
   1.243 +    ok(false, "Didn't thrown");
   1.244 +  } catch (ex) {
   1.245 +    is(ex, "The application isn't installed", "Exception thrown");
   1.246 +  }
   1.247 +
   1.248 +  profileDir = nativeApp.createProfile();
   1.249 +  ok(profileDir && profileDir.exists(), "Profile directory created");
   1.250 +  ok((yield OS.File.exists(profilesIni)), "profiles.ini file created");
   1.251 +
   1.252 +  // On Mac build servers, we don't have enough privileges to write to /Applications,
   1.253 +  // so we install apps in a user-owned directory.
   1.254 +  if (MAC) {
   1.255 +    nativeApp._rootInstallDir = OS.Path.join(OS.Constants.Path.homeDir, "Applications");
   1.256 +    yield OS.File.makeDir(nativeApp._rootInstallDir, { ignoreExisting: true });
   1.257 +  }
   1.258 +
   1.259 +  // Install application
   1.260 +  info("Test installation");
   1.261 +  yield nativeApp.install(manifest, zipPath);
   1.262 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.263 +    yield wait(1000);
   1.264 +  }
   1.265 +  ok(true, "App launchable");
   1.266 +  ok((yield checkFiles(installedFiles)), "Files correctly written");
   1.267 +  is(WebappOSUtils.getInstallPath(app), installPath, "getInstallPath == installPath");
   1.268 +
   1.269 +  let stat = yield OS.File.stat(installPath);
   1.270 +  let installTime = stat.lastModificationDate;
   1.271 +
   1.272 +  // Wait one second, otherwise the last modification date is the same.
   1.273 +  yield wait(1000);
   1.274 +
   1.275 +  // Reinstall application
   1.276 +  info("Test reinstallation");
   1.277 +
   1.278 +  zipFile = yield OS.File.open(zipPath, { create: true });
   1.279 +  yield zipFile.close();
   1.280 +
   1.281 +  yield nativeApp.install(manifest, zipPath);
   1.282 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.283 +    yield wait(1000);
   1.284 +  }
   1.285 +  ok(true, "App launchable");
   1.286 +  ok((yield checkFiles(installedFiles)), "Installation not corrupted");
   1.287 +  ok((yield checkFiles(tempUpdatedFiles)), "Files correctly written in the update subdirectory");
   1.288 +
   1.289 +  yield nativeApp.applyUpdate();
   1.290 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.291 +    yield wait(1000);
   1.292 +  }
   1.293 +  ok(true, "App launchable");
   1.294 +  ok((yield checkFiles(installedFiles)), "Installation not corrupted");
   1.295 +  ok(!(yield OS.File.exists(OS.Path.join(installPath, "update"))), "Update directory removed");
   1.296 +  ok((yield checkDateHigherThan(updatedFiles, installTime)), "Modification date higher");
   1.297 +
   1.298 +  stat = yield OS.File.stat(installPath);
   1.299 +  installTime = stat.lastModificationDate;
   1.300 +
   1.301 +  // Wait one second, otherwise the last modification date is the same.
   1.302 +  yield wait(1000);
   1.303 +
   1.304 +  // Update application
   1.305 +  info("Test update");
   1.306 +
   1.307 +  zipFile = yield OS.File.open(zipPath, { create: true });
   1.308 +  yield zipFile.close();
   1.309 +
   1.310 +  yield nativeApp.prepareUpdate(manifest, zipPath);
   1.311 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.312 +    yield wait(1000);
   1.313 +  }
   1.314 +  ok(true, "App launchable");
   1.315 +  ok((yield checkFiles(installedFiles)), "Installation not corrupted");
   1.316 +  ok((yield checkFiles(tempUpdatedFiles)), "Files correctly written in the update subdirectory");
   1.317 +
   1.318 +  yield nativeApp.applyUpdate();
   1.319 +  while (!WebappOSUtils.isLaunchable(app)) {
   1.320 +    yield wait(1000);
   1.321 +  }
   1.322 +  ok(true, "App launchable");
   1.323 +  ok((yield checkFiles(installedFiles)), "Installation not corrupted");
   1.324 +  ok(!(yield OS.File.exists(OS.Path.join(installPath, "update"))), "Update directory removed");
   1.325 +  ok((yield checkDateHigherThan(updatedFiles, installTime)), "Modification date higher");
   1.326 +
   1.327 +  SimpleTest.finish();
   1.328 +});
   1.329 +
   1.330 +// The test doesn't work yet on Mac OS X 10.6 machines.
   1.331 +// See bug 993690.
   1.332 +if (MAC_106) {
   1.333 +  todo(false, "The test doesn't work on Mac OS X 10.6 machines");
   1.334 +  SimpleTest.finish();
   1.335 +} else {
   1.336 +  runTest().then(null, function(e) {
   1.337 +    ok(false, "Error during test: " + e);
   1.338 +    SimpleTest.finish();
   1.339 +  });
   1.340 +}
   1.341 +
   1.342 +]]>
   1.343 +</script>
   1.344 +</window>

mercurial