5ae6b380de9667805d1ffc3daea7bbaa736f6d62
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ContentViewCoreFocusTest.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.content.Context;
8 import android.os.IBinder;
9 import android.os.ResultReceiver;
10 import android.test.UiThreadTest;
11 import android.test.suitebuilder.annotation.SmallTest;
12 import android.view.View;
13 import android.widget.EditText;
14
15 import org.chromium.content.browser.input.InputMethodManagerWrapper;
16 import org.chromium.content_shell.R;
17 import org.chromium.content_shell_apk.ContentShellTestBase;
18
19 /**
20  * Test that content view core responds to focus changes correctly.
21  */
22 public class ContentViewCoreFocusTest extends ContentShellTestBase {
23     private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper {
24         private boolean mHidden = false;
25         public TestInputMethodManagerWrapper(Context context) {
26             super(context);
27         }
28
29         @Override
30         public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {
31             mHidden = false;
32         }
33
34         @Override
35         public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
36                 ResultReceiver resultReceiver) {
37             mHidden = true;
38             return true;
39         }
40
41         @Override
42         public boolean isActive(View view) {
43             return true;
44         }
45
46         public boolean isHidden() {
47             return mHidden;
48         }
49     }
50
51     @UiThreadTest
52     @RerunWithUpdatedContainerView
53     @SmallTest
54     public void testHideImeOnLosingFocus() throws Throwable {
55         // Test the IME window is hidden from the content view when the content
56         // view loses its focus
57         final ContentViewCore contentViewCore = getContentViewCore();
58         final View view = contentViewCore.getContainerView();
59         final TestInputMethodManagerWrapper immw = new TestInputMethodManagerWrapper(getActivity());
60         assertTrue(view.requestFocus());
61
62         contentViewCore.setInputMethodManagerWrapperForTest(immw);
63
64         immw.showSoftInput(view, 0, null);
65         assertFalse(immw.isHidden());
66
67         final EditText urlBox = (EditText) getActivity().findViewById(R.id.url);
68
69         assertTrue(urlBox.requestFocus());
70
71         // Now another view has taken focus. The original view loses its focus
72         // and the input method window should be hidden
73         assertTrue(immw.isHidden());
74     }
75 }