Bug 561636 (3/4) - Tests for browser invalid form submission (and popup handling). r=dolske a2.0=test-only
authorMounir Lamouri <mounir.lamouri@gmail.com>
Sat, 11 Sep 2010 06:09:38 +0200
changeset 52446 8e70f3fc0d491b228b39092196fab274acb6d8f6
parent 52445 c8685734392d330014eab550db9af25bc8d93cc6
child 52447 e3f194205a3bd556d471bb69c2621a9457945c76
push id15645
push usermlamouri@mozilla.com
push dateSat, 11 Sep 2010 04:22:15 +0000
treeherdermozilla-central@2779c55431a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske
bugs561636
milestone2.0b6pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 561636 (3/4) - Tests for browser invalid form submission (and popup handling). r=dolske a2.0=test-only
browser/base/content/test/Makefile.in
browser/base/content/test/browser_bug561636.js
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -136,16 +136,17 @@ endif
                  browser_bug537474.js \
                  browser_bug550565.js \
                  browser_bug553455.js \
                  browser_bug555224.js \
                  browser_bug555767.js \
                  browser_bug556061.js \
                  browser_bug559991.js \
                  browser_bug561623.js \
+                 browser_bug561636.js \
                  browser_bug562649.js \
                  browser_bug563588.js \
                  browser_bug577121.js \
                  browser_bug579872.js \
                  browser_bug580956.js \
                  browser_bug581242.js \
                  browser_bug581947.js \
                  browser_bug585830.js \
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_bug561636.js
@@ -0,0 +1,374 @@
+var gInvalidFormPopup = document.getElementById('invalid-form-popup');
+ok(gInvalidFormPopup,
+   "The browser should have a popup to show when a form is invalid");
+
+function checkPopupShow()
+{
+  ok(gInvalidFormPopup.state == 'showing' || gInvalidFormPopup.state == 'open',
+     "The invalid form popup should be shown");
+}
+
+function checkPopupHide()
+{
+  ok(gInvalidFormPopup.state != 'showing' && gInvalidFormPopup.state != 'open',
+     "The invalid form popup should not be shown");
+}
+
+function checkPopupMessage(doc)
+{
+  is(gInvalidFormPopup.firstChild.nodeValue,
+     doc.getElementById('i').validationMessage.substring(0,256),
+     "The panel should show the 256 first characters of the validationMessage");
+}
+
+let gObserver = {
+  QueryInterface : XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]),
+
+  notifyInvalidSubmit : function (aFormElement, aInvalidElements)
+  {
+  }
+};
+
+function test()
+{
+  waitForExplicitFinish();
+
+  test1();
+}
+
+/**
+ * In this test, we check that no popup appears if the form is valid.
+ */
+function test1() {
+  let uri = "data:text/html,<html><body><iframe name='t'></iframe><form target='t' action='data:text/html,'><input><input id='s' type='submit'></form></body></html>";
+  let tab = gBrowser.addTab();
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+    let doc = gBrowser.contentDocument;
+
+    doc.getElementById('s').click();
+
+    executeSoon(function() {
+      checkPopupHide();
+
+      // Clean-up
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+
+      // Next test
+      executeSoon(test2);
+    });
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that, when an invalid form is submitted,
+ * the invalid element is focused and a popup appears.
+ */
+function test2()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input required id='i'><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    // Clean-up and next test.
+    gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+    executeSoon(test3);
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that, when an invalid form is submitted,
+ * the first invalid element is focused and a popup appears.
+ */
+function test3()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input><input id='i' required><input required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    // Clean-up and next test.
+    gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+    executeSoon(test4);
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that the validation message is correctly cut.
+ */
+function test4()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i'><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    // Clean-up and next test.
+    gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+    executeSoon(test5);
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    let msg = "";
+    for (let i=0; i<50; ++i) {
+      msg += "abcde ";
+    }
+    // msg has 300 characters
+    gBrowser.contentDocument.getElementById('i').setCustomValidity(msg);
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that, we can hide the popup by interacting with the
+ * invalid element.
+ */
+function test5()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    EventUtils.synthesizeKey("a", {});
+
+    executeSoon(function () {
+      checkPopupHide();
+
+      // Clean-up and next test.
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+      executeSoon(test6);
+    });
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that we can hide the popup by blurring the invalid
+ * element.
+ */
+function test6()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    doc.getElementById('i').blur();
+
+    executeSoon(function () {
+      checkPopupHide();
+
+      // Clean-up and next test.
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+      executeSoon(test7);
+    });
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that we can hide the popup by pressing TAB.
+ */
+function test7()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    EventUtils.synthesizeKey("VK_TAB", {});
+
+    executeSoon(function () {
+      checkPopupHide();
+
+      // Clean-up and next test.
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+      executeSoon(test8);
+    });
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that the popup will hide if we move to another tab.
+ */
+function test8()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gInvalidFormPopup.addEventListener("popupshown", function() {
+    gInvalidFormPopup.removeEventListener("popupshown", arguments.callee, false);
+
+    let doc = gBrowser.contentDocument;
+    is(doc.activeElement, doc.getElementById('i'),
+       "First invalid element should be focused");
+
+    checkPopupShow();
+    checkPopupMessage(doc);
+
+    // Create a new tab and move to it.
+    gBrowser.selectedTab  = gBrowser.addTab("about:blank", {skipAnimation: true});
+
+    executeSoon(function() {
+      checkPopupHide();
+
+      // Clean-up and next test.
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+      gBrowser.removeTab(gBrowser.selectedTab, {animate: false});
+      executeSoon(test9);
+    });
+  }, false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    gBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  gBrowser.selectedTab = tab;
+  gBrowser.selectedTab.linkedBrowser.loadURI(uri);
+}
+
+/**
+ * In this test, we check that nothing happen (no focus nor popup) if the
+ * invalid form is submitted in another tab than the current focused one
+ * (submitted in background).
+ */
+function test9()
+{
+  let uri = "data:text/html,<iframe name='t'></iframe><form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>";
+  let tab = gBrowser.addTab();
+
+  gObserver.notifyInvalidSubmit = function() {
+    executeSoon(function() {
+      let doc = tab.linkedBrowser.contentDocument;
+      isnot(doc.activeElement, doc.getElementById('i'),
+            "We should not focus the invalid element when the form is submitted in background");
+
+      checkPopupHide();
+
+      // Clean-up
+      Services.obs.removeObserver(gObserver, "invalidformsubmit");
+      gObserver.notifyInvalidSubmit = function () {};
+      gBrowser.removeTab(tab, {animate: false});
+
+      // Next test
+      executeSoon(finish);
+    });
+  };
+
+  Services.obs.addObserver(gObserver, "invalidformsubmit", false);
+
+  tab.linkedBrowser.addEventListener("load", function(aEvent) {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+
+    isnot(gBrowser.selectedTab, tab,
+          "This tab should have been loaded in background");
+
+    tab.linkedBrowser.contentDocument.getElementById('s').click();
+  }, true);
+
+  tab.linkedBrowser.loadURI(uri);
+}