summaryrefslogtreecommitdiff
path: root/tests/inputmethod/src/android/view/inputmethod/cts/SearchViewTest.java
blob: 266621f985522a1305051ce709f29782f43f3c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view.inputmethod.cts;

import static android.view.inputmethod.cts.util.InputMethodVisibilityVerifier.expectImeInvisible;
import static android.view.inputmethod.cts.util.InputMethodVisibilityVerifier.expectImeVisible;
import static android.view.inputmethod.cts.util.TestUtils.runOnMainSync;

import static com.android.cts.mockime.ImeEventStreamTestUtils.EventFilterMode.CHECK_EXIT_EVENT_ONLY;
import static com.android.cts.mockime.ImeEventStreamTestUtils.expectBindInput;
import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent;
import static com.android.cts.mockime.ImeEventStreamTestUtils.notExpectEvent;

import android.os.Process;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.cts.util.EndToEndImeTestBase;
import android.view.inputmethod.cts.util.TestActivity;
import android.view.inputmethod.cts.util.UnlockScreenRule;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;

import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.cts.mockime.ImeEventStream;
import com.android.cts.mockime.ImeSettings;
import com.android.cts.mockime.MockImeSession;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class SearchViewTest extends EndToEndImeTestBase {
    static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5);
    static final long NOT_EXPECT_TIMEOUT = TimeUnit.SECONDS.toMillis(2);

    @Rule
    public final UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule();

    public SearchView launchTestActivity(boolean requestFocus) {
        final AtomicReference<SearchView> searchViewRef = new AtomicReference<>();
        TestActivity.startSync(activity -> {
            final LinearLayout layout = new LinearLayout(activity);
            layout.setOrientation(LinearLayout.VERTICAL);

            final EditText initialFocusedEditText = new EditText(activity);
            initialFocusedEditText.setHint("initialFocusedTextView");

            final SearchView searchView = new SearchView(activity);
            searchViewRef.set(searchView);

            searchView.setQueryHint("hint");
            searchView.setIconifiedByDefault(false);
            searchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
            searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
            if (requestFocus) {
                searchView.requestFocus();
            }

            layout.addView(initialFocusedEditText);
            layout.addView(searchView);
            return layout;
        });
        return searchViewRef.get();
    }

    private SearchView launchTestActivityWithListView(boolean requestFocus) {
        final AtomicReference<SearchView> searchViewRef = new AtomicReference<>();
        TestActivity.startSync(activity -> {
            final LinearLayout layout = new LinearLayout(activity);
            layout.setOrientation(LinearLayout.VERTICAL);

            final ListView listView = new ListView(activity);
            layout.addView(listView);

            final SearchView searchView = new SearchView(activity);
            searchViewRef.set(searchView);
            listView.setAdapter(new SingleItemAdapter(searchView));

            searchView.setQueryHint("hint");
            searchView.setIconifiedByDefault(false);
            searchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
            searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
            if (requestFocus) {
                searchView.requestFocus();
            }

            return layout;
        });
        return searchViewRef.get();
    }

    @Test
    public void testTapThenSetQuery() throws Exception {
        try (MockImeSession imeSession = MockImeSession.create(
                InstrumentationRegistry.getInstrumentation().getContext(),
                InstrumentationRegistry.getInstrumentation().getUiAutomation(),
                new ImeSettings.Builder())) {
            final ImeEventStream stream = imeSession.openEventStream();

            final SearchView searchView = launchTestActivity(false /* requestFocus */);

            // Emulate tap event on SearchView
            mCtsTouchUtils.emulateTapOnViewCenter(
                    InstrumentationRegistry.getInstrumentation(), null, searchView);

            // Expect input to bind since EditText is focused.
            expectBindInput(stream, Process.myPid(), TIMEOUT);

            // Wait until "showSoftInput" gets called with a real InputConnection
            expectEvent(stream, event ->
                    "showSoftInput".equals(event.getEventName())
                            && !event.getExitState().hasFallbackInputConnection(),
                    CHECK_EXIT_EVENT_ONLY, TIMEOUT);

            expectImeVisible(TIMEOUT);

            // Make sure that "setQuery" triggers "hideSoftInput" in the IME side.
            runOnMainSync(() -> searchView.setQuery("test", true /* submit */));
            expectEvent(stream, event -> "hideSoftInput".equals(event.getEventName()), TIMEOUT);

            expectImeInvisible(TIMEOUT);
        }
    }

    @Test
    public void testShowImeWithSearchViewFocus() throws Exception {
        try (MockImeSession imeSession = MockImeSession.create(
                InstrumentationRegistry.getInstrumentation().getContext(),
                InstrumentationRegistry.getInstrumentation().getUiAutomation(),
                new ImeSettings.Builder())) {
            final ImeEventStream stream = imeSession.openEventStream();

            final SearchView searchView = launchTestActivity(true /* requestFocus */);

            // Expect input to bind since checkInputConnectionProxy() returns true.
            expectBindInput(stream, Process.myPid(), TIMEOUT);

            runOnMainSync(() -> searchView.getContext().getSystemService(InputMethodManager.class)
                    .showSoftInput(searchView, 0));

            // Wait until "showSoftInput" gets called on searchView's inner editor
            // (SearchAutoComplete) with real InputConnection.
            expectEvent(stream, event ->
                    "showSoftInput".equals(event.getEventName())
                            && !event.getExitState().hasFallbackInputConnection(),
                    CHECK_EXIT_EVENT_ONLY, TIMEOUT);

            expectImeVisible(TIMEOUT);
        }
    }

    @Test
    public void testShowImeWhenSearchViewFocusInListView() throws Exception {
        try (MockImeSession imeSession = MockImeSession.create(
                InstrumentationRegistry.getInstrumentation().getContext(),
                InstrumentationRegistry.getInstrumentation().getUiAutomation(),
                new ImeSettings.Builder())) {
            final ImeEventStream stream = imeSession.openEventStream();

            final SearchView searchView = launchTestActivityWithListView(true /* requestFocus */);

            // Emulate tap event on SearchView
            mCtsTouchUtils.emulateTapOnViewCenter(
                    InstrumentationRegistry.getInstrumentation(), null, searchView);

            // Expect input to bind since EditText is focused.
            expectBindInput(stream, Process.myPid(), TIMEOUT);

            // Wait until "showSoftInput" gets called with a real InputConnection
            expectEvent(stream, event ->
                            "showSoftInput".equals(event.getEventName())
                                    && !event.getExitState().hasFallbackInputConnection(),
                    CHECK_EXIT_EVENT_ONLY, TIMEOUT);

            expectImeVisible(TIMEOUT);

            notExpectEvent(stream, event -> "hideSoftInput".equals(event.getEventName()),
                    NOT_EXPECT_TIMEOUT);
        }
    }

    static final class SingleItemAdapter extends BaseAdapter {
        private final SearchView mSearchView;

        SingleItemAdapter(SearchView searchView) {
            mSearchView = searchView;
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int i) {
            return mSearchView;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return mSearchView;
        }
    }
}