OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Handler; |
8 import android.os.IBinder; | 9 import android.os.IBinder; |
| 10 import android.os.Looper; |
9 import android.os.ResultReceiver; | 11 import android.os.ResultReceiver; |
10 import android.test.suitebuilder.annotation.MediumTest; | 12 import android.test.suitebuilder.annotation.MediumTest; |
11 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
| 14 import android.text.Editable; |
12 import android.view.View; | 15 import android.view.View; |
13 import android.view.inputmethod.EditorInfo; | 16 import android.view.inputmethod.EditorInfo; |
14 | 17 |
15 import org.chromium.base.test.util.Feature; | 18 import org.chromium.base.test.util.Feature; |
16 import org.chromium.content.browser.input.AdapterInputConnection.ImeState; | 19 import org.chromium.content.browser.input.ReplicaInputConnection.ImeState; |
17 import org.chromium.content_shell_apk.ContentShellTestBase; | 20 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 21 import org.chromium.ui.base.ime.TextInputType; |
18 | 22 |
19 import java.util.ArrayList; | 23 import java.util.ArrayList; |
20 | 24 |
21 /** | 25 /** |
22 * Tests AdapterInputConnection class and its callbacks to ImeAdapter. | 26 * Tests AdapterInputConnection class and its callbacks to ImeAdapter. |
23 */ | 27 */ |
24 public class AdapterInputConnectionTest extends ContentShellTestBase { | 28 public class ReplicaInputConnectionTest extends ContentShellTestBase { |
25 | 29 private ReplicaInputConnection mConnection; |
26 private AdapterInputConnection mConnection; | |
27 private TestInputMethodManagerWrapper mWrapper; | 30 private TestInputMethodManagerWrapper mWrapper; |
28 private TestImeAdapter mImeAdapter; | 31 private TestImeAdapter mImeAdapter; |
29 | 32 |
30 @Override | 33 @Override |
31 public void setUp() throws Exception { | 34 public void setUp() throws Exception { |
32 super.setUp(); | 35 super.setUp(); |
33 launchContentShellWithUrl("about:blank"); | 36 launchContentShellWithUrl("about:blank"); |
34 waitForActiveShellToBeDoneLoading(); | 37 waitForActiveShellToBeDoneLoading(); |
35 mWrapper = new TestInputMethodManagerWrapper(getActivity()); | 38 mWrapper = new TestInputMethodManagerWrapper(getActivity()); |
36 TestImeAdapterDelegate imeAdapterDelegate = | 39 TestImeAdapterDelegate imeAdapterDelegate = |
37 new TestImeAdapterDelegate(getContentViewCore().getContainerView
()); | 40 new TestImeAdapterDelegate(getContentViewCore().getContainerView
()); |
38 mImeAdapter = new TestImeAdapter(mWrapper, imeAdapterDelegate); | 41 mImeAdapter = new TestImeAdapter(mWrapper, imeAdapterDelegate); |
39 mConnection = new AdapterInputConnection( | 42 Editable editable = Editable.Factory.getInstance().newEditable(""); |
40 getContentViewCore().getContainerView(), mImeAdapter, 0, 0, new
EditorInfo()); | 43 Handler handler = new Handler(Looper.getMainLooper()); |
| 44 |
| 45 mConnection = new ReplicaInputConnection(getContentViewCore().getContain
erView(), |
| 46 mImeAdapter, handler, editable, TextInputType.TEXT, 0, new Edito
rInfo()); |
41 } | 47 } |
42 | 48 |
43 @SmallTest | 49 @SmallTest |
44 @Feature({"TextInput", "Main"}) | 50 @Feature({"TextInput", "Main"}) |
45 public void testAdjustLengthBeforeAndAfterSelection() throws Throwable { | 51 public void testAdjustLengthBeforeAndAfterSelection() throws Throwable { |
46 final String ga = "\uAC00"; | 52 final String ga = "\uAC00"; |
47 final String smiley = "\uD83D\uDE0A"; // multi character codepoint | 53 final String smiley = "\uD83D\uDE0A"; // multi character codepoint |
48 | 54 |
49 // No need to adjust length. | 55 // No need to adjust length. |
50 assertFalse(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair("a",
0)); | 56 assertFalse(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair("a",
0)); |
51 assertFalse(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair(ga,
0)); | 57 assertFalse(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair(ga,
0)); |
52 assertFalse(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair("aa"
, 1)); | 58 assertFalse(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair("aa"
, 1)); |
53 assertFalse(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair("a"
+ smiley + "a", 1)); | 59 assertFalse(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair("a"
+ smiley + "a", 1)); |
54 | 60 |
55 // Needs to adjust length. | 61 // Needs to adjust length. |
56 assertTrue(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair(smile
y, 1)); | 62 assertTrue(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair(smile
y, 1)); |
57 assertTrue(AdapterInputConnection.isIndexBetweenUtf16SurrogatePair(smile
y + "a", 1)); | 63 assertTrue(ReplicaInputConnection.isIndexBetweenUtf16SurrogatePair(smile
y + "a", 1)); |
58 } | 64 } |
59 | 65 |
60 @MediumTest | 66 @MediumTest |
61 @Feature({"TextInput", "Main"}) | 67 @Feature({"TextInput", "Main"}) |
62 @RerunWithUpdatedContainerView | |
63 public void testSetComposingText() throws Throwable { | 68 public void testSetComposingText() throws Throwable { |
| 69 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); |
| 70 |
64 mConnection.setComposingText("t", 1); | 71 mConnection.setComposingText("t", 1); |
65 assertCorrectState("t", 1, 1, 0, 1, mConnection.getImeStateForTesting())
; | 72 assertCorrectState("t", 1, 1, 0, 1, mConnection.getImeStateForTesting())
; |
66 mWrapper.verifyUpdateSelectionCall(0, 1, 1, 0, 1); | 73 mWrapper.verifyUpdateSelectionCall(0, 1, 1, 0, 1); |
| 74 // BaseInputConnection calls updateSelection() one more time because get
Editable() does |
| 75 // not return a correct value here. |
| 76 mWrapper.verifyUpdateSelectionCall(1, 1, 1, 0, 1); |
| 77 assertEquals(2, mWrapper.getUpdateSelectionCallCount()); |
67 | 78 |
68 mConnection.setComposingText("te", 1); | 79 mConnection.setComposingText("te", 1); |
69 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting()
); | 80 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting()
); |
70 mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0, 2); | 81 mWrapper.verifyUpdateSelectionCall(2, 2, 2, 0, 2); |
| 82 mWrapper.verifyUpdateSelectionCall(3, 2, 2, 0, 2); |
71 | 83 |
72 mConnection.setComposingText("tes", 1); | 84 mConnection.setComposingText("tes", 1); |
73 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting(
)); | 85 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting(
)); |
74 mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3); | 86 mWrapper.verifyUpdateSelectionCall(4, 3, 3, 0, 3); |
| 87 mWrapper.verifyUpdateSelectionCall(5, 3, 3, 0, 3); |
75 | 88 |
76 mConnection.setComposingText("test", 1); | 89 mConnection.setComposingText("test", 1); |
77 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting
()); | 90 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting
()); |
78 mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4); | 91 mWrapper.verifyUpdateSelectionCall(6, 4, 4, 0, 4); |
| 92 mWrapper.verifyUpdateSelectionCall(7, 4, 4, 0, 4); |
79 } | 93 } |
80 | 94 |
81 @MediumTest | 95 @MediumTest |
82 @Feature({"TextInput", "Main"}) | 96 @Feature({"TextInput", "Main"}) |
83 public void testSelectionUpdatesDuringBatch() throws Throwable { | 97 public void testSelectionUpdatesDuringBatch() throws Throwable { |
84 mConnection.beginBatchEdit(); | 98 mConnection.beginBatchEdit(); |
85 mConnection.setComposingText("t", 1); | 99 mConnection.setComposingText("t", 1); |
86 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); | 100 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); |
87 mConnection.setComposingText("te", 1); | 101 mConnection.setComposingText("te", 1); |
88 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); | 102 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 136 |
123 @Override | 137 @Override |
124 public void showSoftInput(View view, int flags, ResultReceiver resultRec
eiver) {} | 138 public void showSoftInput(View view, int flags, ResultReceiver resultRec
eiver) {} |
125 | 139 |
126 @Override | 140 @Override |
127 public boolean isActive(View view) { | 141 public boolean isActive(View view) { |
128 return true; | 142 return true; |
129 } | 143 } |
130 | 144 |
131 @Override | 145 @Override |
132 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, | 146 public boolean hideSoftInputFromWindow( |
133 ResultReceiver resultReceiver) { | 147 IBinder windowToken, int flags, ResultReceiver resultReceiver) { |
134 return true; | 148 return true; |
135 } | 149 } |
136 | 150 |
137 @Override | 151 @Override |
138 public void updateSelection(View view, int selStart, int selEnd, | 152 public void updateSelection( |
139 int candidatesStart, int candidatesEnd) { | 153 View view, int selStart, int selEnd, int candidatesStart, int ca
ndidatesEnd) { |
140 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, can
didatesEnd)); | 154 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, can
didatesEnd)); |
141 } | 155 } |
142 | 156 |
143 public int getUpdateSelectionCallCount() { | 157 public int getUpdateSelectionCallCount() { |
144 return mUpdates.size(); | 158 return mUpdates.size(); |
145 } | 159 } |
146 | 160 |
147 public void verifyUpdateSelectionCall(int index, int selectionStart, int
selectionEnd, | 161 public void verifyUpdateSelectionCall(int index, int selectionStart, int
selectionEnd, |
148 int compositionStart, int compositionEnd) { | 162 int compositionStart, int compositionEnd) { |
149 ImeState state = mUpdates.get(index); | 163 ImeState state = mUpdates.get(index); |
150 assertEquals("Selection start did not match", selectionStart, state.
selectionStart); | 164 assertEquals("Selection start did not match", selectionStart, state.
selectionStart); |
151 assertEquals("Selection end did not match", selectionEnd, state.sele
ctionEnd); | 165 assertEquals("Selection end did not match", selectionEnd, state.sele
ctionEnd); |
152 assertEquals("Composition start did not match", compositionStart, | 166 assertEquals( |
153 state.compositionStart); | 167 "Composition start did not match", compositionStart, state.c
ompositionStart); |
154 assertEquals("Composition end did not match", compositionEnd, state.
compositionEnd); | 168 assertEquals("Composition end did not match", compositionEnd, state.
compositionEnd); |
155 } | 169 } |
156 } | 170 } |
157 | 171 |
158 private static void assertCorrectState(String text, int selectionStart, int
selectionEnd, | 172 private static void assertCorrectState(String text, int selectionStart, int
selectionEnd, |
159 int compositionStart, int compositionEnd, ImeState actual) { | 173 int compositionStart, int compositionEnd, ImeState actual) { |
160 assertEquals("Text did not match", text, actual.text); | 174 assertEquals("Text did not match", text, actual.text); |
161 assertEquals("Selection start did not match", selectionStart, actual.sel
ectionStart); | 175 assertEquals("Selection start did not match", selectionStart, actual.sel
ectionStart); |
162 assertEquals("Selection end did not match", selectionEnd, actual.selecti
onEnd); | 176 assertEquals("Selection end did not match", selectionEnd, actual.selecti
onEnd); |
163 assertEquals("Composition start did not match", compositionStart, actual
.compositionStart); | 177 assertEquals("Composition start did not match", compositionStart, actual
.compositionStart); |
164 assertEquals("Composition end did not match", compositionEnd, actual.com
positionEnd); | 178 assertEquals("Composition end did not match", compositionEnd, actual.com
positionEnd); |
165 } | 179 } |
166 } | 180 } |
OLD | NEW |