Skip to content

Commit 12122ad

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Rewrite EspressoTest#closeSoftKeyboard.
It was never actually opening the soft keyboard. This caused failures in the upcoming android platform that no longer silently ignores attempts to close keyboard that isn't open. This commit rewrites the test so it opens the virtual keyboard and asserts it is open before proceeding. PiperOrigin-RevId: 619640236
1 parent c6c0a5b commit 12122ad

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

espresso/core/javatests/androidx/test/espresso/EspressoTest.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static com.google.common.truth.Truth.assertThat;
3333
import static org.hamcrest.MatcherAssert.assertThat;
3434
import static org.hamcrest.Matchers.allOf;
35-
import static org.hamcrest.Matchers.any;
3635
import static org.hamcrest.Matchers.contains;
3736
import static org.hamcrest.Matchers.containsString;
3837
import static org.hamcrest.Matchers.hasValue;
@@ -58,7 +57,6 @@
5857
import androidx.test.ui.app.KeyboardTestActivity;
5958
import androidx.test.ui.app.MainActivity;
6059
import androidx.test.ui.app.R;
61-
import androidx.test.ui.app.SendActivity;
6260
import java.util.Map;
6361
import java.util.concurrent.CountDownLatch;
6462
import java.util.concurrent.atomic.AtomicBoolean;
@@ -262,45 +260,26 @@ public void openOverflowInActionMode() {
262260
}
263261

264262
@Test
265-
public void closeSoftKeyboard() {
266-
onData(allOf(instanceOf(Map.class), hasValue(SendActivity.class.getSimpleName())))
263+
public void closeSoftKeyboard() throws InterruptedException {
264+
onData(allOf(instanceOf(Map.class), hasValue(KeyboardTestActivity.class.getSimpleName())))
267265
.perform(click());
266+
// click on the edit text which bring the soft keyboard up
267+
onView(withId(R.id.editTextUserInput)).perform(typeText("Espresso"));
268268

269-
onView(withId(R.id.enter_data_edit_text))
270-
.perform(
271-
new ViewAction() {
272-
@Override
273-
public Matcher<View> getConstraints() {
274-
return any(View.class);
275-
}
276-
269+
onView(withId(R.id.editTextUserInput))
270+
.check(
271+
new ViewAssertion() {
277272
@Override
278-
public void perform(UiController uiController, View view) {
279-
// This doesn't do anything if hardware keyboard is present - that is, soft keyboard
280-
// is _not_ present. Whether it's present or not can be verified under the following
281-
// device settings: Settings > Language & Input > Under Keyboard and input method
273+
public void check(View view, NoMatchingViewException noViewFoundException) {
282274
InputMethodManager imm =
283275
(InputMethodManager)
284-
getInstrumentation()
285-
.getTargetContext()
286-
.getSystemService(Context.INPUT_METHOD_SERVICE);
287-
imm.showSoftInput(view, 0);
288-
uiController.loopMainThreadUntilIdle();
289-
}
290-
291-
@Override
292-
public String getDescription() {
293-
return "show soft input";
276+
view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
277+
assertTrue(imm.isActive(view));
294278
}
295279
});
296280

297-
onView(withId(R.id.enter_data_edit_text)).perform(ViewActions.closeSoftKeyboard());
281+
onView(withId(R.id.editTextUserInput)).perform(ViewActions.closeSoftKeyboard());
298282

299-
assertThat(tracer.getSpans())
300-
.containsAtLeast(
301-
"beginSpan: Espresso.perform(show soft input, view.getId() is <ID>)",
302-
"+-endSpan: Espresso.perform(show soft input, view.getId() is <ID>)")
303-
.inOrder();
304283
assertThat(tracer.getSpans())
305284
.containsAtLeast(
306285
"beginSpan: Espresso.perform(CloseKeyboardAction, view.getId() is <ID>)",

0 commit comments

Comments
 (0)