ContentViewCoreInputConnectionTest.java revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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    @Override
36    public void setUp() throws Exception {
37        super.setUp();
38        mContentViewCore = new ContentViewCore(getActivity());
39        mInputMethodManagerWrapper = new TestInputMethodManagerWrapper(mContentViewCore);
40        mImeAdapter = new TestImeAdapter(mInputMethodManagerWrapper);
41        mImeAdapter.setInputMethodManagerWrapper(new TestInputMethodManagerWrapper(
42            mContentViewCore));
43        mContentViewCore.setImeAdapterForTest(mImeAdapter);
44        mContentViewCore.setContainerViewForTest(getActivity().getActiveShell().getContentView());
45    }
46
47    /**
48     * When creating a new InputConnection (e.g. after switching software keyboard), make sure the
49     * text content in the Editable is not lost.
50     */
51    @SmallTest
52    public void testRecreateInputConnection() throws Exception {
53        EditorInfo info = new EditorInfo();
54
55        InputConnection inputConnection = mContentViewCore.onCreateInputConnection(info);
56        AdapterInputConnection adapter = mContentViewCore.getAdapterInputConnectionForTest();
57        adapter.updateState("Is this text restored?", 0, 0, 0, 0, true);
58
59        String text = mContentViewCore.getEditableForTest().toString();
60        assertEquals("Check if the initial text is stored.", "Is this text restored?", text);
61
62        // Create a new InputConnection.
63        EditorInfo info2 = new EditorInfo();
64        inputConnection = mContentViewCore.onCreateInputConnection(info2);
65
66        String newtext = mContentViewCore.getEditableForTest().toString();
67        assertEquals("Check if the string is restored.", "Is this text restored?", newtext);
68    }
69}
70