Skip to content

Commit

Permalink
Add support for recursive thumbnail calls.
Browse files Browse the repository at this point in the history
Fixes #149
  • Loading branch information
sjudd committed Sep 25, 2014
1 parent e781209 commit f2285a8
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 94 deletions.
54 changes: 49 additions & 5 deletions library/src/androidTest/java/com/bumptech/glide/GlideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -569,6 +570,48 @@ public String getId() {
verify(target).onResourceReady(eq(expected), any(GlideAnimation.class));
}

@Test
public void testReceivesThumbnails() {
String full = mockUri("content://full");
String thumb = mockUri("content://thumb");
requestManager
.load(full)
.thumbnail(requestManager
.load(thumb))
.into(target);

verify(target, times(2)).onResourceReady(any(Drawable.class), any(GlideAnimation.class));
}

@Test
public void testReceivesRecursiveThumbnails() {
requestManager
.load(mockUri("content://first"))
.thumbnail(requestManager
.load(mockUri("content://second"))
.thumbnail(requestManager
.load(mockUri("content://third"))
.thumbnail(requestManager
.load(mockUri("content://fourth"))
)
)
)
.into(target);
verify(target, times(4)).onResourceReady(any(Drawable.class), any(GlideAnimation.class));
}

@Test
public void testReceivesRecursiveThumbnailWithPercentage() {
requestManager
.load(mockUri("content://first"))
.thumbnail(requestManager
.load(mockUri("content://second"))
.thumbnail(0.5f)
)
.into(target);
verify(target, times(3)).onResourceReady(any(Drawable.class), any(GlideAnimation.class));
}

@Test
public void testNullModelInGenericImageLoadDoesNotThrow() {
requestManager.load((Double) null).into(target);
Expand Down Expand Up @@ -646,15 +689,15 @@ private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResourc
Glide.get(getContext()).register(failModel, failResource, failFactory);
}

