Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / input / AdapterInputConnectionTest.java
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.
4
5 package org.chromium.content.browser.input;
6
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;
16
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;
21
22 import java.util.ArrayList;
23
24 /**
25  * Tests AdapterInputConnection class and its callbacks to ImeAdapter.
26  */
27 public class AdapterInputConnectionTest extends ContentShellTestBase {
28
29     private AdapterInputConnection mConnection;
30     private TestInputMethodManagerWrapper mWrapper;
31     private Editable mEditable;
32     private TestImeAdapter mImeAdapter;
33
34     @Override
35     public void setUp() throws Exception {
36         super.setUp();
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);
46     }
47
48     @MediumTest
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);
55
56         mConnection.setComposingText("te", 1);
57         assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting());
58         mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0, 2);
59
60         mConnection.setComposingText("tes", 1);
61         assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting());
62         mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3);
63
64         mConnection.setComposingText("test", 1);
65         assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting());
66         mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4);
67     }
68
69     @MediumTest
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);
86     }
87
88     @MediumTest
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());
97
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());
106
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);
113     }
114
115     private static class TestImeAdapter extends ImeAdapter {
116         private final ArrayList<Integer> mKeyEventQueue = new ArrayList<Integer>();
117         private int mDeleteSurroundingTextCounter;
118
119         public TestImeAdapter(InputMethodManagerWrapper wrapper, ImeAdapterDelegate embedder) {
120             super(wrapper, embedder);
121         }
122
123         @Override
124         public boolean deleteSurroundingText(int beforeLength, int afterLength) {
125             ++mDeleteSurroundingTextCounter;
126             return true;
127         }
128
129         @Override
130         public void sendKeyEventWithKeyCode(int keyCode, int flags) {
131             mKeyEventQueue.add(keyCode);
132         }
133
134         public int getDeleteSurroundingTextCallCount() {
135             return mDeleteSurroundingTextCounter;
136         }
137
138         public Integer[] getKeyEvents() {
139             return mKeyEventQueue.toArray(new Integer[mKeyEventQueue.size()]);
140         }
141     }
142
143     private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper {
144         private final ArrayList<ImeState> mUpdates = new ArrayList<ImeState>();
145
146         public TestInputMethodManagerWrapper(Context context) {
147             super(context);
148         }
149
150         @Override
151         public void restartInput(View view) {}
152
153         @Override
154         public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {}
155
156         @Override
157         public boolean isActive(View view) {
158             return true;
159         }
160
161         @Override
162         public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
163                 ResultReceiver resultReceiver) {
164             return true;
165         }
166
167         @Override
168         public void updateSelection(View view, int selStart, int selEnd,
169                 int candidatesStart, int candidatesEnd) {
170             mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, candidatesEnd));
171         }
172
173         public int getUpdateSelectionCallCount() {
174             return mUpdates.size();
175         }
176
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);
185         }
186     }
187
188     private static class TestImeAdapterDelegate implements ImeAdapterDelegate {
189         @Override
190         public void onImeEvent() {}
191
192         @Override
193         public void onDismissInput() {}
194
195         @Override
196         public View getAttachedView() {
197             return null;
198         }
199
200         @Override
201         public ResultReceiver getNewShowKeyboardReceiver() {
202             return null;
203         }
204     }
205
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);
213     }
214 }