summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_ReloadPostRequest.js
blob: 614ed7c4975307e42bdc20a525ba86b5e6c9f697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Bug 793644, fire an event when attempting to reloads browser element after
// POST respest.

"use strict";
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");
browserElementTestHelpers.setEnabledPref(true);

var iframe;
var gotConfirmRepost = false;
var doRepost = true;
var timer;
var isPostRequestSubmitted;

function getExpectedStrings() {
  let result = {};
  let bundleService = SpecialPowers.Cc['@mozilla.org/intl/stringbundle;1'].
    getService(SpecialPowers.Ci.nsIStringBundleService);
  let appBundle = bundleService.createBundle("chrome://global/locale/appstrings.properties");
  let brandBundle = bundleService.createBundle("chrome://branding/locale/brand.properties");
  try {
    let brandName = brandBundle.GetStringFromName("brandShortName");
    result.message = appBundle.formatStringFromName("confirmRepostPrompt",
                                                    [brandName], 1);
  } catch (e) {
    // for the case that we don't have brandShortName
    result.message = appBundle.GetStringFromName("confirmRepostPrompt");
  }
  result.resend = appBundle.GetStringFromName("resendButton.label");

  return result;
}

function failBecauseReloaded() {
  window.clearTimeout(timer);
  timer = null;
  iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded);
  ok(false, "We don't expect browser element to reload, but it did");
  SimpleTest.finish();
};

function reloadDone() {
  iframe.removeEventListener('mozbrowserloadend', reloadDone);
  ok(gotConfirmRepost, "Didn't get confirmEx prompt before reload");

  // Run again, with repost disallowed.
  doRepost = false;
  isPostRequestSubmitted = false;
  iframe.src = 'file_post_request.html';
  iframe.addEventListener('mozbrowserloadend', pageLoadDone);
}

function pageLoadDone() {
  if (!isPostRequestSubmitted) {
    // This loadend is done by setting url in address bar, so we don't need to
    // do anything. The test page will submit a POST request.
    isPostRequestSubmitted = true;
    return;
  }

  gotConfirmRepost = false;
  iframe.removeEventListener('mozbrowserloadend', pageLoadDone);
  if (doRepost) {
    iframe.addEventListener('mozbrowserloadend', reloadDone);
  } else {
    // We don't expect browserelement to reload; use a timer to make sure
    // it is not reloaded.
    iframe.addEventListener('mozbrowserloadend', failBecauseReloaded);
  }
  iframe.reload();
}

function runTest() {
  iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');

  isPostRequestSubmitted = false;
  iframe.src = 'file_post_request.html';
  document.body.appendChild(iframe);

  iframe.addEventListener('mozbrowserloadend', pageLoadDone);

  let expectedMessage = getExpectedStrings();
  iframe.addEventListener('mozbrowsershowmodalprompt', function(e) {
    if (e.detail.promptType == 'custom-prompt') {
      gotConfirmRepost = true;
      e.preventDefault();
      e.detail.returnValue = {
        selectedButton: doRepost ? 0 : 1,
      };
      is(e.detail.returnValue.checked, undefined);
      is(e.detail.buttons[0].messageType, 'custom');
      is(e.detail.buttons[0].message, expectedMessage.resend);
      is(e.detail.buttons[1].messageType, 'builtin');
      is(e.detail.buttons[1].message, 'cancel');
      is(e.detail.message, expectedMessage.message);
      is(e.detail.buttons.length, 2);
      is(e.detail.showCheckbox, false);
      is(e.detail.checkboxMessage, null);
      e.detail.unblock();

      if (!doRepost) {
        // To make sure the page doesn't reload in 1 sec.
        timer = window.setTimeout(function() {
          iframe.removeEventListener('mozbrowserloadend', failBecauseReloaded);
          SimpleTest.finish();
        }, 1000);
      }
    }
  });
}

addEventListener('testready', runTest);