AnimationTracker { public boolean isTheStartOfANewAnimationSet(final String key, long duration) { CountDownTimer latestAnimationCountdown = timerFactory.getTimer( duration, new Runnable() { @Override public void run() { monitoredAnimations.remove(key); } }); latestAnimationCountdown.start(); CountDownTimer parallelAnimationCountdown = monitoredAnimations.get(key); boolean isANewAnimationSet; if (parallelAnimationCountdown == null) { isANewAnimationSet = true; } else { parallelAnimationCountdown.cancel(); isANewAnimationSet = false; } monitoredAnimations.put(key, latestAnimationCountdown); return isANewAnimationSet; } AnimationTracker(AnimationCounter animationCounter, CountDownTimerFactory timerFactory); boolean isTheStartOfANewAnimationSet(final String key, long duration); void incrementAnimationViewCount(String key); long getCount(String key); void reset(String key); void saveDuration(String key, long duration); long getCurrentDuration(String key, Act act);  }
@Test public void givenTwoAnimationsAreNotPartOfASetWhenWeAskThenThereIsTwoIncrements() throws Exception { tracker.isTheStartOfANewAnimationSet("TestKey1", ANY_DURATION); boolean startOfANewAnimation = tracker.isTheStartOfANewAnimationSet("TestKey2", ANY_DURATION); assertThat(startOfANewAnimation).isTrue(); }
@Test public void givenAnAnimationIsNotPartOfASetWhenWeAskThenItSaysItsANewAnimation() throws Exception { boolean startOfANewAnimation = tracker.isTheStartOfANewAnimationSet("TestKey", ANY_DURATION); assertThat(startOfANewAnimation).isTrue(); }
@Test public void givenAnAnimationIsPartOfASetWhenWeAskThenThereItSaysItIsNotANewAnimation() throws Exception { String setKey = "TestKey"; tracker.isTheStartOfANewAnimationSet(setKey, ANY_DURATION); boolean startOfANewAnimation = tracker.isTheStartOfANewAnimationSet(setKey, ANY_DURATION); assertThat(startOfANewAnimation).isFalse(); }