1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/apps/tests/test_webapps_actor.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,315 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id={821589} 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug {821589} Packaged apps installation and update</title> 1.11 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.12 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={821589}">Mozilla Bug {821589}</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 + 1.21 +</div> 1.22 +<pre id="test"> 1.23 +<script class="testbody" type="application/javascript;version=1.8"> 1.24 + 1.25 +"use strict"; 1.26 + 1.27 +var index = -1; 1.28 + 1.29 +function debug(aMsg) { 1.30 + //dump("== Tests debug == " + aMsg + "\n"); 1.31 +} 1.32 + 1.33 +function next() { 1.34 + index += 1; 1.35 + if (index >= steps.length) { 1.36 + ok(false, "Shouldn't get here!"); 1.37 + return; 1.38 + } 1.39 + try { 1.40 + steps[index](); 1.41 + } catch(ex) { 1.42 + ok(false, "Caught exception", ex); 1.43 + } 1.44 +} 1.45 + 1.46 +function start() { 1.47 + next(); 1.48 +} 1.49 + 1.50 +function finish() { 1.51 + SpecialPowers.removePermission("webapps-manage", document); 1.52 + SimpleTest.finish(); 1.53 +} 1.54 + 1.55 +function cbError(aError) { 1.56 + ok(false, "Error callback invoked " + aError); 1.57 + finish(); 1.58 +} 1.59 + 1.60 + 1.61 +SimpleTest.waitForExplicitFinish(); 1.62 + 1.63 +var installTestApp, mm; 1.64 + 1.65 +const PACKAGED_APP_ID = "test-app-id"; 1.66 +const PACKAGED_APP_ORIGIN = "app://" + PACKAGED_APP_ID; 1.67 +const PACKAGED_APP_MANIFEST = PACKAGED_APP_ORIGIN + "/manifest.webapp"; 1.68 +const CERTIFIED_APP_ID = "test-certified-id"; 1.69 +const CERTIFIED_APP_ORIGIN = "app://" + CERTIFIED_APP_ID; 1.70 +const CERTIFIED_APP_MANIFEST = CERTIFIED_APP_ORIGIN + "/manifest.webapp"; 1.71 + 1.72 +var steps = [ 1.73 + function() { 1.74 + info("== SETUP =="); 1.75 + // Set up 1.76 + SpecialPowers.setAllAppsLaunchable(true); 1.77 + SpecialPowers.addPermission("webapps-manage", true, document); 1.78 + SpecialPowers.addPermission("browser", true, document); 1.79 + SpecialPowers.addPermission("embed-apps", true, document); 1.80 + 1.81 + // Required on firefox as these prefs are only set on b2g: 1.82 + SpecialPowers.pushPrefEnv({ 1.83 + set: [["dom.mozBrowserFramesEnabled", true], 1.84 + ["security.apps.privileged.CSP.default", 1.85 + "default-src *; script-src 'self'; object-src 'none'; style-src 'self' 'unsafe-inline'"], 1.86 + ["devtools.debugger.unix-domain-socket", 6000], 1.87 + ["devtools.debugger.prompt-connection", false], 1.88 + ["devtools.debugger.forbid-certified-apps", true] 1.89 + ] 1.90 + }, next); 1.91 + }, 1.92 + function () { 1.93 + // Load a chrome script in order to dispatch devtool debugger requests. 1.94 + // Because of wrapping issues, we can't use SpecialPowers.Cu.import to load 1.95 + // devtools jsm into mochitest scope. We end up not receiving 1.96 + // DebuggerClient.addListener callback arguments... 1.97 + let scriptUrl = SimpleTest.getTestFileURL("debugger-protocol-helper.js"); 1.98 + mm = SpecialPowers.loadChromeScript(scriptUrl); 1.99 + installTestApp = function (url, appId, callback) { 1.100 + let installResponse, appObject; 1.101 + let installedEvent = false; 1.102 + mm.addMessageListener("installed", function onInstalled(aResponse) { 1.103 + mm.removeMessageListener("installed", onInstalled); 1.104 + ok(true, "install request replied"); 1.105 + installResponse = aResponse; 1.106 + checkEnd(); 1.107 + }); 1.108 + mm.addMessageListener("installed-event", function onInstalledEvent(aResponse) { 1.109 + mm.removeMessageListener("installed-event", onInstalledEvent); 1.110 + ok(true, "received appInstall actor event"); 1.111 + installedEvent = true; 1.112 + checkEnd(); 1.113 + }); 1.114 + navigator.mozApps.mgmt.oninstall = function(evt) { 1.115 + appObject = evt.application; 1.116 + ok(true, "mozApps.mgmt install event fired"); 1.117 + checkEnd(); 1.118 + }; 1.119 + function checkEnd() { 1.120 + if (appObject && installResponse && installedEvent) 1.121 + callback(installResponse, appObject); 1.122 + } 1.123 + mm.sendAsyncMessage("install", {url: url, appId: appId}); 1.124 + }; 1.125 + SpecialPowers.autoConfirmAppInstall(next); 1.126 + }, 1.127 + function() { 1.128 + info("== TEST == Install packaged app"); 1.129 + let url = SimpleTest.getTestFileURL("data/app.zip"); 1.130 + installTestApp(url, PACKAGED_APP_ID, 1.131 + function (aResponse, aApp) { 1.132 + ok(true, "Installed"); 1.133 + is(aResponse.appId, PACKAGED_APP_ID, "Got same app id"); 1.134 + if ("error" in aResponse) { 1.135 + ok(false, "Error: " + aResponse.error); 1.136 + } 1.137 + if ("message" in aResponse) { 1.138 + ok(false, "Error message: " + aResponse.message); 1.139 + } 1.140 + ok(!("error" in aResponse), "app installed without any error"); 1.141 + is(aApp.manifest.name, "Test app", "app name is correct"); 1.142 + next(); 1.143 + } 1.144 + ); 1.145 + }, 1.146 + function () { 1.147 + info("== TEST == Reinstall packaged app"); 1.148 + let url = SimpleTest.getTestFileURL("data/app-updated.zip"); 1.149 + installTestApp(url, PACKAGED_APP_ID, 1.150 + function (aResponse, aApp) { 1.151 + ok(true, "Reinstalled"); 1.152 + is(aResponse.appId, PACKAGED_APP_ID, "Got same app id"); 1.153 + if ("error" in aResponse) { 1.154 + ok(false, "Error: " + aResponse.error); 1.155 + } 1.156 + if ("message" in aResponse) { 1.157 + ok(false, "Error message: " + aResponse.message); 1.158 + } 1.159 + ok(!("error" in aResponse), "app installed without any error"); 1.160 + is(aApp.manifest.name, "updated-name", "app name on update is correct"); 1.161 + next(); 1.162 + } 1.163 + ); 1.164 + }, 1.165 + function() { 1.166 + info("== TEST == Install certified app"); 1.167 + let url = SimpleTest.getTestFileURL("data/app-certified.zip"); 1.168 + installTestApp(url, CERTIFIED_APP_ID, 1.169 + function (aResponse, aApp) { 1.170 + ok(true, "Installed"); 1.171 + is(aResponse.appId, CERTIFIED_APP_ID, "Got same app id"); 1.172 + if ("error" in aResponse) { 1.173 + ok(false, "Error: " + aResponse.error); 1.174 + } 1.175 + if ("message" in aResponse) { 1.176 + ok(false, "Error message: " + aResponse.message); 1.177 + } 1.178 + ok(!("error" in aResponse), "app installed without any error"); 1.179 + is(aApp.manifest.name, "Certified app", "app name is correct"); 1.180 + next(); 1.181 + } 1.182 + ); 1.183 + }, 1.184 + function() { 1.185 + info("== TEST == Get all apps"); 1.186 + getAll(false); 1.187 + }, 1.188 + function() { 1.189 + info("== TEST == Get packaged app"); 1.190 + getApp({ 1.191 + id: PACKAGED_APP_ID, 1.192 + manifestURL: PACKAGED_APP_MANIFEST 1.193 + }, true); 1.194 + }, 1.195 + function() { 1.196 + info("== TEST == Get certified app"); 1.197 + getApp({ 1.198 + id: CERTIFIED_APP_ID, 1.199 + manifestURL: CERTIFIED_APP_MANIFEST 1.200 + }, false); 1.201 + }, 1.202 + function() { 1.203 + info("== SETUP == Enable certified app access"); 1.204 + SpecialPowers.pushPrefEnv({ 1.205 + "set": [["devtools.debugger.forbid-certified-apps", false]] 1.206 + }, next); 1.207 + }, 1.208 + function() { 1.209 + info("== TEST == Get all apps (CERTIFIED ENABLED)"); 1.210 + getAll(true); 1.211 + }, 1.212 + function() { 1.213 + info("== TEST == Get packaged app (CERTIFIED ENABLED)"); 1.214 + getApp({ 1.215 + id: PACKAGED_APP_ID, 1.216 + manifestURL: PACKAGED_APP_MANIFEST 1.217 + }, true); 1.218 + }, 1.219 + function() { 1.220 + info("== TEST == Get certified app (CERTIFIED ENABLED)"); 1.221 + getApp({ 1.222 + id: CERTIFIED_APP_ID, 1.223 + manifestURL: CERTIFIED_APP_MANIFEST 1.224 + }, true); 1.225 + }, 1.226 + function() { 1.227 + info("== SETUP == Disable certified app access"); 1.228 + SpecialPowers.popPrefEnv(next); 1.229 + }, 1.230 + function() { 1.231 + info("== TEST == Uninstall packaged app"); 1.232 + uninstall(PACKAGED_APP_MANIFEST); 1.233 + }, 1.234 + function() { 1.235 + info("== TEST == Uninstall certified app"); 1.236 + uninstall(CERTIFIED_APP_MANIFEST); 1.237 + }, 1.238 + function() { 1.239 + ok(true, "all done!\n"); 1.240 + mm.sendAsyncMessage("cleanup"); 1.241 + SpecialPowers.flushPrefEnv(finish); 1.242 + } 1.243 +]; 1.244 + 1.245 +addLoadEvent(start); 1.246 + 1.247 +function getAll(expectCertified) { 1.248 + mm.addMessageListener("appActorResponse", function onResponse(response) { 1.249 + mm.removeMessageListener("appActorResponse", onResponse); 1.250 + 1.251 + ok("apps" in response, "Apps found in getAll reply"); 1.252 + let apps = response.apps; 1.253 + let packagedApp, certifiedApp; 1.254 + for (let app of apps) { 1.255 + switch (app.id) { 1.256 + case PACKAGED_APP_ID: 1.257 + packagedApp = app; 1.258 + break; 1.259 + case CERTIFIED_APP_ID: 1.260 + certifiedApp = app; 1.261 + break; 1.262 + } 1.263 + } 1.264 + 1.265 + ok(packagedApp, "Packaged app found via getAll"); 1.266 + is(!!certifiedApp, expectCertified, "Certified app matches expectation"); 1.267 + 1.268 + next(); 1.269 + }); 1.270 + 1.271 + mm.sendAsyncMessage("appActorRequest", { 1.272 + type: "getAll" 1.273 + }); 1.274 +} 1.275 + 1.276 +function getApp(appInfo, expected) { 1.277 + mm.addMessageListener("appActorResponse", function onResponse(response) { 1.278 + mm.removeMessageListener("appActorResponse", onResponse); 1.279 + 1.280 + is("app" in response, expected, "App existence matches expectation"); 1.281 + is("error" in response, !expected, "Error existence matches expectation"); 1.282 + if (!expected) { 1.283 + is(response.error, "forbidden", "Error message is correct"); 1.284 + next(); 1.285 + return; 1.286 + } 1.287 + 1.288 + let app = response.app; 1.289 + for (let key in appInfo) { 1.290 + is(app[key], appInfo[key], "Value for " + key + " matches"); 1.291 + } 1.292 + 1.293 + next(); 1.294 + }); 1.295 + 1.296 + mm.sendAsyncMessage("appActorRequest", { 1.297 + type: "getApp", 1.298 + manifestURL: appInfo.manifestURL 1.299 + }); 1.300 +} 1.301 + 1.302 +function uninstall(manifestURL) { 1.303 + mm.addMessageListener("appActorResponse", function onResponse(response) { 1.304 + mm.removeMessageListener("appActorResponse", onResponse); 1.305 + ok(!("error" in response), "App uninstalled successfully"); 1.306 + next(); 1.307 + }); 1.308 + 1.309 + mm.sendAsyncMessage("appActorRequest", { 1.310 + type: "uninstall", 1.311 + manifestURL: manifestURL 1.312 + }); 1.313 +} 1.314 + 1.315 +</script> 1.316 +</pre> 1.317 +</body> 1.318 +</html>