dom/browser-element/mochitest/browserElement_ReloadPostRequest.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Bug 793644, fire an event when attempting to reloads browser element after
michael@0 5 // POST respest.
michael@0 6
michael@0 7 "use strict";
michael@0 8 SimpleTest.waitForExplicitFinish();
michael@0 9 browserElementTestHelpers.setEnabledPref(true);
michael@0 10 browserElementTestHelpers.addPermission();
michael@0 11
michael@0 12 var iframe;
michael@0 13 var gotConfirmRepost = false;
michael@0 14 var doRepost = true;
michael@0 15 var timer;
michael@0 16 var isPostRequestSubmitted;
michael@0 17
michael@0 18 function getExpectedStrings() {
michael@0 19 let result = {};
michael@0 20 let bundleService = SpecialPowers.Cc['@mozilla.org/intl/stringbundle;1'].
michael@0 21 getService(SpecialPowers.Ci.nsIStringBundleService);
michael@0 22 let appBundle = bundleService.createBundle("chrome://global/locale/appstrings.properties");
michael@0 23 let brandBundle = bundleService.createBundle("chrome://branding/locale/brand.properties");
michael@0 24 try {
michael@0 25 let brandName = brandBundle.GetStringFromName("brandShortName");
michael@0 26 result.message = appBundle.formatStringFromName("confirmRepostPrompt",
michael@0 27 [brandName], 1);
michael@0 28 } catch (e) {
michael@0 29 // for the case that we don't have brandShortName
michael@0 30 result.message = appBundle.GetStringFromName("confirmRepostPrompt");
michael@0 31 }
michael@0 32 result.resend = appBundle.GetStringFromName("resendButton.label");
michael@0 33
michael@0 34 return result;
michael@0 35 }
michael@0 36
michael@0 37 function failBecauseReloaded() {
michael@0 38 window.clearTimeout(timer);
michael@0 39 timer = null;
michael@0 40 iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded);
michael@0 41 ok(false, "We don't expect browser element to reload, but it did");
michael@0 42 SimpleTest.finish();
michael@0 43 };
michael@0 44
michael@0 45 function reloadDone() {
michael@0 46 iframe.removeEventListener('mozbrowserloadend', reloadDone);
michael@0 47 ok(gotConfirmRepost, "Didn't get confirmEx prompt before reload");
michael@0 48
michael@0 49 // Run again, with repost disallowed.
michael@0 50 doRepost = false;
michael@0 51 isPostRequestSubmitted = false;
michael@0 52 iframe.src = 'file_post_request.html';
michael@0 53 iframe.addEventListener('mozbrowserloadend', pageLoadDone);
michael@0 54 }
michael@0 55
michael@0 56 function pageLoadDone() {
michael@0 57 if (!isPostRequestSubmitted) {
michael@0 58 // This loadend is done by setting url in address bar, so we don't need to
michael@0 59 // do anything. The test page will submit a POST request.
michael@0 60 isPostRequestSubmitted = true;
michael@0 61 return;
michael@0 62 }
michael@0 63
michael@0 64 gotConfirmRepost = false;
michael@0 65 iframe.removeEventListener('mozbrowserloadend', pageLoadDone);
michael@0 66 if (doRepost) {
michael@0 67 iframe.addEventListener('mozbrowserloadend', reloadDone);
michael@0 68 } else {
michael@0 69 // We don't expect browserelement to reload; use a timer to make sure
michael@0 70 // it is not reloaded.
michael@0 71 iframe.addEventListener('mozbrowserloadend', failBecauseReloaded);
michael@0 72 }
michael@0 73 iframe.reload();
michael@0 74 }
michael@0 75
michael@0 76 function runTest() {
michael@0 77 iframe = document.createElement('iframe');
michael@0 78 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 79
michael@0 80 isPostRequestSubmitted = false;
michael@0 81 iframe.src = 'file_post_request.html';
michael@0 82 document.body.appendChild(iframe);
michael@0 83
michael@0 84 iframe.addEventListener('mozbrowserloadend', pageLoadDone);
michael@0 85
michael@0 86 let expectedMessage = getExpectedStrings();
michael@0 87 iframe.addEventListener('mozbrowsershowmodalprompt', function(e) {
michael@0 88 if (e.detail.promptType == 'custom-prompt') {
michael@0 89 gotConfirmRepost = true;
michael@0 90 e.preventDefault();
michael@0 91 e.detail.returnValue = {
michael@0 92 selectedButton: doRepost ? 0 : 1,
michael@0 93 };
michael@0 94 is(e.detail.returnValue.checked, undefined);
michael@0 95 is(e.detail.buttons[0].messageType, 'custom');
michael@0 96 is(e.detail.buttons[0].message, expectedMessage.resend);
michael@0 97 is(e.detail.buttons[1].messageType, 'builtin');
michael@0 98 is(e.detail.buttons[1].message, 'cancel');
michael@0 99 is(e.detail.message, expectedMessage.message);
michael@0 100 is(e.detail.buttons.length, 2);
michael@0 101 is(e.detail.showCheckbox, false);
michael@0 102 is(e.detail.checkMessage, null);
michael@0 103 e.detail.unblock();
michael@0 104
michael@0 105 if (!doRepost) {
michael@0 106 // To make sure the page doesn't reload in 1 sec.
michael@0 107 timer = window.setTimeout(function() {
michael@0 108 iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded);
michael@0 109 SimpleTest.finish();
michael@0 110 }, 1000);
michael@0 111 }
michael@0 112 }
michael@0 113 });
michael@0 114 }
michael@0 115
michael@0 116 addEventListener('testready', runTest);

mercurial