Skip to content

Commit

Permalink
Stop animated Drawables in ImageViewTarge#clear.
Browse files Browse the repository at this point in the history
Fixes #1087.
  • Loading branch information
sjudd committed Nov 19, 2017
1 parent 9d87dea commit 3dad449
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
super.onLoadCleared(placeholder);
if (animatable != null) {
animatable.stop();
}
setResourceInternal(null);
setDrawable(placeholder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.verify;
import static org.mockito.Mockito.when;

Expand All @@ -19,6 +20,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
Expand All @@ -27,12 +30,15 @@
@Config(manifest = Config.NONE, sdk = 18)
public class ImageViewTargetTest {

@Mock private AnimatedDrawable animatedDrawable;
private ImageView view;
private TestTarget target;
private ColorDrawable drawable;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);

view = new ImageView(RuntimeEnvironment.application);
target = new TestTarget(view);
drawable = new ColorDrawable(Color.RED);
Expand Down Expand Up @@ -117,6 +123,17 @@ public void onResourceReady_withAnimatableResource_startsAnimatableAfterSetResou
order.verify(drawable).start();
}

@Test
public void onLoadCleared_withAnimatableDrawable_stopsDrawable() {
target.onResourceReady(animatedDrawable, /*transition=*/ null);
verify(animatedDrawable).start();
verify(animatedDrawable, never()).stop();

target.onLoadCleared(/*placeholder=*/ null);

verify(animatedDrawable).stop();
}

private abstract static class AnimatedDrawable extends Drawable implements Animatable {
// Intentionally empty.
}
Expand Down

0 comments on commit 3dad449

Please sign in to comment.