Commit c809a8b3 authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Remove AssertionError support from CriteriaHelper.

Criteria.checkThat/CriteriaNotSatisfiedException is the only allowed
path in CriteriaHelper going forward.

BUG=1071247

Change-Id: Idde0bd13ce78689b57422e0fc8bdadde57cc2276
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2288091Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786531}
parent 777ba77d
......@@ -13,6 +13,7 @@ import android.widget.TextView;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SmallTest;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
......@@ -31,6 +32,7 @@ import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.NavigationEntry;
import org.chromium.content_public.browser.NavigationHistory;
import org.chromium.content_public.browser.test.mock.MockNavigationController;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -99,8 +101,8 @@ public class NavigationPopupTest {
CriteriaHelper.pollUiThread(() -> {
NavigationHistory history = controller.mHistory;
for (int i = 0; i < history.getEntryCount(); i++) {
Assert.assertNotNull(
"Favicon[" + i + "] not updated", history.getEntryAtIndex(i).getFavicon());
Criteria.checkThat("Favicon[" + i + "] not updated",
history.getEntryAtIndex(i).getFavicon(), Matchers.notNullValue());
}
});
......
......@@ -1605,18 +1605,18 @@ public class BookmarkTest {
}
private void waitForEditActivity() {
CriteriaHelper.pollUiThread(
()
-> Assert.assertThat(ApplicationStatus.getLastTrackedFocusedActivity(),
IsInstanceOf.instanceOf(BookmarkEditActivity.class)));
CriteriaHelper.pollUiThread(() -> {
Criteria.checkThat(ApplicationStatus.getLastTrackedFocusedActivity(),
IsInstanceOf.instanceOf(BookmarkEditActivity.class));
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
}
private ChromeTabbedActivity waitForTabbedActivity() {
CriteriaHelper.pollUiThread(
()
-> Assert.assertThat(ApplicationStatus.getLastTrackedFocusedActivity(),
IsInstanceOf.instanceOf(ChromeTabbedActivity.class)));
CriteriaHelper.pollUiThread(() -> {
Criteria.checkThat(ApplicationStatus.getLastTrackedFocusedActivity(),
IsInstanceOf.instanceOf(ChromeTabbedActivity.class));
});
return (ChromeTabbedActivity) ApplicationStatus.getLastTrackedFocusedActivity();
}
}
......@@ -9,7 +9,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.chromium.chrome.browser.browserservices.TrustedWebActivityTestUtil.createSession;
......@@ -145,7 +144,7 @@ public class RunningInChromeTest {
launch(createTrustedWebActivityIntent(mTestPage));
String scope = Origin.createOrThrow(mTestPage).toString();
CriteriaHelper.pollUiThread(() -> assertTrue(showingNotification(scope)));
CriteriaHelper.pollUiThread(() -> showingNotification(scope));
}
@Test
......@@ -156,13 +155,11 @@ public class RunningInChromeTest {
launch(createTrustedWebActivityIntent(mTestPage));
String scope = Origin.createOrThrow(mTestPage).toString();
CriteriaHelper.pollUiThread(() ->
assertTrue(showingNotification(scope)));
CriteriaHelper.pollUiThread(() -> showingNotification(scope));
mCustomTabActivityTestRule.loadUrl("https://www.example.com/");
CriteriaHelper.pollUiThread(() ->
assertFalse(showingNotification(scope)));
CriteriaHelper.pollUiThread(() -> !showingNotification(scope));
}
@Test
......@@ -173,11 +170,11 @@ public class RunningInChromeTest {
launch(createTrustedWebActivityIntent(mTestPage));
String scope = Origin.createOrThrow(mTestPage).toString();
CriteriaHelper.pollUiThread(() -> assertTrue(showingNotification(scope)));
CriteriaHelper.pollUiThread(() -> showingNotification(scope));
mCustomTabActivityTestRule.getActivity().finish();
CriteriaHelper.pollUiThread(() -> assertFalse(showingNotification(scope)));
CriteriaHelper.pollUiThread(() -> !showingNotification(scope));
}
private boolean showingNotification(String tag) {
......
......@@ -138,7 +138,7 @@ public class ReaderModeTest implements CustomMainActivityStart {
});
CustomTabActivity customTabActivity = waitForCustomTabActivity();
CriteriaHelper.pollUiThread(
() -> Assert.assertThat(customTabActivity.getActivityTab(), notNullValue()));
() -> Criteria.checkThat(customTabActivity.getActivityTab(), notNullValue()));
@NonNull
Tab distillerViewerTab = Objects.requireNonNull(customTabActivity.getActivityTab());
waitForDistillation(TITLE, distillerViewerTab);
......@@ -161,7 +161,7 @@ public class ReaderModeTest implements CustomMainActivityStart {
});
CustomTabActivity customTabActivity = waitForCustomTabActivity();
CriteriaHelper.pollUiThread(
() -> Assert.assertThat(customTabActivity.getActivityTab(), notNullValue()));
() -> Criteria.checkThat(customTabActivity.getActivityTab(), notNullValue()));
@NonNull
Tab distillerViewerTab = Objects.requireNonNull(customTabActivity.getActivityTab());
waitForDistillation(TITLE, distillerViewerTab);
......@@ -187,7 +187,7 @@ public class ReaderModeTest implements CustomMainActivityStart {
});
CustomTabActivity customTabActivity = waitForCustomTabActivity();
CriteriaHelper.pollUiThread(
() -> Assert.assertThat(customTabActivity.getActivityTab(), notNullValue()));
() -> Criteria.checkThat(customTabActivity.getActivityTab(), notNullValue()));
@NonNull
Tab distillerViewerTab = Objects.requireNonNull(customTabActivity.getActivityTab());
waitForDistillation(TITLE, distillerViewerTab);
......
......@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.incognito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.os.Build;
......@@ -13,6 +12,7 @@ import android.support.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
......@@ -39,6 +39,7 @@ import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -91,13 +92,15 @@ public class IncognitoCookieLeakageTest {
}
private void setCookies(Tab tab) throws TimeoutException {
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab.getWebContents(), Matchers.notNullValue()));
JavaScriptUtils.executeJavaScriptAndWaitForResult(tab.getWebContents(), "setCookie()");
assertCookies(tab, "\"Foo=Bar\"");
}
private void assertCookies(Tab tab, String expected) throws TimeoutException {
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab.getWebContents(), Matchers.notNullValue()));
String actual = JavaScriptUtils.executeJavaScriptAndWaitForResult(
tab.getWebContents(), "getCookie()");
if (actual.equalsIgnoreCase("null")) actual = "\"\"";
......
......@@ -5,12 +5,13 @@
package org.chromium.chrome.browser.incognito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import android.app.Activity;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import org.hamcrest.Matchers;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.test.params.ParameterProvider;
import org.chromium.base.test.params.ParameterSet;
......@@ -24,6 +25,7 @@ import org.chromium.chrome.browser.customtabs.CustomTabsTestUtils;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -145,7 +147,8 @@ public class IncognitoDataTestUtils {
Tab tab = testRule.loadUrlInNewTab(url, incognito);
// Giving time to the WebContents to be ready.
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab.getWebContents(), Matchers.notNullValue()));
assertEquals(incognito, tab.getWebContents().isIncognito());
return tab;
......@@ -165,7 +168,8 @@ public class IncognitoDataTestUtils {
Tab tab = testRule.getActivity().getActivityTab();
// Giving time to the WebContents to be ready.
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab.getWebContents(), Matchers.notNullValue()));
assertEquals(incognito, tab.getWebContents().isIncognito());
return tab;
......
......@@ -5,13 +5,13 @@
package org.chromium.chrome.browser.incognito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.support.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
......@@ -43,6 +43,7 @@ import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.content_public.browser.NavigationEntry;
import org.chromium.content_public.browser.NavigationHistory;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
......@@ -179,11 +180,13 @@ public class IncognitoHistoryLeakageTest {
Tab tab2 = activity2.launchUrl(
mChromeActivityTestRule, mCustomTabActivityTestRule, mTestPage2);
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab1.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab1.getWebContents(), Matchers.notNullValue()));
NavigationHistory navigationHistory1 =
tab1.getWebContents().getNavigationController().getNavigationHistory();
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab2.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab2.getWebContents(), Matchers.notNullValue()));
NavigationHistory navigationHistory2 =
tab2.getWebContents().getNavigationController().getNavigationHistory();
......
......@@ -11,7 +11,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.support.test.InstrumentationRegistry;
......@@ -20,6 +19,7 @@ import androidx.test.espresso.Espresso;
import androidx.test.espresso.NoMatchingViewException;
import androidx.test.filters.LargeTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
......@@ -47,6 +47,7 @@ import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.chrome.test.util.browser.LocationSettingsTestUtil;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -106,7 +107,8 @@ public class IncognitoPermissionLeakageTest {
}
private void requestLocationPermission(Tab tab) throws TimeoutException {
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab.getWebContents(), Matchers.notNullValue()));
JavaScriptUtils.executeJavaScriptAndWaitForResult(
tab.getWebContents(), "initiate_getCurrentPosition()");
}
......
......@@ -5,13 +5,13 @@
package org.chromium.chrome.browser.incognito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.support.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
......@@ -35,6 +35,7 @@ import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.JavaScriptUtils;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
......@@ -100,7 +101,8 @@ public class IncognitoStorageLeakageTest {
Tab tab1 = activity1.launchUrl(
mChromeActivityTestRule, mCustomTabActivityTestRule, mSiteDataTestPage);
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab1.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab1.getWebContents(), Matchers.notNullValue()));
// Sets the session storage in tab1
assertEquals("true",
......@@ -114,7 +116,8 @@ public class IncognitoStorageLeakageTest {
Tab tab2 = activity2.launchUrl(
mChromeActivityTestRule, mCustomTabActivityTestRule, mSiteDataTestPage);
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab2.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab2.getWebContents(), Matchers.notNullValue()));
// Checks the session storage in tab2. Session storage set in tab1 should not be accessible.
// The session storage is per tab basis.
......@@ -152,7 +155,8 @@ public class IncognitoStorageLeakageTest {
expected = "true";
}
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab1.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab1.getWebContents(), Matchers.notNullValue()));
// Set the storage in tab1
assertEquals("true",
JavaScriptUtils.runJavascriptWithAsyncResult(
......@@ -163,7 +167,8 @@ public class IncognitoStorageLeakageTest {
JavaScriptUtils.runJavascriptWithAsyncResult(
tab1.getWebContents(), "has" + type + "()"));
CriteriaHelper.pollUiThread(() -> { assertNotNull(tab2.getWebContents()); });
CriteriaHelper.pollUiThread(
() -> Criteria.checkThat(tab2.getWebContents(), Matchers.notNullValue()));
// Access the storage from tab2
assertEquals(expected,
JavaScriptUtils.runJavascriptWithAsyncResult(
......
......@@ -216,8 +216,9 @@ public class LocationBarLayoutTest {
setUrlBarTextAndFocus("testing");
Assert.assertEquals(getDeleteButton().getVisibility(), VISIBLE);
ClickUtils.clickButton(getDeleteButton());
CriteriaHelper.pollUiThread(
() -> Assert.assertThat(getDeleteButton().getVisibility(), Matchers.not(VISIBLE)));
CriteriaHelper.pollUiThread(() -> {
Criteria.checkThat(getDeleteButton().getVisibility(), Matchers.not(VISIBLE));
});
Assert.assertEquals("", getUrlText(getUrlBar()));
}
......
......@@ -120,7 +120,7 @@ public class AutofillTest {
mSyncTestRule.pollInstrumentationThread(() -> {
try {
Autofill modifiedAutofill = getClientAutofillProfiles().get(0);
Assert.assertEquals(MODIFIED_CITY, modifiedAutofill.city);
Criteria.checkThat(modifiedAutofill.city, Matchers.is(MODIFIED_CITY));
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
......
......@@ -125,7 +125,7 @@ public class BookmarksTest {
SyncTestUtil.triggerSync();
mSyncTestRule.pollInstrumentationThread(() -> {
Bookmark modifiedBookmark = getClientBookmarks().get(0);
Assert.assertEquals(MODIFIED_TITLE, modifiedBookmark.title);
Criteria.checkThat(modifiedBookmark.title, Matchers.is(MODIFIED_TITLE));
});
}
......@@ -185,7 +185,7 @@ public class BookmarksTest {
List<Bookmark> bookmarks = getClientBookmarks();
Bookmark modifiedBookmark = bookmarks.get(bookmarks.get(0).isFolder() ? 1 : 0);
// The "s" is prepended because the server adds one to the parentId.
Assert.assertEquals("s" + folder.id, modifiedBookmark.parentId);
Criteria.checkThat(modifiedBookmark.parentId, Matchers.is("s" + folder.id));
});
}
......@@ -224,8 +224,8 @@ public class BookmarksTest {
mSyncTestRule.pollInstrumentationThread(() -> {
Bookmark modifiedFolder = getClientBookmarks().get(0);
Assert.assertTrue(modifiedFolder.isFolder());
Assert.assertEquals(MODIFIED_TITLE, modifiedFolder.title);
Criteria.checkThat(modifiedFolder.isFolder(), Matchers.is(true));
Criteria.checkThat(modifiedFolder.title, Matchers.is(MODIFIED_TITLE));
});
}
......@@ -317,7 +317,7 @@ public class BookmarksTest {
List<Bookmark> serverBookmarks = getServerBookmarks();
Bookmark modifiedBookmark =
serverBookmarks.get(serverBookmarks.get(0).isFolder() ? 1 : 0);
Assert.assertEquals(folder.id, modifiedBookmark.parentId);
Criteria.checkThat(modifiedBookmark.parentId, Matchers.is(folder.id));
});
}
......
......@@ -281,10 +281,10 @@ public class OpenTabsTest {
int size =
SyncTestUtil.getLocalData(mSyncTestRule.getTargetContext(), OPEN_TABS_TYPE)
.size();
Assert.assertThat("Expected at least one tab entity to exist.", size,
Criteria.checkThat("Expected at least one tab entity to exist.", size,
Matchers.greaterThan(0));
} catch (JSONException ex) {
Assert.fail(ex.toString());
throw new CriteriaNotSatisfiedException(ex);
}
});
List<Pair<String, JSONObject>> tabEntities =
......
......@@ -73,16 +73,12 @@ public class CriteriaHelper {
*/
public static void pollInstrumentationThread(
Runnable criteria, long maxTimeoutMs, long checkIntervalMs) {
Throwable throwable;
CriteriaNotSatisfiedException throwable;
try {
criteria.run();
return;
} catch (CriteriaNotSatisfiedException cnse) {
throwable = cnse;
} catch (AssertionError ae) {
// TODO(tedchoc): Remove support for this once all clients move over to
// CriteriaNotSatisfiedException.
throwable = ae;
}
TimeoutTimer timer = new TimeoutTimer(maxTimeoutMs);
while (!timer.isTimedOut()) {
......@@ -97,19 +93,9 @@ public class CriteriaHelper {
return;
} catch (CriteriaNotSatisfiedException cnse) {
throwable = cnse;
} catch (AssertionError ae) {
// TODO(tedchoc): Remove support for this once all clients move over to
// CriteriaNotSatisfiedException.
throwable = ae;
}
}
if (throwable instanceof CriteriaNotSatisfiedException) {
throw new AssertionError(throwable);
} else if (throwable instanceof AssertionError) {
throw(AssertionError) throwable;
}
assert false : "Invalid throwable";
throw new RuntimeException(throwable);
throw new AssertionError(throwable);
}
/**
......@@ -220,10 +206,6 @@ public class CriteriaHelper {
if (throwable != null) {
if (throwable instanceof CriteriaNotSatisfiedException) {
throw new CriteriaNotSatisfiedException(throwable);
} else if (throwable instanceof AssertionError) {
// TODO(tedchoc): Remove support for this once all clients move over to
// CriteriaNotSatisfiedException.
throw new CriteriaNotSatisfiedException(throwable);
} else if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
} else {
......
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