1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/webapps/tests/test_hosted_launch.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,229 @@ 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" 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=981249" 1.19 + target="_blank">Mozilla Bug 981249</a> 1.20 + </body> 1.21 + 1.22 +<script type="application/javascript"> 1.23 +<![CDATA[ 1.24 + 1.25 +/** Test for Bug 981249 **/ 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 +Cu.import("resource://gre/modules/Promise.jsm"); 1.35 + 1.36 +let manifest = { 1.37 + name: "test_desktop_hosted_launch", 1.38 + launch_path: "/chrome/toolkit/webapps/tests/app.sjs?appreq", 1.39 +}; 1.40 + 1.41 +let app = { 1.42 + name: "test_desktop_hosted_launch", 1.43 + manifestURL: "http://127.0.0.1:8888/sample.manifest", 1.44 + manifest: manifest, 1.45 + origin: "http://127.0.0.1:8888/", 1.46 + categories: [], 1.47 + installOrigin: "http://127.0.0.1:8888/", 1.48 + receipts: [], 1.49 + installTime: Date.now(), 1.50 +}; 1.51 + 1.52 +let profileDir; 1.53 +let installPath; 1.54 +let exePath; 1.55 +let appProcess = Cc["@mozilla.org/process/util;1"]. 1.56 + createInstance(Ci.nsIProcess); 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 + exePath = OS.Path.join(installPath, "webapprt-stub"); 1.63 + 1.64 + let xdg_data_home = Cc["@mozilla.org/process/environment;1"]. 1.65 + getService(Ci.nsIEnvironment). 1.66 + get("XDG_DATA_HOME"); 1.67 + if (!xdg_data_home) { 1.68 + xdg_data_home = OS.Path.join(OS.Constants.Path.homeDir, ".local", "share"); 1.69 + } 1.70 + 1.71 + let desktopINI = OS.Path.join(xdg_data_home, "applications", 1.72 + "owa-" + WebappOSUtils.getUniqueName(app) + ".desktop"); 1.73 + 1.74 + cleanup = function() { 1.75 + return Task.spawn(function*() { 1.76 + if (appProcess.isRunning) { 1.77 + appProcess.kill(); 1.78 + } 1.79 + 1.80 + if (profileDir) { 1.81 + yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true }); 1.82 + } 1.83 + 1.84 + yield OS.File.removeDir(installPath, { ignoreAbsent: true }); 1.85 + 1.86 + yield OS.File.remove(desktopINI, { ignoreAbsent: true }); 1.87 + }); 1.88 + }; 1.89 +} else if (WIN) { 1.90 + installPath = OS.Path.join(OS.Constants.Path.winAppDataDir, WebappOSUtils.getUniqueName(app)); 1.91 + exePath = OS.Path.join(installPath, "test_desktop_hosted_launch.exe"); 1.92 + 1.93 + let desktopShortcut = OS.Path.join(OS.Constants.Path.desktopDir, "test_desktop_hosted_launch.lnk"); 1.94 + let startMenuShortcut = OS.Path.join(OS.Constants.Path.winStartMenuProgsDir, "test_desktop_hosted_launch.lnk"); 1.95 + 1.96 + cleanup = function() { 1.97 + return Task.spawn(function*() { 1.98 + if (appProcess.isRunning) { 1.99 + appProcess.kill(); 1.100 + } 1.101 + 1.102 + let uninstallKey; 1.103 + try { 1.104 + uninstallKey = Cc["@mozilla.org/windows-registry-key;1"]. 1.105 + createInstance(Ci.nsIWindowsRegKey); 1.106 + uninstallKey.open(uninstallKey.ROOT_KEY_CURRENT_USER, 1.107 + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 1.108 + uninstallKey.ACCESS_WRITE); 1.109 + if (uninstallKey.hasChild(WebappOSUtils.getUniqueName(app))) { 1.110 + uninstallKey.removeChild(WebappOSUtils.getUniqueName(app)); 1.111 + } 1.112 + } catch (e) { 1.113 + } finally { 1.114 + if (uninstallKey) { 1.115 + uninstallKey.close(); 1.116 + } 1.117 + } 1.118 + 1.119 + if (profileDir) { 1.120 + yield OS.File.removeDir(profileDir.parent.parent.path, { ignoreAbsent: true }); 1.121 + } 1.122 + 1.123 + yield OS.File.removeDir(installPath, { ignoreAbsent: true }); 1.124 + 1.125 + yield OS.File.remove(desktopShortcut, { ignoreAbsent: true }); 1.126 + yield OS.File.remove(startMenuShortcut, { ignoreAbsent: true }); 1.127 + }); 1.128 + }; 1.129 +} else if (MAC) { 1.130 + installPath = OS.Path.join(OS.Constants.Path.homeDir, "Applications", "test_desktop_hosted_launch.app"); 1.131 + exePath = OS.Path.join(installPath, "Contents", "MacOS", "webapprt"); 1.132 + 1.133 + let appProfileDir = OS.Path.join(OS.Constants.Path.macUserLibDir, "Application Support", 1.134 + WebappOSUtils.getUniqueName(app)); 1.135 + 1.136 + cleanup = function() { 1.137 + return Task.spawn(function*() { 1.138 + if (appProcess.isRunning) { 1.139 + appProcess.kill(); 1.140 + } 1.141 + 1.142 + if (profileDir) { 1.143 + yield OS.File.removeDir(profileDir.parent.path, { ignoreAbsent: true }); 1.144 + } 1.145 + 1.146 + yield OS.File.removeDir(installPath, { ignoreAbsent: true }); 1.147 + 1.148 + yield OS.File.removeDir(appProfileDir, { ignoreAbsent: true }); 1.149 + }); 1.150 + }; 1.151 +} 1.152 + 1.153 +function wasAppSJSAccessed() { 1.154 + let deferred = Promise.defer(); 1.155 + 1.156 + var xhr = new XMLHttpRequest(); 1.157 + 1.158 + xhr.addEventListener("load", function() { 1.159 + let ret = (xhr.responseText == "done") ? true : false; 1.160 + deferred.resolve(ret); 1.161 + }); 1.162 + 1.163 + xhr.addEventListener("error", aError => deferred.reject(aError)); 1.164 + xhr.addEventListener("abort", aError => deferred.reject(aError)); 1.165 + 1.166 + xhr.open('GET', 'http://test/chrome/toolkit/webapps/tests/app.sjs?testreq', true); 1.167 + xhr.send(); 1.168 + 1.169 + return deferred.promise; 1.170 +} 1.171 + 1.172 +let runTest = Task.async(function*() { 1.173 + // Get to a clean state before the test 1.174 + yield cleanup(); 1.175 + 1.176 + SimpleTest.registerCleanupFunction(cleanup); 1.177 + 1.178 + setDryRunPref(); 1.179 + 1.180 + let nativeApp = new NativeApp(app, manifest, app.categories); 1.181 + ok(nativeApp, "NativeApp object created"); 1.182 + 1.183 + profileDir = nativeApp.createProfile(); 1.184 + ok(profileDir && profileDir.exists(), "Profile directory created"); 1.185 + 1.186 + // On Mac build servers, we don't have enough privileges to write to /Applications, 1.187 + // so we install apps in a user-owned directory. 1.188 + if (MAC) { 1.189 + nativeApp._rootInstallDir = OS.Path.join(OS.Constants.Path.homeDir, "Applications"); 1.190 + yield OS.File.makeDir(nativeApp._rootInstallDir, { ignoreExisting: true }); 1.191 + } 1.192 + 1.193 + // Install application 1.194 + info("Test installation"); 1.195 + yield nativeApp.install(manifest); 1.196 + while (!WebappOSUtils.isLaunchable(app)) { 1.197 + yield wait(1000); 1.198 + } 1.199 + ok(true, "App launchable"); 1.200 + 1.201 + let exeFile = getFile(exePath); 1.202 + 1.203 + ok(exeFile.isExecutable(), "webapprt executable is executable"); 1.204 + 1.205 + let appClosed = false; 1.206 + 1.207 + appProcess.init(exeFile) 1.208 + appProcess.runAsync([], 0, () => appClosed = true); 1.209 + 1.210 + while (!(yield wasAppSJSAccessed()) && !appClosed) { 1.211 + yield wait(1000); 1.212 + } 1.213 + ok(!appClosed, "App was launched and is still running"); 1.214 + 1.215 + SimpleTest.finish(); 1.216 +}); 1.217 + 1.218 +// The test doesn't work yet on Mac OS X 10.6 machines. 1.219 +// See bug 993690. 1.220 +if (MAC_106) { 1.221 + todo(false, "The test doesn't work on Mac OS X 10.6 machines"); 1.222 + SimpleTest.finish(); 1.223 +} else { 1.224 + runTest().then(null, function(e) { 1.225 + ok(false, "Error during test: " + e); 1.226 + SimpleTest.finish(); 1.227 + }); 1.228 +} 1.229 + 1.230 +]]> 1.231 +</script> 1.232 +</window>