Use utility method to execute things on the handler

Replace Handler.post() with visibleOnHandlerTread().

Test: atest FrameworksNetTests
Change-Id: I3a2f166d939db8ed6ac7fbb1cb9306256ae22189
This commit is contained in:
chiachangwang
2023-07-12 08:59:32 +00:00
parent 92e1d5182d
commit 26bd4adfc2

View File

@@ -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.