Bug 1051556 - Convert testInputConnection to a JS test; r=esawin
☠☠ backed out by 8d22eea111d9 ☠ ☠
authorJim Chen <nchen@mozilla.com>
Wed, 09 Dec 2015 17:46:44 -0500
changeset 310167 fe3d880efce840e6aed79ac66e83c8de4cd3daa6
parent 310166 3b487da5a6d4f52c0a3e020b134cf8b3703572e1
child 310168 4a83a8594ddc07b26e2afdadf90d3da07cb82c76
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1051556
milestone45.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 1051556 - Convert testInputConnection to a JS test; r=esawin A test for this bug requires the Robocop test call some JS functions. This patch gives testInputConnection the ability to make calls to content JS. It also converts the existing tests to use JS instead of using previous workarounds (e.g. using URL hash to set the initial text, using tab key to change input focus).
mobile/android/tests/browser/robocop/robocop_head.js
mobile/android/tests/browser/robocop/robocop_input.html
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testInputConnection.java
--- a/mobile/android/tests/browser/robocop/robocop_head.js
+++ b/mobile/android/tests/browser/robocop/robocop_head.js
@@ -1,14 +1,28 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+// The test js is shared between sandboxed (which has no SpecialPowers object)
+// and content mochitests (where the |Components| object is accessible only as
+// SpecialPowers.Components). Expose Components if necessary here to make things
+// work everywhere.
+//
+// Even if the real |Components| doesn't exist, we might shim in a simple JS
+// placebo for compat. An easy way to differentiate this from the real thing
+// is whether the property is read-only or not.
+{
+  let c = Object.getOwnPropertyDescriptor(this, 'Components');
+  if ((!c.value || c.writable) && typeof SpecialPowers === 'object')
+    Components = SpecialPowers.wrap(SpecialPowers.Components);
+}
+
 /*
  * This file contains common code that is loaded before each test file(s).
  * See http://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests
  * for more information.
  */
 
 var _quit = false;
 var _tests_pending = 0;
--- a/mobile/android/tests/browser/robocop/robocop_input.html
+++ b/mobile/android/tests/browser/robocop/robocop_input.html
@@ -1,25 +1,39 @@
 <!DOCTYPE html>
 <html>
   <head>
     <title>Robocop Input</title>
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
-  <body onload="fillInput()">
+  <body>
     <p><input id="input" type="text"></p>
     <p><input id="resetting-input" type="text"></p>
-    <script>
-      function fillInput() {
-        // fill the input with the URL hash if provided
-        var input = document.getElementById("input");
-        input.value = window.location.hash.slice(1); // remove leading #
-        input.focus();
+    <script type="application/javascript;version=1.8" src="robocop_head.js"></script>
+    <script type="application/javascript;version=1.8">
+      let input = document.getElementById("input");
+
+      // An input that resets the editor on every input by resetting the value property.
+      let resetting_input = document.getElementById("resetting-input");
+      resetting_input.addEventListener('input', function () {
+          this.value = this.value;
+      });
 
-        // An input that resets the editor on every input by resetting the value property.
-        var resetting_input = document.getElementById("resetting-input");
-        resetting_input.addEventListener('input', function () {
-            this.value = this.value;
-        });
-      }
+      let test = {
+        focus_input: function(val) {
+          input.value = val;
+          input.focus();
+        },
+
+        focus_resetting_input: function(val) {
+          resetting_input.value = val;
+          resetting_input.focus();
+        },
+
+        finish_test: function() {
+          java.disconnect();
+        },
+      };
+
+      var java = new JavaBridge(test);
     </script>
   </body>
 </html>
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testInputConnection.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testInputConnection.java
@@ -10,45 +10,66 @@ import static org.mozilla.gecko.tests.he
 import static org.mozilla.gecko.tests.helpers.TextInputHelper.assertText;
 import static org.mozilla.gecko.tests.helpers.TextInputHelper.assertTextAndSelection;
 import static org.mozilla.gecko.tests.helpers.TextInputHelper.assertTextAndSelectionAt;
 import static org.mozilla.gecko.tests.helpers.TextInputHelper.getText;
 import static org.mozilla.gecko.tests.helpers.WaitHelper.waitFor;
 
 import org.mozilla.gecko.tests.components.GeckoViewComponent.InputConnectionTest;
 import org.mozilla.gecko.tests.helpers.GeckoHelper;
