Merge "Use utility method to execute things on the handler" into main

This commit is contained in:
Chiachang Wang
2023-07-13 03:24:29 +00:00
committed by Gerrit Code Review

View File

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