Commit 68425b08 authored by Michele Mancina's avatar Michele Mancina Committed by Commit Bot

[Autofill Assistant] Fix broken test with retry mechanism

Change-Id: Ia351a4496c5e782c283e2aea08e6d20393979391
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2297562
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: default avatarSandro Maggi <sandromaggi@google.com>
Cr-Commit-Position: refs/heads/master@{#790383}
parent 798f51c9
......@@ -33,10 +33,12 @@ import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.getElementValue;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.isNextAfterSibling;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.startAutofillAssistant;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.waitUntilViewInRootMatchesCondition;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.waitUntilViewMatchesCondition;
import android.support.test.InstrumentationRegistry;
......@@ -857,6 +859,20 @@ public class AutofillAssistantPersonalDataManagerTest {
onView(withContentDescription("ZIP code*")).perform(scrollTo(), typeText("1234"));
onView(withContentDescription("Phone*")).perform(scrollTo(), typeText("8008080808"));
onView(withText("Done")).perform(scrollTo(), click());
addCreditCardAndSelectAddress();
int tryNumber = 0;
int maxRetries = 3;
while (!hasAddress() && tryNumber++ < maxRetries) {
// If the new address is not yet present, we first need to close the popup dialog.
Espresso.pressBack();
onView(withText("Cancel")).perform(scrollTo(), click());
addCreditCardAndSelectAddress();
}
assertThat(tryNumber, lessThan(maxRetries));
}
private void addCreditCardAndSelectAddress() {
waitUntilViewMatchesCondition(
allOf(withId(R.id.section_title_add_button_label), withText("Add card")),
isCompletelyDisplayed());
......@@ -867,11 +883,16 @@ public class AutofillAssistantPersonalDataManagerTest {
Espresso.closeSoftKeyboard();
onView(allOf(withId(org.chromium.chrome.R.id.spinner), withChild(withText("Select"))))
.perform(scrollTo(), click());
onData(anything())
.atPosition(1 /* address of John, 0 is SELECT (empty) */)
.inRoot(withDecorView(withClassName(containsString("Popup"))))
.perform(click());
waitUntilViewMatchesCondition(withText(containsString("John Doe")), isDisplayed());
}
private boolean hasAddress() {
try {
waitUntilViewInRootMatchesCondition(withText(containsString("John Doe")),
withDecorView(withClassName(containsString("Popup"))), isDisplayed());
return true;
} catch (AssertionError e) {
return false;
}
}
private void runScript(AutofillAssistantTestScript script) {
......
......@@ -31,6 +31,7 @@ import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.test.espresso.NoMatchingViewException;
import androidx.test.espresso.Root;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewAssertion;
......@@ -397,6 +398,24 @@ class AutofillAssistantUiTestUtil {
waitUntilViewMatchesCondition(matcher, condition, DEFAULT_MAX_TIME_TO_POLL);
}
/**
* Same as {@link #waitUntilViewMatchesCondition(Matcher, Matcher)} but also uses {@code
* rootMatcher} to select the correct window.
*/
public static void waitUntilViewInRootMatchesCondition(
Matcher<View> matcher, Matcher<Root> rootMatcher, Matcher<View> condition) {
CriteriaHelper.pollInstrumentationThread(() -> {
try {
onView(matcher).inRoot(rootMatcher).check(matches(condition));
} catch (NoMatchingViewException | AssertionError e) {
// Note: all other exceptions are let through, in particular
// AmbiguousViewMatcherException.
throw new CriteriaNotSatisfiedException(
"Timeout while waiting for " + matcher + " to satisfy " + condition);
}
}, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLING_INTERVAL);
}
/** @see {@link #waitUntilViewMatchesCondition(Matcher, Matcher)} */
public static void waitUntilViewMatchesCondition(
Matcher<View> matcher, Matcher<View> condition, long maxTimeoutMs) {
......
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