Skip to content

Commit

Permalink
Pause requests when they’re started while the RequestManager is paused.
Browse files Browse the repository at this point in the history
Pausing the request allows Glide to show a placeholder without starting the request.
  • Loading branch information
sjudd committed May 29, 2018
1 parent afeb674 commit a55e935
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {

private final Request toRemove;
Expand Down

0 comments on commit a55e935

Please sign in to comment.