1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser.input;
7 import android.content.Context;
8 import android.os.IBinder;
9 import android.os.ResultReceiver;
10 import android.test.suitebuilder.annotation.MediumTest;
11 import android.text.Editable;
12 import android.text.Selection;
13 import android.view.KeyEvent;
14 import android.view.View;
15 import android.view.inputmethod.EditorInfo;
17 import org.chromium.base.test.util.Feature;
18 import org.chromium.content.browser.input.AdapterInputConnection.ImeState;
19 import org.chromium.content.browser.input.ImeAdapter.ImeAdapterDelegate;
20 import org.chromium.content_shell_apk.ContentShellTestBase;
22 import java.util.ArrayList;
25 * Tests AdapterInputConnection class and its callbacks to ImeAdapter.
27 public class AdapterInputConnectionTest extends ContentShellTestBase {
29 private AdapterInputConnection mConnection;
30 private TestInputMethodManagerWrapper mWrapper;
31 private Editable mEditable;
32 private TestImeAdapter mImeAdapter;
35 public void setUp() throws Exception {
37 launchContentShellWithUrl("about:blank");
38 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
39 mWrapper = new TestInputMethodManagerWrapper(getActivity());
40 ImeAdapterDelegate delegate = new TestImeAdapterDelegate();
41 mImeAdapter = new TestImeAdapter(mWrapper, delegate);
42 EditorInfo info = new EditorInfo();
43 mEditable = Editable.Factory.getInstance().newEditable("");
44 mConnection = new AdapterInputConnection(
45 getContentViewCore().getContainerView(), mImeAdapter, mEditable, info);
49 @Feature({"TextInput", "Main"})
50 @RerunWithUpdatedContainerView
51 public void testSetComposingText() throws Throwable {
52 mConnection.setComposingText("t", 1);
53 assertCorrectState("t", 1, 1, 0, 1, mConnection.getImeStateForTesting());
54 mWrapper.verifyUpdateSelectionCall(0, 1, 1, 0, 1);
56 mConnection.setComposingText("te", 1);
57 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting());
58 mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0, 2);
60 mConnection.setComposingText("tes", 1);
61 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting());
62 mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3);
64 mConnection.setComposingText("test", 1);
65 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting());
66 mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4);
70 @Feature({"TextInput", "Main"})
71 public void testSelectionUpdatesDuringBatch() throws Throwable {
72 mConnection.beginBatchEdit();
73 mConnection.setComposingText("t", 1);
74 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
75 mConnection.setComposingText("te", 1);
76 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
77 mConnection.beginBatchEdit();
78 mConnection.setComposingText("tes", 1);
79 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
80 mConnection.endBatchEdit();
81 mConnection.setComposingText("test", 1);
82 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
83 mConnection.endBatchEdit();
84 assertEquals(1, mWrapper.getUpdateSelectionCallCount());
85 mWrapper.verifyUpdateSelectionCall(0, 4, 4, 0, 4);
89 @Feature({"TextInput", "Main"})
90 public void testDeleteSurroundingText() throws Throwable {
91 // Tests back deletion of a single character with empty input.
92 mConnection.deleteSurroundingText(1, 0);
93 assertEquals(0, mImeAdapter.getDeleteSurroundingTextCallCount());
94 Integer[] keyEvents = mImeAdapter.getKeyEvents();
95 assertEquals(1, keyEvents.length);
96 assertEquals(KeyEvent.KEYCODE_DEL, keyEvents[0].intValue());
98 // Tests forward deletion of a single character with non-empty input.
99 mEditable.replace(0, mEditable.length(), " hello");
100 Selection.setSelection(mEditable, 0, 0);
101 mConnection.deleteSurroundingText(0, 1);
102 assertEquals(0, mImeAdapter.getDeleteSurroundingTextCallCount());
103 keyEvents = mImeAdapter.getKeyEvents();
104 assertEquals(2, keyEvents.length);
105 assertEquals(KeyEvent.KEYCODE_FORWARD_DEL, keyEvents[1].intValue());
107 // Tests back deletion of multiple characters with non-empty input.
108 mEditable.replace(0, mEditable.length(), "hello ");
109 Selection.setSelection(mEditable, mEditable.length(), mEditable.length());
110 mConnection.deleteSurroundingText(2, 0);
111 assertEquals(1, mImeAdapter.getDeleteSurroundingTextCallCount());
112 assertEquals(2, mImeAdapter.getKeyEvents().length);
115 private static class TestImeAdapter extends ImeAdapter {
116 private final ArrayList<Integer> mKeyEventQueue = new ArrayList<Integer>();
117 private int mDeleteSurroundingTextCounter;
119 public TestImeAdapter(InputMethodManagerWrapper wrapper, ImeAdapterDelegate embedder) {
120 super(wrapper, embedder);
124 public boolean deleteSurroundingText(int beforeLength, int afterLength) {
125 ++mDeleteSurroundingTextCounter;
130 public void sendKeyEventWithKeyCode(int keyCode, int flags) {
131 mKeyEventQueue.add(keyCode);
134 public int getDeleteSurroundingTextCallCount() {
135 return mDeleteSurroundingTextCounter;
138 public Integer[] getKeyEvents() {
139 return mKeyEventQueue.toArray(new Integer[mKeyEventQueue.size()]);
143 private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper {
144 private final ArrayList<ImeState> mUpdates = new ArrayList<ImeState>();
146 public TestInputMethodManagerWrapper(Context context) {
151 public void restartInput(View view) {}
154 public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {}
157 public boolean isActive(View view) {
162 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
163 ResultReceiver resultReceiver) {
168 public void updateSelection(View view, int selStart, int selEnd,
169 int candidatesStart, int candidatesEnd) {
170 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, candidatesEnd));
173 public int getUpdateSelectionCallCount() {
174 return mUpdates.size();
177 public void verifyUpdateSelectionCall(int index, int selectionStart, int selectionEnd,
178 int compositionStart, int compositionEnd) {
179 ImeState state = mUpdates.get(index);
180 assertEquals("Selection start did not match", selectionStart, state.selectionStart);
181 assertEquals("Selection end did not match", selectionEnd, state.selectionEnd);
182 assertEquals("Composition start did not match", compositionStart,
183 state.compositionStart);
184 assertEquals("Composition end did not match", compositionEnd, state.compositionEnd);
188 private static class TestImeAdapterDelegate implements ImeAdapterDelegate {
190 public void onImeEvent() {}
193 public void onDismissInput() {}
196 public View getAttachedView() {
201 public ResultReceiver getNewShowKeyboardReceiver() {
206 private static void assertCorrectState(String text, int selectionStart, int selectionEnd,
207 int compositionStart, int compositionEnd, ImeState actual) {
208 assertEquals("Text did not match", text, actual.text);
209 assertEquals("Selection start did not match", selectionStart, actual.selectionStart);
210 assertEquals("Selection end did not match", selectionEnd, actual.selectionEnd);
211 assertEquals("Composition start did not match", compositionStart, actual.compositionStart);
212 assertEquals("Composition end did not match", compositionEnd, actual.compositionEnd);