diff --git a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java index b30c9ce16c..f4d391547a 100644 --- a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java +++ b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java @@ -408,22 +408,22 @@ public class AutomaticOnOffKeepaliveTrackerTest { @Test public void testIsAnyTcpSocketConnected_withTargetNetId() throws Exception { setupResponseWithSocketExisting(); - mTestHandler.post( - () -> assertTrue(mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID))); + assertTrue(visibleOnHandlerThread(mTestHandler, + () -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID))); } @Test public void testIsAnyTcpSocketConnected_withIncorrectNetId() throws Exception { setupResponseWithSocketExisting(); - mTestHandler.post( - () -> assertFalse(mAOOKeepaliveTracker.isAnyTcpSocketConnected(OTHER_NETID))); + assertFalse(visibleOnHandlerThread(mTestHandler, + () -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(OTHER_NETID))); } @Test public void testIsAnyTcpSocketConnected_noSocketExists() throws Exception { setupResponseWithoutSocketExisting(); - mTestHandler.post( - () -> assertFalse(mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID))); + assertFalse(visibleOnHandlerThread(mTestHandler, + () -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID))); } private void triggerEventKeepalive(int slot, int reason) { @@ -500,9 +500,7 @@ public class AutomaticOnOffKeepaliveTrackerTest { final AlarmManager.OnAlarmListener listener = listenerCaptor.getValue(); // For realism, the listener should be posted on the handler - mTestHandler.post(() -> listener.onAlarm()); - // Wait for the listener to be called. The listener enqueues a message to the handler. - HandlerUtils.waitForIdle(mTestHandler, TIMEOUT_MS); + visibleOnHandlerThread(mTestHandler, () -> listener.onAlarm()); // Wait for the message posted by the listener to be processed. HandlerUtils.waitForIdle(mTestHandler, TIMEOUT_MS); @@ -525,8 +523,7 @@ public class AutomaticOnOffKeepaliveTrackerTest { doReturn(METRICS_COLLECTION_DURATION_MS).when(mDependencies).getElapsedRealtime(); // For realism, the listener should be posted on the handler - mTestHandler.post(() -> listener.onAlarm()); - HandlerUtils.waitForIdle(mTestHandler, TIMEOUT_MS); + visibleOnHandlerThread(mTestHandler, () -> listener.onAlarm()); verify(mKeepaliveStatsTracker).writeAndResetMetrics(); // Alarm is rescheduled.