gecko/dom/apps/tests/test_packaged_app_install.html

331 lines
12 KiB
HTML
Raw Normal View History

<!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>
<script type="text/javascript" src="test_packaged_app_common.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";
function debug(aMsg) {
//dump("== Tests debug == " + aMsg + "\n");
}
function checkAppInstallError(aMiniManifestURL, aExpectedError) {
var req = navigator.mozApps.installPackage(aMiniManifestURL);
req.onsuccess = function() {
ok(false, "We are supposed to throw " + aExpectedError);
PackagedTestHelper.finish();
};
req.onerror = function(evt) {
var error = evt.target.error.name;
if (error == aExpectedError) {
info("Got expected " + aExpectedError);
PackagedTestHelper.next();
} else {
ok(false, "Got unexpected " + error);
PackagedTestHelper.finish();
}
};
}
function checkUninstallApp(aApp) {
var req = navigator.mozApps.mgmt.uninstall(aApp);
req.onsuccess = function() {
info("App uninstalled");
aApp.ondownloadsuccess = null;
aApp.ondownloaderror = null;
aApp.onprogress = null;
PackagedTestHelper.next();
};
req.onerror = function(evt) {
ok(false, "Got unexpected " + evt.target.error.name);
PackagedTestHelper.finish();
};
}
function checkInstalledApp(aMiniManifestURL,
aVersion,
aExpectedApp,
aLaunchable,
aCb) {
var req = navigator.mozApps.checkInstalled(aMiniManifestURL);
req.onsuccess = function(evt) {
ok(req.result, "The app is installed");
if (!req.result) {
PackagedTestHelper.finish();
}
PackagedTestHelper.checkAppState(evt.application, aVersion, aExpectedApp,
aLaunchable, false, aCb);
};
}
SimpleTest.waitForExplicitFinish();
var steps = [
function() {
// Set up
SpecialPowers.setAllAppsLaunchable(true);
SpecialPowers.addPermission("webapps-manage", true, document);
info("Set up");
PackagedTestHelper.next();
},
function() {
info("autoConfirmAppInstall");
SpecialPowers.autoConfirmAppInstall(PackagedTestHelper.next);
},
function() {
ok(true, "autoConfirmAppUninstall");
SpecialPowers.autoConfirmAppUninstall(PackagedTestHelper.next);
},
function() {
PackagedTestHelper.setAppVersion(0, PackagedTestHelper.next);
},
function() {
// Bug 927699 - navigator.mozApps.install(url) lets NS_ERROR_FAILURE onto
// the web.
info("== TEST == INVALID_URL");
checkAppInstallError("", "INVALID_URL");
},
function() {
// Test network error.
info("== TEST == Network error");
checkAppInstallError("http://notvalidurl", "NETWORK_ERROR");
},
function() {
// Test wrong mini-manifest content type.
info("== TEST == Not valid mini-manifest content type");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&noManifestContentType=true";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST_CONTENT_TYPE");
},
function() {
// Test mini-manifest 'size' value is not number. Bug 839435.
info("== TEST == Size value is not a number");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&packageSize=\"NotANumber\"";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test mini-manifest negative 'size' value. Bug 839435.
info("== TEST == Negative size value");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&packageSize=-1";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test wrong package path
info("== TEST == Installing app with wrong package path");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&wrongPackagePath=true";
checkAppInstallError(miniManifestURL, "INVALID_MANIFEST");
},
function() {
// Test no manifest in zip file.
info("== TEST == No manifest in the zip file");
var miniManifestURL = PackagedTestHelper.gSJS + "?getManifest=true";
PackagedTestHelper.checkAppDownloadError(miniManifestURL,
"MISSING_MANIFEST", 0, true, true,
PackagedTestHelper.gAppName);
},
function() {
PackagedTestHelper.setAppVersion(1, PackagedTestHelper.next);
},
function() {
// Test mini-manifest app name is different from the webapp manifest name.
// Bug 844243.
info("== TEST == Mini-manifest app name is different from webapp " +
"manifest name");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&appName=arandomname";
PackagedTestHelper.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 = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&devName=arandomdevname";
PackagedTestHelper.checkAppDownloadError(miniManifestURL,
"MANIFEST_MISMATCH", 1, true, true,
PackagedTestHelper.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 = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&devUrl=arandomdevurl";
PackagedTestHelper.checkAppDownloadError(miniManifestURL,
"MANIFEST_MISMATCH", 1, true, true,
PackagedTestHelper.gAppName);
},
function() {
PackagedTestHelper.setAppVersion(2, PackagedTestHelper.next);
},
function() {
info("== TEST == Install packaged app");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true";
navigator.mozApps.mgmt.oninstall = function(evt) {
info("Got oninstall event");
PackagedTestHelper.gApp = evt.application;
PackagedTestHelper.gApp.ondownloaderror = function() {
ok(false, "Download error " +
PackagedTestHelper.gApp.downloadError.name);
PackagedTestHelper.finish();
};
PackagedTestHelper.gApp.ondownloadsuccess = function() {
info("App downloaded");
var expected = {
name: PackagedTestHelper.gAppName,
manifestURL: miniManifestURL,
installOrigin: PackagedTestHelper.gInstallOrigin,
progress: 0,
installState: "installed",
downloadAvailable: false,
downloading: false,
downloadSize: 0,
size: 0,
readyToApplyDownload: false
};
PackagedTestHelper.checkAppState(PackagedTestHelper.gApp, 2, expected,
true, false, PackagedTestHelper.next);
};
};
var request = navigator.mozApps.installPackage(miniManifestURL);
request.onerror = PackagedTestHelper.mozAppsError;
request.onsuccess = function() {
info("Application installed");
};
},
function() {
PackagedTestHelper.setAppVersion(3, PackagedTestHelper.next, false, true);
},
function() {
info("== TEST == Install packaged app with a cancel/resume");
var miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true&allowCancel";
navigator.mozApps.mgmt.oninstall = function(evt) {
info("Got oninstall event");
PackagedTestHelper.gApp = evt.application;
PackagedTestHelper.gApp.onprogress = function() {
// The first onprogress event is generated *before* the actual
// download is started. And the next one will be generated too late.
// So we just try to delay the canceling a bit.
setTimeout(function() {
// Let's try cancelling and resuming the download later on.
info("Cancelling the download");
PackagedTestHelper.gApp.cancelDownload();
// And only do this once.
PackagedTestHelper.gApp.onprogress = null;
}, 10);
};
var alreadyCanceled = false;
PackagedTestHelper.gApp.ondownloaderror = function() {
info("Got a expected download error");
ok(!alreadyCanceled, "The download should be cancelled only once!");
is(PackagedTestHelper.gApp.downloadError.name, "DOWNLOAD_CANCELED",
"Download error " + PackagedTestHelper.gApp.downloadError.name);
if (!alreadyCanceled) {
PackagedTestHelper.gApp.download();
alreadyCanceled = true;
}
};
PackagedTestHelper.gApp.ondownloadsuccess = function() {
info("App downloaded");
// Sometimes we can't cancel in time (since setting a high timer on the
// sjs just doesn't work). Let's fail gracefully in that case, and just
// give a warning here.
if (!alreadyCanceled) {
todo(alreadyCanceled, "The app download wasn't cancelled in time!");
}
var expected = {
name: PackagedTestHelper.gAppName,
manifestURL: miniManifestURL,
installOrigin: PackagedTestHelper.gInstallOrigin,
progress: 0,
installState: alreadyCanceled?"pending":"installed",
downloadAvailable: false,
downloading: false,
downloadSize: 0,
size: 0,
readyToApplyDownload: alreadyCanceled
};
PackagedTestHelper.checkAppState(PackagedTestHelper.gApp, 3, expected,
true, false, function() {});
};
PackagedTestHelper.gApp.ondownloadapplied = function() {
info("App download applied.");
var expected = {
name: PackagedTestHelper.gAppName,
manifestURL: miniManifestURL,
installOrigin: PackagedTestHelper.gInstallOrigin,
progress: 0,
installState: "installed",
downloadAvailable: false,
downloading: false,
downloadSize: 0,
size: 0,
readyToApplyDownload: false
};
PackagedTestHelper.checkAppState(PackagedTestHelper.gApp, 3, expected,
true, false, PackagedTestHelper.next);
}
};
var request = navigator.mozApps.installPackage(miniManifestURL);
request.onerror = PackagedTestHelper.mozAppsError;
request.onsuccess = function() {
info("Application installed");
};
},
function() {
info("all done!\n");
PackagedTestHelper.finish();
}
];
PackagedTestHelper.setSteps(steps);
addLoadEvent(PackagedTestHelper.start);
</script>
</pre>
</body>
</html>