+import org.mozilla.gecko.tests.helpers.JavascriptBridge;
 import org.mozilla.gecko.tests.helpers.NavigationHelper;
 
 import com.jayway.android.robotium.solo.Condition;
 
 import android.view.KeyEvent;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputConnection;
 
 /**
  * Tests the proper operation of GeckoInputConnection
  */
 public class testInputConnection extends UITest {
 
     private static final String INITIAL_TEXT = "foo";
 
+    private JavascriptBridge js;
+
+    @Override // UITest
+    public void setUp() throws Exception {
+        super.setUp();
+        js = new JavascriptBridge(this);
+    }
+
+    @Override // UITest
+    public void tearDown() throws Exception {
+        js.disconnect();
+        super.tearDown();
+    }
+
     public void testInputConnection() throws InterruptedException {
         GeckoHelper.blockForReady();
 
-        final String url = mStringHelper.ROBOCOP_INPUT_URL + "#" + INITIAL_TEXT;
+        final String url = mStringHelper.ROBOCOP_INPUT_URL;
         NavigationHelper.enterAndLoadUrl(url);
         mToolbar.assertTitle(url);
 
+        // First run tests inside the normal input field.
+        js.syncCall("focus_input", INITIAL_TEXT);
         mGeckoView.mTextInput
             .waitForInputConnection()
-            // First run tests inside the normal input field.
-            .testInputConnection(new BasicInputConnectionTest())
-            // Then switch focus to the resetting input field, and run tests there.
-            .testInputConnection(new FocusNextInputFieldTest())
+            .testInputConnection(new BasicInputConnectionTest());
+
+        // Then switch focus to the resetting input field, and run tests there.
+        js.syncCall("focus_resetting_input", "");
+        mGeckoView.mTextInput
+            .waitForInputConnection()
             .testInputConnection(new ResettingInputConnectionTest());
+
+        js.syncCall("finish_test");
     }
 
     private class BasicInputConnectionTest extends InputConnectionTest {
         @Override
         public void test(InputConnection ic, EditorInfo info) {
             // Test initial text provided by the hash in the test page URL
             assertText("Initial text matches URL hash", ic, INITIAL_TEXT);
 
@@ -152,45 +173,20 @@ public class testInputConnection extends
             assertTextAndSelectionAt("Can clear text", ic, "", 0);
 
             // Bug 1209465, cannot enter ideographic space character by itself (U+3000).
             ic.commitText("\u3000", 1);
             assertTextAndSelectionAt("Can commit ideographic space", ic, "\u3000", 1);
 
             ic.deleteSurroundingText(1, 0);
             assertTextAndSelectionAt("Can clear text", ic, "", 0);
-        }
-    }
 
-    /**
-     * FocusNextInputFieldTest is used to switch focus from one input field to
-     * another on the test page by sending a tab key.
-     */
-    private class FocusNextInputFieldTest extends InputConnectionTest {
-        @Override
-        public void test(final InputConnection ic, EditorInfo info) {
-            // First clear all text.
-            ic.setSelection(0, 0);
-            assertSelectionAt("Can set selection to start", ic, 0);
-
-            ic.deleteSurroundingText(0, Integer.MAX_VALUE);
-            assertTextAndSelectionAt("Can clear all text", ic, "", 0);
-
-            // Set dummy text in currently focused input so we can check when we have switched focus.
-            final String dummyText = "dummy switch input text";
-            ic.commitText(dummyText, 1);
-            assertTextAndSelectionAt("Can commit text", ic, dummyText, dummyText.length());
-
-            // Finish processing events from the old input field.
+            // Make sure we don't leave behind stale events for the following test.
             processGeckoEvents(ic);
             processInputConnectionEvents();
-
-            final KeyEvent tabKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB);
-            ic.sendKeyEvent(tabKey);
-            ic.sendKeyEvent(KeyEvent.changeAction(tabKey, KeyEvent.ACTION_UP));
         }
     }
 
     /**
      * ResettingInputConnectionTest performs tests on the resetting input in
      * robocop_input.html. Any test that uses the normal input should be put in
      * BasicInputConnectionTest.
      */