@Test public void playWhenPrepared() { Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); PlaybackGlue glue = Mockito.spy(new PlaybackGlueImpl(context)); PlaybackGlueHostImpl host = new PlaybackGlueHostImpl(); when(glue.isPrepared()).thenReturn(false); glue.setHost(host); glue.playWhenPrepared(); assertFalse(glue.isPrepared()); Mockito.verify(glue, never()).play(); when(glue.isPrepared()).thenReturn(true); for (PlaybackGlue.PlayerCallback callback: glue.getPlayerCallbacks()) { callback.onPreparedStateChanged(glue); } assertTrue(glue.isPrepared()); Mockito.verify(glue, times(1)).play(); assertEquals(0, glue.getPlayerCallbacks().size()); }
public void playWhenPrepared() { if (isPrepared()) { play(); } else { addPlayerCallback(new PlayerCallback() { @Override public void onPreparedStateChanged(PlaybackGlue glue) { if (glue.isPrepared()) { removePlayerCallback(this); play(); } } }); } }
PlaybackGlue { public void playWhenPrepared() { if (isPrepared()) { play(); } else { addPlayerCallback(new PlayerCallback() { @Override public void onPreparedStateChanged(PlaybackGlue glue) { if (glue.isPrepared()) { removePlayerCallback(this); play(); } } }); } } }
PlaybackGlue { public void playWhenPrepared() { if (isPrepared()) { play(); } else { addPlayerCallback(new PlayerCallback() { @Override public void onPreparedStateChanged(PlaybackGlue glue) { if (glue.isPrepared()) { removePlayerCallback(this); play(); } } }); } } PlaybackGlue(Context context); }
PlaybackGlue { public void playWhenPrepared() { if (isPrepared()) { play(); } else { addPlayerCallback(new PlayerCallback() { @Override public void onPreparedStateChanged(PlaybackGlue glue) { if (glue.isPrepared()) { removePlayerCallback(this); play(); } } }); } } PlaybackGlue(Context context); Context getContext(); boolean isPrepared(); void addPlayerCallback(PlayerCallback playerCallback); void removePlayerCallback(PlayerCallback callback); boolean isPlaying(); void play(); void playWhenPrepared(); void pause(); void next(); void previous(); final void setHost(PlaybackGlueHost host); PlaybackGlueHost getHost(); }
PlaybackGlue { public void playWhenPrepared() { if (isPrepared()) { play(); } else { addPlayerCallback(new PlayerCallback() { @Override public void onPreparedStateChanged(PlaybackGlue glue) { if (glue.isPrepared()) { removePlayerCallback(this); play(); } } }); } } PlaybackGlue(Context context); Context getContext(); boolean isPrepared(); void addPlayerCallback(PlayerCallback playerCallback); void removePlayerCallback(PlayerCallback callback); boolean isPlaying(); void play(); void playWhenPrepared(); void pause(); void next(); void previous(); final void setHost(PlaybackGlueHost host); PlaybackGlueHost getHost();  }