1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_ReloadPostRequest.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +/* Any copyright is dedicated to the public domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Bug 793644, fire an event when attempting to reloads browser element after 1.8 +// POST respest. 1.9 + 1.10 +"use strict"; 1.11 +SimpleTest.waitForExplicitFinish(); 1.12 +browserElementTestHelpers.setEnabledPref(true); 1.13 +browserElementTestHelpers.addPermission(); 1.14 + 1.15 +var iframe; 1.16 +var gotConfirmRepost = false; 1.17 +var doRepost = true; 1.18 +var timer; 1.19 +var isPostRequestSubmitted; 1.20 + 1.21 +function getExpectedStrings() { 1.22 + let result = {}; 1.23 + let bundleService = SpecialPowers.Cc['@mozilla.org/intl/stringbundle;1']. 1.24 + getService(SpecialPowers.Ci.nsIStringBundleService); 1.25 + let appBundle = bundleService.createBundle("chrome://global/locale/appstrings.properties"); 1.26 + let brandBundle = bundleService.createBundle("chrome://branding/locale/brand.properties"); 1.27 + try { 1.28 + let brandName = brandBundle.GetStringFromName("brandShortName"); 1.29 + result.message = appBundle.formatStringFromName("confirmRepostPrompt", 1.30 + [brandName], 1); 1.31 + } catch (e) { 1.32 + // for the case that we don't have brandShortName 1.33 + result.message = appBundle.GetStringFromName("confirmRepostPrompt"); 1.34 + } 1.35 + result.resend = appBundle.GetStringFromName("resendButton.label"); 1.36 + 1.37 + return result; 1.38 +} 1.39 + 1.40 +function failBecauseReloaded() { 1.41 + window.clearTimeout(timer); 1.42 + timer = null; 1.43 + iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded); 1.44 + ok(false, "We don't expect browser element to reload, but it did"); 1.45 + SimpleTest.finish(); 1.46 +}; 1.47 + 1.48 +function reloadDone() { 1.49 + iframe.removeEventListener('mozbrowserloadend', reloadDone); 1.50 + ok(gotConfirmRepost, "Didn't get confirmEx prompt before reload"); 1.51 + 1.52 + // Run again, with repost disallowed. 1.53 + doRepost = false; 1.54 + isPostRequestSubmitted = false; 1.55 + iframe.src = 'file_post_request.html'; 1.56 + iframe.addEventListener('mozbrowserloadend', pageLoadDone); 1.57 +} 1.58 + 1.59 +function pageLoadDone() { 1.60 + if (!isPostRequestSubmitted) { 1.61 + // This loadend is done by setting url in address bar, so we don't need to 1.62 + // do anything. The test page will submit a POST request. 1.63 + isPostRequestSubmitted = true; 1.64 + return; 1.65 + } 1.66 + 1.67 + gotConfirmRepost = false; 1.68 + iframe.removeEventListener('mozbrowserloadend', pageLoadDone); 1.69 + if (doRepost) { 1.70 + iframe.addEventListener('mozbrowserloadend', reloadDone); 1.71 + } else { 1.72 + // We don't expect browserelement to reload; use a timer to make sure 1.73 + // it is not reloaded. 1.74 + iframe.addEventListener('mozbrowserloadend', failBecauseReloaded); 1.75 + } 1.76 + iframe.reload(); 1.77 +} 1.78 + 1.79 +function runTest() { 1.80 + iframe = document.createElement('iframe'); 1.81 + SpecialPowers.wrap(iframe).mozbrowser = true; 1.82 + 1.83 + isPostRequestSubmitted = false; 1.84 + iframe.src = 'file_post_request.html'; 1.85 + document.body.appendChild(iframe); 1.86 + 1.87 + iframe.addEventListener('mozbrowserloadend', pageLoadDone); 1.88 + 1.89 + let expectedMessage = getExpectedStrings(); 1.90 + iframe.addEventListener('mozbrowsershowmodalprompt', function(e) { 1.91 + if (e.detail.promptType == 'custom-prompt') { 1.92 + gotConfirmRepost = true; 1.93 + e.preventDefault(); 1.94 + e.detail.returnValue = { 1.95 + selectedButton: doRepost ? 0 : 1, 1.96 + }; 1.97 + is(e.detail.returnValue.checked, undefined); 1.98 + is(e.detail.buttons[0].messageType, 'custom'); 1.99 + is(e.detail.buttons[0].message, expectedMessage.resend); 1.100 + is(e.detail.buttons[1].messageType, 'builtin'); 1.101 + is(e.detail.buttons[1].message, 'cancel'); 1.102 + is(e.detail.message, expectedMessage.message); 1.103 + is(e.detail.buttons.length, 2); 1.104 + is(e.detail.showCheckbox, false); 1.105 + is(e.detail.checkMessage, null); 1.106 + e.detail.unblock(); 1.107 + 1.108 + if (!doRepost) { 1.109 + // To make sure the page doesn't reload in 1 sec. 1.110 + timer = window.setTimeout(function() { 1.111 + iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded); 1.112 + SimpleTest.finish(); 1.113 + }, 1000); 1.114 + } 1.115 + } 1.116 + }); 1.117 +} 1.118 + 1.119 +addEventListener('testready', runTest);