Commit 2ee8671d authored by Wei-Yin Chen (陳威尹)'s avatar Wei-Yin Chen (陳威尹) Committed by Commit Bot

Simplify usage of waitForView()

When needing to use waitForView() on the whole view hierarchy,
the API of waitForView() requires more boilerplate code than ideal.

This CL makes it possible to write shorter code.

Bug: None
Change-Id: Ic97f67df1e833a2c38a5a7e09133170e79910b1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2138964Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757550}
parent 4b78b510
...@@ -452,7 +452,7 @@ public class ManualFillingTestHelper { ...@@ -452,7 +452,7 @@ public class ManualFillingTestHelper {
* @return An interaction on the view matching |matcher. * @return An interaction on the view matching |matcher.
*/ */
public static ViewInteraction whenDisplayed(Matcher<View> matcher) { public static ViewInteraction whenDisplayed(Matcher<View> matcher) {
onView(isRoot()).check((r, e) -> waitForView((ViewGroup) r, allOf(matcher, isDisplayed()))); onView(isRoot()).check(waitForView(allOf(matcher, isDisplayed())));
return onView(matcher); return onView(matcher);
} }
...@@ -468,9 +468,7 @@ public class ManualFillingTestHelper { ...@@ -468,9 +468,7 @@ public class ManualFillingTestHelper {
} }
public static void waitToBeHidden(Matcher<View> matcher) { public static void waitToBeHidden(Matcher<View> matcher) {
onView(isRoot()).check((r, e) -> { onView(isRoot()).check(waitForView(matcher, VIEW_INVISIBLE | VIEW_NULL | VIEW_GONE));
waitForView((ViewGroup) r, matcher, VIEW_INVISIBLE | VIEW_NULL | VIEW_GONE);
});
} }
public String getAttribute(String node, String attribute) public String getAttribute(String node, String attribute)
......
...@@ -34,7 +34,6 @@ import android.graphics.Rect; ...@@ -34,7 +34,6 @@ import android.graphics.Rect;
import android.support.test.espresso.matcher.RootMatchers; import android.support.test.espresso.matcher.RootMatchers;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub; import android.view.ViewStub;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -205,7 +204,7 @@ public class KeyboardAccessoryModernViewTest { ...@@ -205,7 +204,7 @@ public class KeyboardAccessoryModernViewTest {
createAutofillChipAndTab("Johnathan", result -> clickRecorded.set(true))); createAutofillChipAndTab("Johnathan", result -> clickRecorded.set(true)));
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Johnathan"))); onView(isRoot()).check(waitForView(withText("Johnathan")));
onView(withText("Johnathan")).perform(click()); onView(withText("Johnathan")).perform(click());
assertTrue(clickRecorded.get()); assertTrue(clickRecorded.get());
...@@ -255,7 +254,7 @@ public class KeyboardAccessoryModernViewTest { ...@@ -255,7 +254,7 @@ public class KeyboardAccessoryModernViewTest {
mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()}); mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()});
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Johnathan"))); onView(isRoot()).check(waitForView(withText("Johnathan")));
waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_password)); waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_password));
onView(withChild(withText("Johnathan"))).check(matches(isSelected())); onView(withChild(withText("Johnathan"))).check(matches(isSelected()));
onView(withText("Johnathan")).perform(click()); onView(withText("Johnathan")).perform(click());
...@@ -283,7 +282,7 @@ public class KeyboardAccessoryModernViewTest { ...@@ -283,7 +282,7 @@ public class KeyboardAccessoryModernViewTest {
mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()}); mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()});
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Johnathan"))); onView(isRoot()).check(waitForView(withText("Johnathan")));
waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_address)); waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_address));
onView(withText("Johnathan")).perform(click()); onView(withText("Johnathan")).perform(click());
...@@ -309,7 +308,7 @@ public class KeyboardAccessoryModernViewTest { ...@@ -309,7 +308,7 @@ public class KeyboardAccessoryModernViewTest {
mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()}); mModel.get(BAR_ITEMS).set(new BarItem[] {itemWithIPH, createTabs()});
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Johnathan"))); onView(isRoot()).check(waitForView(withText("Johnathan")));
waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_payment)); waitForHelpBubble(withText(R.string.iph_keyboard_accessory_fill_payment));
onView(withText("Johnathan")).perform(click()); onView(withText("Johnathan")).perform(click());
...@@ -322,7 +321,7 @@ public class KeyboardAccessoryModernViewTest { ...@@ -322,7 +321,7 @@ public class KeyboardAccessoryModernViewTest {
View mainDecorView = mActivityTestRule.getActivity().getWindow().getDecorView(); View mainDecorView = mActivityTestRule.getActivity().getWindow().getDecorView();
onView(isRoot()) onView(isRoot())
.inRoot(RootMatchers.withDecorView(not(is(mainDecorView)))) .inRoot(RootMatchers.withDecorView(not(is(mainDecorView))))
.check((root, e) -> waitForView((ViewGroup) root, matcher)); .check(waitForView(matcher));
} }
private void rotateActivityToLandscape() { private void rotateActivityToLandscape() {
......
...@@ -30,7 +30,6 @@ import static org.chromium.chrome.test.util.ViewUtils.waitForView; ...@@ -30,7 +30,6 @@ import static org.chromium.chrome.test.util.ViewUtils.waitForView;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub; import android.view.ViewStub;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
...@@ -134,7 +133,7 @@ public class KeyboardAccessoryViewTest { ...@@ -134,7 +133,7 @@ public class KeyboardAccessoryViewTest {
mModel.get(BAR_ITEMS).add(testItem); mModel.get(BAR_ITEMS).add(testItem);
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Test Button"))); onView(isRoot()).check(waitForView(withText("Test Button")));
onView(withText("Test Button")).perform(click()); onView(withText("Test Button")).perform(click());
assertTrue(buttonClicked.get()); assertTrue(buttonClicked.get());
...@@ -152,7 +151,7 @@ public class KeyboardAccessoryViewTest { ...@@ -152,7 +151,7 @@ public class KeyboardAccessoryViewTest {
new Action("Second", AUTOFILL_SUGGESTION, action -> {}))}); new Action("Second", AUTOFILL_SUGGESTION, action -> {}))});
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("First"))); onView(isRoot()).check(waitForView(withText("First")));
onView(withText("First")).check(matches(isDisplayed())); onView(withText("First")).check(matches(isDisplayed()));
onView(withText("Second")).check(matches(isDisplayed())); onView(withText("Second")).check(matches(isDisplayed()));
...@@ -161,7 +160,7 @@ public class KeyboardAccessoryViewTest { ...@@ -161,7 +160,7 @@ public class KeyboardAccessoryViewTest {
new Action("Third", GENERATE_PASSWORD_AUTOMATIC, action -> {}))); new Action("Third", GENERATE_PASSWORD_AUTOMATIC, action -> {})));
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("Third"))); onView(isRoot()).check(waitForView(withText("Third")));
onView(withText("First")).check(matches(isDisplayed())); onView(withText("First")).check(matches(isDisplayed()));
onView(withText("Second")).check(matches(isDisplayed())); onView(withText("Second")).check(matches(isDisplayed()));
onView(withText("Third")).check(matches(isDisplayed())); onView(withText("Third")).check(matches(isDisplayed()));
...@@ -181,7 +180,7 @@ public class KeyboardAccessoryViewTest { ...@@ -181,7 +180,7 @@ public class KeyboardAccessoryViewTest {
new Action("Third", GENERATE_PASSWORD_AUTOMATIC, action -> {}))}); new Action("Third", GENERATE_PASSWORD_AUTOMATIC, action -> {}))});
}); });
onView(isRoot()).check((root, e) -> waitForView((ViewGroup) root, withText("First"))); onView(isRoot()).check(waitForView(withText("First")));
onView(withText("First")).check(matches(isDisplayed())); onView(withText("First")).check(matches(isDisplayed()));
onView(withText("Second")).check(matches(isDisplayed())); onView(withText("Second")).check(matches(isDisplayed()));
onView(withText("Third")).check(matches(isDisplayed())); onView(withText("Third")).check(matches(isDisplayed()));
...@@ -189,9 +188,8 @@ public class KeyboardAccessoryViewTest { ...@@ -189,9 +188,8 @@ public class KeyboardAccessoryViewTest {
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
() -> mModel.get(BAR_ITEMS).remove(mModel.get(BAR_ITEMS).get(1))); () -> mModel.get(BAR_ITEMS).remove(mModel.get(BAR_ITEMS).get(1)));
onView(isRoot()).check((root, e) onView(isRoot()).check(
-> waitForView((ViewGroup) root, withText("Second"), waitForView(withText("Second"), VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
onView(withText("First")).check(matches(isDisplayed())); onView(withText("First")).check(matches(isDisplayed()));
onView(withText("Second")).check(doesNotExist()); onView(withText("Second")).check(doesNotExist());
onView(withText("Third")).check(matches(isDisplayed())); onView(withText("Third")).check(matches(isDisplayed()));
......
...@@ -153,7 +153,7 @@ public class AccessorySheetViewTest { ...@@ -153,7 +153,7 @@ public class AccessorySheetViewTest {
TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(ACTIVE_TAB_INDEX, 1)); TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(ACTIVE_TAB_INDEX, 1));
onView(isRoot()).check((r, e) -> waitForView((ViewGroup) r, withText(kSecondTab))); onView(isRoot()).check(waitForView(withText(kSecondTab)));
} }
@Test @Test
...@@ -194,7 +194,7 @@ public class AccessorySheetViewTest { ...@@ -194,7 +194,7 @@ public class AccessorySheetViewTest {
mModel.get(TABS).add(createTestTabWithTextView(kSecondTab)); mModel.get(TABS).add(createTestTabWithTextView(kSecondTab));
mModel.set(ACTIVE_TAB_INDEX, 0); mModel.set(ACTIVE_TAB_INDEX, 0);
}); });
onView(isRoot()).check((r, e) -> waitForView((ViewGroup) r, withText(kSecondTab))); onView(isRoot()).check(waitForView(withText(kSecondTab)));
} }
@Test @Test
...@@ -203,19 +203,15 @@ public class AccessorySheetViewTest { ...@@ -203,19 +203,15 @@ public class AccessorySheetViewTest {
mModel.get(TABS).add(createTestTabWithTextView("SomeTab")); mModel.get(TABS).add(createTestTabWithTextView("SomeTab"));
mModel.set(TOP_SHADOW_VISIBLE, false); mModel.set(TOP_SHADOW_VISIBLE, false);
TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(VISIBLE, true)); // Render view. TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(VISIBLE, true)); // Render view.
onView(isRoot()).check((r, e) -> { onView(isRoot()).check(
waitForView( waitForView(withId(R.id.accessory_sheet_shadow), ViewUtils.VIEW_INVISIBLE));
(ViewGroup) r, withId(R.id.accessory_sheet_shadow), ViewUtils.VIEW_INVISIBLE);
});
TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(TOP_SHADOW_VISIBLE, true)); TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(TOP_SHADOW_VISIBLE, true));
onView(withId(R.id.accessory_sheet_shadow)).check(matches(isDisplayed())); onView(withId(R.id.accessory_sheet_shadow)).check(matches(isDisplayed()));
TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(TOP_SHADOW_VISIBLE, false)); TestThreadUtils.runOnUiThreadBlocking(() -> mModel.set(TOP_SHADOW_VISIBLE, false));
onView(isRoot()).check((r, e) -> { onView(isRoot()).check(
waitForView( waitForView(withId(R.id.accessory_sheet_shadow), ViewUtils.VIEW_INVISIBLE));
(ViewGroup) r, withId(R.id.accessory_sheet_shadow), ViewUtils.VIEW_INVISIBLE);
});
} }
private Tab createTestTabWithTextView(String textViewCaption) { private Tab createTestTabWithTextView(String textViewCaption) {
......
...@@ -12,6 +12,7 @@ import static android.support.test.espresso.action.ViewActions.typeText; ...@@ -12,6 +12,7 @@ import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.Visibility.GONE; import static android.support.test.espresso.matcher.ViewMatchers.Visibility.GONE;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent; import static android.support.test.espresso.matcher.ViewMatchers.withParent;
...@@ -164,18 +165,10 @@ public class StartSurfaceTest { ...@@ -164,18 +165,10 @@ public class StartSurfaceTest {
.check(matches(isDisplayed())); .check(matches(isDisplayed()));
onView(withId(R.id.ss_explore_tab)).perform(click()); onView(withId(R.id.ss_explore_tab)).perform(click());
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.start_surface_explore_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.start_surface_explore_view));
pressBack(); pressBack();
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.primary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.primary_tasks_surface_view));
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
() -> mActivityTestRule.getActivity().getLayoutManager().hideOverview(false)); () -> mActivityTestRule.getActivity().getLayoutManager().hideOverview(false));
...@@ -214,18 +207,10 @@ public class StartSurfaceTest { ...@@ -214,18 +207,10 @@ public class StartSurfaceTest {
} catch (ExecutionException e) { } catch (ExecutionException e) {
fail("Failed to tap 'more tabs' " + e.toString()); fail("Failed to tap 'more tabs' " + e.toString());
} }
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.secondary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.secondary_tasks_surface_view));
pressBack(); pressBack();
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.primary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.primary_tasks_surface_view));
OverviewModeBehaviorWatcher hideWatcher = OverviewModeBehaviorWatcher hideWatcher =
TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity()); TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity());
...@@ -274,18 +259,10 @@ public class StartSurfaceTest { ...@@ -274,18 +259,10 @@ public class StartSurfaceTest {
} catch (ExecutionException e) { } catch (ExecutionException e) {
fail("Failed to tap 'more tabs' " + e.toString()); fail("Failed to tap 'more tabs' " + e.toString());
} }
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.secondary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.secondary_tasks_surface_view));
pressBack(); pressBack();
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.primary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.primary_tasks_surface_view));
OverviewModeBehaviorWatcher hideWatcher = OverviewModeBehaviorWatcher hideWatcher =
TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity()); TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity());
...@@ -336,18 +313,10 @@ public class StartSurfaceTest { ...@@ -336,18 +313,10 @@ public class StartSurfaceTest {
} catch (ExecutionException e) { } catch (ExecutionException e) {
fail("Failed to tap 'more tabs' " + e.toString()); fail("Failed to tap 'more tabs' " + e.toString());
} }
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.secondary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.secondary_tasks_surface_view));
pressBack(); pressBack();
waitForView((ViewGroup) mActivityTestRule.getActivity() onView(isRoot()).check(waitForView(withId(R.id.primary_tasks_surface_view)));
.getWindow()
.getDecorView()
.getRootView(),
withId(R.id.primary_tasks_surface_view));
OverviewModeBehaviorWatcher hideWatcher = OverviewModeBehaviorWatcher hideWatcher =
TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity()); TabUiTestHelper.createOverviewHideWatcher(mActivityTestRule.getActivity());
......
...@@ -61,7 +61,6 @@ import android.support.test.espresso.matcher.BoundedMatcher; ...@@ -61,7 +61,6 @@ import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
...@@ -1782,10 +1781,7 @@ public class PasswordSettingsTest { ...@@ -1782,10 +1781,7 @@ public class PasswordSettingsTest {
setPasswordSource(null); setPasswordSource(null);
SettingsActivityTest.startSettingsActivity( SettingsActivityTest.startSettingsActivity(
InstrumentationRegistry.getInstrumentation(), PasswordSettings.class.getName()); InstrumentationRegistry.getInstrumentation(), PasswordSettings.class.getName());
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(waitForView(withText(R.string.password_settings_title)));
(root, e)
-> waitForView(
(ViewGroup) root, withText(R.string.password_settings_title)));
// Retrieve the initial status and ensure that the help option is there at all. // Retrieve the initial status and ensure that the help option is there at all.
final AtomicReference<Boolean> helpInOverflowMenu = new AtomicReference<>(false); final AtomicReference<Boolean> helpInOverflowMenu = new AtomicReference<>(false);
...@@ -1806,10 +1802,7 @@ public class PasswordSettingsTest { ...@@ -1806,10 +1802,7 @@ public class PasswordSettingsTest {
Espresso.onView(withSearchMenuIdOrText()).perform(click()); Espresso.onView(withSearchMenuIdOrText()).perform(click());
Espresso.onView(withContentDescription(R.string.abc_action_bar_up_description)) Espresso.onView(withContentDescription(R.string.abc_action_bar_up_description))
.perform(click()); .perform(click());
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(waitForView(withText(R.string.password_settings_title)));
(root, e)
-> waitForView(
(ViewGroup) root, withText(R.string.password_settings_title)));
// Check that the help option is exactly where it was to begin with. // Check that the help option is exactly where it was to begin with.
if (helpInOverflowMenu.get()) { if (helpInOverflowMenu.get()) {
...@@ -1962,12 +1955,9 @@ public class PasswordSettingsTest { ...@@ -1962,12 +1955,9 @@ public class PasswordSettingsTest {
Espresso.onView(withSearchMenuIdOrText()).perform(click()); Espresso.onView(withSearchMenuIdOrText()).perform(click());
Espresso.onView(withText(R.string.passwords_auto_signin_title)).check(doesNotExist()); Espresso.onView(withText(R.string.passwords_auto_signin_title)).check(doesNotExist());
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(waitForView(
(root, e) withParent(withContentDescription(R.string.abc_action_menu_overflow_description)),
-> waitForView((ViewGroup) root, VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
withParent(withContentDescription(
R.string.abc_action_menu_overflow_description)),
VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
Espresso.onView(withContentDescription(R.string.abc_action_bar_up_description)) Espresso.onView(withContentDescription(R.string.abc_action_bar_up_description))
.perform(click()); .perform(click());
...@@ -2018,9 +2008,7 @@ public class PasswordSettingsTest { ...@@ -2018,9 +2008,7 @@ public class PasswordSettingsTest {
// Trigger search which shouldn't have the button yet. // Trigger search which shouldn't have the button yet.
Espresso.onView(withSearchMenuIdOrText()).perform(click()); Espresso.onView(withSearchMenuIdOrText()).perform(click());
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(
(root, e) waitForView(withId(R.id.search_close_btn), VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
-> waitForView((ViewGroup) root, withId(R.id.search_close_btn),
VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
// Type something and see the button appear. // Type something and see the button appear.
Espresso.onView(withId(R.id.search_src_text)) Espresso.onView(withId(R.id.search_src_text))
...@@ -2031,9 +2019,7 @@ public class PasswordSettingsTest { ...@@ -2031,9 +2019,7 @@ public class PasswordSettingsTest {
// Clear the search which should hide the button again. // Clear the search which should hide the button again.
Espresso.onView(withId(R.id.search_close_btn)).perform(click()); // Clear search. Espresso.onView(withId(R.id.search_close_btn)).perform(click()); // Clear search.
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(
(root, e) waitForView(withId(R.id.search_close_btn), VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
-> waitForView((ViewGroup) root, withId(R.id.search_close_btn),
VIEW_INVISIBLE | VIEW_GONE | VIEW_NULL));
} }
/** /**
...@@ -2105,8 +2091,7 @@ public class PasswordSettingsTest { ...@@ -2105,8 +2091,7 @@ public class PasswordSettingsTest {
// Open the search and filter all but "Zeus". // Open the search and filter all but "Zeus".
Espresso.onView(withSearchMenuIdOrText()).perform(click()); Espresso.onView(withSearchMenuIdOrText()).perform(click());
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(waitForView(withId(R.id.search_src_text)));
(root, e) -> waitForView((ViewGroup) root, withId(R.id.search_src_text)));
Espresso.onView(withId(R.id.search_src_text)) Espresso.onView(withId(R.id.search_src_text))
.perform(click(), typeText("Zeu"), closeSoftKeyboard()); .perform(click(), typeText("Zeu"), closeSoftKeyboard());
InstrumentationRegistry.getInstrumentation().waitForIdleSync(); InstrumentationRegistry.getInstrumentation().waitForIdleSync();
...@@ -2144,9 +2129,7 @@ public class PasswordSettingsTest { ...@@ -2144,9 +2129,7 @@ public class PasswordSettingsTest {
// The search bar should still be open and still display the search query. // The search bar should still be open and still display the search query.
Espresso.onView(isRoot()).check( Espresso.onView(isRoot()).check(
(root, e) waitForView(allOf(withId(R.id.search_src_text), withText("Zeu"))));
-> waitForView((ViewGroup) root,
allOf(withId(R.id.search_src_text), withText("Zeu"))));
Espresso.onView(withId(R.id.search_src_text)).check(matches(withText("Zeu"))); Espresso.onView(withId(R.id.search_src_text)).check(matches(withText("Zeu")));
} }
......
...@@ -6,6 +6,9 @@ package org.chromium.chrome.test.util; ...@@ -6,6 +6,9 @@ package org.chromium.chrome.test.util;
import static java.lang.annotation.RetentionPolicy.SOURCE; import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.ViewInteraction;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -95,8 +98,7 @@ public class ViewUtils { ...@@ -95,8 +98,7 @@ public class ViewUtils {
/** /**
* Waits until a view matching the given matches any of the given {@link ExpectedViewState}s. * Waits until a view matching the given matches any of the given {@link ExpectedViewState}s.
* Fails if the matcher applies to multiple views. Times out if no view was found while waiting * Fails if the matcher applies to multiple views. Times out if no view was found while waiting
* up to * up to {@link CriteriaHelper#DEFAULT_MAX_TIME_TO_POLL} milliseconds.
* {@link CriteriaHelper#DEFAULT_MAX_TIME_TO_POLL} milliseconds.
* @param root The view group to search in. * @param root The view group to search in.
* @param viewMatcher The matcher matching the view that should be waited for. * @param viewMatcher The matcher matching the view that should be waited for.
* @param viewState State that the matching view should be in. If multiple states are passed, * @param viewState State that the matching view should be in. If multiple states are passed,
...@@ -107,6 +109,28 @@ public class ViewUtils { ...@@ -107,6 +109,28 @@ public class ViewUtils {
CriteriaHelper.pollUiThread(new ExpectedViewCriteria(viewMatcher, viewState, root)); CriteriaHelper.pollUiThread(new ExpectedViewCriteria(viewMatcher, viewState, root));
} }
/**
* Waits until a view matching the given matches any of the given {@link ExpectedViewState}s.
* Fails if the matcher applies to multiple views. Times out if no view was found while waiting
* up to {@link CriteriaHelper#DEFAULT_MAX_TIME_TO_POLL} milliseconds.
* This should be used on {@link ViewInteraction#check} with a {@link ViewGroup}. For example,
* the following usage assumes the root view is a {@link ViewGroup}.
* <pre>
* onView(isRoot()).check(waitForView(withId(R.id.example_id));
* </pre>
* @param viewMatcher The matcher matching the view that should be waited for.
* @param viewState State that the matching view should be in. If multiple states are passed,
* the waiting will stop if at least one applies.
*/
public static ViewAssertion waitForView(
Matcher<View> viewMatcher, @ExpectedViewState int viewState) {
return (View view, NoMatchingViewException noMatchException) -> {
if (noMatchException != null) throw noMatchException;
CriteriaHelper.pollUiThread(
new ExpectedViewCriteria(viewMatcher, viewState, (ViewGroup) view));
};
}
/** /**
* Waits until a visible view matching the given matcher appears. Fails if the matcher applies * Waits until a visible view matching the given matcher appears. Fails if the matcher applies
* to multiple views. Times out if no view was found while waiting up to * to multiple views. Times out if no view was found while waiting up to
...@@ -118,6 +142,16 @@ public class ViewUtils { ...@@ -118,6 +142,16 @@ public class ViewUtils {
waitForView(root, viewMatcher, VIEW_VISIBLE); waitForView(root, viewMatcher, VIEW_VISIBLE);
} }
/**
* Waits until a visible view matching the given matcher appears. Fails if the matcher applies
* to multiple views. Times out if no view was found while waiting up to
* {@link CriteriaHelper#DEFAULT_MAX_TIME_TO_POLL} milliseconds.
* @param viewMatcher The matcher matching the view that should be waited for.
*/
public static ViewAssertion waitForView(Matcher<View> viewMatcher) {
return waitForView(viewMatcher, VIEW_VISIBLE);
}
/** /**
* Wait until the specified view has finished layout updates. * Wait until the specified view has finished layout updates.
* @param view The specified view. * @param view The specified view.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment