1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)package org.chromium.content.browser;
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.content.Context;
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.os.IBinder;
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.os.ResultReceiver;
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.test.UiThreadTest;
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.test.suitebuilder.annotation.SmallTest;
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.view.View;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.widget.EditText;
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.chromium.content.browser.input.InputMethodManagerWrapper;
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.chromium.content_shell.R;
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.chromium.content_shell_apk.ContentShellTestBase;
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/**
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Test that content view core responds to focus changes correctly.
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)public class ContentViewCoreFocusTest extends ContentShellTestBase {
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    private static class TestInputMethodManagerWrapper extends InputMethodManagerWrapper {
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        private boolean mHidden = false;
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        public TestInputMethodManagerWrapper(Context context) {
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            super(context);
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        @Override
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) {
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            mHidden = false;
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        @Override
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        public boolean hideSoftInputFromWindow(IBinder windowToken, int flags,
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                ResultReceiver resultReceiver) {
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            mHidden = true;
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return true;
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        @Override
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        public boolean isActive(View view) {
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return true;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        public boolean isHidden() {
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            return mHidden;
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @UiThreadTest
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    @RerunWithUpdatedContainerView
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @SmallTest
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public void testHideImeOnLosingFocus() throws Throwable {
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // Test the IME window is hidden from the content view when the content
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // view loses its focus
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        final ContentViewCore contentViewCore = getContentViewCore();
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        final View view = contentViewCore.getContainerView();
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        final TestInputMethodManagerWrapper immw = new TestInputMethodManagerWrapper(getActivity());
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        assertTrue(view.requestFocus());
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        contentViewCore.setInputMethodManagerWrapperForTest(immw);
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        immw.showSoftInput(view, 0, null);
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        assertFalse(immw.isHidden());
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        final EditText urlBox = (EditText) getActivity().findViewById(R.id.url);
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        assertTrue(urlBox.requestFocus());
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // Now another view has taken focus. The original view loses its focus
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // and the input method window should be hidden
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        assertTrue(immw.isHidden());
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
76