toolkit/webapps/tests/test_hosted.xul

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

mercurial