michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var PackagedTestHelper = (function PackagedTestHelper() { michael@0: "use strict"; michael@0: michael@0: var steps; michael@0: var index = -1; michael@0: var gSJSPath = "tests/dom/apps/tests/file_packaged_app.sjs"; michael@0: var gSJS = "http://test/" + gSJSPath; michael@0: var gAppName = "appname"; michael@0: var gApp = null; michael@0: var gInstallOrigin = "http://mochi.test:8888"; michael@0: michael@0: function debug(aMsg) { michael@0: //dump("== PackageTestHelper debug == " + aMsg + "\n"); michael@0: } michael@0: michael@0: function next() { michael@0: index += 1; michael@0: if (index >= steps.length) { michael@0: ok(false, "Shouldn't get here!"); michael@0: return; michael@0: } michael@0: try { michael@0: steps[index](); michael@0: } catch(ex) { michael@0: ok(false, "Caught exception", ex); michael@0: } michael@0: } michael@0: michael@0: function start() { michael@0: next(); michael@0: } michael@0: michael@0: function finish() { michael@0: SpecialPowers.removePermission("webapps-manage", document); michael@0: SpecialPowers.removePermission("browser", document); michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: function mozAppsError() { michael@0: ok(false, "mozApps error: " + this.error.name); michael@0: finish(); michael@0: } michael@0: michael@0: function xhrError(event, url) { michael@0: var xhr = event.target; michael@0: ok(false, "XHR error loading " + url + ": " + xhr.status + " - " + michael@0: xhr.statusText); michael@0: finish(); michael@0: } michael@0: michael@0: function xhrAbort(url) { michael@0: ok(false, "XHR abort loading " + url); michael@0: finish(); michael@0: } michael@0: michael@0: function setAppVersion(aVersion, aCb, aDontUpdatePackage) { michael@0: var xhr = new XMLHttpRequest(); michael@0: var dontUpdate = ""; michael@0: if (aDontUpdatePackage) { michael@0: dontUpdate = "&dontUpdatePackage=1"; michael@0: } michael@0: var url = gSJS + "?setVersion=" + aVersion + dontUpdate; michael@0: xhr.addEventListener("load", function() { michael@0: is(xhr.responseText, "OK", "setAppVersion OK"); michael@0: aCb(); michael@0: }); michael@0: xhr.addEventListener("error", event => xhrError(event, url)); michael@0: xhr.addEventListener("abort", event => xhrAbort(url)); michael@0: xhr.open("GET", url, true); michael@0: xhr.send(); michael@0: } michael@0: michael@0: function checkAppDownloadError(aMiniManifestURL, michael@0: aExpectedError, michael@0: aVersion, michael@0: aUninstall, michael@0: aDownloadAvailable, michael@0: aName, michael@0: aCb) { michael@0: var req = navigator.mozApps.installPackage(aMiniManifestURL); michael@0: req.onsuccess = function(evt) { michael@0: ok(true, "App installed"); michael@0: if (!aUninstall) { michael@0: // Save it for later. michael@0: gApp = req.result; michael@0: } michael@0: }; michael@0: req.onerror = function(evt) { michael@0: ok(false, "Got unexpected " + evt.target.error.name); michael@0: finish(); michael@0: }; michael@0: michael@0: navigator.mozApps.mgmt.oninstall = function(evt) { michael@0: var aApp = evt.application; michael@0: aApp.ondownloaderror = function(evt) { michael@0: var error = aApp.downloadError.name; michael@0: if (error == aExpectedError) { michael@0: ok(true, "Got expected " + aExpectedError); michael@0: var expected = { michael@0: name: aName, michael@0: manifestURL: aMiniManifestURL, michael@0: installOrigin: gInstallOrigin, michael@0: progress: 0, michael@0: installState: "pending", michael@0: downloadAvailable: aDownloadAvailable, michael@0: downloading: false, michael@0: downloadSize: 0, michael@0: size: 0, michael@0: readyToApplyDownload: false michael@0: }; michael@0: checkAppState(aApp, aVersion, expected, false, aUninstall, michael@0: aCb || next); michael@0: } else { michael@0: ok(false, "Got unexpected " + error); michael@0: finish(); michael@0: } michael@0: }; michael@0: aApp.ondownloadsuccess = function(evt) { michael@0: ok(false, "We are supposed to throw " + aExpectedError); michael@0: finish(); michael@0: }; michael@0: }; michael@0: } michael@0: michael@0: function checkAppState(aApp, michael@0: aVersion, michael@0: aExpectedApp, michael@0: aLaunchable, michael@0: aUninstall, michael@0: aCb) { michael@0: debug(JSON.stringify(aApp, null, 2)); michael@0: if (aApp.manifest) { michael@0: debug(JSON.stringify(aApp.manifest, null, 2)); michael@0: } michael@0: michael@0: if (aExpectedApp.name) { michael@0: if (aApp.manifest) { michael@0: is(aApp.manifest.name, aExpectedApp.name, "Check name"); michael@0: } michael@0: is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest"); michael@0: } michael@0: if (aApp.manifest) { michael@0: is(aApp.manifest.version, aVersion, "Check version"); michael@0: } michael@0: if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) { michael@0: is(aApp.manifest.size, aExpectedApp.size, "Check size"); michael@0: } michael@0: if (aApp.manifest) { michael@0: is(aApp.manifest.launch_path, aExpectedApp.launch_path || gSJSPath, "Check launch path"); michael@0: } michael@0: if (aExpectedApp.manifestURL) { michael@0: is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL"); michael@0: } michael@0: if (aExpectedApp.installOrigin) { michael@0: is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin"); michael@0: } michael@0: ok(aApp.removable, "Removable app"); michael@0: if (typeof aExpectedApp.progress !== "undefined") { michael@0: todo(aApp.progress == aExpectedApp.progress, "Check progress"); michael@0: } michael@0: if (aExpectedApp.installState) { michael@0: is(aApp.installState, aExpectedApp.installState, "Check installState"); michael@0: } michael@0: if (typeof aExpectedApp.downloadAvailable !== "undefined") { michael@0: is(aApp.downloadAvailable, aExpectedApp.downloadAvailable, michael@0: "Check download available"); michael@0: } michael@0: if (typeof aExpectedApp.downloading !== "undefined") { michael@0: is(aApp.downloading, aExpectedApp.downloading, "Check downloading"); michael@0: } michael@0: if (typeof aExpectedApp.downloadSize !== "undefined") { michael@0: is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize"); michael@0: } michael@0: if (typeof aExpectedApp.readyToApplyDownload !== "undefined") { michael@0: is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload, michael@0: "Check readyToApplyDownload"); michael@0: } michael@0: if (typeof aExpectedApp.origin !== "undefined") { michael@0: is(aApp.origin, aExpectedApp.origin, "Check origin"); michael@0: } michael@0: if (aLaunchable) { michael@0: if (aUninstall) { michael@0: checkUninstallApp(aApp); michael@0: } else if (aCb && typeof aCb === "function") { michael@0: aCb(); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: // Check if app is not launchable. michael@0: var req = aApp.launch(); michael@0: req.onsuccess = function () { michael@0: ok(false, "We shouldn't be here"); michael@0: finish(); michael@0: }; michael@0: req.onerror = function() { michael@0: ok(true, "App is not launchable"); michael@0: if (aUninstall) { michael@0: checkUninstallApp(aApp); michael@0: } else if (aCb && typeof aCb === "function") { michael@0: aCb(); michael@0: } michael@0: return; michael@0: }; michael@0: } michael@0: michael@0: return { michael@0: setSteps: function (aSteps) { michael@0: steps = aSteps; michael@0: }, michael@0: next: next, michael@0: start: start, michael@0: finish: finish, michael@0: mozAppsError: mozAppsError, michael@0: setAppVersion: setAppVersion, michael@0: checkAppState: checkAppState, michael@0: checkAppDownloadError: checkAppDownloadError, michael@0: get gSJSPath() { return gSJSPath; }, michael@0: set gSJSPath(aValue) { gSJSPath = aValue }, michael@0: get gSJS() { return gSJS; }, michael@0: get gAppName() { return gAppName;}, michael@0: get gApp() { return gApp; }, michael@0: set gApp(aValue) { gApp = aValue; }, michael@0: gInstallOrigin: gInstallOrigin michael@0: }; michael@0: michael@0: })();