ContentViewCoreInputConnectionTest.java revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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.
4
5package org.chromium.content.browser;
6
7import android.test.suitebuilder.annotation.SmallTest;
8import android.view.inputmethod.EditorInfo;
9import android.view.inputmethod.InputConnection;
10
11import org.chromium.content.browser.input.AdapterInputConnection;
12import org.chromium.content.browser.input.ImeAdapter;
13import org.chromium.content.browser.input.InputMethodManagerWrapper;
14import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
15import org.chromium.content_shell_apk.ContentShellTestBase;
16
17/**
18 * Tests that when InputConnection is recreated, the text is still retained.
19 */
20public class ContentViewCoreInputConnectionTest extends ContentShellTestBase {
21    private ContentViewCore mContentViewCore;
22    private TestImeAdapter mImeAdapter;
23    private TestInputMethodManagerWrapper mInputMethodManagerWrapper;
24
25    private static class TestImeAdapter extends ImeAdapter {
26        public TestImeAdapter(InputMethodManagerWrapper immw) {
27            super(immw, null);
28        }
29        @Override
30        public boolean hasTextInputType() {
31            return true;
32        }
33    }
34
35    public void setUp() throws Exception {
36        super.setUp();
37        mContentViewCore = new ContentViewCore(getActivity());
38        mInputMethodManagerWrapper = new TestInputMethodManagerWrapper(mContentViewCore);
39        mImeAdapter = new TestImeAdapter(mInputMethodManagerWrapper);
40        mImeAdapter.setInputMethodManagerWrapper(new TestInputMethodManagerWrapper(
41            mContentViewCore));
42        mContentViewCore.setImeAdapterForTest(mImeAdapter);
43        mContentViewCore.setContainerViewForTest(getActivity().getActiveContentView());
44    }
45
46    /**
47     * When creating a new InputConnection (e.g. after switching software keyboard), make sure the
48     * text content in the Editable is not lost.
49     */
50    @SmallTest
51    public void testRecreateInputConnection() throws Exception {
52        EditorInfo info = new EditorInfo();
53
54        InputConnection inputConnection = mContentViewCore.onCreateInputConnection(info);
55        AdapterInputConnection adapter = mContentViewCore.getAdapterInputConnectionForTest();
56        adapter.updateState("Is this text restored?", 0, 0, 0, 0, true);
57
58        String text = mContentViewCore.getEditableForTest().toString();
59        assertEquals("Check if the initial text is stored.", "Is this text restored?", text);
60
61        // Create a new InputConnection.
62        EditorInfo info2 = new EditorInfo();
63        inputConnection = mContentViewCore.onCreateInputConnection(info2);
64
65        String newtext = mContentViewCore.getEditableForTest().toString();
66        assertEquals("Check if the string is restored.", "Is this text restored?", newtext);
67    }
68}
69