michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.tests.helpers; michael@0: michael@0: import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertEquals; michael@0: import android.view.inputmethod.ExtractedText; michael@0: import android.view.inputmethod.ExtractedTextRequest; michael@0: import android.view.inputmethod.InputConnection; michael@0: michael@0: /** michael@0: * Provides helper functions for accessing the InputConnection interface michael@0: */ michael@0: public final class TextInputHelper { michael@0: michael@0: private TextInputHelper() { /* To disallow instantiation. */ } michael@0: michael@0: private static ExtractedText getExtractedText(final InputConnection ic) { michael@0: ExtractedTextRequest req = new ExtractedTextRequest(); michael@0: ExtractedText extract = ic.getExtractedText(req, 0); michael@0: return extract; michael@0: } michael@0: michael@0: private static String getText(final InputConnection ic) { michael@0: return getExtractedText(ic).text.toString(); michael@0: } michael@0: michael@0: public static void assertText(final String message, michael@0: final InputConnection ic, michael@0: final String text) { michael@0: fAssertEquals(message, text, getText(ic)); michael@0: } michael@0: michael@0: public static void assertSelection(final String message, michael@0: final InputConnection ic, michael@0: final int start, michael@0: final int end) { michael@0: ExtractedText extract = getExtractedText(ic); michael@0: fAssertEquals(message, start, extract.selectionStart); michael@0: fAssertEquals(message, end, extract.selectionEnd); michael@0: } michael@0: michael@0: public static void assertSelectionAt(final String message, michael@0: final InputConnection ic, michael@0: final int value) { michael@0: assertSelection(message, ic, value, value); michael@0: } michael@0: michael@0: public static void assertTextAndSelection(final String message, michael@0: final InputConnection ic, michael@0: final String text, michael@0: final int start, michael@0: final int end) { michael@0: ExtractedText extract = getExtractedText(ic); michael@0: fAssertEquals(message, text, extract.text); michael@0: fAssertEquals(message, start, extract.selectionStart); michael@0: fAssertEquals(message, end, extract.selectionEnd); michael@0: } michael@0: michael@0: public static void assertTextAndSelectionAt(final String message, michael@0: final InputConnection ic, michael@0: final String text, michael@0: final int selection) { michael@0: assertTextAndSelection(message, ic, text, selection, selection); michael@0: } michael@0: }