@Test public void savePlayerAdapterEventBeforeAttachToHost() { mContext = new ContextThemeWrapper( InstrumentationRegistry.getInstrumentation().getTargetContext(), androidx.leanback.test.R.style.Theme_Leanback); final PlayerAdapter impl = Mockito.mock(PlayerAdapter.class); when(impl.isPrepared()).thenReturn(true); when(impl.getCurrentPosition()).thenReturn(123L); when(impl.getDuration()).thenReturn(20000L); when(impl.getBufferedPosition()).thenReturn(321L); final PlaybackGlueHost.PlayerCallback hostCallback = Mockito.mock( PlaybackGlueHost.PlayerCallback.class); InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { mGlue = new PlaybackBannerControlGlueImpl(mContext, impl); impl.getCallback().onBufferingStateChanged(impl, true); impl.getCallback().onVideoSizeChanged(impl, 200, 150); impl.getCallback().onError(impl, 12, "abc"); PlaybackGlueHostImpl host = new PlaybackGlueHostImpl(); host.setPlayerCallback(hostCallback); mGlue.setHost(host); } }); Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(true); Mockito.verify(hostCallback, times(1)).onVideoSizeChanged(200, 150); Mockito.verify(hostCallback, times(1)).onError(12, "abc"); Mockito.reset(hostCallback); final PlaybackGlueHost.PlayerCallback hostCallback2 = Mockito.mock( PlaybackGlueHost.PlayerCallback.class); InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { PlaybackGlueHostImpl host = new PlaybackGlueHostImpl(); host.setPlayerCallback(hostCallback2); mGlue.setHost(host); } }); Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(false); Mockito.verify(hostCallback, times(0)).onVideoSizeChanged(anyInt(), anyInt()); Mockito.verify(hostCallback, times(0)).onError(anyInt(), anyString()); Mockito.verify(hostCallback2, times(1)).onBufferingStateChanged(true); Mockito.verify(hostCallback2, times(1)).onVideoSizeChanged(200, 150); Mockito.verify(hostCallback2, times(0)).onError(anyInt(), anyString()); }
@Override public long getCurrentPosition() { int speed; if (mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_PAUSED || mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_NORMAL) { return mPlayerAdapter.getCurrentPosition(); } else if (mPlaybackSpeed >= PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedFastForwardSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = getFastForwardSpeeds()[index]; } else if (mPlaybackSpeed <= -PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedRewindSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = -mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = -getRewindSpeeds()[index]; } else { return -1; } long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed; if (position > getDuration()) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = getDuration(); mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } else if (position < 0) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = 0; mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } return position; }
PlaybackBannerControlGlue extends PlaybackBaseControlGlue<T> { @Override public long getCurrentPosition() { int speed; if (mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_PAUSED || mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_NORMAL) { return mPlayerAdapter.getCurrentPosition(); } else if (mPlaybackSpeed >= PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedFastForwardSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = getFastForwardSpeeds()[index]; } else if (mPlaybackSpeed <= -PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedRewindSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = -mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = -getRewindSpeeds()[index]; } else { return -1; } long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed; if (position > getDuration()) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = getDuration(); mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } else if (position < 0) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = 0; mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } return position; } }
PlaybackBannerControlGlue extends PlaybackBaseControlGlue<T> { @Override public long getCurrentPosition() { int speed; if (mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_PAUSED || mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_NORMAL) { return mPlayerAdapter.getCurrentPosition(); } else if (mPlaybackSpeed >= PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedFastForwardSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = getFastForwardSpeeds()[index]; } else if (mPlaybackSpeed <= -PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedRewindSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = -mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = -getRewindSpeeds()[index]; } else { return -1; } long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed; if (position > getDuration()) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = getDuration(); mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } else if (position < 0) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = 0; mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } return position; } PlaybackBannerControlGlue(Context context,
            int[] seekSpeeds,
            T impl);  PlaybackBannerControlGlue(Context context,
                                    int[] fastForwardSpeeds,
                                    int[] rewindSpeeds,
                                    T impl); }
PlaybackBannerControlGlue extends PlaybackBaseControlGlue<T> { @Override public long getCurrentPosition() { int speed; if (mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_PAUSED || mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_NORMAL) { return mPlayerAdapter.getCurrentPosition(); } else if (mPlaybackSpeed >= PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedFastForwardSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = getFastForwardSpeeds()[index]; } else if (mPlaybackSpeed <= -PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedRewindSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = -mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = -getRewindSpeeds()[index]; } else { return -1; } long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed; if (position > getDuration()) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = getDuration(); mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } else if (position < 0) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = 0; mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } return position; } PlaybackBannerControlGlue(Context context,
            int[] seekSpeeds,
            T impl);  PlaybackBannerControlGlue(Context context,
                                    int[] fastForwardSpeeds,
                                    int[] rewindSpeeds,
                                    T impl); @Override void setControlsRow(PlaybackControlsRow controlsRow); @Override void onActionClicked(Action action); @Override boolean onKey(View v, int keyCode, KeyEvent event); @NonNull int[] getFastForwardSpeeds(); @NonNull int[] getRewindSpeeds(); @Override long getCurrentPosition(); @Override void play(); @Override void pause(); }
PlaybackBannerControlGlue extends PlaybackBaseControlGlue<T> { @Override public long getCurrentPosition() { int speed; if (mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_PAUSED || mPlaybackSpeed == PlaybackControlGlue.PLAYBACK_SPEED_NORMAL) { return mPlayerAdapter.getCurrentPosition(); } else if (mPlaybackSpeed >= PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedFastForwardSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = getFastForwardSpeeds()[index]; } else if (mPlaybackSpeed <= -PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0) { if (mIsCustomizedRewindSupported) { return mPlayerAdapter.getCurrentPosition(); } int index = -mPlaybackSpeed - PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0; speed = -getRewindSpeeds()[index]; } else { return -1; } long position = mStartPosition + (System.currentTimeMillis() - mStartTime) * speed; if (position > getDuration()) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = getDuration(); mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } else if (position < 0) { mPlaybackSpeed = PLAYBACK_SPEED_PAUSED; position = 0; mPlayerAdapter.seekTo(position); mStartPosition = 0; pause(); } return position; } PlaybackBannerControlGlue(Context context,
            int[] seekSpeeds,
            T impl);  PlaybackBannerControlGlue(Context context,
                                    int[] fastForwardSpeeds,
                                    int[] rewindSpeeds,
                                    T impl); @Override void setControlsRow(PlaybackControlsRow controlsRow); @Override void onActionClicked(Action action); @Override boolean onKey(View v, int keyCode, KeyEvent event); @NonNull int[] getFastForwardSpeeds(); @NonNull int[] getRewindSpeeds(); @Override long getCurrentPosition(); @Override void play(); @Override void pause(); static final int ACTION_CUSTOM_LEFT_FIRST; static final int ACTION_SKIP_TO_PREVIOUS; static final int ACTION_REWIND; static final int ACTION_PLAY_PAUSE; static final int ACTION_FAST_FORWARD; static final int ACTION_SKIP_TO_NEXT; static final int ACTION_CUSTOM_RIGHT_FIRST; static final int PLAYBACK_SPEED_INVALID; static final int PLAYBACK_SPEED_PAUSED; static final int PLAYBACK_SPEED_NORMAL; static final int PLAYBACK_SPEED_FAST_L0; static final int PLAYBACK_SPEED_FAST_L1; static final int PLAYBACK_SPEED_FAST_L2; static final int PLAYBACK_SPEED_FAST_L3; static final int PLAYBACK_SPEED_FAST_L4; }