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.view.View;
13 import android.view.inputmethod.EditorInfo;
15 import org.chromium.base.test.util.Feature;
16 import org.chromium.content.browser.input.AdapterInputConnection.ImeState;
17 import org.chromium.content.browser.input.ImeAdapter.ImeAdapterDelegate;
18 import org.chromium.content_shell_apk.ContentShellTestBase;
20 import java.util.ArrayList;
23 * Tests AdapterInputConnection class and its callbacks to ImeAdapter.
25 public class AdapterInputConnectionTest extends ContentShellTestBase {
27 private AdapterInputConnection mConnection;
28 private TestInputMethodManagerWrapper mWrapper;
29 private Editable mEditable;
32 public void setUp() throws Exception {
34 launchContentShellWithUrl("about:blank");
35 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
36 mWrapper = new TestInputMethodManagerWrapper(getActivity());
37 ImeAdapterDelegate delegate = new TestImeAdapterDelegate();
38 ImeAdapter imeAdapter = new TestImeAdapter(mWrapper, delegate);
39 EditorInfo info = new EditorInfo();
40 mEditable = Editable.Factory.getInstance().newEditable("");
41 mConnection = new AdapterInputConnection(
42 getContentViewCore().getContainerView(), imeAdapter, mEditable, info);
46 @Feature({"TextInput", "Main"})
47 public void testSetComposingText() throws Throwable {
48 mConnection.setComposingText("t", 1);
49 assertCorrectState("t", 1, 1, 0, 1, mConnection.getImeStateForTesting());
50 mWrapper.verifyUpdateSelectionCall(0, 1, 1, 0 ,1);
52 mConnection.setComposingText("te", 1);
53 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting());
54 mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0 ,2);
56 mConnection.setComposingText("tes", 1);
57 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting());
58 mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3);
60 mConnection.setComposingText("test", 1);
61 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting());
62 mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4);
66 @Feature({"TextInput", "Main"})
67 public void testSelectionUpdatesDuringBatch() throws Throwable {
68 mConnection.beginBatchEdit();
69 mConnection.setComposingText("t", 1);
70 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
71 mConnection.setComposingText("te", 1);
72 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
73 mConnection.beginBatchEdit();
74 mConnection.setComposingText("tes", 1);
75 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
76 mConnection.endBatchEdit();
77 mConnection.setComposingText("test", 1);
78 assertEquals(0, mWrapper.getUpdateSelectionCallCount());
79 mConnection.endBatchEdit();
80 assertEquals(1, mWrapper.getUpdateSelectionCallCount());
81 mWrapper.verifyUpdateSelectionCall(0, 4, 4, 0 ,4);
84 private static class TestImeAdapter extends ImeAdapter {
85 public TestImeAdapter(InputMethodManagerWrapper wrapper, ImeAdapterDelegate embedder) {
86 super(wrapper, embedder);
90 private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper {
91 private final ArrayList<ImeState> mUpdates = new ArrayList<ImeState>();
93 public TestInputMethodManagerWrapper(Context context) {
98 public void restartInput(View view) {}
101 public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {}
104 public boolean isActive(View view) {
109 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
110 ResultReceiver resultReceiver) {
115 public void updateSelection(View view, int selStart, int selEnd,
116 int candidatesStart, int candidatesEnd) {
117 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, candidatesEnd));
120 public int getUpdateSelectionCallCount() {
121 return mUpdates.size();
124 public void verifyUpdateSelectionCall(int index, int selectionStart, int selectionEnd,
125 int compositionStart, int compositionEnd) {
126 ImeState state = mUpdates.get(index);
127 assertEquals("Selection start did not match", selectionStart, state.selectionStart);
128 assertEquals("Selection end did not match", selectionEnd, state.selectionEnd);
129 assertEquals("Composition start did not match", compositionStart,
130 state.compositionStart);
131 assertEquals("Composition end did not match", compositionEnd, state.compositionEnd);
135 private static class TestImeAdapterDelegate implements ImeAdapterDelegate {
137 public void onImeEvent(boolean isFinish) {}
140 public void onDismissInput() {}
143 public View getAttachedView() {
148 public ResultReceiver getNewShowKeyboardReceiver() {
153 private static void assertCorrectState(String text, int selectionStart, int selectionEnd,
154 int compositionStart, int compositionEnd, ImeState actual) {
155 assertEquals("Text did not match", text, actual.text);
156 assertEquals("Selection start did not match", selectionStart, actual.selectionStart);
157 assertEquals("Selection end did not match", selectionEnd, actual.selectionEnd);
158 assertEquals("Composition start did not match", compositionStart, actual.compositionStart);
159 assertEquals("Composition end did not match", compositionEnd, actual.compositionEnd);