AdapterInputConnectionTest.java revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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 5package org.chromium.content.browser.input; 6 7import android.content.Context; 8import android.os.IBinder; 9import android.os.ResultReceiver; 10import android.test.suitebuilder.annotation.MediumTest; 11import android.text.Editable; 12import android.view.View; 13import android.view.inputmethod.EditorInfo; 14 15import org.chromium.base.test.util.Feature; 16import org.chromium.content.browser.input.AdapterInputConnection.ImeState; 17import org.chromium.content.browser.input.ImeAdapter.ImeAdapterDelegate; 18import org.chromium.content_shell_apk.ContentShellTestBase; 19 20import java.util.ArrayList; 21 22/** 23 * Tests AdapterInputConnection class and its callbacks to ImeAdapter. 24 */ 25public class AdapterInputConnectionTest extends ContentShellTestBase { 26 27 private AdapterInputConnection mConnection; 28 private TestInputMethodManagerWrapper mWrapper; 29 private Editable mEditable; 30 31 @Override 32 public void setUp() throws Exception { 33 super.setUp(); 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 getActivity().getActiveContentView(), imeAdapter, mEditable, info); 43 } 44 45 @MediumTest 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); 51 52 mConnection.setComposingText("te", 1); 53 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting()); 54 mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0 ,2); 55 56 mConnection.setComposingText("tes", 1); 57 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting()); 58 mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3); 59 60 mConnection.setComposingText("test", 1); 61 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting()); 62 mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4); 63 } 64 65 @MediumTest 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); 82 } 83 84 private static class TestImeAdapter extends ImeAdapter { 85 public TestImeAdapter(InputMethodManagerWrapper wrapper, ImeAdapterDelegate embedder) { 86 super(wrapper, embedder); 87 } 88 } 89 90 private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper { 91 private final ArrayList<ImeState> mUpdates = new ArrayList<ImeState>(); 92 93 public TestInputMethodManagerWrapper(Context context) { 94 super(context); 95 } 96 97 @Override 98 public void restartInput(View view) {} 99 100 @Override 101 public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {} 102 103 @Override 104 public boolean isActive(View view) { 105 return true; 106 } 107 108 @Override 109 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, 110 ResultReceiver resultReceiver) { 111 return true; 112 } 113 114 @Override 115 public void updateSelection(View view, int selStart, int selEnd, 116 int candidatesStart, int candidatesEnd) { 117 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, candidatesEnd)); 118 } 119 120 public int getUpdateSelectionCallCount() { 121 return mUpdates.size(); 122 } 123 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); 132 } 133 } 134 135 private static class TestImeAdapterDelegate implements ImeAdapterDelegate { 136 @Override 137 public void onImeEvent(boolean isFinish) {} 138 139 @Override 140 public void onSetFieldValue() {} 141 142 @Override 143 public void onDismissInput() {} 144 145 @Override 146 public View getAttachedView() { 147 return null; 148 } 149 150 @Override 151 public ResultReceiver getNewShowKeyboardReceiver() { 152 return null; 153 } 154 } 155 156 private static void assertCorrectState(String text, int selectionStart, int selectionEnd, 157 int compositionStart, int compositionEnd, ImeState actual) { 158 assertEquals("Text did not match", text, actual.text); 159 assertEquals("Selection start did not match", selectionStart, actual.selectionStart); 160 assertEquals("Selection end did not match", selectionEnd, actual.selectionEnd); 161 assertEquals("Composition start did not match", compositionStart, actual.compositionStart); 162 assertEquals("Composition end did not match", compositionEnd, actual.compositionEnd); 163 } 164} 165