Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ContentViewCoreInputConnectionTest.java
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
5 package org.chromium.content.browser;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.view.inputmethod.EditorInfo;
9
10 import org.chromium.content.browser.input.AdapterInputConnection;
11 import org.chromium.content.browser.input.ImeAdapter;
12 import org.chromium.content.browser.input.InputMethodManagerWrapper;
13 import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper;
14 import org.chromium.content_shell_apk.ContentShellTestBase;
15
16 /**
17  * Tests that when InputConnection is recreated, the text is still retained.
18  */
19 public 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.createContentViewAndroidDelegate();
44         mContentViewCore.setContainerView(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     @RerunWithUpdatedContainerView
53     public void testRecreateInputConnection() throws Exception {
54         EditorInfo info = new EditorInfo();
55
56         mContentViewCore.onCreateInputConnection(info);
57         AdapterInputConnection adapter = mContentViewCore.getAdapterInputConnectionForTest();
58         adapter.updateState("Is this text restored?", 0, 0, 0, 0, true);
59
60         String text = mContentViewCore.getEditableForTest().toString();
61         assertEquals("Check if the initial text is stored.", "Is this text restored?", text);
62
63         // Create a new InputConnection.
64         EditorInfo info2 = new EditorInfo();
65         mContentViewCore.onCreateInputConnection(info2);
66
67         String newtext = mContentViewCore.getEditableForTest().toString();
68         assertEquals("Check if the string is restored.", "Is this text restored?", newtext);
69     }
70 }