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