gecko/dom/apps/tests/test_packaged_app_update.html
Fernando Jiménez 20ee412e73 Bug 903291 - App download hangs indefinitely if the child process dies before confirming the install. r=fabrice
* * *
Bug 903291 - App download hangs indefinitely if the child process dies before confirming the install
2014-05-24 12:28:13 -07:00

289 lines
9.3 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={900533}
-->
<head>
<title>Test for Bug {900533} Packaged app update tests</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={900533}">Mozilla Bug {900533}</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");
}
var miniManifestURL;
SimpleTest.waitForExplicitFinish();
function checkForUpdate(aExpected, aOnSuccess, aOnApplied, aOnDownloadError,
aLaunchDownload, aOnRequestError) {
var lApp = PackagedTestHelper.gApp;
lApp.ondownloadsuccess = aOnSuccess || null;
lApp.ondownloadapplied = aOnApplied || null;
lApp.ondownloaderror = aOnDownloadError || null;
var request = lApp.checkForUpdate();
request.onerror = aOnRequestError ? aOnRequestError.bind(undefined, request) :
PackagedTestHelper.mozAppsError;
request.onsuccess = function(event) {
var expectingDownload = aExpected ? "": "not ";
ok(lApp.downloadAvailable === aExpected,
"Download should " + expectingDownload + "be available");
if (aLaunchDownload) {
lApp.download();
} else {
PackagedTestHelper.next();
}
};
}
function checkLastAppState(aMiniManifestURL, aExpectedReady, aExpectedDownload,
aExpectedVersion, aCb) {
info(aExpectedReady ? "App downloaded" : "App download applied");
var expected = {
name: PackagedTestHelper.gAppName,
manifestURL: aMiniManifestURL,
installOrigin: PackagedTestHelper.gInstallOrigin,
progress: 0,
installState: aExpectedReady ? "updating" : "installed",
downloadAvailable: aExpectedDownload,
downloading: false,
size: 0,
readyToApplyDownload: aExpectedReady
};
PackagedTestHelper.checkAppState(PackagedTestHelper.gApp, aExpectedVersion,
expected, true, false, aCb);
}
function updateApp(aExpectedReady, aPreviousVersion, aNextVersion) {
var lApp = PackagedTestHelper.gApp;
var ondownloadappliedhandler =
checkLastAppState.bind(PackagedTestHelper, miniManifestURL, false, false,
aNextVersion, PackagedTestHelper.next);
var ondownloadsuccesshandler =
checkLastAppState.bind(undefined, miniManifestURL,
aExpectedReady, false, aPreviousVersion,
function() {
navigator.mozApps.mgmt.applyDownload(lApp);
});
checkForUpdate(true, ondownloadsuccesshandler, ondownloadappliedhandler,
null, true);
}
var initialPermissionState = {
"geolocation": "prompt",
"audio-capture": "prompt",
"video-capture": "prompt",
"test-permission-read": "prompt",
"downloads": "deny"
}
var permissionsToSet = {
"geolocation": "allow",
"test-permission-read": "allow",
"audio-capture": "deny"
}
var permissionsToCheck = {
"geolocation": "allow",
"audio-capture": "deny",
"video-capture": "prompt",
"test-permission-read": "allow",
"downloads": "deny"
}
function validatePermissions(aList, aDontFail) {
var gApp = PackagedTestHelper.gApp;
var mozPermissions = window.navigator.mozPermissionSettings;
var permission;
for (permission in aList) {
var permValue = mozPermissions.get(permission, gApp.manifestURL,
gApp.origin, false);
var wouldFail = permValue != aList[permission];
var checkFun = (aDontFail && wouldFail) ? todo_is : is;
checkFun(permValue, aList[permission],
"Permission " + permission + " should be " + aList[permission]);
}
}
var steps = [
function() {
// Set up
SpecialPowers.setAllAppsLaunchable(true);
SpecialPowers.addPermission("webapps-manage", true, document);
info("Set up");
// Note that without useCurrentProfile the permissions just aren't added.
SpecialPowers.pushPermissions(
[{'type': 'permissions', 'allow': true, 'context': document}],
function() {
SpecialPowers.pushPrefEnv(
{"set": [["dom.mozPermissionSettings.enabled", true],
["dom.webapps.useCurrentProfile", true]]},
PackagedTestHelper.next);
}
);
},
function() {
info("autoConfirmAppInstall");
SpecialPowers.autoConfirmAppInstall(PackagedTestHelper.next);
},
function() {
PackagedTestHelper.setAppVersion(2, PackagedTestHelper.next);
},
function() {
info("== TEST == Install packaged app");
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 =
checkLastAppState.bind(undefined, miniManifestURL, false, false,
2, PackagedTestHelper.next);
};
var request = navigator.mozApps.installPackage(miniManifestURL);
request.onerror = PackagedTestHelper.mozAppsError;
request.onsuccess = function() {
info("Application installed");
};
},
function() {
info("== TEST == Permissions installed correctly");
validatePermissions(initialPermissionState);
PackagedTestHelper.next();
},
function() {
info("== TEST == Check for Update and try to download it without update available");
function onerror() {
is(PackagedTestHelper.gApp.downloadError.name, "NO_DOWNLOAD_AVAILABLE", "Download not available");
ok(!PackagedTestHelper.gApp.readyToApplyDownload, "Not ready to apply download");
PackagedTestHelper.next();
}
function onsuccess() {
ok(false, "ondownloadsuccess fired");
PackagedTestHelper.next();
}
checkForUpdate(false, onsuccess, null, onerror, true);
},
function() {
info("== TEST == Remember permissions");
var gApp = PackagedTestHelper.gApp;
var mozPermissions = window.navigator.mozPermissionSettings;
var permission;
for (permission in permissionsToSet) {
try {
window.navigator.mozPermissionSettings.set(permission,
permissionsToSet[permission],
gApp.manifestURL,
gApp.origin, false);
} catch (e) {
ok(false,
"mozPermissionSettings.set failed for " + permission + " - " + e);
}
}
PackagedTestHelper.next();
},
function() {
info("== TEST == Check that the permissions have been saved");
// Since the permission API isn't really synchronous, just log any errors
// here.
validatePermissions(permissionsToCheck, true /*dontFail*/);
PackagedTestHelper.next();
},
function() {
PackagedTestHelper.setAppVersion(3, PackagedTestHelper.next);
},
function() {
info("== TEST == Update packaged app");
updateApp(true, 2, 3);
},
function() {
info("== TEST == Check that saved permissions were kept");
validatePermissions(permissionsToCheck);
PackagedTestHelper.next();
},
function() {
info("== TEST == Check for Update after getting a new package");
checkForUpdate(false);
},
function() {
PackagedTestHelper.setAppVersion(4, PackagedTestHelper.next, true);
},
function() {
info("== TEST == Update packaged app - same package");
updateApp(false, 3, 3);
},
function() {
info("== TEST == Check for Update after getting the same package");
checkForUpdate(false);
},
function() {
PackagedTestHelper.setAppVersion(1, PackagedTestHelper.next);
},
function() {
info("== TEST == Update packaged app - Updating a pending app");
miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true" +
"&appName=arandomname" +
"&appToFail1";
PackagedTestHelper.checkAppDownloadError(miniManifestURL,
"MANIFEST_MISMATCH", 1, false, true,
"arandomname",
function () {
checkForUpdate(false, null, null, null, false,
function (request) {
if (request.error.name === "PENDING_APP_NOT_UPDATABLE") {
info("Got expected PENDING_APP_NOT_UPDATEABLE");
} else {
ok(false, "Got unexpected " + request.error.name);
}
PackagedTestHelper.next();
});
});
},
function() {
info("all done!\n");
PackagedTestHelper.finish();
}
];
PackagedTestHelper.setSteps(steps);
// appToUpdate added to the URL so we get a unique URL for this app.
// Unique in this case meaning different from the ones used on the
// install tests
miniManifestURL = PackagedTestHelper.gSJS + "?getManifest=true&appToUpdate";
addLoadEvent(PackagedTestHelper.start);
</script>
</pre>
</body>
</html>