dom/apps/tests/test_packaged_app_common.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var PackagedTestHelper = (function PackagedTestHelper() {
michael@0 6 "use strict";
michael@0 7
michael@0 8 var steps;
michael@0 9 var index = -1;
michael@0 10 var gSJSPath = "tests/dom/apps/tests/file_packaged_app.sjs";
michael@0 11 var gSJS = "http://test/" + gSJSPath;
michael@0 12 var gAppName = "appname";
michael@0 13 var gApp = null;
michael@0 14 var gInstallOrigin = "http://mochi.test:8888";
michael@0 15
michael@0 16 function debug(aMsg) {
michael@0 17 //dump("== PackageTestHelper debug == " + aMsg + "\n");
michael@0 18 }
michael@0 19
michael@0 20 function next() {
michael@0 21 index += 1;
michael@0 22 if (index >= steps.length) {
michael@0 23 ok(false, "Shouldn't get here!");
michael@0 24 return;
michael@0 25 }
michael@0 26 try {
michael@0 27 steps[index]();
michael@0 28 } catch(ex) {
michael@0 29 ok(false, "Caught exception", ex);
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 function start() {
michael@0 34 next();
michael@0 35 }
michael@0 36
michael@0 37 function finish() {
michael@0 38 SpecialPowers.removePermission("webapps-manage", document);
michael@0 39 SpecialPowers.removePermission("browser", document);
michael@0 40 SimpleTest.finish();
michael@0 41 }
michael@0 42
michael@0 43 function mozAppsError() {
michael@0 44 ok(false, "mozApps error: " + this.error.name);
michael@0 45 finish();
michael@0 46 }
michael@0 47
michael@0 48 function xhrError(event, url) {
michael@0 49 var xhr = event.target;
michael@0 50 ok(false, "XHR error loading " + url + ": " + xhr.status + " - " +
michael@0 51 xhr.statusText);
michael@0 52 finish();
michael@0 53 }
michael@0 54
michael@0 55 function xhrAbort(url) {
michael@0 56 ok(false, "XHR abort loading " + url);
michael@0 57 finish();
michael@0 58 }
michael@0 59
michael@0 60 function setAppVersion(aVersion, aCb, aDontUpdatePackage) {
michael@0 61 var xhr = new XMLHttpRequest();
michael@0 62 var dontUpdate = "";
michael@0 63 if (aDontUpdatePackage) {
michael@0 64 dontUpdate = "&dontUpdatePackage=1";
michael@0 65 }
michael@0 66 var url = gSJS + "?setVersion=" + aVersion + dontUpdate;
michael@0 67 xhr.addEventListener("load", function() {
michael@0 68 is(xhr.responseText, "OK", "setAppVersion OK");
michael@0 69 aCb();
michael@0 70 });
michael@0 71 xhr.addEventListener("error", event => xhrError(event, url));
michael@0 72 xhr.addEventListener("abort", event => xhrAbort(url));
michael@0 73 xhr.open("GET", url, true);
michael@0 74 xhr.send();
michael@0 75 }
michael@0 76
michael@0 77 function checkAppDownloadError(aMiniManifestURL,
michael@0 78 aExpectedError,
michael@0 79 aVersion,
michael@0 80 aUninstall,
michael@0 81 aDownloadAvailable,
michael@0 82 aName,
michael@0 83 aCb) {
michael@0 84 var req = navigator.mozApps.installPackage(aMiniManifestURL);
michael@0 85 req.onsuccess = function(evt) {
michael@0 86 ok(true, "App installed");
michael@0 87 if (!aUninstall) {
michael@0 88 // Save it for later.
michael@0 89 gApp = req.result;
michael@0 90 }
michael@0 91 };
michael@0 92 req.onerror = function(evt) {
michael@0 93 ok(false, "Got unexpected " + evt.target.error.name);
michael@0 94 finish();
michael@0 95 };
michael@0 96
michael@0 97 navigator.mozApps.mgmt.oninstall = function(evt) {
michael@0 98 var aApp = evt.application;
michael@0 99 aApp.ondownloaderror = function(evt) {
michael@0 100 var error = aApp.downloadError.name;
michael@0 101 if (error == aExpectedError) {
michael@0 102 ok(true, "Got expected " + aExpectedError);
michael@0 103 var expected = {
michael@0 104 name: aName,
michael@0 105 manifestURL: aMiniManifestURL,
michael@0 106 installOrigin: gInstallOrigin,
michael@0 107 progress: 0,
michael@0 108 installState: "pending",
michael@0 109 downloadAvailable: aDownloadAvailable,
michael@0 110 downloading: false,
michael@0 111 downloadSize: 0,
michael@0 112 size: 0,
michael@0 113 readyToApplyDownload: false
michael@0 114 };
michael@0 115 checkAppState(aApp, aVersion, expected, false, aUninstall,
michael@0 116 aCb || next);
michael@0 117 } else {
michael@0 118 ok(false, "Got unexpected " + error);
michael@0 119 finish();
michael@0 120 }
michael@0 121 };
michael@0 122 aApp.ondownloadsuccess = function(evt) {
michael@0 123 ok(false, "We are supposed to throw " + aExpectedError);
michael@0 124 finish();
michael@0 125 };
michael@0 126 };
michael@0 127 }
michael@0 128
michael@0 129 function checkAppState(aApp,
michael@0 130 aVersion,
michael@0 131 aExpectedApp,
michael@0 132 aLaunchable,
michael@0 133 aUninstall,
michael@0 134 aCb) {
michael@0 135 debug(JSON.stringify(aApp, null, 2));
michael@0 136 if (aApp.manifest) {
michael@0 137 debug(JSON.stringify(aApp.manifest, null, 2));
michael@0 138 }
michael@0 139
michael@0 140 if (aExpectedApp.name) {
michael@0 141 if (aApp.manifest) {
michael@0 142 is(aApp.manifest.name, aExpectedApp.name, "Check name");
michael@0 143 }
michael@0 144 is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
michael@0 145 }
michael@0 146 if (aApp.manifest) {
michael@0 147 is(aApp.manifest.version, aVersion, "Check version");
michael@0 148 }
michael@0 149 if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
michael@0 150 is(aApp.manifest.size, aExpectedApp.size, "Check size");
michael@0 151 }
michael@0 152 if (aApp.manifest) {
michael@0 153 is(aApp.manifest.launch_path, aExpectedApp.launch_path || gSJSPath, "Check launch path");
michael@0 154 }
michael@0 155 if (aExpectedApp.manifestURL) {
michael@0 156 is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
michael@0 157 }
michael@0 158 if (aExpectedApp.installOrigin) {
michael@0 159 is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
michael@0 160 }
michael@0 161 ok(aApp.removable, "Removable app");
michael@0 162 if (typeof aExpectedApp.progress !== "undefined") {
michael@0 163 todo(aApp.progress == aExpectedApp.progress, "Check progress");
michael@0 164 }
michael@0 165 if (aExpectedApp.installState) {
michael@0 166 is(aApp.installState, aExpectedApp.installState, "Check installState");
michael@0 167 }
michael@0 168 if (typeof aExpectedApp.downloadAvailable !== "undefined") {
michael@0 169 is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
michael@0 170 "Check download available");
michael@0 171 }
michael@0 172 if (typeof aExpectedApp.downloading !== "undefined") {
michael@0 173 is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
michael@0 174 }
michael@0 175 if (typeof aExpectedApp.downloadSize !== "undefined") {
michael@0 176 is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
michael@0 177 }
michael@0 178 if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
michael@0 179 is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
michael@0 180 "Check readyToApplyDownload");
michael@0 181 }
michael@0 182 if (typeof aExpectedApp.origin !== "undefined") {
michael@0 183 is(aApp.origin, aExpectedApp.origin, "Check origin");
michael@0 184 }
michael@0 185 if (aLaunchable) {
michael@0 186 if (aUninstall) {
michael@0 187 checkUninstallApp(aApp);
michael@0 188 } else if (aCb && typeof aCb === "function") {
michael@0 189 aCb();
michael@0 190 }
michael@0 191 return;
michael@0 192 }
michael@0 193
michael@0 194 // Check if app is not launchable.
michael@0 195 var req = aApp.launch();
michael@0 196 req.onsuccess = function () {
michael@0 197 ok(false, "We shouldn't be here");
michael@0 198 finish();
michael@0 199 };
michael@0 200 req.onerror = function() {
michael@0 201 ok(true, "App is not launchable");
michael@0 202 if (aUninstall) {
michael@0 203 checkUninstallApp(aApp);
michael@0 204 } else if (aCb && typeof aCb === "function") {
michael@0 205 aCb();
michael@0 206 }
michael@0 207 return;
michael@0 208 };
michael@0 209 }
michael@0 210
michael@0 211 return {
michael@0 212 setSteps: function (aSteps) {
michael@0 213 steps = aSteps;
michael@0 214 },
michael@0 215 next: next,
michael@0 216 start: start,
michael@0 217 finish: finish,
michael@0 218 mozAppsError: mozAppsError,
michael@0 219 setAppVersion: setAppVersion,
michael@0 220 checkAppState: checkAppState,
michael@0 221 checkAppDownloadError: checkAppDownloadError,
michael@0 222 get gSJSPath() { return gSJSPath; },
michael@0 223 set gSJSPath(aValue) { gSJSPath = aValue },
michael@0 224 get gSJS() { return gSJS; },
michael@0 225 get gAppName() { return gAppName;},
michael@0 226 get gApp() { return gApp; },
michael@0 227 set gApp(aValue) { gApp = aValue; },
michael@0 228 gInstallOrigin: gInstallOrigin
michael@0 229 };
michael@0 230
michael@0 231 })();

mercurial