-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Requests to notify Targets when cleared after loads complete.
Fixes #2555.
- Loading branch information
Showing
12 changed files
with
711 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
instrumentation/src/androidTest/java/com/bumptech/glide/RequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
package com.bumptech.glide; | ||
|
||
import static com.bumptech.glide.test.Matchers.anyDrawable; | ||
import static com.bumptech.glide.test.Matchers.anyTarget; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyBoolean; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import android.content.Context; | ||
import android.graphics.drawable.Drawable; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import android.widget.ImageView; | ||
import com.bumptech.glide.load.DataSource; | ||
import com.bumptech.glide.load.engine.executor.GlideExecutor; | ||
import com.bumptech.glide.request.RequestListener; | ||
import com.bumptech.glide.test.ConcurrencyHelper; | ||
import com.bumptech.glide.test.GlideApp; | ||
import com.bumptech.glide.test.ResourceIds; | ||
import com.bumptech.glide.test.TearDownGlide; | ||
import com.bumptech.glide.test.WaitModelLoader; | ||
import com.bumptech.glide.test.WaitModelLoader.WaitModel; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
/** | ||
* Tests the behaviors of Requests of all types. | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class RequestTest { | ||
@Rule public TearDownGlide tearDownGlide = new TearDownGlide(); | ||
@Mock private RequestListener<Drawable> requestListener; | ||
private ConcurrencyHelper concurrency = new ConcurrencyHelper(); | ||
private Context context; | ||
private ImageView imageView; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
context = InstrumentationRegistry.getTargetContext(); | ||
imageView = new ImageView(context); | ||
imageView.measure(100, 100); | ||
imageView.layout(0, 0, 100, 100); | ||
|
||
// Some emulators only have a single resize thread, so waiting on a latch will block them | ||
// forever. | ||
Glide.init(context, | ||
new GlideBuilder().setResizeExecutor(GlideExecutor.newUnlimitedSourceExecutor())); | ||
} | ||
|
||
@Test | ||
public void clear_withSingleRequest_nullsOutDrawableInView() { | ||
concurrency.loadOnMainThread( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical), | ||
imageView); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
|
||
concurrency.clearOnMainThread(imageView); | ||
assertThat(imageView.getDrawable()).isNull(); | ||
} | ||
|
||
@Test | ||
public void clear_withRequestWithThumbnail_nullsOutDrawableInView() { | ||
concurrency.loadOnMainThread( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.thumbnail( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.override(100, 100)), | ||
imageView); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
|
||
concurrency.clearOnMainThread(imageView); | ||
assertThat(imageView.getDrawable()).isNull(); | ||
} | ||
|
||
@Test | ||
public void onStop_withSingleRequest_doesNotNullOutDrawableInView() { | ||
concurrency.loadOnMainThread( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical), | ||
imageView); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
|
||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void onStop_withRequestWithThumbnail_doesNotNullOutDrawableInView() { | ||
concurrency.loadOnMainThread( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.thumbnail( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.override(100, 100)), | ||
imageView); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
|
||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void onStop_withSingleRequestInProgress_nullsOutDrawableInView() { | ||
final WaitModel<Integer> model = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical); | ||
concurrency.runOnMainThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.into(imageView); | ||
} | ||
}); | ||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
assertThat(imageView.getDrawable()).isNull(); | ||
model.countDown(); | ||
} | ||
|
||
@Test | ||
public void onStop_withRequestWithThumbnailBothInProgress_nullsOutDrawableInView() { | ||
final WaitModel<Integer> model = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical); | ||
concurrency.runOnMainThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context) | ||
.load(model) | ||
.thumbnail( | ||
GlideApp.with(context) | ||
.load(model) | ||
.override(100, 100)) | ||
.into(imageView); | ||
|
||
} | ||
}); | ||
|
||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
assertThat(imageView.getDrawable()).isNull(); | ||
model.countDown(); | ||
} | ||
|
||
/** Tests #2555. */ | ||
@Test | ||
public void onStop_withRequestWithOnlyFullInProgress_nullsOutDrawableInView() { | ||
final WaitModel<Integer> mainModel = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical); | ||
concurrency.loadUntilFirstFinish( | ||
GlideApp.with(context) | ||
.load(mainModel) | ||
.listener(requestListener) | ||
.thumbnail(GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.listener(requestListener) | ||
.override(100, 100)), | ||
imageView); | ||
|
||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
|
||
verify(requestListener, never()) | ||
.onResourceReady( | ||
anyDrawable(), | ||
any(), | ||
anyTarget(), | ||
eq(DataSource.DATA_DISK_CACHE), | ||
anyBoolean()); | ||
verify(requestListener, never()) | ||
.onResourceReady( | ||
anyDrawable(), | ||
any(), | ||
anyTarget(), | ||
eq(DataSource.RESOURCE_DISK_CACHE), | ||
anyBoolean()); | ||
assertThat(imageView.getDrawable()).isNull(); | ||
mainModel.countDown(); | ||
} | ||
|
||
@Test | ||
public void onStop_withRequestWithOnlyThumbnailInProgress_doesNotNullOutDrawableInView() { | ||
final WaitModel<Integer> thumbModel = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical); | ||
concurrency.loadUntilFirstFinish( | ||
GlideApp.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.listener(requestListener) | ||
.thumbnail(GlideApp.with(context) | ||
.load(thumbModel) | ||
.listener(requestListener) | ||
.override(100, 100)), | ||
imageView); | ||
|
||
concurrency.runOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GlideApp.with(context).onStop(); | ||
} | ||
}); | ||
|
||
verify(requestListener, never()) | ||
.onResourceReady( | ||
anyDrawable(), | ||
any(), | ||
anyTarget(), | ||
eq(DataSource.DATA_DISK_CACHE), | ||
anyBoolean()); | ||
verify(requestListener, never()) | ||
.onResourceReady( | ||
anyDrawable(), | ||
any(), | ||
anyTarget(), | ||
eq(DataSource.RESOURCE_DISK_CACHE), | ||
anyBoolean()); | ||
|
||
// Only requests that are running are paused in onStop. The full request should take priority | ||
// over the thumbnail request. Therefore, if the full request is finished in onStop, it should | ||
// not be cleared, even if the thumbnail request is still running. | ||
assertThat(imageView.getDrawable()).isNotNull(); | ||
thumbModel.countDown(); | ||
} | ||
} |
Oops, something went wrong.