[go: nahoru, domu]

Android: Use a custom Exception for CriteriaHelper timeouts

Public Transit exception messages are very long, so my motivation here
was to not reuse the exception message from the caused-by exception,
since then the super long message is printed twice.

I changed the exception type while at it since AssertionError didn't
seem appropriate.

E.g.: https://ci.chromium.org/ui/p/chromium/builders/try/android-x64-rel/98410/overview

Bug: None
Change-Id: Id946097d092319c10ac4e7b82d165e0eb8d2e0bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5637150
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Owners-Override: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1316587}
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsGarbageCollectionTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsGarbageCollectionTest.java
index a54be72..541fdbb 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsGarbageCollectionTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsGarbageCollectionTest.java
@@ -444,7 +444,7 @@
                 CriteriaHelper.pollInstrumentationThread(
                         criteria, timeoutBetweenGcMs, CHECK_INTERVAL);
                 break;
-            } catch (AssertionError e) {
+            } catch (CriteriaHelper.TimeoutException e) {
                 Runtime.getRuntime().gc();
             }
         }
diff --git a/base/test/android/javatests/src/org/chromium/base/test/util/CriteriaHelper.java b/base/test/android/javatests/src/org/chromium/base/test/util/CriteriaHelper.java
index 1677bf8..b75de8f 100644
--- a/base/test/android/javatests/src/org/chromium/base/test/util/CriteriaHelper.java
+++ b/base/test/android/javatests/src/org/chromium/base/test/util/CriteriaHelper.java
@@ -56,6 +56,13 @@
  * </pre>
  */
 public class CriteriaHelper {
+    /** Exception thrown for timeouts. */
+    public static class TimeoutException extends RuntimeException {
+        private TimeoutException(String message, Throwable causedBy) {
+            super(message, causedBy);
+        }
+    }
+
     /** The default maximum time to wait for a criteria to become valid. */
     public static final long DEFAULT_MAX_TIME_TO_POLL = 3000L;
 
@@ -120,7 +127,7 @@
                 }
             }
         }
-        throw new AssertionError(throwable);
+        throw new TimeoutException("Timed out after " + maxTimeoutMs + " milliseconds", throwable);
     }
 
     private static void sleepThread(long checkIntervalMs) {
@@ -269,8 +276,8 @@
                             });
                     Throwable throwable = throwableRef.get();
                     if (throwable != null) {
-                        if (throwable instanceof CriteriaNotSatisfiedException) {
-                            throw new CriteriaNotSatisfiedException(throwable);
+                        if (throwable instanceof Error) {
+                            throw (Error) throwable;
                         } else if (throwable instanceof RuntimeException) {
                             throw (RuntimeException) throwable;
                         } else {
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/feedback/ConnectivityTaskTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/feedback/ConnectivityTaskTest.java
index 8d96e8d..9186b470 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/feedback/ConnectivityTaskTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/feedback/ConnectivityTaskTest.java
@@ -175,7 +175,6 @@
     @Test
     @MediumTest
     @Feature({"Feedback"})
-    @SuppressWarnings("TryFailThrowable") // TODO(tedchoc): Remove after fixing timeout.
     public void testTwoTimeoutsShouldFillInTheRest() {
         final ConnectivityTask task =
                 TestThreadUtils.runOnUiThreadBlockingNoException(
@@ -194,16 +193,13 @@
                                         null);
                             }
                         });
-        thrown.expect(AssertionError.class);
+        thrown.expect(CriteriaHelper.TimeoutException.class);
         CriteriaHelper.pollUiThread(
                 () -> {
                     return task.isDone();
                 },
                 TIMEOUT_MS / 5,
                 RESULT_CHECK_INTERVAL_MS);
-        FeedbackData feedback = getResult(task);
-        verifyConnections(feedback, ConnectivityCheckResult.UNKNOWN);
-        Assert.assertEquals("The timeout value is wrong.", TIMEOUT_MS, feedback.getTimeoutMs());
     }
 
     @Test
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java
index a568dff..f083bfa 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/gsa/GSAAccountChangeListenerTest.java
@@ -13,6 +13,7 @@
 import androidx.test.filters.SmallTest;
 
 import org.hamcrest.Matchers;
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -27,7 +28,7 @@
     private static final String ACCOUNT_NAME2 = "you@gmail.com";
     private static final String PERMISSION = "permission.you.dont.have";
 
