<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=589696
-->
<head>
  <title>Test for Bug 589696</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=589696">Mozilla Bug 589696</a>
<p id="display"></p>
<iframe style='width:50px; height: 50px;' name='t'></iframe>
<div id="content">
  <!-- Next forms should not submit because formnovalidate isn't set on the
       element used for the submission. -->
  <form target='t' action='data:text/html,'>
    <input id='av' required>
    <input type='submit' formnovalidate>
    <input id='a' type='submit'>
  </form>
  <form target='t' action='data:text/html,'>
    <input id='bv' type='checkbox' required>
    <button type='submit' formnovalidate></button>
    <button id='b' type='submit'></button>
  </form>
  <!-- Next form should not submit because formnovalidate only applies for
       submit controls. -->
  <form target='t' action='data:text/html,'>
    <input id='c' required formnovalidate>
  </form>
  <!--- Next forms should submit without any validation check. -->
  <form target='t' action='data:text/html,'>
    <input id='dv' required>
    <input id='d' type='submit' formnovalidate>
  </form>
  <form target='t' action='data:text/html,'>
    <input id='ev' type='checkbox' required>
    <button id='e' type='submit' formnovalidate></button>
  </form>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 589696 **/

document.getElementById('av').addEventListener("invalid", function(aEvent) {
  aEvent.target.removeAttribute("invalid", arguments.callee, false);
  ok(true, "formnovalidate should not apply on if not set on the submit " +
           "control used for the submission");
  document.getElementById('b').click();
});

document.getElementById('bv').addEventListener("invalid", function(aEvent) {
  aEvent.target.removeAttribute("invalid", arguments.callee, false);
  ok(true, "formnovalidate should not apply on if not set on the submit " +
           "control used for the submission");
  var c = document.getElementById('c');
  c.focus();
  synthesizeKey("KEY_Enter");
});

document.getElementById('c').addEventListener("invalid", function(aEvent) {
  aEvent.target.removeAttribute("invalid", arguments.callee, false);
  ok(true, "formnovalidate should only apply on submit controls");
  document.getElementById('d').click();
});

document.forms[3].addEventListener("submit", function(aEvent) {
  aEvent.target.removeAttribute("submit", arguments.callee, false);
  ok(true, "formnovalidate applies if set on the submit control used for the submission");
  document.getElementById('e').click();
});

document.forms[4].addEventListener("submit", function(aEvent) {
  aEvent.target.removeAttribute("submit", arguments.callee, false);
  ok(true, "formnovalidate applies if set on the submit control used for the submission");
  SimpleTest.executeSoon(SimpleTest.finish);
});

/**
 * We have to be sure invalid events behave as expected.
 * They should be sent before the submit event so we can just create a test
 * failure if we got one when unexpected. All of them should be caught if
 * sent.
 * At worst, we got random green which isn't harmful.
 * If expected, they will be part of the chain reaction.
 */
function unexpectedInvalid(aEvent)
{
  aEvent.target.removeAttribute("invalid", unexpectedInvalid, false);
  ok(false, "invalid event should not be sent");
}

document.getElementById('dv').addEventListener("invalid", unexpectedInvalid);
document.getElementById('ev').addEventListener("invalid", unexpectedInvalid);

/**
 * Some submission have to be canceled. In that case, the submit events should
 * not be sent.
 * Same behavior as unexpected invalid events.
 */
function unexpectedSubmit(aEvent)
{
  aEvent.target.removeAttribute("submit", unexpectedSubmit, false);
  ok(false, "submit event should not be sent");
}

document.forms[0].addEventListener("submit", unexpectedSubmit);
document.forms[1].addEventListener("submit", unexpectedSubmit);
document.forms[2].addEventListener("submit", unexpectedSubmit);

SimpleTest.waitForExplicitFinish();

// This is going to call all the tests (with a chain reaction).
SimpleTest.waitForFocus(function() {
  document.getElementById('a').click();
});

</script>
</pre>
</body>
</html>