private void mockUri(String uriString) {
mockUri(Uri.parse(uriString), null);
private String mockUri(String uriString) {
return mockUri(Uri.parse(uriString), null);
}

private void mockUri(Uri uri) {
mockUri(uri, null);
private String mockUri(Uri uri) {
return mockUri(uri, null);
}

private void mockUri(Uri uri, InputStream is) {
private String mockUri(Uri uri, InputStream is) {
if (is == null) {
is = new ByteArrayInputStream(new byte[0]);
}
Expand All @@ -667,6 +710,7 @@ private void mockUri(Uri uri, InputStream is) {
when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor);

shadowContentResolver.registerAssetFileDescriptor(uri, assetFileDescriptor);
return uri.toString();
}

private Context getContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,19 +792,19 @@ public void testRequestListenerIsCalledWithIsFirstResourceIfNoRequestCoordinator
}

@Test
public void testRequestListenerIsCalledWithIsFirstImageIfRequestCoordinatorReturnsNoRequestComplete() {
public void testRequestListenerIsCalledWithIsFirstImageIfRequestCoordinatorReturnsNoResourceSet() {
GenericRequest request = harness.getRequest();
when(harness.requestCoordinator.isAnyRequestComplete()).thenReturn(false);
when(harness.requestCoordinator.isAnyResourceSet()).thenReturn(false);
request.onResourceReady(harness.resource);

verify(harness.requestListener).onResourceReady(eq(harness.result), any(Number.class), any(Target.class),
anyBoolean(), eq(true));
}

@Test
public void testRequestListenerIsCalledWithNotIsFirstRequestIfRequestCoordinatorReturnsARequestComplete() {
public void testRequestListenerIsCalledWithNotIsFirstRequestIfRequestCoordinatorReturnsResourceSet() {
GenericRequest request = harness.getRequest();
when(harness.requestCoordinator.isAnyRequestComplete()).thenReturn(true);
when(harness.requestCoordinator.isAnyResourceSet()).thenReturn(true);
request.onResourceReady(harness.resource);

verify(harness.requestListener).onResourceReady(eq(harness.result), any(Number.class), any(Target.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand All @@ -16,11 +17,13 @@ public class ThumbnailRequestCoordinatorTest {
private Request full;
private Request thumb;
private ThumbnailRequestCoordinator coordinator;
private RequestCoordinator parent;

@Before
public void setUp() {
full = mock(Request.class);
thumb = mock(Request.class);
parent = mock(RequestCoordinator.class);
coordinator = new ThumbnailRequestCoordinator();
coordinator.setRequests(full, thumb);
}
Expand Down Expand Up @@ -75,22 +78,6 @@ public void testDoesNotStartThumbOnRunIfRunning() {
verify(thumb, never()).begin();
}

@Test
public void testDoesNotAllowThumbToSetPlaceholder() {
assertFalse(coordinator.canNotifyStatusChanged(thumb));
}

@Test
public void testAllowsFullToSetPlaceholder() {
assertTrue(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testDoesNotAllowFullToSetPlaceholderIfThumbComplete() {
when(thumb.isComplete()).thenReturn(true);
assertFalse(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCallsClearOnRequestsWhenCleared() {
coordinator.clear();
Expand Down Expand Up @@ -119,4 +106,161 @@ public void testPausesBothRequestsWhenPaused() {
verify(thumb).pause();
}

@Test
public void testCanSetImageReturnsTrueForFullRequestIfCoordinatorIsNull() {
coordinator = new ThumbnailRequestCoordinator();
coordinator.setRequests(full, thumb);
assertTrue(coordinator.canSetImage(full));
}

@Test
public void testCanSetImageReturnsTrueForFullRequestIfParentAllowsSetImage() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.canSetImage(eq(coordinator))).thenReturn(true);
assertTrue(coordinator.canSetImage(full));
}

@Test
public void testCanSetImageReturnsFalseForFullRequestIfParentDoesNotAllowSetImage() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.canSetImage(eq(coordinator))).thenReturn(false);
assertFalse(coordinator.canSetImage(full));
}

@Test
public void testCanSetImageReturnsTrueForThumbRequestIfParentIsNullAndFullDoesNotHaveResourceSet() {
when(full.isResourceSet()).thenReturn(false);
assertTrue(coordinator.canSetImage(thumb));
}

@Test
public void testCanSetImageReturnsFalseForThumbRequestIfParentIsNullAndFullHasResourceSet() {
when(full.isResourceSet()).thenReturn(true);
assertFalse(coordinator.canSetImage(thumb));
}

@Test
public void testCanSetImageReturnsFalseForThumbRequestIfParentDoesNotAllowSetImageAndFullDoesNotHaveResourceSet() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.canSetImage(eq(coordinator))).thenReturn(false);
when(full.isResourceSet()).thenReturn(false);
assertFalse(coordinator.canSetImage(thumb));
}

@Test
public void testCanNotifyStatusChangedIfFullAndNoRequestsAreComplete() {
assertTrue(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCanNotNotifyStatusChangedIfThumb() {
assertFalse(coordinator.canNotifyStatusChanged(thumb));
}

@Test
public void testCanNotNotifyStatusChangedIfFullHasResourceSet() {
when(full.isResourceSet()).thenReturn(true);
assertFalse(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCanNotNotifyStatusChangedIfThumbHasResourceSet() {
when(thumb.isResourceSet()).thenReturn(true);
assertFalse(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCanNotNotifyStatusChangedIfParentHasResourceSet() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.isAnyResourceSet()).thenReturn(true);
assertFalse(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCanNotifyStatusChangedIfParentAllowsNotify() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.canNotifyStatusChanged(eq(coordinator))).thenReturn(true);
assertTrue(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testCanNotNotifyStatusChangedIfParentDoesNotAllowNotify() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);
when(parent.canNotifyStatusChanged(eq(coordinator))).thenReturn(false);
assertFalse(coordinator.canNotifyStatusChanged(full));
}

@Test
public void testIsAnyResourceSetIsFalseIfNeitherRequestHasResourceSet() {
when(full.isResourceSet()).thenReturn(false);
when(thumb.isResourceSet()).thenReturn(false);
assertFalse(coordinator.isAnyResourceSet());
}

@Test
public void testIsAnyResourceSetIsTrueIfFullHasResourceSet() {
when(full.isResourceSet()).thenReturn(true);
when(thumb.isResourceSet()).thenReturn(false);
assertTrue(coordinator.isAnyResourceSet());
}

@Test
public void testIsAnyResourceSetIsTrueIfThumbHasResourceSet() {
when(full.isResourceSet()).thenReturn(false);
when(thumb.isResourceSet()).thenReturn(true);
assertTrue(coordinator.isAnyResourceSet());
}

@Test
public void testIsAnyResourceSetIsTrueIfParentIsNonNullAndParentHasResourceSet() {
coordinator = new ThumbnailRequestCoordinator(parent);
coordinator.setRequests(full, thumb);

when(parent.isAnyResourceSet()).thenReturn(true);
when(full.isResourceSet()).thenReturn(false);
when(thumb.isResourceSet()).thenReturn(false);

assertTrue(coordinator.isAnyResourceSet());
}

@Test
public void testIsNotCompleteIfNeitherRequestIsComplete() {
assertFalse(coordinator.isComplete());
}

@Test
public void testIsCompleteIfFullIsComplete() {
when(full.isComplete()).thenReturn(true);
assertTrue(coordinator.isComplete());
}

@Test
public void testIsCompleteIfThumbIsComplete() {
when(thumb.isComplete()).thenReturn(true);
assertTrue(coordinator.isComplete());
}

@Test
public void testIsResourceSetIsFalseIfNeitherRequestHasResourceSet() {
assertFalse(coordinator.isResourceSet());
}

@Test
public void testIsResourceSetIsTrueIfFullRequestHasResourceSet() {
when(full.isResourceSet()).thenReturn(true);
assertTrue(coordinator.isResourceSet());
}

@Test
public void testIsResourceSetIsTrueIfThumbRequestHasResourceSet() {
when(thumb.isResourceSet()).thenReturn(true);
assertTrue(coordinator.isResourceSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
Expand All @@ -32,6 +33,7 @@
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -146,6 +148,35 @@ public void testSizeCallbackIsCalledPreDrawIfNoDimensAndNoLayoutParams() {
verify(cb).onSizeReady(eq(width), eq(height));
}

@Test
public void testSizeCallbacksAreCalledInOrderPreDraw() {
SizeReadyCallback[] cbs = new SizeReadyCallback[25];
for (int i = 0; i < cbs.length; i++) {
cbs[i] = mock(SizeReadyCallback.class);
target.getSize(cbs[i]);
}

int width = 100, height = 111;
SizedShadowView shadowView = Robolectric.shadowOf_(view);
shadowView.setWidth(width);
shadowView.setHeight(height);

PreDrawShadowViewTreeObserver shadowObserver = Robolectric.shadowOf_(view.getViewTreeObserver());
shadowObserver.fireOnPreDrawListeners();

InOrder order = inOrder((Object[]) cbs);
for (SizeReadyCallback cb : cbs) {
order.verify(cb).onSizeReady(eq(width), eq(height));
}
}

@Test(expected = IllegalArgumentException.class)
public void testThrowsIfCallbackIsQueuedTwice() {
SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);
target.getSize(cb);
}

@Test
public void testSizeCallbackIsNotCalledPreDrawIfNoDimensSetOnPreDraw() {
SizeReadyCallback cb = mock(SizeReadyCallback.class);
Expand Down
Loading

0 comments on commit f2285a8

Please sign in to comment.