-    @Test(expected = AssertionError.class)
+    @Test
     @SmallTest
     public void testReceivesBroadcastIntents() {
         final Context context = ApplicationProvider.getApplicationContext();
@@ -60,12 +61,16 @@
         context.sendBroadcast(intent, "permission.you.dont.have");
 
         // This is ugly, but so is checking that some asynchronous call was never received.
-        CriteriaHelper.pollUiThread(
+        Assert.assertThrows(
+                CriteriaHelper.TimeoutException.class,
                 () -> {
-                    String currentAccount = GSAState.getInstance().getGsaAccount();
-                    Criteria.checkThat(currentAccount, Matchers.is(ACCOUNT_NAME2));
-                },
-                1000,
-                100);
+                    CriteriaHelper.pollUiThread(
+                            () -> {
+                                String currentAccount = GSAState.getInstance().getGsaAccount();
+                                Criteria.checkThat(currentAccount, Matchers.is(ACCOUNT_NAME2));
+                            },
+                            1000,
+                            100);
+                });
     }
 }
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelperTest.java b/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelperTest.java
index 9ea6e88..edc2450 100644
--- a/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelperTest.java
+++ b/content/public/android/javatests/src/org/chromium/content/browser/CriteriaHelperTest.java
@@ -4,9 +4,6 @@
 
 package org.chromium.content.browser;
 
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.junit.Assert.assertThat;
-
 import static org.chromium.base.test.util.CriteriaHelper.DEFAULT_POLLING_INTERVAL;
 
 import androidx.test.annotation.UiThreadTest;
@@ -14,9 +11,7 @@
 
 import org.hamcrest.Matchers;
 import org.junit.Assert;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 
 import org.chromium.base.ThreadUtils;
@@ -24,22 +19,28 @@
 import org.chromium.base.test.util.Batch;
 import org.chromium.base.test.util.Criteria;
 import org.chromium.base.test.util.CriteriaHelper;
+import org.chromium.base.test.util.CriteriaHelper.TimeoutException;
 import org.chromium.base.test.util.CriteriaNotSatisfiedException;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
