Remove dependency on legacy Robolectric Looper mode

The default will soon change to Looper mode PAUSED. Some parts of our code
relies on the legacy behaviour when setting set SystemClock and expecting
pending messages to be delivered. With the new mode, we need to explicitly
request to idle the main looper so that pending messages can be delivered.

PiperOrigin-RevId: 297814964
This commit is contained in:
tonihei 2020-02-28 12:22:06 +00:00 committed by Oliver Woodman
parent 6c7a2c3cfd
commit ffdc5805bd

View File

@ -63,9 +63,13 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers; import org.mockito.ArgumentMatchers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.LooperMode;
import org.robolectric.annotation.LooperMode.Mode;
import org.robolectric.shadows.ShadowLooper;
/** Tests for {@link CronetDataSource}. */ /** Tests for {@link CronetDataSource}. */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@LooperMode(Mode.PAUSED)
public final class CronetDataSourceTest { public final class CronetDataSourceTest {
private static final int TEST_CONNECT_TIMEOUT_MS = 100; private static final int TEST_CONNECT_TIMEOUT_MS = 100;
@ -902,10 +906,12 @@ public final class CronetDataSourceTest {
// We should still be trying to open. // We should still be trying to open.
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// We should still be trying to open as we approach the timeout. // We should still be trying to open as we approach the timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1); setSystemClockInMsAndTriggerPendingMessages(
/* nowMs= */ startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// Now we timeout. // Now we timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS + 10); setSystemClockInMsAndTriggerPendingMessages(
/* nowMs= */ startTimeMs + TEST_CONNECT_TIMEOUT_MS + 10);
timedOutLatch.await(); timedOutLatch.await();
verify(mockTransferListener, never()) verify(mockTransferListener, never())
@ -941,7 +947,8 @@ public final class CronetDataSourceTest {
// We should still be trying to open. // We should still be trying to open.
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// We should still be trying to open as we approach the timeout. // We should still be trying to open as we approach the timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1); setSystemClockInMsAndTriggerPendingMessages(
/* nowMs= */ startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// Now we interrupt. // Now we interrupt.
thread.interrupt(); thread.interrupt();
@ -975,7 +982,8 @@ public final class CronetDataSourceTest {
// We should still be trying to open. // We should still be trying to open.
assertNotCountedDown(openLatch); assertNotCountedDown(openLatch);
// We should still be trying to open as we approach the timeout. // We should still be trying to open as we approach the timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1); setSystemClockInMsAndTriggerPendingMessages(
/* nowMs= */ startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(openLatch); assertNotCountedDown(openLatch);
// The response arrives just in time. // The response arrives just in time.
dataSourceUnderTest.urlRequestCallback.onResponseStarted(mockUrlRequest, testUrlResponseInfo); dataSourceUnderTest.urlRequestCallback.onResponseStarted(mockUrlRequest, testUrlResponseInfo);
@ -1010,14 +1018,15 @@ public final class CronetDataSourceTest {
// We should still be trying to open. // We should still be trying to open.
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// We should still be trying to open as we approach the timeout. // We should still be trying to open as we approach the timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1); setSystemClockInMsAndTriggerPendingMessages(
/* nowMs= */ startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// A redirect arrives just in time. // A redirect arrives just in time.
dataSourceUnderTest.urlRequestCallback.onRedirectReceived( dataSourceUnderTest.urlRequestCallback.onRedirectReceived(
mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl1"); mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl1");
long newTimeoutMs = 2 * TEST_CONNECT_TIMEOUT_MS - 1; long newTimeoutMs = 2 * TEST_CONNECT_TIMEOUT_MS - 1;
SystemClock.setCurrentTimeMillis(startTimeMs + newTimeoutMs - 1); setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */ startTimeMs + newTimeoutMs - 1);
// We should still be trying to open as we approach the new timeout. // We should still be trying to open as we approach the new timeout.
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// A redirect arrives just in time. // A redirect arrives just in time.
@ -1025,11 +1034,11 @@ public final class CronetDataSourceTest {
mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl2"); mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl2");
newTimeoutMs = 3 * TEST_CONNECT_TIMEOUT_MS - 2; newTimeoutMs = 3 * TEST_CONNECT_TIMEOUT_MS - 2;
SystemClock.setCurrentTimeMillis(startTimeMs + newTimeoutMs - 1); setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */ startTimeMs + newTimeoutMs - 1);
// We should still be trying to open as we approach the new timeout. // We should still be trying to open as we approach the new timeout.
assertNotCountedDown(timedOutLatch); assertNotCountedDown(timedOutLatch);
// Now we timeout. // Now we timeout.
SystemClock.setCurrentTimeMillis(startTimeMs + newTimeoutMs + 10); setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */ startTimeMs + newTimeoutMs + 10);
timedOutLatch.await(); timedOutLatch.await();
verify(mockTransferListener, never()) verify(mockTransferListener, never())
@ -1459,4 +1468,9 @@ public final class CronetDataSourceTest {
} }
return copy; return copy;
} }
private static void setSystemClockInMsAndTriggerPendingMessages(long nowMs) {
SystemClock.setCurrentTimeMillis(nowMs);
ShadowLooper.idleMainLooper();
}
} }