1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/apps/tests/unit/test_webappsActor.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,234 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/osfile.jsm"); 1.8 +const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); 1.9 +const {require} = devtools; 1.10 +const {installHosted, installPackaged} = require("devtools/app-actor-front"); 1.11 + 1.12 +let gAppId = "actor-test"; 1.13 +const APP_ORIGIN = "app://" + gAppId; 1.14 + 1.15 +add_test(function testLaunchInexistantApp() { 1.16 + let request = {type: "launch", manifestURL: "http://foo.com"}; 1.17 + webappActorRequest(request, function (aResponse) { 1.18 + do_check_eq(aResponse.error, "NO_SUCH_APP"); 1.19 + run_next_test(); 1.20 + }); 1.21 +}); 1.22 + 1.23 +add_test(function testCloseInexistantApp() { 1.24 + let request = {type: "close", manifestURL: "http://foo.com"}; 1.25 + webappActorRequest(request, function (aResponse) { 1.26 + do_check_eq(aResponse.error, "missingParameter"); 1.27 + do_check_eq(aResponse.message, "No application for http://foo.com"); 1.28 + run_next_test(); 1.29 + }); 1.30 +}); 1.31 + 1.32 +// Install a test app 1.33 +add_test(function testInstallPackaged() { 1.34 + installTestApp("app.zip", gAppId, function () { 1.35 + run_next_test(); 1.36 + }); 1.37 +}); 1.38 + 1.39 +// Now check that the app appear in getAll 1.40 +add_test(function testGetAll() { 1.41 + let request = {type: "getAll"}; 1.42 + webappActorRequest(request, function (aResponse) { 1.43 + do_check_true("apps" in aResponse); 1.44 + let apps = aResponse.apps; 1.45 + do_check_true(apps.length > 0); 1.46 + for (let i = 0; i < apps.length; i++) { 1.47 + let app = apps[i]; 1.48 + if (app.id == gAppId) { 1.49 + do_check_eq(app.name, "Test app"); 1.50 + do_check_eq(app.manifest.description, "Testing webapps actor"); 1.51 + do_check_eq(app.manifest.launch_path, "/index.html"); 1.52 + do_check_eq(app.origin, APP_ORIGIN); 1.53 + do_check_eq(app.installOrigin, app.origin); 1.54 + do_check_eq(app.manifestURL, app.origin + "/manifest.webapp"); 1.55 + run_next_test(); 1.56 + return; 1.57 + } 1.58 + } 1.59 + do_throw("Unable to find the test app by its id"); 1.60 + }); 1.61 +}); 1.62 + 1.63 +add_test(function testGetApp() { 1.64 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.65 + let request = {type: "getApp", manifestURL: manifestURL}; 1.66 + webappActorRequest(request, function (aResponse) { 1.67 + do_check_true("app" in aResponse); 1.68 + let app = aResponse.app; 1.69 + do_check_eq(app.id, gAppId); 1.70 + do_check_eq(app.name, "Test app"); 1.71 + do_check_eq(app.manifest.description, "Testing webapps actor"); 1.72 + do_check_eq(app.manifest.launch_path, "/index.html"); 1.73 + do_check_eq(app.origin, APP_ORIGIN); 1.74 + do_check_eq(app.installOrigin, app.origin); 1.75 + do_check_eq(app.manifestURL, app.origin + "/manifest.webapp"); 1.76 + run_next_test(); 1.77 + }); 1.78 +}); 1.79 + 1.80 +add_test(function testLaunchApp() { 1.81 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.82 + let startPoint = "/index.html"; 1.83 + let request = { 1.84 + type: "launch", 1.85 + manifestURL: manifestURL, 1.86 + startPoint: startPoint 1.87 + }; 1.88 + Services.obs.addObserver(function observer(subject, topic, data) { 1.89 + Services.obs.removeObserver(observer, topic); 1.90 + let json = JSON.parse(data); 1.91 + do_check_eq(json.manifestURL, manifestURL); 1.92 + do_check_eq(json.startPoint, startPoint); 1.93 + run_next_test(); 1.94 + }, "webapps-launch", false); 1.95 + 1.96 + webappActorRequest(request, function (aResponse) { 1.97 + do_check_false("error" in aResponse); 1.98 + }); 1.99 +}); 1.100 + 1.101 +add_test(function testCloseApp() { 1.102 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.103 + let request = { 1.104 + type: "close", 1.105 + manifestURL: manifestURL 1.106 + }; 1.107 + Services.obs.addObserver(function observer(subject, topic, data) { 1.108 + Services.obs.removeObserver(observer, topic); 1.109 + let json = JSON.parse(data); 1.110 + do_check_eq(json.manifestURL, manifestURL); 1.111 + 1.112 + }, "webapps-close", false); 1.113 + 1.114 + webappActorRequest(request, function (aResponse) { 1.115 + do_check_false("error" in aResponse); 1.116 + run_next_test(); 1.117 + }); 1.118 +}); 1.119 + 1.120 +// The 128px icon is a single red pixel and the 64px one is a blue one 1.121 +// bug 899177: there is a bug with xhr and app:// and jar:// uris 1.122 +// that ends up forcing the content type to application/xml 1.123 +let red1px = "data:application/xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4z8AAAAMBAQAY3Y2wAAAAAElFTkSuQmCC"; 1.124 +let blue1px = "data:application/xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12MwZDgHAAFlAQBDpjhLAAAAAElFTkSuQmCC"; 1.125 + 1.126 +add_test(function testGetIcon() { 1.127 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.128 + let request = { 1.129 + type: "getIconAsDataURL", 1.130 + manifestURL: manifestURL 1.131 + }; 1.132 + 1.133 + webappActorRequest(request, function (aResponse) { 1.134 + do_check_false("error" in aResponse); 1.135 + 1.136 + // By default, getIconAsDataURL return the 128x128 icon 1.137 + do_check_eq(aResponse.url, red1px); 1.138 + run_next_test(); 1.139 + }); 1.140 +}); 1.141 + 1.142 +add_test(function testGetIconWithCustomSize() { 1.143 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.144 + let request = { 1.145 + type: "getIconAsDataURL", 1.146 + manifestURL: manifestURL, 1.147 + size: 64 1.148 + }; 1.149 + 1.150 + webappActorRequest(request, function (aResponse) { 1.151 + do_check_false("error" in aResponse); 1.152 + 1.153 + do_check_eq(aResponse.url, blue1px); 1.154 + run_next_test(); 1.155 + }); 1.156 +}); 1.157 + 1.158 +add_test(function testUninstall() { 1.159 + let manifestURL = APP_ORIGIN + "/manifest.webapp"; 1.160 + let request = { 1.161 + type: "uninstall", 1.162 + manifestURL: manifestURL 1.163 + }; 1.164 + 1.165 + Services.obs.addObserver(function observer(subject, topic, data) { 1.166 + Services.obs.removeObserver(observer, topic); 1.167 + let json = JSON.parse(data); 1.168 + do_check_eq(json.manifestURL, manifestURL); 1.169 + do_check_eq(json.origin, APP_ORIGIN); 1.170 + do_check_eq(json.id, gAppId); 1.171 + run_next_test(); 1.172 + }, "webapps-uninstall", false); 1.173 + 1.174 + webappActorRequest(request, function (aResponse) { 1.175 + do_check_false("error" in aResponse); 1.176 + }); 1.177 +}); 1.178 + 1.179 +add_test(function testFileUploadInstall() { 1.180 + let packageFile = do_get_file("data/app.zip"); 1.181 + installPackaged(gClient, gActor, packageFile.path, gAppId) 1.182 + .then(function ({ appId }) { 1.183 + do_check_eq(appId, gAppId); 1.184 + run_next_test(); 1.185 + }, function (e) { 1.186 + do_throw("Failed install uploaded packaged app: " + e.error + ": " + e.message); 1.187 + }); 1.188 +}); 1.189 + 1.190 +add_test(function testInstallHosted() { 1.191 + gAppId = "hosted-app"; 1.192 + let metadata = { 1.193 + origin: "http://foo.com", 1.194 + installOrigin: "http://metadata.foo.com", 1.195 + manifestURL: "http://foo.com/metadata/manifest.webapp" 1.196 + }; 1.197 + let manifest = { 1.198 + name: "My hosted app" 1.199 + }; 1.200 + installHosted(gClient, gActor, gAppId, metadata, manifest).then( 1.201 + function ({ appId }) { 1.202 + do_check_eq(appId, gAppId); 1.203 + run_next_test(); 1.204 + }, 1.205 + function (e) { 1.206 + do_throw("Failed installing hosted app: " + e.error + ": " + e.message); 1.207 + } 1.208 + ); 1.209 +}); 1.210 + 1.211 +add_test(function testCheckHostedApp() { 1.212 + let request = {type: "getAll"}; 1.213 + webappActorRequest(request, function (aResponse) { 1.214 + do_check_true("apps" in aResponse); 1.215 + let apps = aResponse.apps; 1.216 + do_check_true(apps.length > 0); 1.217 + for (let i = 0; i < apps.length; i++) { 1.218 + let app = apps[i]; 1.219 + if (app.id == gAppId) { 1.220 + do_check_eq(app.name, "My hosted app"); 1.221 + do_check_eq(app.origin, "http://foo.com"); 1.222 + do_check_eq(app.installOrigin, "http://metadata.foo.com"); 1.223 + do_check_eq(app.manifestURL, "http://foo.com/metadata/manifest.webapp"); 1.224 + run_next_test(); 1.225 + return; 1.226 + } 1.227 + } 1.228 + do_throw("Unable to find the test app by its id"); 1.229 + }); 1.230 +}); 1.231 + 1.232 +function run_test() { 1.233 + setup(); 1.234 + 1.235 + run_next_test(); 1.236 +} 1.237 +