+import java.util.concurrent.Callable;
 
 /** Tests for {@link CriteriaHelper}. */
 @RunWith(BaseJUnit4ClassRunner.class)
 @Batch(Batch.UNIT_TESTS)
 public class CriteriaHelperTest {
     private static final String ERROR_MESSAGE = "my special error message";
+    private static final String OUTER_ERROR_MESSAGE = "Timed out after 0 milliseconds";
 
-    @Rule public ExpectedException thrown = ExpectedException.none();
+    private static final Runnable NEVER_SATISFIED_RUNNABLE =
+            () -> {
+                throw new CriteriaNotSatisfiedException(ERROR_MESSAGE);
+            };
+    private static final Callable FALSE_CALLABLE = () -> false;
 
     @Test
     @MediumTest
     public void testUiThread() {
+        // Also tests Criteria.checkThat().
         CriteriaHelper.pollUiThread(
                 () -> Criteria.checkThat(ThreadUtils.runningOnUiThread(), Matchers.is(true)));
     }
@@ -61,257 +62,115 @@
 
     @Test
     @MediumTest
-    public void testPass_Runnable_UiThread() {
-        CriteriaHelper.pollUiThread(() -> {});
+    public void testUiThread_Callable() {
+        CriteriaHelper.pollUiThread(
+                () -> {
+                    Assert.assertTrue(ThreadUtils.runningOnUiThread());
+                    return true;
+                });
     }
 
     @Test
     @MediumTest
     @UiThreadTest
-    public void testPass_Runnable_UiThreadNested() {
-        CriteriaHelper.pollUiThreadNested(() -> {});
+    public void testUiThreadNestedCallable() {
+        CriteriaHelper.pollUiThreadNested(
+                () -> {
+                    Assert.assertTrue(ThreadUtils.runningOnUiThread());
+                    return true;
+                });
     }
 
     @Test
     @MediumTest
-    public void testPass_Runnable_InstrumentationThread() {
-        CriteriaHelper.pollInstrumentationThread(() -> {});
-    }
-
-    @Test
-    @MediumTest
-    public void testPass_Callable_UiThread() {
-        CriteriaHelper.pollUiThread(() -> true);
-    }
-
-    @Test
-    @MediumTest
-    @UiThreadTest
-    public void testPass_Callable_UiThreadNested() {
-        CriteriaHelper.pollUiThreadNested(() -> true);
-    }
-
-    @Test
-    @MediumTest
-    public void testPass_Callable_InstrumentationThread() {
-        CriteriaHelper.pollInstrumentationThread(() -> true);
+    public void testInstrumentationThread_Callable() {
+        CriteriaHelper.pollInstrumentationThread(
+                () -> {
+                    Assert.assertFalse(ThreadUtils.runningOnUiThread());
+                    return true;
+                });
     }
 
     @Test
     @MediumTest
     public void testThrow_Runnable_UiThread() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollUiThread(
-                () -> {
-                    throw new CriteriaNotSatisfiedException("");
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollUiThread(
+                                    NEVER_SATISFIED_RUNNABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
+        Assert.assertEquals(ERROR_MESSAGE, t.getCause().getMessage());
     }
 
     @Test
     @MediumTest
     @UiThreadTest
     public void testThrow_Runnable_UiThreadNested() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollUiThreadNested(
-                () -> {
-                    throw new CriteriaNotSatisfiedException("");
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollUiThreadNested(
+                                    NEVER_SATISFIED_RUNNABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
+        Assert.assertEquals(ERROR_MESSAGE, t.getCause().getMessage());
     }
 
     @Test
     @MediumTest
     public void testThrow_Runnable_InstrumentationThread() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollInstrumentationThread(
-                () -> {
-                    throw new CriteriaNotSatisfiedException("");
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollInstrumentationThread(
+                                    NEVER_SATISFIED_RUNNABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
+        Assert.assertEquals(ERROR_MESSAGE, t.getCause().getMessage());
     }
 
     @Test
     @MediumTest
     public void testThrow_Callable_UiThread() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollUiThread(() -> false, 0, DEFAULT_POLLING_INTERVAL);
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollUiThread(
+                                    FALSE_CALLABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
     }
 
     @Test
     @MediumTest
     @UiThreadTest
     public void testThrow_Callable_UiThreadNested() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollUiThreadNested(() -> false, 0, DEFAULT_POLLING_INTERVAL);
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollUiThreadNested(
+                                    FALSE_CALLABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
     }
 
     @Test
     @MediumTest
     public void testThrow_Callable_InstrumentationThread() {
-        thrown.expect(AssertionError.class);
-        CriteriaHelper.pollInstrumentationThread(() -> false, 0, DEFAULT_POLLING_INTERVAL);
-    }
-
-    @Test
-    @MediumTest
-    public void testMessage_Runnable_UiThread() {
-        thrown.expectMessage(ERROR_MESSAGE);
-        CriteriaHelper.pollUiThread(
-                () -> {
-                    throw new CriteriaNotSatisfiedException(ERROR_MESSAGE);
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
-    }
-
-    @Test
-    @MediumTest
-    @UiThreadTest
-    public void testMessage_Runnable_UiThreadNested() {
-        thrown.expectMessage(ERROR_MESSAGE);
-        CriteriaHelper.pollUiThreadNested(
-                () -> {
-                    throw new CriteriaNotSatisfiedException(ERROR_MESSAGE);
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
-    }
-
-    @Test
-    @MediumTest
-    public void testMessage_Runnable_InstrumentationThread() {
-        thrown.expectMessage(ERROR_MESSAGE);
-        CriteriaHelper.pollInstrumentationThread(
-                () -> {
-                    throw new CriteriaNotSatisfiedException(ERROR_MESSAGE);
-                },
-                0,
-                DEFAULT_POLLING_INTERVAL);
-    }
-
-    @Test
-    @MediumTest
-    public void testMessage_Callback_UiThread() {
-        thrown.expectMessage(ERROR_MESSAGE);
-        CriteriaHelper.pollUiThread(() -> false, ERROR_MESSAGE, 0, DEFAULT_POLLING_INTERVAL);
-    }
-
-    @Test
-    @MediumTest
-    public void testMessage_Callback_InstrumentationThread() {
-        thrown.expectMessage(ERROR_MESSAGE);
-        CriteriaHelper.pollInstrumentationThread(
-                () -> false, ERROR_MESSAGE, 0, DEFAULT_POLLING_INTERVAL);
-    }
-
-    private String getStackTrace(Throwable e) {
-        StringWriter sw = new StringWriter();
-        e.printStackTrace(new PrintWriter(sw));
-        return sw.toString();
-    }
-
-    @Test
-    @MediumTest
-    public void testStack_Runnable_UiThread() {
-        try {
-            CriteriaHelper.pollUiThread(
-                    () -> {
-                        throw new CriteriaNotSatisfiedException("test");
-                    },
-                    0,
-                    DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Runnable_UiThread("));
-            return;
-        }
-        Assert.fail();
-    }
-
-    @Test
-    @MediumTest
-    @UiThreadTest
-    public void testStack_Runnable_UiThreadNested() {
-        try {
-            CriteriaHelper.pollUiThreadNested(
-                    () -> {
-                        throw new CriteriaNotSatisfiedException("test");
-                    },
-                    0,
-                    DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Runnable_UiThreadNested("));
-            return;
-        }
-        Assert.fail();
-    }
-
-    @Test
-    @MediumTest
-    public void testStack_Runnable_InstrumentationThread() {
-        try {
-            CriteriaHelper.pollInstrumentationThread(
-                    () -> {
-                        throw new CriteriaNotSatisfiedException("test");
-                    },
-                    0,
-                    DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Runnable_InstrumentationThread("));
-            return;
-        }
-        Assert.fail();
-    }
-
-    @Test
-    @MediumTest
-    public void testStack_Callable_UiThread() {
-        try {
-            CriteriaHelper.pollUiThread(() -> false, 0, DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Callable_UiThread("));
-            return;
-        }
-        Assert.fail();
-    }
-
-    @Test
-    @MediumTest
-    @UiThreadTest
-    public void testStack_Callable_UiThreadNested() {
-        try {
-            CriteriaHelper.pollUiThreadNested(() -> false, 0, DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Callable_UiThreadNested("));
-            return;
-        }
-        Assert.fail();
-    }
-
-    @Test
-    @MediumTest
-    public void testStack_Callable_InstrumentationThread() {
-        try {
-            CriteriaHelper.pollInstrumentationThread(() -> false, 0, DEFAULT_POLLING_INTERVAL);
-        } catch (AssertionError e) {
-            assertThat(
-                    getStackTrace(e),
-                    containsString("CriteriaHelperTest.testStack_Callable_InstrumentationThread("));
-            return;
-        }
-        Assert.fail();
+        TimeoutException t =
+                Assert.assertThrows(
+                        TimeoutException.class,
+                        () -> {
+                            CriteriaHelper.pollInstrumentationThread(
+                                    FALSE_CALLABLE, 0, DEFAULT_POLLING_INTERVAL);
+                        });
+        Assert.assertEquals(OUTER_ERROR_MESSAGE, t.getMessage());
     }
 }
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/input/ImeTest.java b/content/public/android/javatests/src/org/chromium/content/browser/input/ImeTest.java
index a193b61..6e8ad4e 100644
--- a/content/public/android/javatests/src/org/chromium/content/browser/input/ImeTest.java
+++ b/content/public/android/javatests/src/org/chromium/content/browser/input/ImeTest.java
@@ -725,7 +725,6 @@
     @Test
     @SmallTest
     @Feature({"TextInput"})
-    @SuppressWarnings("TryFailThrowable") // TODO(tedchoc): Remove after fixing timeout.
     public void testPhysicalKeyboard_AttachDetach() throws Throwable {
         mRule.attachPhysicalKeyboard();
         // We still call showSoftKeyboard, which will be ignored by physical keyboard.
@@ -748,7 +747,7 @@
         mRule.detachPhysicalKeyboard();
 
         // We should not show soft keyboard here because focus has been lost.
-        thrown.expect(AssertionError.class);
+        thrown.expect(CriteriaHelper.TimeoutException.class);
         CriteriaHelper.pollUiThread(
                 () -> mRule.getInputMethodManagerWrapper().isShowWithoutHideOutstanding());
     }