Skip to content

Commit 4c4c7c9

Browse files
rlazodaymxn
andauthored
[Storage] Migrate from Robolectric.flushForegroundThreadScheduler to ShadowLooper.runToEndOfTasks (#6927)
The Robolectric Scheduler APIs are designed for LEGACY Looper mode, where tasks were controlled by Scheduler objects. LEGACY Looper mode has been deprecated for 5+ years. Migrate from Robolectric.flushForegroundThreadScheduler to ShadowLooper APIs. See cl/752917858 for reference --------- Co-authored-by: Daymon <17409137+daymxn@users.noreply.github.com>
1 parent 4c29e0b commit 4c4c7c9

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

firebase-storage/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [fixed] Fixed an issue where tests were depending on a deprecated feature of the test framework (#6927)
23

34
# 21.0.1
45
* [fixed] Fixed an issue where `maxUploadRetryTimeMillis` parameter is ignored when uploading using

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

+5-4
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,14 +42,13 @@
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

4748
/** Tests for {@link FirebaseStorage}. */
4849
@SuppressWarnings("ConstantConditions")
4950
@RunWith(RobolectricTestRunner.class)
50-
@Config(sdk = Build.VERSION_CODES.LOLLIPOP_MR1)
51+
@Config(sdk = Build.VERSION_CODES.M)
5152
public class DownloadTest {
5253

5354
@Rule public RetryRule retryRule = new RetryRule(3);
@@ -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)