|
| 1 | +package com.bumptech.glide; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.Bitmap; |
| 7 | +import android.graphics.Bitmap.CompressFormat; |
| 8 | +import android.graphics.Bitmap.Config; |
| 9 | +import android.graphics.Canvas; |
| 10 | +import android.graphics.Color; |
| 11 | +import android.graphics.drawable.Drawable; |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import androidx.test.core.app.ApplicationProvider; |
| 15 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 16 | +import com.bumptech.glide.load.DataSource; |
| 17 | +import com.bumptech.glide.load.engine.GlideException; |
| 18 | +import com.bumptech.glide.load.engine.executor.GlideExecutor; |
| 19 | +import com.bumptech.glide.request.Request; |
| 20 | +import com.bumptech.glide.request.RequestListener; |
| 21 | +import com.bumptech.glide.request.target.CustomTarget; |
| 22 | +import com.bumptech.glide.request.target.Target; |
| 23 | +import com.bumptech.glide.request.transition.Transition; |
| 24 | +import com.bumptech.glide.test.ModelGeneratorRule; |
| 25 | +import com.bumptech.glide.testutil.ConcurrencyHelper; |
| 26 | +import com.bumptech.glide.testutil.TearDownGlide; |
| 27 | +import java.io.BufferedOutputStream; |
| 28 | +import java.io.File; |
| 29 | +import java.io.FileOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.OutputStream; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.rules.TemporaryFolder; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | + |
| 40 | +@RunWith(AndroidJUnit4.class) |
| 41 | +public class MultiRequestTest { |
| 42 | + private final Context context = ApplicationProvider.getApplicationContext(); |
| 43 | + @Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); |
| 44 | + @Rule public final ModelGeneratorRule modelGeneratorRule = new ModelGeneratorRule(); |
| 45 | + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 46 | + private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void thumbnail_onResourceReady_forPrimary_isComplete_whenRequestListenerIsCalled() |
| 50 | + throws IOException, InterruptedException { |
| 51 | + |
| 52 | + // Make sure the requests complete in the same order |
| 53 | + Glide.init( |
| 54 | + context, |
| 55 | + new GlideBuilder() |
| 56 | + .setSourceExecutor(GlideExecutor.newSourceBuilder().setThreadCount(1).build())); |
| 57 | + |
| 58 | + AtomicBoolean isPrimaryRequestComplete = new AtomicBoolean(false); |
| 59 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 60 | + |
| 61 | + RequestBuilder<Drawable> request = |
| 62 | + Glide.with(context) |
| 63 | + .load(newImageFile()) |
| 64 | + .thumbnail(Glide.with(context).load(newImageFile())) |
| 65 | + .listener( |
| 66 | + new RequestListener<>() { |
| 67 | + @Override |
| 68 | + public boolean onLoadFailed( |
| 69 | + @Nullable GlideException e, |
| 70 | + Object model, |
| 71 | + Target<Drawable> target, |
| 72 | + boolean isFirstResource) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean onResourceReady( |
| 78 | + Drawable resource, |
| 79 | + Object model, |
| 80 | + Target<Drawable> target, |
| 81 | + DataSource dataSource, |
| 82 | + boolean isFirstResource) { |
| 83 | + isPrimaryRequestComplete.set(target.getRequest().isComplete()); |
| 84 | + countDownLatch.countDown(); |
| 85 | + return false; |
| 86 | + } |
| 87 | + }); |
| 88 | + concurrency.runOnMainThread(() -> request.into(new DoNothingTarget())); |
| 89 | + |
| 90 | + assertThat(countDownLatch.await(3, TimeUnit.SECONDS)).isTrue(); |
| 91 | + assertThat(isPrimaryRequestComplete.get()).isTrue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void thumbnail_onLoadFailed_forPrimary_isNotRunningOrComplete_whenRequestListenerIsCalled() |
| 96 | + throws IOException, InterruptedException { |
| 97 | + |
| 98 | + // Make sure the requests complete in the same order |
| 99 | + Glide.init( |
| 100 | + context, |
| 101 | + new GlideBuilder() |
| 102 | + .setSourceExecutor(GlideExecutor.newSourceBuilder().setThreadCount(1).build())); |
| 103 | + |
| 104 | + AtomicBoolean isNeitherRunningNorComplete = new AtomicBoolean(false); |
| 105 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 106 | + |
| 107 | + int missingResourceId = 123; |
| 108 | + RequestBuilder<Drawable> requestBuilder = |
| 109 | + Glide.with(context) |
| 110 | + .load(missingResourceId) |
| 111 | + .thumbnail(Glide.with(context).load(newImageFile())) |
| 112 | + .listener( |
| 113 | + new RequestListener<>() { |
| 114 | + @Override |
| 115 | + public boolean onLoadFailed( |
| 116 | + @Nullable GlideException e, |
| 117 | + Object model, |
| 118 | + Target<Drawable> target, |
| 119 | + boolean isFirstResource) { |
| 120 | + Request request = target.getRequest(); |
| 121 | + isNeitherRunningNorComplete.set(!request.isComplete() && !request.isRunning()); |
| 122 | + countDownLatch.countDown(); |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean onResourceReady( |
| 128 | + Drawable resource, |
| 129 | + Object model, |
| 130 | + Target<Drawable> target, |
| 131 | + DataSource dataSource, |
| 132 | + boolean isFirstResource) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + }); |
| 136 | + concurrency.runOnMainThread(() -> requestBuilder.into(new DoNothingTarget())); |
| 137 | + |
| 138 | + assertThat(countDownLatch.await(3, TimeUnit.SECONDS)).isTrue(); |
| 139 | + assertThat(isNeitherRunningNorComplete.get()).isTrue(); |
| 140 | + } |
| 141 | + |
| 142 | + private File newImageFile() throws IOException { |
| 143 | + Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888); |
| 144 | + Canvas canvas = new Canvas(bitmap); |
| 145 | + canvas.drawColor(Color.RED); |
| 146 | + File result = temporaryFolder.newFile(); |
| 147 | + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(result))) { |
| 148 | + bitmap.compress(CompressFormat.JPEG, 75, os); |
| 149 | + } |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + // We don't store or do anything with the resource, so we don't need to do anything to release it |
| 154 | + // in onLoadCleared. |
| 155 | + private static final class DoNothingTarget extends CustomTarget<Drawable> { |
| 156 | + @Override |
| 157 | + public void onResourceReady( |
| 158 | + @NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {} |
| 159 | + |
| 160 | + @Override |
| 161 | + public void onLoadCleared(@Nullable Drawable placeholder) {} |
| 162 | + } |
| 163 | +} |
0 commit comments