1 // Copyright 2014 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;
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.view.inputmethod.EditorInfo;
9 import android.view.inputmethod.InputConnection;
11 import org.chromium.content.browser.input.AdapterInputConnection;
12 import org.chromium.content.browser.input.ImeAdapter;
13 import org.chromium.content.browser.input.InputMethodManagerWrapper;
14 import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
15 import org.chromium.content_shell_apk.ContentShellTestBase;
18 * Tests that when InputConnection is recreated, the text is still retained.
20 public class ContentViewCoreInputConnectionTest extends ContentShellTestBase {
21 private ContentViewCore mContentViewCore;
22 private TestImeAdapter mImeAdapter;
23 private TestInputMethodManagerWrapper mInputMethodManagerWrapper;
25 private static class TestImeAdapter extends ImeAdapter {
26 public TestImeAdapter(InputMethodManagerWrapper immw) {
30 public boolean hasTextInputType() {
36 public void setUp() throws Exception {
38 mContentViewCore = new ContentViewCore(getActivity());
39 mInputMethodManagerWrapper = new TestInputMethodManagerWrapper(mContentViewCore);
40 mImeAdapter = new TestImeAdapter(mInputMethodManagerWrapper);
41 mImeAdapter.setInputMethodManagerWrapper(new TestInputMethodManagerWrapper(
43 mContentViewCore.setImeAdapterForTest(mImeAdapter);
44 mContentViewCore.setContainerViewForTest(getActivity().getActiveShell().getContentView());
48 * When creating a new InputConnection (e.g. after switching software keyboard), make sure the
49 * text content in the Editable is not lost.
52 public void testRecreateInputConnection() throws Exception {
53 EditorInfo info = new EditorInfo();
55 InputConnection inputConnection = mContentViewCore.onCreateInputConnection(info);
56 AdapterInputConnection adapter = mContentViewCore.getAdapterInputConnectionForTest();
57 adapter.updateState("Is this text restored?", 0, 0, 0, 0, true);
59 String text = mContentViewCore.getEditableForTest().toString();
60 assertEquals("Check if the initial text is stored.", "Is this text restored?", text);
62 // Create a new InputConnection.
63 EditorInfo info2 = new EditorInfo();
64 inputConnection = mContentViewCore.onCreateInputConnection(info2);
66 String newtext = mContentViewCore.getEditableForTest().toString();
67 assertEquals("Check if the string is restored.", "Is this text restored?", newtext);