From f145e1732e2db55007e338b8b64b7dd9497d1988 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 17 Sep 2019 14:51:09 -0700 Subject: [PATCH] TestLooper: Some changes to test looper behavior a) Add a new stopAutoDispatchAndIgnoreExceptions method This method ignores exceptions raised when there are no messages dispatched in the looper. Helps write negative unit tests for methods where the runnables are not posted when some preconditions are not met. Not starting the looper in these tests would make it unclear whether the test failed because the looper was not running or because the preconditions (like permission checks) where not satisfied. b) Auto dispatch will only stop running when stopAutoDispatch is invoked. Previously it stops running when it runs a single message. This causes failures in unit tests for methods which posts multiple runnables in the same method. Bug: 138403307 Bug: 117601161 Test: atest com.android.server.wifi Change-Id: Iab27d92d443c6d7364c52b86d51dd2cb7902358a --- .../java/android/os/test/TestLooper.java | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/tests/utils/testutils/java/android/os/test/TestLooper.java b/tests/utils/testutils/java/android/os/test/TestLooper.java index a49eda3d86d05..01bd47b9c608a 100644 --- a/tests/utils/testutils/java/android/os/test/TestLooper.java +++ b/tests/utils/testutils/java/android/os/test/TestLooper.java @@ -210,33 +210,36 @@ public class TestLooper { /** * Run method for the auto dispatch thread. * The thread loops a maximum of MAX_LOOPS times with a 10ms sleep between loops. - * The thread continues looping and attempting to dispatch all messages until at - * least one message has been dispatched. + * The thread continues looping and attempting to dispatch all messages until + * {@link #stopAutoDispatch()} has been invoked. */ @Override public void run() { int dispatchCount = 0; for (int i = 0; i < MAX_LOOPS; i++) { try { - dispatchCount = dispatchAll(); + dispatchCount += dispatchAll(); } catch (RuntimeException e) { mAutoDispatchException = e; - } - Log.d(TAG, "dispatched " + dispatchCount + " messages"); - if (dispatchCount > 0) { return; } + Log.d(TAG, "dispatched " + dispatchCount + " messages"); try { Thread.sleep(LOOP_SLEEP_TIME_MS); } catch (InterruptedException e) { - mAutoDispatchException = new IllegalStateException( - "stopAutoDispatch called before any messages were dispatched."); + if (dispatchCount == 0) { + Log.e(TAG, "stopAutoDispatch called before any messages were dispatched."); + mAutoDispatchException = new IllegalStateException( + "stopAutoDispatch called before any messages were dispatched."); + } return; } } - Log.e(TAG, "AutoDispatchThread did not dispatch any messages."); - mAutoDispatchException = new IllegalStateException( - "TestLooper did not dispatch any messages before exiting."); + if (dispatchCount == 0) { + Log.e(TAG, "AutoDispatchThread did not dispatch any messages."); + mAutoDispatchException = new IllegalStateException( + "TestLooper did not dispatch any messages before exiting."); + } } /** @@ -287,4 +290,17 @@ public class TestLooper { "stopAutoDispatch called without startAutoDispatch."); } } + + /** + * If an AutoDispatchThread is currently running, stop and clean up. + * This method ignores exceptions raised for indicating that no messages were dispatched. + */ + public void stopAutoDispatchAndIgnoreExceptions() { + try { + stopAutoDispatch(); + } catch (IllegalStateException e) { + Log.e(TAG, "stopAutoDispatch", e); + } + + } }