Commit 2e4e5b4f authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Add support for Runnables in CriteriaHelper.

If a Runnable throws an AssertionError, it will be re-attempted.  This
should allow for using assertions directly inside of the runnable
with causes attached w/o the need for using Criteria/error conditions.

BUG=1071247

Change-Id: I6a0f2a5625f3de5b18c49d59b4d1b8b87f5acf0e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2151483
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759920}
parent ffe64b3d
......@@ -30,6 +30,7 @@ import androidx.preference.PreferenceScreen;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
......@@ -60,7 +61,6 @@ import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
/**
......@@ -443,27 +443,16 @@ public class ClearBrowsingDataFragmentTest {
*/
private void waitForImportantDialogToShow(
final ClearBrowsingDataFragment preferences, final int numImportantSites) {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
Assert.assertNotNull(preferences);
if (preferences.getImportantSitesDialogFragment() == null
|| !preferences.getImportantSitesDialogFragment().getDialog().isShowing()) {
updateFailureReason("Dialog was null or not shown.");
return false;
}
ListView sitesList = preferences.getImportantSitesDialogFragment().getSitesList();
if (sitesList.getAdapter().getCount() != numImportantSites) {
updateFailureReason(
String.format(Locale.US, "Adapter item count, %d, did not match %d",
sitesList.getAdapter().getCount(), numImportantSites));
return false;
}
updateFailureReason(
String.format(Locale.US, "ListView child count, %d, expected to be >= %d",
sitesList.getChildCount(), numImportantSites));
return sitesList.getChildCount() >= numImportantSites;
}
CriteriaHelper.pollUiThread(() -> {
Assert.assertNotNull(preferences);
Assert.assertNotNull(preferences.getImportantSitesDialogFragment());
Assert.assertTrue(
preferences.getImportantSitesDialogFragment().getDialog().isShowing());
ListView sitesList = preferences.getImportantSitesDialogFragment().getSitesList();
Assert.assertEquals(numImportantSites, sitesList.getAdapter().getCount());
Assert.assertThat(
sitesList.getChildCount(), Matchers.greaterThanOrEqualTo(numImportantSites));
});
}
......
......@@ -55,7 +55,11 @@ public class ChromeSmokeTest {
IUi2Locator locatorChrome = Ui2Locators.withPackageName(mPackageName);
CriteriaHelper.pollInstrumentationThread(() -> {
return locatorChrome.locateOne(device) != null;
try {
return locatorChrome.locateOne(device) != null;
} catch (NullPointerException e) {
return false; // Throws an NPE on older Android versions.
}
}, mPackageName + " should have loaded", TIMEOUT_MS, UI_CHECK_INTERVAL);
}
}
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