@Test(expected = IllegalArgumentException.class) public void testThrowsIfGivenLoopCountLessThanZeroAndNotInfinite() { drawable.setLoopCount(-2); }
@Test public void testLoopsForeverIfLoopCountIsLoopIntrinsicAndTotalIterationCountIsForever() { final int frameCount = 3; final int loopCount = 40; when(frameLoader.getLoopCount()).thenReturn(GifDecoder.TOTAL_ITERATION_COUNT_FOREVER); when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(GifDrawable.LOOP_INTRINSIC); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertTrue("drawable should be still running", drawable.isRunning()); }
@Test public void testUsesDecoderTotalLoopCountIfLoopCountIsLoopIntrinsic() { final int frameCount = 3; final int loopCount = 2; when(frameLoader.getLoopCount()).thenReturn(loopCount); when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(GifDrawable.LOOP_INTRINSIC); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Test public void testLoopsThreeTimesIfLoopCountIsSetToThree() { final int loopCount = 3; final int frameCount = 2; when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(loopCount); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Test public void testLoopsASingleTimeIfLoopCountIsSetToOne() { final int loopCount = 1; final int frameCount = 2; when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(loopCount); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Test public void testChangingTheLoopCountAfterHittingTheMaxLoopCount() { final int initialLoopCount = 1; final int frameCount = 2; when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(initialLoopCount); drawable.setVisible(true, true); drawable.start(); runLoops(initialLoopCount, frameCount); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); final int newLoopCount = 2; drawable.setLoopCount(newLoopCount); drawable.start(); runLoops(newLoopCount, frameCount); int numStarts = 2; int expectedFrames = (initialLoopCount + newLoopCount) * frameCount + numStarts; verify(cb, times(expectedFrames)).invalidateDrawable(eq(drawable)); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Test public void testCallingStartResetsLoopCounter() { when(frameLoader.getFrameCount()).thenReturn(2); drawable.setLoopCount(1); drawable.setVisible(true, true); drawable.start(); drawable.onFrameReady(); when(frameLoader.getCurrentIndex()).thenReturn(1); drawable.onFrameReady(); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); drawable.start(); when(frameLoader.getCurrentIndex()).thenReturn(0); drawable.onFrameReady(); when(frameLoader.getCurrentIndex()).thenReturn(1); drawable.onFrameReady(); // 4 onFrameReady(), 2 start() verify(cb, times(4 + 2)).invalidateDrawable(eq(drawable)); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Test public void testLoopsForeverIfLoopCountIsSetToLoopForever() { final int loopCount = 40; final int frameCount = 2; when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(GifDrawable.LOOP_FOREVER); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertTrue("drawable should be still running", drawable.isRunning()); }
@Test public void testLoopsOnceIfLoopCountIsSetToOneWithThreeFrames() { final int loopCount = 1; final int frameCount = 3; when(frameLoader.getFrameCount()).thenReturn(frameCount); drawable.setLoopCount(loopCount); drawable.setVisible(true, true); drawable.start(); runLoops(loopCount, frameCount); verifyRanLoops(loopCount, frameCount); assertFalse("drawable should be stopped after loop is completed", drawable.isRunning()); }
@Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { Log.i(TAG, "onResourceReady: " + resource); mPlaceHolderDrawable.setDrawable(resource); if (resource instanceof GifDrawable) { ((GifDrawable) resource).setLoopCount(GifDrawable.LOOP_FOREVER); ((GifDrawable) resource).start(); } mPlaceHolderDrawable.invalidateSelf(); }
@Override public void onLoadStarted(@Nullable Drawable placeholder) { Log.i(TAG, "onLoadCleared: " + placeholder); mPlaceHolderDrawable.setDrawable(placeholder); if (placeholder instanceof GifDrawable) { ((GifDrawable) placeholder).setLoopCount(GifDrawable.LOOP_FOREVER); ((GifDrawable) placeholder).start(); } mPlaceHolderDrawable.invalidateSelf(); }
@Override public void onLoadFailed(@Nullable Drawable errorDrawable) { Log.i(TAG, "onLoadFailed: " + errorDrawable); mPlaceHolderDrawable.setDrawable(errorDrawable); if (errorDrawable instanceof GifDrawable) { ((GifDrawable) errorDrawable).setLoopCount(GifDrawable.LOOP_FOREVER); ((GifDrawable) errorDrawable).start(); } mPlaceHolderDrawable.invalidateSelf(); }
@Override public void onLoadCleared(@Nullable Drawable placeholder) { Log.i(TAG, "onLoadCleared: " + placeholder); if (mPlaceHolderDrawable.getDrawable() != null) { return; } mPlaceHolderDrawable.setDrawable(placeholder); if (placeholder instanceof GifDrawable) { ((GifDrawable) placeholder).setLoopCount(GifDrawable.LOOP_FOREVER); ((GifDrawable) placeholder).start(); } mPlaceHolderDrawable.invalidateSelf(); }
@Override public void onResourceReady(GifDrawable resource, GlideAnimation<? super GifDrawable> glideAnimation) { int w = MeasureUtil.getScreenSize(mContext).x; int hh=resource.getIntrinsicHeight(); int ww=resource.getIntrinsicWidth() ; int high = hh * (w - 50)/ww; Rect rect = new Rect(20, 20,w-30,high); resource.setBounds(rect); urlDrawable.setBounds(rect); urlDrawable.setDrawable(resource); gifDrawables.add(resource); resource.setCallback(mTextView); resource.start(); resource.setLoopCount(GlideDrawable.LOOP_FOREVER); mTextView.setText(mTextView.getText()); mTextView.invalidate(); } }