michael@0: /* Any copyright is dedicated to the public domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Bug 793644, fire an event when attempting to reloads browser element after michael@0: // POST respest. michael@0: michael@0: "use strict"; michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: var iframe; michael@0: var gotConfirmRepost = false; michael@0: var doRepost = true; michael@0: var timer; michael@0: var isPostRequestSubmitted; michael@0: michael@0: function getExpectedStrings() { michael@0: let result = {}; michael@0: let bundleService = SpecialPowers.Cc['@mozilla.org/intl/stringbundle;1']. michael@0: getService(SpecialPowers.Ci.nsIStringBundleService); michael@0: let appBundle = bundleService.createBundle("chrome://global/locale/appstrings.properties"); michael@0: let brandBundle = bundleService.createBundle("chrome://branding/locale/brand.properties"); michael@0: try { michael@0: let brandName = brandBundle.GetStringFromName("brandShortName"); michael@0: result.message = appBundle.formatStringFromName("confirmRepostPrompt", michael@0: [brandName], 1); michael@0: } catch (e) { michael@0: // for the case that we don't have brandShortName michael@0: result.message = appBundle.GetStringFromName("confirmRepostPrompt"); michael@0: } michael@0: result.resend = appBundle.GetStringFromName("resendButton.label"); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: function failBecauseReloaded() { michael@0: window.clearTimeout(timer); michael@0: timer = null; michael@0: iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded); michael@0: ok(false, "We don't expect browser element to reload, but it did"); michael@0: SimpleTest.finish(); michael@0: }; michael@0: michael@0: function reloadDone() { michael@0: iframe.removeEventListener('mozbrowserloadend', reloadDone); michael@0: ok(gotConfirmRepost, "Didn't get confirmEx prompt before reload"); michael@0: michael@0: // Run again, with repost disallowed. michael@0: doRepost = false; michael@0: isPostRequestSubmitted = false; michael@0: iframe.src = 'file_post_request.html'; michael@0: iframe.addEventListener('mozbrowserloadend', pageLoadDone); michael@0: } michael@0: michael@0: function pageLoadDone() { michael@0: if (!isPostRequestSubmitted) { michael@0: // This loadend is done by setting url in address bar, so we don't need to michael@0: // do anything. The test page will submit a POST request. michael@0: isPostRequestSubmitted = true; michael@0: return; michael@0: } michael@0: michael@0: gotConfirmRepost = false; michael@0: iframe.removeEventListener('mozbrowserloadend', pageLoadDone); michael@0: if (doRepost) { michael@0: iframe.addEventListener('mozbrowserloadend', reloadDone); michael@0: } else { michael@0: // We don't expect browserelement to reload; use a timer to make sure michael@0: // it is not reloaded. michael@0: iframe.addEventListener('mozbrowserloadend', failBecauseReloaded); michael@0: } michael@0: iframe.reload(); michael@0: } michael@0: michael@0: function runTest() { michael@0: iframe = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe).mozbrowser = true; michael@0: michael@0: isPostRequestSubmitted = false; michael@0: iframe.src = 'file_post_request.html'; michael@0: document.body.appendChild(iframe); michael@0: michael@0: iframe.addEventListener('mozbrowserloadend', pageLoadDone); michael@0: michael@0: let expectedMessage = getExpectedStrings(); michael@0: iframe.addEventListener('mozbrowsershowmodalprompt', function(e) { michael@0: if (e.detail.promptType == 'custom-prompt') { michael@0: gotConfirmRepost = true; michael@0: e.preventDefault(); michael@0: e.detail.returnValue = { michael@0: selectedButton: doRepost ? 0 : 1, michael@0: }; michael@0: is(e.detail.returnValue.checked, undefined); michael@0: is(e.detail.buttons[0].messageType, 'custom'); michael@0: is(e.detail.buttons[0].message, expectedMessage.resend); michael@0: is(e.detail.buttons[1].messageType, 'builtin'); michael@0: is(e.detail.buttons[1].message, 'cancel'); michael@0: is(e.detail.message, expectedMessage.message); michael@0: is(e.detail.buttons.length, 2); michael@0: is(e.detail.showCheckbox, false); michael@0: is(e.detail.checkMessage, null); michael@0: e.detail.unblock(); michael@0: michael@0: if (!doRepost) { michael@0: // To make sure the page doesn't reload in 1 sec. michael@0: timer = window.setTimeout(function() { michael@0: iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded); michael@0: SimpleTest.finish(); michael@0: }, 1000); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);