4 https://bugzilla.mozilla.org/show_bug.cgi?id={900533}
7 <title>Test for Bug {
900533} Packaged app update tests
</title>
8 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
9 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <script type=
"text/javascript" src=
"test_packaged_app_common.js"></script>
11 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
15 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id={900533}">Mozilla Bug {
900533}
</a>
17 <div id=
"content" style=
"display: none">
21 <script class=
"testbody" type=
"text/javascript">
25 function debug(aMsg) {
26 //dump(
"== Tests debug == " + aMsg +
"\n");
31 SimpleTest.waitForExplicitFinish();
33 function checkForUpdate(aExpected, aOnSuccess, aOnApplied, aOnDownloadError,
34 aLaunchDownload, aOnRequestError) {
35 var lApp = PackagedTestHelper.gApp;
37 lApp.ondownloadsuccess = aOnSuccess || null;
38 lApp.ondownloadapplied = aOnApplied || null;
39 lApp.ondownloaderror = aOnDownloadError || null;
41 var request = lApp.checkForUpdate();
42 request.onerror = aOnRequestError ? aOnRequestError.bind(undefined, request) :
43 PackagedTestHelper.mozAppsError;
44 request.onsuccess = function(event) {
45 var expectingDownload = aExpected ?
"":
"not ";
46 ok(lApp.downloadAvailable === aExpected,
47 "Download should " + expectingDownload +
"be available");
48 if (aLaunchDownload) {
51 PackagedTestHelper.next();
56 function checkLastAppState(aMiniManifestURL, aExpectedReady, aExpectedDownload,
57 aExpectedVersion, aCb) {
58 info(aExpectedReady ?
"App downloaded" :
"App download applied");
60 name: PackagedTestHelper.gAppName,
61 manifestURL: aMiniManifestURL,
62 installOrigin: PackagedTestHelper.gInstallOrigin,
64 installState: aExpectedReady ?
"updating" :
"installed",
65 downloadAvailable: aExpectedDownload,
68 readyToApplyDownload: aExpectedReady
71 PackagedTestHelper.checkAppState(PackagedTestHelper.gApp, aExpectedVersion,
72 expected, true, false, aCb);
75 function updateApp(aExpectedReady, aPreviousVersion, aNextVersion, aFailOnError) {
76 var lApp = PackagedTestHelper.gApp;
78 var ondownloadappliedhandler =
79 checkLastAppState.bind(PackagedTestHelper, miniManifestURL, false, false,
81 setTimeout.bind(undefined, PackagedTestHelper.next,
500));
83 var ondownloadsuccesshandler =
84 checkLastAppState.bind(undefined, miniManifestURL,
85 aExpectedReady, false, aPreviousVersion,
87 navigator.mozApps.mgmt.applyDownload(lApp);
90 var ondownloaderrorhandler = aFailOnError ?
92 ok(false,
"We should not get an error but got " +
93 lApp.downloadError.name);
94 PackagedTestHelper.finish();
97 checkForUpdate(true, ondownloadsuccesshandler, ondownloadappliedhandler,
98 ondownloaderrorhandler, true);
102 var initialPermissionState = {
103 "geolocation":
"prompt",
104 "audio-capture":
"prompt",
105 "video-capture":
"prompt",
106 "test-permission-read":
"prompt",
110 var permissionsToSet = {
111 "geolocation":
"allow",
112 "test-permission-read":
"allow",
113 "audio-capture":
"deny"
116 var permissionsToCheck = {
117 "geolocation":
"allow",
118 "audio-capture":
"deny",
119 "video-capture":
"prompt",
120 "test-permission-read":
"allow",
124 function validatePermissions(aList, aDontFail) {
125 var gApp = PackagedTestHelper.gApp;
126 var mozPermissions = window.navigator.mozPermissionSettings;
128 for (permission in aList) {
129 var permValue = mozPermissions.get(permission, gApp.manifestURL,
131 var wouldFail = permValue != aList[permission];
132 var checkFun = (aDontFail && wouldFail) ? todo_is : is;
133 checkFun(permValue, aList[permission],
134 "Permission " + permission +
" should be " + aList[permission]);
142 SpecialPowers.setAllAppsLaunchable(true);
143 SpecialPowers.addPermission(
"webapps-manage", true, document);
145 // Note that without useCurrentProfile the permissions just aren't added.
146 SpecialPowers.pushPermissions(
147 [{'type': 'permissions', 'allow': true, 'context': document}],
149 SpecialPowers.pushPrefEnv(
150 {
"set": [[
"dom.mozPermissionSettings.enabled", true],
151 [
"dom.webapps.useCurrentProfile", true]]},
152 PackagedTestHelper.next);
157 info(
"autoConfirmAppInstall");
158 SpecialPowers.autoConfirmAppInstall(PackagedTestHelper.next);
161 PackagedTestHelper.setAppVersion(
2, PackagedTestHelper.next);
164 info(
"== TEST == Install packaged app");
165 navigator.mozApps.mgmt.oninstall = function(evt) {
166 info(
"Got oninstall event");
167 PackagedTestHelper.gApp = evt.application;
168 PackagedTestHelper.gApp.ondownloaderror = function() {
169 ok(false,
"Download error " + PackagedTestHelper.gApp.downloadError.name);
170 PackagedTestHelper.finish();
172 PackagedTestHelper.gApp.ondownloadsuccess =
173 checkLastAppState.bind(undefined, miniManifestURL, false, false,
174 2, PackagedTestHelper.next);
177 var request = navigator.mozApps.installPackage(miniManifestURL);
178 request.onerror = PackagedTestHelper.mozAppsError;
179 request.onsuccess = function() {
180 info(
"Application installed");
184 info(
"== TEST == Permissions installed correctly");
185 validatePermissions(initialPermissionState);
186 PackagedTestHelper.next();
189 info(
"== TEST == Check for Update and try to download it without update available");
192 is(PackagedTestHelper.gApp.downloadError.name,
"NO_DOWNLOAD_AVAILABLE",
"Download not available");
193 ok(!PackagedTestHelper.gApp.readyToApplyDownload,
"Not ready to apply download");
194 PackagedTestHelper.next();
196 function onsuccess() {
197 ok(false,
"ondownloadsuccess fired");
198 PackagedTestHelper.next();
201 checkForUpdate(false, onsuccess, null, onerror, true);
204 info(
"== TEST == Remember permissions");
205 var gApp = PackagedTestHelper.gApp;
206 var mozPermissions = window.navigator.mozPermissionSettings;
208 for (permission in permissionsToSet) {
210 window.navigator.mozPermissionSettings.set(permission,
211 permissionsToSet[permission],
216 "mozPermissionSettings.set failed for " + permission +
" - " + e);
219 PackagedTestHelper.next();
222 info(
"== TEST == Check that the permissions have been saved");
223 // Since the permission API isn't really synchronous, just log any errors
225 validatePermissions(permissionsToCheck, true /*dontFail*/);
226 PackagedTestHelper.next();
229 PackagedTestHelper.setAppVersion(
3, PackagedTestHelper.next);
232 info(
"== TEST == Update packaged app");
233 updateApp(true,
2,
3);
236 info(
"== TEST == Check that saved permissions were kept");
237 validatePermissions(permissionsToCheck);
238 PackagedTestHelper.next();
241 info(
"== TEST == Check for Update after getting a new package");
242 checkForUpdate(false);
245 PackagedTestHelper.setAppVersion(
4, PackagedTestHelper.next, true);
248 info(
"== TEST == Update packaged app - same package");
249 updateApp(false,
3,
3, true);
252 info(
"== TEST == Check for Update after getting the same package");
253 checkForUpdate(false);
257 PackagedTestHelper.setAppVersion(
5, PackagedTestHelper.next,
258 /* aDontUpdatePackage */ false,
259 /* aAllowCancel */ false,
260 /* aRole */ undefined,
261 /* aFailOnce */ true);
265 info(
"== TEST == Update packaged app - fail the update once");
267 var downloadSuccess = function() {
268 ok(false,
"We got an unexpected downloadsuccess or downloadapplied");
269 PackagedTestHelper.finish();
271 var downloadError = function() {
272 info(
"We got an expected download error: " +
273 PackagedTestHelper.gApp.downloadError.name);
274 PackagedTestHelper.next();
276 checkForUpdate(true, downloadSuccess, downloadSuccess, downloadError,
277 /* aLaunchDownload */ true);
280 info(
"=== TEST == Update packaged app - apply the update after failing");
281 var lApp = PackagedTestHelper.gApp;
282 lApp.ondownloadsuccess = function() {
283 info(
"Download succeeded, applying it");
284 navigator.mozApps.mgmt.applyDownload(lApp);
286 lApp.ondownloadapplied = function() {
287 info(
"Download successfuly applied");
288 PackagedTestHelper.next();
290 lApp.ondownloaderror = function() {
291 ok(false,
"Got an unexpected error: " + lApp.downloaderror.name);
292 PackagedTestHelper.finish();
297 info(
"== TEST == Check for Update applying a previously failed package");
298 checkForUpdate(false);
302 PackagedTestHelper.setAppVersion(
1, PackagedTestHelper.next);
305 info(
"== TEST == Update packaged app - Updating a pending app");
306 miniManifestURL = PackagedTestHelper.gSJS +
307 "?getManifest=true" +
308 "&appName=arandomname" +
310 PackagedTestHelper.checkAppDownloadError(miniManifestURL,
311 "MANIFEST_MISMATCH",
1, false, true,
314 checkForUpdate(false, null, null, null, false,
316 if (request.error.name ===
"PENDING_APP_NOT_UPDATABLE") {
317 info(
"Got expected PENDING_APP_NOT_UPDATEABLE");
319 ok(false,
"Got unexpected " + request.error.name);
321 PackagedTestHelper.next();
327 PackagedTestHelper.finish();
331 PackagedTestHelper.setSteps(steps);
332 // appToUpdate added to the URL so we get a unique URL for this app.
333 // Unique in this case meaning different from the ones used on the
335 miniManifestURL = PackagedTestHelper.gSJS +
"?getManifest=true&appToUpdate";
337 addLoadEvent(PackagedTestHelper.start);