| /* |
| * Copyright (C) 2018 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.TestUtils.getOnMainSync; |
| import static android.view.inputmethod.cts.util.TestUtils.runOnMainSync; |
| |
| import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent; |
| import static com.android.cts.mockime.ImeEventStreamTestUtils.notExpectEvent; |
| |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assert.assertTrue; |
| |
| import android.os.SystemClock; |
| import android.text.TextUtils; |
| import android.util.Pair; |
| import android.view.inputmethod.EditorInfo; |
| import android.view.inputmethod.InputMethod; |
| import android.view.inputmethod.InputMethodManager; |
| import android.view.inputmethod.cts.util.EndToEndImeTestBase; |
| import android.view.inputmethod.cts.util.TestActivity; |
| import android.widget.EditText; |
| import android.widget.LinearLayout; |
| |
| import androidx.annotation.NonNull; |
| import androidx.test.InstrumentationRegistry; |
| import androidx.test.filters.MediumTest; |
| import androidx.test.runner.AndroidJUnit4; |
| |
| import com.android.cts.mockime.ImeEvent; |
| import com.android.cts.mockime.ImeEventStream; |
| import com.android.cts.mockime.ImeSettings; |
| import com.android.cts.mockime.MockImeSession; |
| |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| import java.util.concurrent.TimeUnit; |
| import java.util.concurrent.atomic.AtomicReference; |
| import java.util.function.Predicate; |
| |
| @MediumTest |
| @RunWith(AndroidJUnit4.class) |
| public class KeyboardVisibilityControlTest extends EndToEndImeTestBase { |
| private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5); |
| |
| private static final String TEST_MARKER_PREFIX = |
| "android.view.inputmethod.cts.KeyboardVisibilityControlTest"; |
| |
| private static String getTestMarker() { |
| return TEST_MARKER_PREFIX + "/" + SystemClock.elapsedRealtimeNanos(); |
| } |
| |
| private static Predicate<ImeEvent> editorMatcher( |
| @NonNull String eventName, @NonNull String marker) { |
| return event -> { |
| if (!TextUtils.equals(eventName, event.getEventName())) { |
| return false; |
| } |
| final EditorInfo editorInfo = event.getArguments().getParcelable("editorInfo"); |
| return TextUtils.equals(marker, editorInfo.privateImeOptions); |
| }; |
| } |
| |
| private static Predicate<ImeEvent> showSoftInputMatcher(int requiredFlags) { |
| return event -> { |
| if (!TextUtils.equals("showSoftInput", event.getEventName())) { |
| return false; |
| } |
| final int flags = event.getArguments().getInt("flags"); |
| return (flags & requiredFlags) == requiredFlags; |
| }; |
| } |
| |
| private static Predicate<ImeEvent> hideSoftInputMatcher() { |
| return event -> TextUtils.equals("hideSoftInput", event.getEventName()); |
| } |
| |
| private static Predicate<ImeEvent> onFinishInputViewMatcher(boolean expectedFinishingInput) { |
| return event -> { |
| if (!TextUtils.equals("onFinishInputView", event.getEventName())) { |
| return false; |
| } |
| final boolean finishingInput = event.getArguments().getBoolean("finishingInput"); |
| return finishingInput == expectedFinishingInput; |
| }; |
| } |
| |
| private Pair<EditText, EditText> launchTestActivity(@NonNull String focusedMarker, |
| @NonNull String nonFocusedMarker) { |
| final AtomicReference<EditText> focusedEditTextRef = new AtomicReference<>(); |
| final AtomicReference<EditText> nonFocusedEditTextRef = new AtomicReference<>(); |
| TestActivity.startSync(activity -> { |
| final LinearLayout layout = new LinearLayout(activity); |
| layout.setOrientation(LinearLayout.VERTICAL); |
| |
| final EditText focusedEditText = new EditText(activity); |
| focusedEditText.setHint("focused editText"); |
| focusedEditText.setPrivateImeOptions(focusedMarker); |
| focusedEditText.requestFocus(); |
| focusedEditTextRef.set(focusedEditText); |
| layout.addView(focusedEditText); |
| |
| final EditText nonFocusedEditText = new EditText(activity); |
| nonFocusedEditText.setPrivateImeOptions(nonFocusedMarker); |
| nonFocusedEditText.setHint("target editText"); |
| nonFocusedEditTextRef.set(nonFocusedEditText); |
| layout.addView(nonFocusedEditText); |
| return layout; |
| }); |
| return new Pair<>(focusedEditTextRef.get(), nonFocusedEditTextRef.get()); |
| } |
| |
| private EditText launchTestActivity(@NonNull String marker) { |
| return launchTestActivity(marker, getTestMarker()).first; |
| } |
| |
| @Test |
| public void testBasicShowHideSoftInput() throws Exception { |
| final InputMethodManager imm = InstrumentationRegistry.getInstrumentation() |
| .getTargetContext().getSystemService(InputMethodManager.class); |
| |
| try (MockImeSession imeSession = MockImeSession.create( |
| InstrumentationRegistry.getContext(), |
| InstrumentationRegistry.getInstrumentation().getUiAutomation(), |
| new ImeSettings.Builder())) { |
| final ImeEventStream stream = imeSession.openEventStream(); |
| |
| final String marker = getTestMarker(); |
| final EditText editText = launchTestActivity(marker); |
| |
| expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT); |
| notExpectEvent(stream, editorMatcher("onStartInputView", marker), TIMEOUT); |
| |
| assertTrue("isActive() must return true if the View has IME focus", |
| getOnMainSync(() -> imm.isActive(editText))); |
| |
| // Test showSoftInput() flow |
| assertTrue("showSoftInput must success if the View has IME focus", |
| getOnMainSync(() -> imm.showSoftInput(editText, 0))); |
| |
| expectEvent(stream, showSoftInputMatcher(InputMethod.SHOW_EXPLICIT), TIMEOUT); |
| expectEvent(stream, editorMatcher("onStartInputView", marker), TIMEOUT); |
| |
| // Test hideSoftInputFromWindow() flow |
| assertTrue("hideSoftInputFromWindow must success if the View has IME focus", |
| getOnMainSync(() -> imm.hideSoftInputFromWindow(editText.getWindowToken(), 0))); |
| |
| expectEvent(stream, hideSoftInputMatcher(), TIMEOUT); |
| expectEvent(stream, onFinishInputViewMatcher(false), TIMEOUT); |
| } |
| } |
| |
| @Test |
| public void testShowHideSoftInputShouldBeIgnoredOnNonFocusedView() throws Exception { |
| final InputMethodManager imm = InstrumentationRegistry.getInstrumentation() |
| .getTargetContext().getSystemService(InputMethodManager.class); |
| |
| try (MockImeSession imeSession = MockImeSession.create( |
| InstrumentationRegistry.getContext(), |
| InstrumentationRegistry.getInstrumentation().getUiAutomation(), |
| new ImeSettings.Builder())) { |
| final ImeEventStream stream = imeSession.openEventStream(); |
| |
| final String focusedMarker = getTestMarker(); |
| final String nonFocusedMarker = getTestMarker(); |
| final Pair<EditText, EditText> editTextPair = |
| launchTestActivity(focusedMarker, nonFocusedMarker); |
| final EditText nonFocusedEditText = editTextPair.second; |
| |
| expectEvent(stream, editorMatcher("onStartInput", focusedMarker), TIMEOUT); |
| |
| assertFalse("isActive() must return false if the View does not have IME focus", |
| getOnMainSync(() -> imm.isActive(nonFocusedEditText))); |
| assertFalse("showSoftInput must fail if the View does not have IME focus", |
| getOnMainSync(() -> imm.showSoftInput(nonFocusedEditText, 0))); |
| notExpectEvent(stream, showSoftInputMatcher(InputMethod.SHOW_EXPLICIT), TIMEOUT); |
| |
| assertFalse("hideSoftInputFromWindow must fail if the View does not have IME focus", |
| getOnMainSync(() -> imm.hideSoftInputFromWindow( |
| nonFocusedEditText.getWindowToken(), 0))); |
| notExpectEvent(stream, hideSoftInputMatcher(), TIMEOUT); |
| } |
| } |
| |
| @Test |
| public void testToggleSoftInput() throws Exception { |
| final InputMethodManager imm = InstrumentationRegistry.getInstrumentation() |
| .getTargetContext().getSystemService(InputMethodManager.class); |
| |
| try (MockImeSession imeSession = MockImeSession.create( |
| InstrumentationRegistry.getContext(), |
| InstrumentationRegistry.getInstrumentation().getUiAutomation(), |
| new ImeSettings.Builder())) { |
| final ImeEventStream stream = imeSession.openEventStream(); |
| |
| final String marker = getTestMarker(); |
| final EditText editText = launchTestActivity(marker); |
| |
| expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT); |
| notExpectEvent(stream, editorMatcher("onStartInputView", marker), TIMEOUT); |
| |
| // Test toggleSoftInputFromWindow() flow |
| runOnMainSync(() -> imm.toggleSoftInputFromWindow(editText.getWindowToken(), 0, 0)); |
| |
| expectEvent(stream.copy(), showSoftInputMatcher(InputMethod.SHOW_EXPLICIT), TIMEOUT); |
| expectEvent(stream.copy(), editorMatcher("onStartInputView", marker), TIMEOUT); |
| |
| // Calling toggleSoftInputFromWindow() must hide the IME. |
| runOnMainSync(() -> imm.toggleSoftInputFromWindow(editText.getWindowToken(), 0, 0)); |
| |
| expectEvent(stream, hideSoftInputMatcher(), TIMEOUT); |
| expectEvent(stream, onFinishInputViewMatcher(false), TIMEOUT); |
| } |
| } |
| } |