From a55e935eba859b9ea4f6e4357b8ac4e7e4a8327f Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 29 May 2018 12:27:39 -0700 Subject: [PATCH] =?UTF-8?q?Pause=20requests=20when=20they=E2=80=99re=20sta?= =?UTF-8?q?rted=20while=20the=20RequestManager=20is=20paused.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pausing the request allows Glide to show a placeholder without starting the request. --- .../bumptech/glide/PausedRequestsTest.java | 54 +++++++++++++++++++ .../glide/manager/RequestTracker.java | 1 + .../glide/manager/RequestTrackerTest.java | 18 +++++++ 3 files changed, 73 insertions(+) create mode 100644 instrumentation/src/androidTest/java/com/bumptech/glide/PausedRequestsTest.java diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/PausedRequestsTest.java b/instrumentation/src/androidTest/java/com/bumptech/glide/PausedRequestsTest.java new file mode 100644 index 0000000000..493bf7292e --- /dev/null +++ b/instrumentation/src/androidTest/java/com/bumptech/glide/PausedRequestsTest.java @@ -0,0 +1,54 @@ +package com.bumptech.glide; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.support.test.InstrumentationRegistry; +import android.widget.ImageView; +import com.bumptech.glide.test.ConcurrencyHelper; +import com.bumptech.glide.test.GlideApp; +import com.bumptech.glide.test.GlideRequests; +import com.bumptech.glide.test.ResourceIds; +import com.bumptech.glide.test.TearDownGlide; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests how {@link com.bumptech.glide.request.Request}s behave when the corresponding + * {@link RequestManager} is paused. + */ +public final class PausedRequestsTest { + @Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); + private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); + private final Context context = InstrumentationRegistry.getTargetContext(); + + @SuppressWarnings("unchecked") + @Test + public void load_withPlaceHolderSet_requestsPaused_displaysPlaceholder() { + final ImageView imageView = new ImageView(context); + + final GlideRequests requests = GlideApp.with(context); + concurrency.runOnMainThread(new Runnable() { + @Override + public void run() { + requests.pauseAllRequests(); + } + }); + + final ColorDrawable expected = new ColorDrawable(Color.RED); + concurrency.runOnMainThread( + new Runnable() { + @Override + public void run() { + requests + .load(ResourceIds.drawable.bitmap_alias) + .placeholder(expected) + .into(imageView); + } + }); + + assertThat(imageView.getDrawable()).isEqualTo(expected); + } +} diff --git a/library/src/main/java/com/bumptech/glide/manager/RequestTracker.java b/library/src/main/java/com/bumptech/glide/manager/RequestTracker.java index d654f2a1c4..034a66067b 100644 --- a/library/src/main/java/com/bumptech/glide/manager/RequestTracker.java +++ b/library/src/main/java/com/bumptech/glide/manager/RequestTracker.java @@ -43,6 +43,7 @@ public void runRequest(@NonNull Request request) { if (!isPaused) { request.begin(); } else { + request.pause(); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Paused, delaying request"); } diff --git a/library/test/src/test/java/com/bumptech/glide/manager/RequestTrackerTest.java b/library/test/src/test/java/com/bumptech/glide/manager/RequestTrackerTest.java index 825ac1bce0..b39d3d8c8e 100644 --- a/library/test/src/test/java/com/bumptech/glide/manager/RequestTrackerTest.java +++ b/library/test/src/test/java/com/bumptech/glide/manager/RequestTrackerTest.java @@ -376,6 +376,24 @@ public void testPauseAllRequests_whenRequestComplete_pausesRequest() { verify(request).begin(); } + @Test + public void runRequest_withAllRequestsPaused_pausesNewRequest() { + Request request = mock(Request.class); + tracker.pauseAllRequests(); + tracker.runRequest(request); + + verify(request).pause(); + } + + @Test + public void runRequest_withRequestsPaused_pausesNewRequest() { + Request request = mock(Request.class); + tracker.pauseRequests(); + tracker.runRequest(request); + + verify(request).pause(); + } + private class ClearAndRemoveRequest implements Answer { private final Request toRemove;