@Test public void cancelBeforeNextLooperCycle() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); final AnimatorContext testContext = spy(new AnimatorContext("Test")); final MultiAnimator animator = MultiAnimator.animatorFor(fakeView, testContext); animator.translationY(0f); animator.start(); verify(testContext).beginAnimation(any(String.class)); animator.cancel(); verify(testContext).endAnimation(any(String.class)); }
@Test public void end() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); final AnimatorContext testContext = spy(new AnimatorContext("Test")); final MultiAnimator animator = MultiAnimator.animatorFor(fakeView, testContext); animator.translationY(100f); final Animator.AnimatorListener listener = mock(Animator.AnimatorListener.class); animator.addListener(listener); animator.start(); verify(testContext).beginAnimation(any(String.class)); animator.end(); verify(testContext).endAnimation(any(String.class)); verify(listener, never()).onAnimationCancel(animator); verify(listener).onAnimationEnd(animator); assertThat(fakeView.getTranslationY(), is(equalTo(100f))); assertThat(animator.isRunning(), is(false)); }
@Test public void serialization() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); powerUpTimer.schedulePowerUp(PowerUp.SLOW_DOWN_TIME, CountUp.NUMBER_TICKS); assertThat(powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(true)); final Bundle savedState = new Bundle(); powerUpTimer.saveState(savedState); final PowerUpTimer restored = new PowerUpTimer(bus); restored.restoreState(savedState); assertThat(restored.isPending(PowerUp.SLOW_DOWN_TIME), is(true)); assertThat(restored.isPending(PowerUp.MULTIPLY_SCORE), is(false)); assertThat(restored.isPending(PowerUp.CLEAR_ALL), is(false)); }
@Test public void multiplyScore() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); game.newGame(); assertThat(game.multiplyScore(), is(false)); game.board.addPowerUp(PowerUp.MULTIPLY_SCORE); assertThat(game.multiplyScore(), is(true)); assertThat(game.multiplyScore(), is(false)); assertThat(game.powerUpTimer.isPending(PowerUp.MULTIPLY_SCORE), is(true)); assertThat(game.score.getMultiplier(), is(equalTo(2f))); scheduler.advanceBy(PowerUpTimer.TICK_DURATION_MS * PowerUpTimer.STANDARD_NUMBER_TICKS); assertThat(game.powerUpTimer.isPending(PowerUp.MULTIPLY_SCORE), is(false)); assertThat(game.score.getMultiplier(), is(equalTo(1f))); }
@Test public void slowDownTime() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); game.newGame(); assertThat(game.slowDownTime(), is(false)); game.board.addPowerUp(PowerUp.SLOW_DOWN_TIME); assertThat(game.slowDownTime(), is(true)); assertThat(game.slowDownTime(), is(false)); assertThat(game.powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(true)); assertThat(game.countUp.getTickDurationMs(), is(equalTo(2000L))); scheduler.advanceBy(PowerUpTimer.TICK_DURATION_MS * PowerUpTimer.STANDARD_NUMBER_TICKS); assertThat(game.powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(false)); assertThat(game.countUp.getTickDurationMs(), is(not(equalTo(2000L)))); }
@Test public void basicCountDown() { final CountUp countUp = new CountUp(bus); assertThat(countUp.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); countUp.start(); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(10))); assertThat(completed, is(true)); }
@Test public void pause() { final CountUp countUp = new CountUp(bus); assertThat(countUp.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); countUp.start(); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); countUp.pause(); assertThat(countUp.isRunning(), is(true)); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); countUp.resume(); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(10))); assertThat(completed, is(true)); }
@Test public void basicCountUp() { assertThat(timer.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); timer.start(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(10))); assertThat(completed, is(true)); }
@Test public void pause() { assertThat(timer.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); timer.start(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); timer.pause(); assertThat(timer.isRunning(), is(true)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); timer.resume(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(10))); assertThat(completed, is(true)); }
@Test public void setTickDuration() { assertThat(timer.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); timer.setTickDurationMs(200L); timer.start(); scheduler.advanceBy(200L * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); scheduler.advanceBy(400L * 6); assertThat(counter, is(equalTo(10))); assertThat(completed, is(true)); }
@Test public void setNumberOfTicks() { assertThat(timer.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); timer.setNumberOfTicks(12); timer.start(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 5); assertThat(counter, is(equalTo(10))); assertThat(completed, is(false)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 3); assertThat(counter, is(equalTo(12))); assertThat(completed, is(true)); }
/** * Generic method for asserting view animation method functionality * * @param view The animated view * @param trigger A {@link Runnable} that triggers the animation */ protected void assertAnimateLayouts(View view, Runnable trigger) { // The foreground scheduler needs to be paused so that the // temporary visibility of the animated View can be verified. Scheduler foregroundScheduler = ShadowApplication.getInstance() .getForegroundThreadScheduler(); boolean wasPaused = foregroundScheduler.isPaused(); if (!wasPaused) { foregroundScheduler.pause(); } assertThat(view).isGone(); trigger.run(); assertThat(view).isVisible(); Animation animation = view.getAnimation(); assertNotNull(animation); assertThat(animation.getStartTime()) .isLessThanOrEqualTo(AnimationUtils.currentAnimationTimeMillis()); assertThat(animation).hasStartOffset(0); foregroundScheduler.unPause(); assertThat(view).isGone(); if (wasPaused) { foregroundScheduler.pause(); } }
@Test public void idleTasks() throws Exception { final AtomicBoolean taskWasRun = new AtomicBoolean(false); final Runnable task = new Runnable() { @Override public void run() { taskWasRun.set(true); } }; animatorContext.runWhenIdle(task); assertThat(taskWasRun.get(), is(true)); taskWasRun.set(false); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); animatorContext.beginAnimation("Test animation"); animatorContext.runWhenIdle(task); assertThat(taskWasRun.get(), is(false)); // Otherwise the animator context's Handler will // execute anything sent to it immediately. scheduler.pause(); animatorContext.endAnimation("Test animation"); assertThat(taskWasRun.get(), is(false)); animatorContext.beginAnimation("Test animation"); assertThat(taskWasRun.get(), is(false)); animatorContext.endAnimation("Test animation"); scheduler.advanceToLastPostedRunnable(); assertThat(taskWasRun.get(), is(true)); }
@Test public void schedule() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); powerUpTimer.schedulePowerUp(PowerUp.SLOW_DOWN_TIME, PowerUpTimer.STANDARD_NUMBER_TICKS); assertThat(powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(true)); scheduler.advanceBy(PowerUpTimer.TICK_DURATION_MS * PowerUpTimer.STANDARD_NUMBER_TICKS + 100L); assertThat(events.size(), is(equalTo(2))); assertThat(events, hasItem(new PowerUpTimer.PowerUpScheduled(PowerUp.SLOW_DOWN_TIME))); assertThat(events, hasItem(new PowerUpTimer.PowerUpExpired(PowerUp.SLOW_DOWN_TIME))); assertThat(powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(false)); }
@Test public void stop() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); powerUpTimer.schedulePowerUp(PowerUp.SLOW_DOWN_TIME, PowerUpTimer.STANDARD_NUMBER_TICKS); assertThat(powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(true)); powerUpTimer.stop(); scheduler.advanceBy(PowerUpTimer.TICK_DURATION_MS * PowerUpTimer.STANDARD_NUMBER_TICKS + 100L); assertThat(events.size(), is(equalTo(1))); assertThat(powerUpTimer.isPending(PowerUp.SLOW_DOWN_TIME), is(false)); }
@Test public void pause() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); game.newGame(); game.powerUpTimer.schedulePowerUp(PowerUp.MULTIPLY_SCORE, CountUp.NUMBER_TICKS); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * 2L); final long beforePause = game.countUp.getTickDurationMs(); game.pause(); assertThat(game.isPaused(), is(true)); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS + 10L); assertThat(game.countUp.getTickDurationMs(), is(equalTo(beforePause))); assertThat(game.powerUpTimer.isPending(PowerUp.MULTIPLY_SCORE), is(true)); game.resume(); assertThat(game.isPaused(), is(false)); scheduler.advanceBy(CountUp.DEFAULT_TICK_DURATION_MS * CountUp.NUMBER_TICKS * 3); assertThat(game.countUp.getTickDurationMs(), is(not(equalTo(beforePause)))); assertThat(game.powerUpTimer.isPending(PowerUp.MULTIPLY_SCORE), is(false)); game.pause(); assertThat(game.isPaused(), is(true)); game.gameOver(Game.GameOver.How.GAME_LOGIC); assertThat(game.isPaused(), is(false)); }
@Test public void startAfterPause() { assertThat(timer.isRunning(), is(false)); final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); timer.start(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); timer.pause(); assertThat(timer.isRunning(), is(true)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); this.counter = 0; timer.start(); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 4); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); timer.pause(); assertThat(timer.isRunning(), is(true)); scheduler.advanceBy(Timer.DEFAULT_TICK_DURATION_MS * 6); assertThat(counter, is(equalTo(5))); assertThat(completed, is(false)); }
static public void execAllAsyncTask(){ Scheduler bgTask = Robolectric.getBackgroundThreadScheduler(); Scheduler fgTask = Robolectric.getForegroundThreadScheduler(); while (fgTask.size()!=0 || bgTask.size()!=0){ Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Test public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception { Looper mainLooper = Looper.getMainLooper(); Scheduler scheduler = shadowOf(mainLooper).getScheduler(); shadowOf(mainLooper).quit = true; assertThat(Robolectric.application.getMainLooper()).isSameAs(mainLooper); ShadowLooper.resetThreadLoopers(); Robolectric.application = new Application(); assertThat(Looper.getMainLooper()).isSameAs(mainLooper); assertThat(Robolectric.application.getMainLooper()).isSameAs(mainLooper); assertThat(shadowOf(mainLooper).getScheduler()).isNotSameAs(scheduler); assertThat(shadowOf(mainLooper).hasQuit()).isFalse(); }
@Test public void cancelFromAnimeBeforeNextLooperCycle() { final Scheduler scheduler = Robolectric.getForegroundThreadScheduler(); scheduler.pause(); final AnimatorContext testContext = spy(new AnimatorContext("Test")); final MultiAnimator animator = MultiAnimator.animatorFor(fakeView, testContext); animator.translationY(100f); animator.start(); verify(testContext).beginAnimation(any(String.class)); Anime.cancelAll(fakeView); verify(testContext).endAnimation(any(String.class)); assertThat(fakeView.getTranslationY(), is(equalTo(0f))); }
public Scheduler getBackgroundScheduler() { return backgroundScheduler; }
/** * Causes all enqueued tasks to be discarded, and pause state to be reset */ public void reset() { scheduler = new Scheduler(); quit = false; }
private Scheduler getScheduler() { return shadowOf(Looper.getMainLooper()).getScheduler(); }
public static Scheduler getUiThreadScheduler() { return shadowOf(Looper.getMainLooper()).getScheduler(); }
public static Scheduler getBackgroundScheduler() { return getShadowApplication().getBackgroundScheduler(); }
/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return scheduler; }