@Override public void onClick(View view) { ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("giphy_url", result.images.original.url); clipboard.setPrimaryClip(clip); if (gifDrawable != null) { if (gifDrawable.isRunning()) { gifDrawable.stop(); } else { gifDrawable.start(); } } } });
@Test public void testReturnsFalseFromIsRunningWhenNotRunning() { assertFalse(drawable.isRunning()); }
private void runLoops(int loopCount, int frameCount) { for (int loop = 0; loop < loopCount; loop++) { for (int frame = 0; frame < frameCount; frame++) { when(frameLoader.getCurrentIndex()).thenReturn(frame); assertTrue("drawable should be started before calling drawable.onFrameReady()", drawable.isRunning()); drawable.onFrameReady(); } } }
@Test public void testDoesNotStartOnVisibleTrueIfNotRunning() { drawable.setVisible(true, true); assertFalse(drawable.isRunning()); }
@Test public void testReturnsTrueFromIsRunningWhenRunning() { drawable.setVisible(true, true); drawable.start(); assertTrue(drawable.isRunning()); }
@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 testSetsIsRunningFalseOnStop() { drawable.start(); drawable.stop(); assertFalse(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 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 testStopsOnSetVisibleFalse() { drawable.start(); drawable.setVisible(false, true); assertFalse(drawable.isRunning()); }
@Test public void testDoesNotImmediatelyRunIfStartedWhileNotVisible() { drawable.setVisible(false, false); drawable.start(); assertFalse(drawable.isRunning()); }
@Test public void onFrameReady_whenAttachedtoDrawableCallbackWithViewCallbackParent_doesNotStop() { TransitionDrawable topLevel = new TransitionDrawable(new Drawable[] { drawable }); drawable.setCallback(topLevel); topLevel.setCallback(new View(context)); drawable.start(); drawable.onFrameReady(); assertThat(drawable.isRunning()).isTrue(); }
@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 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 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()); }
@Test public void testStartsOnSetVisibleTrueIfRunning() { drawable.start(); drawable.setVisible(false, false); drawable.setVisible(true, true); assertTrue(drawable.isRunning()); }
@Test public void testDoesNotStartOnSetVisibleIfStartedAndStopped() { drawable.start(); drawable.stop(); drawable.setVisible(true, true); assertFalse(drawable.isRunning()); }
@Test public void onFrameReady_whenAttachedToDrawableCallbackButNotViewCallback_stops() { TransitionDrawable topLevel = new TransitionDrawable(new Drawable[] { drawable }); drawable.setCallback(topLevel); topLevel.setCallback(null); drawable.start(); drawable.onFrameReady(); assertThat(drawable.isRunning()).isFalse(); }
@Test public void testStopsWhenCurrentFrameFinishesIfHasNoCallback() { drawable.setIsRunning(true); drawable.setCallback(null); drawable.onFrameReady(); assertFalse(drawable.isRunning()); }