Bug 968409 - Add TextInputHelper for UITests; r=mcomella
authorJim Chen <nchen@mozilla.com>
Thu, 27 Feb 2014 16:17:55 -0500
changeset 171356 38921bba8197f4f5ce1fd10ef3135ec31f564367
parent 171355 385d0c08dfe25c75eba936e774f6cd0960b8d6b4
child 171357 ca6fe7cea127b0924e643a88e2a5c887daec8de6
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmcomella
bugs968409
milestone30.0a1
Bug 968409 - Add TextInputHelper for UITests; r=mcomella
mobile/android/base/tests/helpers/TextInputHelper.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/helpers/TextInputHelper.java
@@ -0,0 +1,70 @@
+/* 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/. */
+
+package org.mozilla.gecko.tests.helpers;
+
+import static org.mozilla.gecko.tests.helpers.AssertionHelper.*;
+
+import org.mozilla.gecko.tests.UITestContext;
+
+import android.view.inputmethod.ExtractedText;
+import android.view.inputmethod.ExtractedTextRequest;
+import android.view.inputmethod.InputConnection;
+
+/**
+ * Provides helper functions for accessing the InputConnection interface
+ */
+public final class TextInputHelper {
+
+    private TextInputHelper() { /* To disallow instantiation. */ }
+
+    private static ExtractedText getExtractedText(final InputConnection ic) {
+        ExtractedTextRequest req = new ExtractedTextRequest();
+        ExtractedText extract = ic.getExtractedText(req, 0);
+        return extract;
+    }
+
+    private static String getText(final InputConnection ic) {
+        return getExtractedText(ic).text.toString();
+    }
+
+    public static void assertText(final String message,
+                                  final InputConnection ic,
+                                  final String text) {
+        assertEquals(message, text, getText(ic));
+    }
+
+    public static void assertSelection(final String message,
+                                       final InputConnection ic,
+                                       final int start,
+                                       final int end) {
+        ExtractedText extract = getExtractedText(ic);
+        assertEquals(message, start, extract.selectionStart);
+        assertEquals(message, end, extract.selectionEnd);
+    }
+
+    public static void assertSelectionAt(final String message,
+                                         final InputConnection ic,
+                                         final int value) {
+        assertSelection(message, ic, value, value);
+    }
+
+    public static void assertTextAndSelection(final String message,
+                                              final InputConnection ic,
+                                              final String text,
+                                              final int start,
+                                              final int end) {
+        ExtractedText extract = getExtractedText(ic);
+        assertEquals(message, text, extract.text);
+        assertEquals(message, start, extract.selectionStart);
+        assertEquals(message, end, extract.selectionEnd);
+    }
+
+    public static void assertTextAndSelectionAt(final String message,
+                                                final InputConnection ic,
+                                                final String text,
+                                                final int selection) {
+        assertTextAndSelection(message, ic, text, selection, selection);
+    }
+}