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;
9
10import org.chromium.content.browser.input.AdapterInputConnection;
11import org.chromium.content.browser.input.ImeAdapter;
12import org.chromium.content.browser.input.InputMethodManagerWrapper;
13import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
14import org.chromium.content_shell_apk.ContentShellTestBase;
15
16/**
17 * Tests that when InputConnection is recreated, the text is still retained.
18 */
19public class ContentViewCoreInputConnectionTest extends ContentShellTestBase {
20    private ContentViewCore mContentViewCore;
21    private TestImeAdapter mImeAdapter;
22    private TestInputMethodManagerWrapper mInputMethodManagerWrapper;
23
24    private static class TestImeAdapter extends ImeAdapter {
25        public TestImeAdapter(InputMethodManagerWrapper immw) {
26            super(immw, null);
27        }
28        @Override
29        public boolean hasTextInputType() {
30            return true;
31        }
32    }
33
34    @Override
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.setContainerView(getActivity().getActiveShell().getContentView());
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    @RerunWithUpdatedContainerView
52    public void testRecreateInputConnection() throws Exception {
53        EditorInfo info = new EditorInfo();
54
55        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        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