Skip to content

Commit

Permalink
Allow setting options for byte arrays in any order.
Browse files Browse the repository at this point in the history
Previously we’d silently override disk cache strategies or skip memory cache options set prior to the load call.

There’s still a TODO to make this work for the rest of the options.
  • Loading branch information
sjudd committed Nov 25, 2017
1 parent cb69694 commit c7b7dfe
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,66 @@ public void run() {
anyDrawable(), any(), anyTarget(), eq(DataSource.LOCAL), anyBoolean());
}

@Test
public void loadFromBuilder_withDiskCacheStrategySetBeforeLoad_doesNotOverrideDiskCacheStrategy()
throws IOException {
byte[] data = getCanonicalBytes();
concurrency.wait(
GlideApp.with(context)
.asDrawable()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.load(data)
.submit());

concurrency.runOnMainThread(new Runnable() {
@Override
public void run() {
GlideApp.get(context).clearMemory();
}
});

concurrency.wait(
GlideApp.with(context)
.asDrawable()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.listener(requestListener)
.load(data)
.submit());

verify(requestListener).onResourceReady(
anyDrawable(), any(), anyTarget(), eq(DataSource.RESOURCE_DISK_CACHE), anyBoolean());
}

@Test
public void loadFromBuilder_withSkipMemoryCacheSetBeforeLoad_doesNotOverrideSkipMemoryCache()
throws IOException {
byte[] data = getCanonicalBytes();
concurrency.wait(
GlideApp.with(context)
.asDrawable()
.skipMemoryCache(false)
.load(data)
.submit());

concurrency.runOnMainThread(new Runnable() {
@Override
public void run() {
GlideApp.get(context).clearMemory();
}
});

concurrency.wait(
GlideApp.with(context)
.asDrawable()
.skipMemoryCache(false)
.listener(requestListener)
.load(data)
.submit());

verify(requestListener).onResourceReady(
anyDrawable(), any(), anyTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean());
}

private Bitmap copyFromImageViewDrawable(ImageView imageView) {
if (imageView.getDrawable() == null) {
fail("Drawable unexpectedly null");
Expand Down
13 changes: 9 additions & 4 deletions library/src/main/java/com/bumptech/glide/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.bumptech.glide.request.RequestOptions.diskCacheStrategyOf;
import static com.bumptech.glide.request.RequestOptions.signatureOf;
import static com.bumptech.glide.request.RequestOptions.skipMemoryCacheOf;

import android.content.Context;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -502,10 +503,14 @@ public RequestBuilder<TranscodeType> load(@Nullable URL url) {
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable byte[] model) {
return
loadGeneric(model)
.apply(diskCacheStrategyOf(DiskCacheStrategy.NONE)
.skipMemoryCache(true /*skipMemoryCache*/));
RequestBuilder<TranscodeType> result = loadGeneric(model);
if (!result.requestOptions.isDiskCacheStrategySet()) {
result = result.apply(diskCacheStrategyOf(DiskCacheStrategy.NONE));
}
if (!result.requestOptions.isSkipMemoryCacheSet()) {
result = result.apply(skipMemoryCacheOf(true /*skipMemoryCache*/));
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,14 @@ protected boolean isAutoCloneEnabled() {
return isAutoCloneEnabled;
}

public final boolean isDiskCacheStrategySet() {
return isSet(DISK_CACHE_STRATEGY);
}

public final boolean isSkipMemoryCacheSet() {
return isSet(IS_CACHEABLE);
}

@NonNull
public final Map<Class<?>, Transformation<?>> getTransformations() {
return transformations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,38 @@ public void testApplyMultiTransform() {
.isInstanceOf(MultiTransformation.class);
}

@Test
public void isSkipMemoryCacheSet_withoutSkipMemoryCache_isFalse() {
assertThat(options.isSkipMemoryCacheSet()).isFalse();
}

@Test
public void isSkipMemoryCacheSet_withSkipMemoryCacheTrue_isTrue() {
assertThat(options.skipMemoryCache(true).isSkipMemoryCacheSet()).isTrue();
}

@Test
public void isSkipMemoryCacheSet_withSkipMemoryCacheFalse_isTrue() {
assertThat(options.skipMemoryCache(false).isSkipMemoryCacheSet()).isTrue();
}

@Test
public void isDiskCacheStrategySet_withoutDiskCacheStrategy_isFalse() {
assertThat(options.isDiskCacheStrategySet()).isFalse();
}

@Test
public void isDiskCacheStrategySet_withDiskCacheStrategyDefault_isTrue() {
assertThat(options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).isDiskCacheStrategySet())
.isTrue();
}

@Test
public void isDiskCacheStrategySet_withDiskCacheStrategyNonDefault_isTrue() {
assertThat(options.diskCacheStrategy(DiskCacheStrategy.ALL).isDiskCacheStrategySet())
.isTrue();
}

@Test
public void testEqualsHashCode() {
Drawable first = new ColorDrawable(Color.RED);
Expand Down

0 comments on commit c7b7dfe

Please sign in to comment.