Bug 793644 - Test case r=jlebar
authorPatrick Wang <kk1fff@patrickz.net>
Fri, 19 Oct 2012 18:39:22 +0800
changeset 111782 085868531b69bddf5cd9f5c246c72242a60a2b31
parent 111781 698b72833c2b19972a73b1edc09f22511095c02a
child 111783 26d9f5a3c57e2d7260b6eaad04c4715a995a7697
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersjlebar
bugs793644
milestone19.0a1
Bug 793644 - Test case r=jlebar
dom/browser-element/mochitest/Makefile.in
dom/browser-element/mochitest/browserElement_ReloadPostRequest.js
dom/browser-element/mochitest/file_post_request.html
dom/browser-element/mochitest/test_browserElement_inproc_ReloadPostRequest.html
dom/browser-element/mochitest/test_browserElement_oop_ReloadPostRequest.html
--- a/dom/browser-element/mochitest/Makefile.in
+++ b/dom/browser-element/mochitest/Makefile.in
@@ -142,16 +142,19 @@ MOCHITEST_FILES = \
 		browserElement_AppFramePermission.js \
 		test_browserElement_inproc_AppFramePermission.html \
 		file_wyciwyg.html \
 		browserElement_ExposableURI.js \
 		test_browserElement_inproc_ExposableURI.html \
 		browserElement_FrameWrongURI.js \
 		test_browserElement_inproc_FrameWrongURI.html \
 		file_browserElement_FrameWrongURI.html \
+		file_post_request.html \
+		test_browserElement_inproc_ReloadPostRequest.html \
+		browserElement_ReloadPostRequest.js \
 		$(NULL)
 
 # Disabled due to https://bugzilla.mozilla.org/show_bug.cgi?id=774100
 #		test_browserElement_inproc_Reload.html \
 
 # OOP tests don't work on Windows (bug 763081) or native-fennec (bug
 # 774939).
 #
@@ -207,14 +210,15 @@ MOCHITEST_FILES += \
 		test_browserElement_oop_SendEvent.html \
 		test_browserElement_oop_ScrollEvent.html \
 		test_browserElement_oop_Auth.html \
 		test_browserElement_oop_RemoveBrowserElement.html \
 		test_browserElement_oop_DOMRequestError.html \
 		test_browserElement_oop_AppFramePermission.html \
 		test_browserElement_oop_ExposableURI.html \
 		test_browserElement_oop_FrameWrongURI.html \
-		$(NULL)
+		test_browserElement_oop_ReloadPostRequest.html \
+	$(NULL)
 endif #}
 endif #}
 
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/browserElement_ReloadPostRequest.js
@@ -0,0 +1,117 @@
+/* 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();
+
+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() {
+  browserElementTestHelpers.setEnabledPref(true);
+  browserElementTestHelpers.addPermission();
+
+  iframe = document.createElement('iframe');
+  iframe.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.checkMessage, 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);
+      }
+    }
+  });
+}
+
+runTest();
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/file_post_request.html
@@ -0,0 +1,15 @@
+<html>
+<head>
+  <script>
+  addEventListener('load', function() {
+    document.getElementsByTagName('form')[0].submit();
+  });
+  </script>
+</head>
+<body>
+  <form action="file_empty.html" method="POST">
+    <input type="text" name="postvalue" value="I am a test string!!" />
+    <input type="submit" />
+  </form>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/test_browserElement_inproc_ReloadPostRequest.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test of browser element.</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="browserElementTestHelpers.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script type="application/javascript;version=1.7" src="browserElement_ReloadPostRequest.js">
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/test_browserElement_oop_ReloadPostRequest.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test of browser element.</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="browserElementTestHelpers.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script type="application/javascript;version=1.7" src="browserElement_ReloadPostRequest.js">
+</script>
+</body>
+</html>