Bug 1132349 - Test for focus removal after pagehide/submit/beforeload. r=janjongboom
authorTim Chien <timdream@gmail.com>
Mon, 15 Jun 2015 03:13:00 -0400
changeset 279893 cf4421a96636450d4fcd8a83be9e779edc47e88e
parent 279892 60b6d9795c7a7c3d3e50c10790929d4479adc1ae
child 279894 e44f689c37565d889ebf96e70cfd891d9c992821
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjanjongboom
bugs1132349
milestone41.0a1
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 1132349 - Test for focus removal after pagehide/submit/beforeload. r=janjongboom
dom/inputmethod/mochitest/mochitest.ini
dom/inputmethod/mochitest/test_unload.html
--- a/dom/inputmethod/mochitest/mochitest.ini
+++ b/dom/inputmethod/mochitest/mochitest.ini
@@ -19,8 +19,9 @@ support-files =
 [test_bug1026997.html]
 [test_bug1043828.html]
 [test_bug1059163.html]
 [test_bug1066515.html]
 [test_sendkey_cancel.html]
 [test_sync_edit.html]
 [test_two_inputs.html]
 [test_two_selects.html]
+[test_unload.html]
new file mode 100644
--- /dev/null
+++ b/dom/inputmethod/mochitest/test_unload.html
@@ -0,0 +1,167 @@
+<!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="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript;version=1.7" src="inputmethod_common.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=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 = 'data:text/html,<html><body><input value="Second"></body></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 = 'data:text/html,<html><body><form id="form"><input value="First"><input type="submit"></form></body></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>
+