Skip to content

Commit 0bd3f29

Browse files
committed
Migrate from Robolectric.flushForegroundThreadScheduler to ShadowLooper.runToEndOfTasks
Robolectric.flushForegroundThreadScheduler is going to be released in a forthcoming Robolectric release. The Robolectric Scheduler APIs are designed for LEGACY Looper mode which has been deprecated for years.
1 parent 7cbb80b commit 0bd3f29

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

firebase-inappmessaging/src/test/java/com/google/firebase/inappmessaging/internal/ForegroundNotifierTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.google.firebase.inappmessaging.internal.InAppMessageStreamManager.ON_FOREGROUND;
19+
import static org.robolectric.Shadows.shadowOf;
1920

21+
import android.os.Looper;
2022
import io.reactivex.flowables.ConnectableFlowable;
2123
import io.reactivex.subscribers.TestSubscriber;
2224
import org.junit.Before;
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
25-
import org.robolectric.Robolectric;
2627
import org.robolectric.RobolectricTestRunner;
2728
import org.robolectric.annotation.Config;
2829

@@ -61,7 +62,7 @@ public void notifier_onActivityResumedAfterRunnableExecution_notifiesListener()
6162
foregroundNotifier.onActivityResumed(null); // 1
6263
assertThat(subscriber.getEvents().get(0)).hasSize(1);
6364
foregroundNotifier.onActivityPaused(null);
64-
Robolectric.flushForegroundThreadScheduler();
65+
shadowOf(Looper.getMainLooper()).runToEndOfTasks();
6566
foregroundNotifier.onActivityResumed(null); // 2
6667
assertThat(subscriber.getEvents().get(0)).hasSize(2);
6768
}

firebase-storage/src/test/java/com/google/firebase/storage/DownloadTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import static org.junit.Assert.assertNotNull;
1919
import static org.junit.Assert.assertTrue;
2020
import static org.junit.Assert.fail;
21+
import static org.robolectric.Shadows.shadowOf;
2122

2223
import android.net.Uri;
2324
import android.os.Build;
25+
import android.os.Looper;
2426
import com.google.android.gms.tasks.Task;
2527
import com.google.firebase.FirebaseApp;
2628
import com.google.firebase.storage.TestDownloadHelper.StreamDownloadResponse;
@@ -40,7 +42,6 @@
4042
import org.junit.Test;
4143
import org.junit.rules.TemporaryFolder;
4244
import org.junit.runner.RunWith;
43-
import org.robolectric.Robolectric;
4445
import org.robolectric.RobolectricTestRunner;
4546
import org.robolectric.annotation.Config;
4647

@@ -137,7 +138,7 @@ public void streamDownloadStateVerification() throws Exception {
137138
ControllableSchedulerHelper.getInstance().resume();
138139

139140
for (int i = 0; i < 3000; i++) {
140-
Robolectric.flushForegroundThreadScheduler();
141+
shadowOf(Looper.getMainLooper()).runToEndOfTasks();
141142
if (semaphore.tryAcquire(2, 1, TimeUnit.MILLISECONDS)) {
142143
Assert.assertEquals(bytesDownloaded.get(), bytesTransferred.get());
143144
return;
@@ -256,7 +257,7 @@ public void byteDownload() throws Exception {
256257
TestDownloadHelper.byteDownload(
257258
new StringBuilder(), bitmap -> assertEquals(1076408, bitmap.length));
258259
for (int i = 0; i < 3000; i++) {
259-
Robolectric.flushForegroundThreadScheduler();
260+
shadowOf(Looper.getMainLooper()).runToEndOfTasks();
260261
if (semaphore.tryAcquire(1, 1, TimeUnit.MILLISECONDS)) {
261262
// success!
262263
factory.verifyOldMock();

firebase-storage/src/test/java/com/google/firebase/storage/TestUtil.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package com.google.firebase.storage;
1616

17+
import static org.robolectric.Shadows.shadowOf;
18+
19+
import android.os.Looper;
1720
import androidx.annotation.Nullable;
1821
import androidx.test.core.app.ApplicationProvider;
1922
import com.google.android.gms.tasks.Task;
@@ -26,7 +29,6 @@
2629
import java.io.StringReader;
2730
import java.util.concurrent.TimeUnit;
2831
import org.junit.Assert;
29-
import org.robolectric.Robolectric;
3032

3133
/** Test helpers. */
3234
public class TestUtil {
@@ -138,7 +140,7 @@ static void await(Task<?> task, int timeout, TimeUnit timeUnit) throws Interrupt
138140
long timeoutMillis = timeUnit.toMillis(timeout);
139141

140142
for (int i = 0; i < timeoutMillis; i++) {
141-
Robolectric.flushForegroundThreadScheduler();
143+
shadowOf(Looper.getMainLooper()).runToEndOfTasks();
142144
if (task.isComplete()) {
143145
// success!
144146
return;

0 commit comments

Comments
 (0)