<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1122463
https://bugzilla.mozilla.org/show_bug.cgi?id=820057
-->
<head>
  <title>Test focus when page unloads</title>
  <script type="application/javascript;version=1.7" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1122463">Mozilla Bug 1122463</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=820057">Mozilla Bug 820057</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

inputmethod_setup(function() {
  runTest();
});

let appFrameScript = function appFrameScript() {
  let form1 = content.document.body.firstElementChild;
  let input1 = form1.firstElementChild;
  let submit1 = form1.lastElementChild;
  let input2;

  let cancelSubmit = function(evt) {
    evt.preventDefault();
  };

  // Content of the second page.
  form1.action = 'file_test_unload_action.html';

  let i = 1;

  input1.focus();

  addMessageListener('test:next', function() {
    i++;
    switch (i) {
      case 2:
        // Click the submit button, trigger the submit event and make our
        // installed event listener preventing the submission.
        form1.addEventListener('submit', cancelSubmit);
        submit1.click();

        sendAsyncMessage('test:step');

        break;

      case 3:
        // Actually submit the form.
        form1.removeEventListener('submit', cancelSubmit);
        submit1.click();

        break;

      case 4:
        if (!content.document.body) {
          content.onload = function() {
            content.onload = null;

            let input2 = content.document.body.firstElementChild;
            input2.focus();
          };

          return;
        }

        input2 = content.document.body.firstElementChild;
        input2.focus();

        break;

      case 5:
        content.location.href = 'data:text/html,Hello!';

        break;
    }
  });
};

function runTest() {
  let im = navigator.mozInputMethod;

  let i = 0;
  function nextStep() {
    let inputcontext = navigator.mozInputMethod.inputcontext;

    i++;
    switch (i) {
      // focus on the first input receives the first input context.
      case 1:
        ok(!!inputcontext, '1) Receving the first input context');
        is(inputcontext.textAfterCursor, 'First');

        mm.sendAsyncMessage('test:next');
        break;

      // Cancelled submission should not cause us lost focus.
      case 2:
        ok(!!inputcontext, '2) Receving the first input context');
        is(inputcontext.textAfterCursor, 'First');

        mm.sendAsyncMessage('test:next');
        break;

      // Real submit and page transition should cause us lost focus.
      // XXX: Unless we could delay the page transition, we does not know if
      // the inputcontext is lost because of the submit or the pagehide/beforeload
      // event.
      case 3:
        is(inputcontext, null, '3) Receving null inputcontext');

        mm.sendAsyncMessage('test:next');

        break;

      // Regaining focus of input in the second page.
      case 4:
        ok(!!inputcontext, '4) Receving the second input context');
        is(inputcontext.textAfterCursor, 'Second');

        mm.sendAsyncMessage('test:next');

        break;

      // Page transition should cause us lost focus
      case 5:
        is(inputcontext, null, '5) Receving null inputcontext');

        inputmethod_cleanup();

        break;
    }
  }

  // Set current page as an input method.
  SpecialPowers.wrap(im).setActive(true);

  let iframe = document.createElement('iframe');
  iframe.src = 'file_test_unload.html';
  iframe.setAttribute('mozbrowser', true);
  document.body.appendChild(iframe);

  let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
  im.oninputcontextchange = nextStep;

  let frameScriptLoaded = false;
  iframe.addEventListener('mozbrowserloadend', function() {
    if (frameScriptLoaded)
      return;

    frameScriptLoaded = true;
    mm.addMessageListener('test:step', nextStep);
    mm.loadFrameScript('data:,(' + encodeURIComponent(appFrameScript.toString()) + ')();', false);
  });
}

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