AdapterInputConnectionTest.java revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.view.View; 12import android.view.inputmethod.EditorInfo; 13 14import org.chromium.base.test.util.Feature; 15import org.chromium.content.browser.input.AdapterInputConnection.ImeState; 16import org.chromium.content.browser.input.ImeAdapter.ImeAdapterDelegate; 17import org.chromium.content_shell_apk.ContentShellTestBase; 18 19import java.util.ArrayList; 20 21/** 22 * Tests AdapterInputConnection class and its callbacks to ImeAdapter. 23 */ 24public class AdapterInputConnectionTest extends ContentShellTestBase { 25 26 private AdapterInputConnection mConnection; 27 private TestInputMethodManagerWrapper mWrapper; 28 29 @Override 30 public void setUp() throws Exception { 31 super.setUp(); 32 launchContentShellWithUrl("about:blank"); 33 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); 34 mWrapper = new TestInputMethodManagerWrapper(getActivity()); 35 ImeAdapterDelegate delegate = new TestImeAdapterDelegate(); 36 ImeAdapter imeAdapter = new TestImeAdapter(mWrapper, delegate); 37 EditorInfo info = new EditorInfo(); 38 mConnection = new AdapterInputConnection( 39 getActivity().getActiveContentView(), imeAdapter, info); 40 } 41 42 @MediumTest 43 @Feature({"TextInput", "Main"}) 44 public void testSetComposingText() throws Throwable { 45 mConnection.setComposingText("t", 1); 46 assertCorrectState("t", 1, 1, 0, 1, mConnection.getImeStateForTesting()); 47 mWrapper.verifyUpdateSelectionCall(0, 1, 1, 0 ,1); 48 49 mConnection.setComposingText("te", 1); 50 assertCorrectState("te", 2, 2, 0, 2, mConnection.getImeStateForTesting()); 51 mWrapper.verifyUpdateSelectionCall(1, 2, 2, 0 ,2); 52 53 mConnection.setComposingText("tes", 1); 54 assertCorrectState("tes", 3, 3, 0, 3, mConnection.getImeStateForTesting()); 55 mWrapper.verifyUpdateSelectionCall(2, 3, 3, 0, 3); 56 57 mConnection.setComposingText("test", 1); 58 assertCorrectState("test", 4, 4, 0, 4, mConnection.getImeStateForTesting()); 59 mWrapper.verifyUpdateSelectionCall(3, 4, 4, 0, 4); 60 } 61 62 @MediumTest 63 @Feature({"TextInput", "Main"}) 64 public void testSelectionUpdatesDuringBatch() throws Throwable { 65 mConnection.beginBatchEdit(); 66 mConnection.setComposingText("t", 1); 67 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); 68 mConnection.setComposingText("te", 1); 69 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); 70 mConnection.beginBatchEdit(); 71 mConnection.setComposingText("tes", 1); 72 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); 73 mConnection.endBatchEdit(); 74 mConnection.setComposingText("test", 1); 75 assertEquals(0, mWrapper.getUpdateSelectionCallCount()); 76 mConnection.endBatchEdit(); 77 assertEquals(1, mWrapper.getUpdateSelectionCallCount()); 78 mWrapper.verifyUpdateSelectionCall(0, 4, 4, 0 ,4); 79 } 80 81 private static class TestImeAdapter extends ImeAdapter { 82 public TestImeAdapter(InputMethodManagerWrapper wrapper, ImeAdapterDelegate embedder) { 83 super(wrapper, embedder); 84 } 85 } 86 87 private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper { 88 private final ArrayList<ImeState> mUpdates = new ArrayList<ImeState>(); 89 90 public TestInputMethodManagerWrapper(Context context) { 91 super(context); 92 } 93 94 @Override 95 public void restartInput(View view) {} 96 97 @Override 98 public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {} 99 100 @Override 101 public boolean isActive(View view) { 102 return true; 103 } 104 105 @Override 106 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, 107 ResultReceiver resultReceiver) { 108 return true; 109 } 110 111 @Override 112 public void updateSelection(View view, int selStart, int selEnd, 113 int candidatesStart, int candidatesEnd) { 114 mUpdates.add(new ImeState("", selStart, selEnd, candidatesStart, candidatesEnd)); 115 } 116 117 public int getUpdateSelectionCallCount() { 118 return mUpdates.size(); 119 } 120 121 public void verifyUpdateSelectionCall(int index, int selectionStart, int selectionEnd, 122 int compositionStart, int compositionEnd) { 123 ImeState state = mUpdates.get(index); 124 assertEquals("Selection start did not match", selectionStart, state.selectionStart); 125 assertEquals("Selection end did not match", selectionEnd, state.selectionEnd); 126 assertEquals("Composition start did not match", compositionStart, 127 state.compositionStart); 128 assertEquals("Composition end did not match", compositionEnd, state.compositionEnd); 129 } 130 } 131 132 private static class TestImeAdapterDelegate implements ImeAdapterDelegate { 133 @Override 134 public void onImeEvent(boolean isFinish) {} 135 136 @Override 137 public void onSetFieldValue() {} 138 139 @Override 140 public void onDismissInput() {} 141 142 @Override 143 public View getAttachedView() { 144 return null; 145 } 146 147 @Override 148 public ResultReceiver getNewShowKeyboardReceiver() { 149 return null; 150 } 151 } 152 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); 160 } 161} 162