Bug 821589 - mozApps.installPackage doesn't have any automated tests at the DOM level; r=fabrice

This commit is contained in:
Fernando Jiménez 2013-05-20 11:23:33 +02:00
parent 73f1e1a650
commit 88e955f7b5
6 changed files with 602 additions and 3 deletions

View File

@ -12,14 +12,23 @@ relativesrcdir = @relativesrcdir@
include $(DEPTH)/config/autoconf.mk
MOCHITEST_FILES = \
test_app_update.html \
file_app.sjs \
file_app.template.html \
file_hosted_app.template.webapp \
file_cached_app.template.webapp \
file_cached_app.template.appcache \
file_hosted_app.template.webapp \
test_app_update.html \
$(NULL)
ifdef MOZ_B2G
MOCHITEST_FILES += \
file_packaged_app.sjs \
file_packaged_app.template.webapp \
file_packaged_app.template.html \
test_packaged_app_install.html \
$(NULL)
endif
MOCHITEST_CHROME_FILES = \
test_apps_service.xul \
$(NULL)

View File

@ -40,7 +40,10 @@ function cbError() {
function go() {
ok(true, "Launched app");
var request = window.navigator.mozApps.getSelf();
request.onsuccess = function() { var app = request.result; checkApp(app); }
request.onsuccess = function() {
var app = request.result;
checkApp(app);
}
request.onerror = cbError;
}

View File

@ -0,0 +1,166 @@
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
// From prio.h
const PR_RDWR = 0x04;
const PR_CREATE_FILE = 0x08;
const PR_TRUNCATE = 0x20;
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
var gBasePath = "tests/dom/apps/tests/";
var gMiniManifestTemplate = "file_packaged_app.template.webapp";
var gAppTemplate = "file_packaged_app.template.html";
var gAppName = "appname";
var gDevName = "devname";
var gDevUrl = "http://dev.url";
function handleRequest(request, response) {
var query = getQuery(request);
response.setHeader("Access-Control-Allow-Origin", "*", false);
var packageSize = ("packageSize" in query) ? query.packageSize : 0;
var appName = ("appName" in query) ? query.appName : gAppName;
var devName = ("devName" in query) ? query.devName : gDevName;
var devUrl = ("devUrl" in query) ? query.devUrl : gDevUrl;
// If this is a version update, update state, prepare the manifest,
// the application package and return.
if ("setVersion" in query) {
var version = query.setVersion;
setState("version", version);
var packageName = "test_packaged_app_" + version + ".zip";
setState("packageName", packageName);
var packagePath = "/" + gBasePath + "file_packaged_app.sjs?getPackage=" +
packageName;
setState("packagePath", packagePath);
// Create the application package.
var zipWriter = Cc["@mozilla.org/zipwriter;1"]
.createInstance(Ci.nsIZipWriter);
var zipFile = FileUtils.getFile("TmpD", [packageName]);
zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
// We want to run some tests without the manifest included in the zip.
if (version != "0") {
var manifestTemplate = gBasePath + gMiniManifestTemplate;
var manifest = makeResource(manifestTemplate, version, packagePath,
packageSize, appName, devName, devUrl);
addZipEntry(zipWriter, manifest, "manifest.webapp");
}
var appTemplate = gBasePath + gAppTemplate;
var app = makeResource(appTemplate, version, packagePath, packageSize,
appName, devName, devUrl);
addZipEntry(zipWriter, app, "index.html");
zipWriter.close();
response.setHeader("Content-Type", "text/html", false);
response.write("OK");
return;
}
// Get the version from server state
var version = Number(getState("version"));
var packageName = String(getState("packageName"));
var packagePath = String(getState("packagePath"));
var etag = getEtag(request, version);
if (etagMatches(request, etag)) {
dump("Etags Match. Sending 304\n");
response.setStatusLine(request.httpVersion, "304", "Not modified");
return;
}
response.setHeader("Etag", etag, false);
// Serve the application package corresponding to the requested app version.
if ("getPackage" in query) {
var resource = readFile(packageName, true);
response.setHeader("Content-Type",
"Content-Type: application/java-archive", false);
response.write(resource);
return;
}
// Serve the mini-manifest corresponding to the requested app version.
if ("getManifest" in query) {
var template = gBasePath + gMiniManifestTemplate;
if (!("noManifestContentType" in query)) {
response.setHeader("Content-Type",
"application/x-web-app-manifest+json", false);
}
packagePath = "wrongPackagePath" in query ? "" : packagePath;
var manifest = makeResource(template, version, packagePath, packageSize,
appName, devName, devUrl);
response.write(manifest);
return;
}
response.setHeader("Content-type", "text-html", false);
response.write("KO");
}
function getQuery(request) {
var query = {};
request.queryString.split('&').forEach(function (val) {
var [name, value] = val.split('=');
query[name] = unescape(value);
});
return query;
}
function getEtag(request, version) {
return request.queryString.replace(/&/g, '-').replace(/=/g, '-') +
'-' + version;
}
function etagMatches(request, etag) {
return request.hasHeader("If-None-Match") &&
request.getHeader("If-None-Match") == etag;
}
// File and resources helpers
function addZipEntry(zipWriter, entry, entryName) {
var stream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stream.setData(entry, entry.length);
zipWriter.addEntryStream(entryName, Date.now(),
Ci.nsIZipWriter.COMPRESSION_BEST, stream, false);
}
function readFile(path, fromTmp) {
var dir = fromTmp ? "TmpD" : "CurWorkD";
var file = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get(dir, Ci.nsILocalFile);
var fstream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
var split = path.split("/");
for(var i = 0; i < split.length; ++i) {
file.append(split[i]);
}
fstream.init(file, -1, 0, 0);
var data = NetUtil.readInputStreamToString(fstream, fstream.available());
fstream.close();
return data;
}
function makeResource(templatePath, version, packagePath, packageSize,
appName, developerName, developerUrl) {
var res = readFile(templatePath, false)
.replace(/VERSIONTOKEN/g, version)
.replace(/PACKAGEPATHTOKEN/g, packagePath)
.replace(/PACKAGESIZETOKEN/g, packageSize)
.replace(/NAMETOKEN/g, appName)
.replace(/DEVELOPERTOKEN/g, developerName)
.replace(/DEVELOPERURLTOKEN/g, developerUrl);
return res;
}

View File

@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
App Body. Version: VERSIONTOKEN
</body>
</html>

View File

@ -0,0 +1,13 @@
{
"name" : "NAMETOKEN",
"version" : "VERSIONTOKEN",
"size" : PACKAGESIZETOKEN,
"package_path": "PACKAGEPATHTOKEN",
"description": "Updated even faster than Firefox, just to annoy slashdotters",
"launch_path": "tests/dom/apps/tests/file_packaged_app.sjs",
"developer": {
"name": "DEVELOPERTOKEN",
"url": "DEVELOPERURLTOKEN"
},
"default_locale": "en-US"
}

View File

@ -0,0 +1,401 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={821589}
-->
<head>
<title>Test for Bug {821589} Packaged apps installation and update</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={821589}">Mozilla Bug {821589}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
var gInstallOrigin = "http://mochi.test:8888";
var gSJSPath = "tests/dom/apps/tests/file_packaged_app.sjs";
var gSJS = "http://test/" + gSJSPath;
var gAppName = "appname";
var gApp = null;
var launchableValue = undefined;
var index = -1;
function debug(aMsg) {
//dump("== Tests debug == " + aMsg + "\n");
}
function next() {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
function go() {
next();
}
function finish() {
SpecialPowers.setAllAppsLaunchable(launchableValue);
SpecialPowers.removePermission("webapps-manage", document);
SimpleTest.finish();
}
function cbError(aError) {
ok(false, "Error callback invoked " + aError);
finish();
}
function setAppVersion(aVersion, aCb) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
is(xhr.responseText, "OK", "setVersion OK");
aCb();
});
xhr.addEventListener("error", cbError);
xhr.addEventListener("abort", cbError);
xhr.open("GET", gSJS + "?setVersion=" + aVersion, true);
xhr.send();
}
function checkAppInstallError(aMiniManifestURL, aExpectedError) {
var req = navigator.mozApps.installPackage(aMiniManifestURL);
req.onsuccess = function() {
ok(false, "We are supposed to throw " + aExpectedError);
finish();
};
req.onerror = function(evt) {
var error = evt.target.error.name;
if (error == aExpectedError) {
ok(true, "Got expected " + aExpectedError);
next();
} else {
ok(false, "Got unexpected " + aError);
finish();
}
};
}
function checkUninstallApp(aApp) {
var req = navigator.mozApps.mgmt.uninstall(aApp);
req.onsuccess = function() {
ok(true, "App uninstalled");
aApp.ondownloadsuccess = null;
aApp.ondownloaderror = null;
aApp.onprogress = null;
next();
};
req.onerror = function(evt) {
ok(false, "Got unexpected " + evt.target.error.name);
finish();
};
}
function checkAppDownloadError(aMiniManifestURL,
aExpectedError,
aVersion,
aUninstall,
aDownloadAvailable,
aName) {
var req = navigator.mozApps.installPackage(aMiniManifestURL);
req.onsuccess = function() {
ok(true, "App installed");
};
req.onerror = function(evt) {
ok(false, "Got unexpected " + evt.target.error.name);
finish();
};
navigator.mozApps.mgmt.oninstall = function(evt) {
var aApp = evt.application;
aApp.ondownloaderror = function(evt) {
var error = aApp.downloadError.name;
if (error == aExpectedError) {
ok(true, "Got expected " + aExpectedError);
var expected = {
name: aName,
manifestURL: aMiniManifestURL,
installOrigin: gInstallOrigin,
progress: 0,
installState: "pending",
downloadAvailable: aDownloadAvailable,
downloading: false,
downloadSize: 0,
size: 0,
readyToApplyDownload: false,
};
checkAppState(aApp, aVersion, expected, false, aUninstall, next);
} else {
ok(false, "Got unexpected " + error);
finish();
}
};
aApp.ondownloadsuccess = function(evt) {
ok(false, "We are supposed to throw " + aExpectedError);
finish();
};
};
}
function checkInstalledApp(aMiniManifestURL,
aVersion,
aExpectedApp,
aLaunchable,
aCb) {
var req = navigator.mozApps.checkInstalled(aMiniManifestURL);
req.onsuccess = function(evt) {
ok(true, "The app is installed");
checkAppState(evt.application, aVersion, aExpectedApp, aLaunchable,
false, aCb);
};
req.onerror = function() {
ok(false, "The app is not installed");
finish();
};
}
function checkAppState(aApp,
aVersion,
aExpectedApp,
aLaunchable,
aUninstall,
aCb) {
debug(JSON.stringify(aApp, null, 2));
if (aApp.manifest) {
debug(JSON.stringify(aApp.manifest, null, 2));
}
if (aExpectedApp.name) {
if (aApp.manifest) {
is(aApp.manifest.name, aExpectedApp.name, "Check name");
}
is(aApp.updateManifest.name, aExpectedApp.name, "Check name mini-manifest");
}
if (aApp.manifest) {
is(aApp.manifest.version, aVersion, "Check version");
}
if (typeof aExpectedApp.size !== "undefined" && aApp.manifest) {
is(aApp.manifest.size, aExpectedApp.size, "Check size");
}
if (aApp.manifest) {
is(aApp.manifest.launch_path, gSJSPath, "Check launch path");
}
if (aExpectedApp.manifestURL) {
is(aApp.manifestURL, aExpectedApp.manifestURL, "Check manifestURL");
}
if (aExpectedApp.installOrigin) {
is(aApp.installOrigin, aExpectedApp.installOrigin, "Check installOrigin");
}
ok(aApp.removable, "Removable app");
if (typeof aExpectedApp.progress !== "undefined") {
todo(aApp.progress == aExpectedApp.progress, "Check progress");
}
if (aExpectedApp.installState) {
is(aApp.installState, aExpectedApp.installState, "Check installState");
}
if (typeof aExpectedApp.downloadAvailable !== "undefined") {
is(aApp.downloadAvailable, aExpectedApp.downloadAvailable,
"Check download available");
}
if (typeof aExpectedApp.downloading !== "undefined") {
is(aApp.downloading, aExpectedApp.downloading, "Check downloading");
}
if (typeof aExpectedApp.downloadSize !== "undefined") {
is(aApp.downloadSize, aExpectedApp.downloadSize, "Check downloadSize");
}
if (typeof aExpectedApp.readyToApplyDownload !== "undefined") {
is(aApp.readyToApplyDownload, aExpectedApp.readyToApplyDownload,
"Check readyToApplyDownload");
}
if (aLaunchable) {
if (aUninstall) {
checkUninstallApp(aApp);
} else if (aCb && typeof aCb === 'function') {
aCb();
}
return;
}
// Check if app is not launchable.
var req = aApp.launch();
req.onsuccess = function () {
ok(false, "We shouldn't be here");
finish();
};
req.onerror = function() {
ok(true, "App is not launchable");
if (aUninstall) {
checkUninstallApp(aApp);
} else if (aCb && typeof aCb === 'function') {
aCb();
}
return;
};
}
SimpleTest.waitForExplicitFinish();
var steps = [
function() {
// Set up
launchableValue = SpecialPowers.setAllAppsLaunchable(true);
SpecialPowers.addPermission("webapps-manage", true, document);
ok(true, "Set up");
next();
},
function() {
ok(true, "autoConfirmAppInstall");
SpecialPowers.autoConfirmAppInstall(next);
},
function() {
setAppVersion(0, next);
},
function() {
// Test network error.
ok(true, "== TEST == Network error");
checkAppInstallError("http://notvalidurl", "NETWORK_ERROR");
},
function() {
// Test wrong mini-manifest content type.
ok(true, "== TEST == Not valid mini-manifest content type");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&noManifestContentType=true";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test mini-manifest 'size' value is not number. Bug 839435.
ok(true, "== TEST == Size value is not a number");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&packageSize=\"NotANumber\"";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test mini-manifest negative 'size' value. Bug 839435.
ok(true, "== TEST == Negative size value");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&packageSize=-1";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test wrong package path
ok(true, "== TEST == Installing app with wrong package path");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&wrongPackagePath=true";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test no manifest in zip file.
ok(true, "== TEST == No manifest in the zip file");
var miniManifestURL = gSJS + "?getManifest=true";
checkAppDownloadError(miniManifestURL, "MISSING_MANIFEST", 0, true, true,
gAppName);
},
function() {
setAppVersion(1, next);
},
function() {
// Test mini-manifest app name is different from the webapp manifest name.
// Bug 844243.
ok(true, "== TEST == Mini-manifest app name is different from webapp " +
"manifest name");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&appName=arandomname";
checkAppDownloadError(miniManifestURL, "MANIFEST_MISMATCH", 1, true, true,
"arandomname");
},
function() {
// Test mini-manifest dev name is different from the webapp manifest dev
// name.
ok (true, "== TEST == Mini-manifest dev name is different from manifest " +
"dev name");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&devName=arandomdevname";
checkAppDownloadError(miniManifestURL, "MANIFEST_MISMATCH", 1, true, true,
gAppName);
},
function() {
// Test mini-manifest dev url is different from the webapp manifest dev
// url.
ok (true, "== TEST == Mini-manifest dev url is different from manifest " +
"dev url");
var miniManifestURL = gSJS +
"?getManifest=true" +
"&devUrl=arandomdevurl";
checkAppDownloadError(miniManifestURL, "MANIFEST_MISMATCH", 1, true, true,
gAppName);
},
function() {
setAppVersion(2, next);
},
function() {
ok(true, "== TEST == Install packaged app");
var miniManifestURL = gSJS +
"?getManifest=true";
navigator.mozApps.mgmt.oninstall = function(evt) {
ok(true, "Got oninstall event");
gApp = evt.application;
gApp.ondownloaderror = function() {
ok(false, "Download error " + gApp.downloadError.name);
finish();
};
gApp.ondownloadsuccess = function() {
ok(true, "App downloaded");
var expected = {
name: gAppName,
manifestURL: miniManifestURL,
installOrigin: gInstallOrigin,
progress: 0,
installState: "installed",
downloadAvailable: false,
downloading: false,
downloadSize: 0,
size: 0,
readyToApplyDownload: false,
};
checkAppState(gApp, 2, expected, true, false, next);
};
};
var request = navigator.mozApps.installPackage(miniManifestURL);
request.onerror = function(evt) {
cbError(evt.target.error.name);
};
request.onsuccess = function() {
ok(true, "Application installed");
};
},
function() {
ok(true, "all done!\n");
SimpleTest.finish();
}
];
addLoadEvent(go);
</script>
</pre>
</body>
</html>