13bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu/*
23bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Copyright (C) 2017 The Android Open Source Project
33bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
43bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Licensed under the Apache License, Version 2.0 (the "License");
53bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * you may not use this file except in compliance with the License.
63bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * You may obtain a copy of the License at
73bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
83bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *      http://www.apache.org/licenses/LICENSE-2.0
93bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Unless required by applicable law or agreed to in writing, software
113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * distributed under the License is distributed on an "AS IS" BASIS,
123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * See the License for the specific language governing permissions and
143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * limitations under the License.
153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */
163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.leanback.media;
183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
19fa39e2bca1b284ad7c931d9194287770b7b507faAurimas Liutikasimport static org.junit.Assert.assertEquals;
203bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport static org.junit.Assert.assertSame;
21fa39e2bca1b284ad7c931d9194287770b7b507faAurimas Liutikasimport static org.junit.Assert.assertTrue;
22290cc49f6210525560e9d6c92fcca3789d28dd7cDake Guimport static org.mockito.Matchers.anyInt;
23290cc49f6210525560e9d6c92fcca3789d28dd7cDake Guimport static org.mockito.Matchers.anyString;
24748e59a062f5d0bf5a8696abff09c649fca0775aDake Guimport static org.mockito.Mockito.never;
253bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport static org.mockito.Mockito.times;
263bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport static org.mockito.Mockito.when;
273bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
283bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.content.Context;
293bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.test.InstrumentationRegistry;
303bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.test.filters.SmallTest;
313bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.view.ContextThemeWrapper;
32748e59a062f5d0bf5a8696abff09c649fca0775aDake Guimport android.view.KeyEvent;
333bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.view.ViewGroup;
343bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.widget.FrameLayout;
353bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.widget.LinearLayout;
363bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
378619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackControlsRow;
388619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackRowPresenter;
398619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackTransportRowPresenter;
408619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.RowPresenter;
418619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikas
423bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport org.junit.Test;
433bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport org.mockito.Mockito;
443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
453bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu@SmallTest
463bcad88cbf4488e747d84893c35f2351b8f84afeDake Gupublic class PlaybackTransportControlGlueTest {
473bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
483bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public static class PlayerAdapterSample extends PlayerAdapter {
493bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        @Override
503bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        public void play() {
513bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
523bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
533bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        @Override
543bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        public void pause() {
553bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
563bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
573bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
583bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public static class PlaybackTransportControlGlueImpl
593bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            extends PlaybackTransportControlGlue {
603bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        public PlaybackTransportControlGlueImpl(Context context) {
613bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            super(context, new PlayerAdapterSample());
623bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
633bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
643bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        public PlaybackTransportControlGlueImpl(Context context, PlayerAdapter impl) {
653bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            super(context, impl);
663bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
673bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
683bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
693bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    Context mContext;
703bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    PlaybackTransportControlGlueImpl mGlue;
713bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    PlaybackTransportRowPresenter.ViewHolder mViewHolder;
72748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    PlayerAdapter mAdapter;
73748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
74748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    void setupWithMockAdapterAndViewHolder() {
75748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mContext = new ContextThemeWrapper(
76748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                InstrumentationRegistry.getInstrumentation().getTargetContext(),
77ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                androidx.leanback.test.R.style.Theme_Leanback);
78748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
79748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mAdapter = Mockito.mock(PlayerAdapter.class);
80748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.isPrepared()).thenReturn(true);
81748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getCurrentPosition()).thenReturn(123L);
82748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getDuration()).thenReturn(20000L);
83748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getBufferedPosition()).thenReturn(321L);
84748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
85748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu            @Override
86748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu            public void run() {
87748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext, mAdapter);
88748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
89748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                mGlue.setHost(host);
90748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
91748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                PlaybackTransportRowPresenter presenter = (PlaybackTransportRowPresenter)
92748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                        mGlue.getPlaybackRowPresenter();
93748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                FrameLayout parent = new FrameLayout(mContext);
94748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                mViewHolder = (PlaybackTransportRowPresenter.ViewHolder)
95748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                        presenter.onCreateViewHolder(parent);
96748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                presenter.onBindViewHolder(mViewHolder, mGlue.getControlsRow());
97748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu            }
98748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        });
99748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
100748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
101748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    void playMockAdapter() {
102748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.play();
103748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).play();
104748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.isPlaying()).thenReturn(true);
105748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mAdapter.getCallback().onPlayStateChanged(mAdapter);
106748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
1073bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1083bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Test
1093bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void usingDefaultRowAndPresenter() {
1103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
1113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            public void run() {
1143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext);
1153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        });
1173bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
1183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1193bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setHost(host);
1203bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(mGlue, host.mGlue);
1213bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host, mGlue.getHost());
1223bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackTransportRowPresenter);
1233bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertTrue(host.mRow instanceof PlaybackControlsRow);
1243bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1253bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1263bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Test
1273bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void customRowPresenter() {
1283bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
1293bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1303bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1313bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            public void run() {
1323bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext);
1333bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1343bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        });
1353bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
1363bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1373bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
1383bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
1393bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1403bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        };
1413bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setPlaybackRowPresenter(presenter);
1423bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
1433bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setHost(host);
1453bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(mGlue, host.mGlue);
1463bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host, mGlue.getHost());
1473bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host.mPlaybackRowPresenter, presenter);
1483bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertTrue(host.mRow instanceof PlaybackControlsRow);
1493bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1503bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1513bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1523bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Test
1533bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void customControlsRow() {
1543bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
1553bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1563bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1573bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            public void run() {
1583bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext);
1593bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1603bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        });
1613bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
1623bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setControlsRow(row);
1633bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
1643bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1653bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setHost(host);
1663bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(mGlue, host.mGlue);
1673bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host, mGlue.getHost());
1683bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackTransportRowPresenter);
1693bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host.mRow, row);
1703bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1713bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1723bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1733bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Test
1743bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void customRowAndPresenter() {
1753bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
1763bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
1773bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1783bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            public void run() {
1793bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext);
1803bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1813bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        });
1823bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
1833bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setControlsRow(row);
1843bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
1853bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
1863bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
1873bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
1883bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
1893bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        };
1903bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setPlaybackRowPresenter(presenter);
1913bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
1923bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1933bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setHost(host);
1943bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(mGlue, host.mGlue);
1953bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host, mGlue.getHost());
1963bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host.mPlaybackRowPresenter, presenter);
1973bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertSame(host.mRow, row);
1983bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1993bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
2003bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
2013bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Test
2023bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void playerAdapterTest() {
203748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
2043bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
2053bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.play();
206748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).play();
2073bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.pause();
208748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).pause();
2093bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.seekTo(123L);
210748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).seekTo(123L);
2113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals(123L, mGlue.getCurrentPosition());
2123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals(20000L, mGlue.getDuration());
2133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals(321L, mGlue.getBufferedPosition());
2143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
215748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        assertSame(mGlue.mAdapterCallback, mAdapter.getCallback());
2163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
217748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getCurrentPosition()).thenReturn(124L);
218748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mAdapter.getCallback().onCurrentPositionChanged(mAdapter);
2193bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals(124L, mGlue.getControlsRow().getCurrentPosition());
2203bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
221748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getBufferedPosition()).thenReturn(333L);
222748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mAdapter.getCallback().onBufferedPositionChanged(mAdapter);
2233bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals(333L, mGlue.getControlsRow().getBufferedPosition());
2243bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
225748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        when(mAdapter.getDuration()).thenReturn((long) (Integer.MAX_VALUE) * 2);
226748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mAdapter.getCallback().onDurationChanged(mAdapter);
2273bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        assertEquals((long) (Integer.MAX_VALUE) * 2, mGlue.getControlsRow().getDuration());
2283bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
2293bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
2303bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
231290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu    @Test
232290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu    public void savePlayerAdapterEventBeforeAttachToHost() {
233290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        mContext = new ContextThemeWrapper(
234290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                InstrumentationRegistry.getInstrumentation().getTargetContext(),
235ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                androidx.leanback.test.R.style.Theme_Leanback);
236290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
237290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        final PlayerAdapter impl = Mockito.mock(PlayerAdapter.class);
238290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        when(impl.isPrepared()).thenReturn(true);
239290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        when(impl.getCurrentPosition()).thenReturn(123L);
240290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        when(impl.getDuration()).thenReturn(20000L);
241290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        when(impl.getBufferedPosition()).thenReturn(321L);
242290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        final PlaybackGlueHost.PlayerCallback hostCallback = Mockito.mock(
243290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                PlaybackGlueHost.PlayerCallback.class);
244290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
245290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            @Override
246290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            public void run() {
247290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                mGlue = new PlaybackTransportControlGlueImpl(mContext, impl);
248290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                // fire events before attach to host.
249290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                impl.getCallback().onBufferingStateChanged(impl, true);
250290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                impl.getCallback().onVideoSizeChanged(impl, 200, 150);
251290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                impl.getCallback().onError(impl, 12, "abc");
252290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
253290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                host.setPlayerCallback(hostCallback);
254290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                mGlue.setHost(host);
255290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            }
256290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        });
257290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
258290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        // when attach to host, should pass the buffering state, video size and last error message
259290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        // to the host.
260290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(true);
261290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(1)).onVideoSizeChanged(200, 150);
262290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(1)).onError(12, "abc");
263290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.reset(hostCallback);
264290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
265290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        final PlaybackGlueHost.PlayerCallback hostCallback2 = Mockito.mock(
266290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                PlaybackGlueHost.PlayerCallback.class);
267290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
268290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            @Override
269290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            public void run() {
270290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
271290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                host.setPlayerCallback(hostCallback2);
272290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu                mGlue.setHost(host);
273290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu            }
274290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        });
275290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
276290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        // when detach from host, should have host stop buffering.
277290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(false);
278290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(0)).onVideoSizeChanged(anyInt(), anyInt());
279290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback, times(0)).onError(anyInt(), anyString());
280290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
281290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        // attach to a different host, buffering state and video size should be saved, one time
282290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        // error state is not saved.
283290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback2, times(1)).onBufferingStateChanged(true);
284290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback2, times(1)).onVideoSizeChanged(200, 150);
285290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu        Mockito.verify(hostCallback2, times(0)).onError(anyInt(), anyString());
286290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu    }
287290cc49f6210525560e9d6c92fcca3789d28dd7cDake Gu
288748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
289748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void playStateReceivePlayPause() {
290748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
291748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        playMockAdapter();
292748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
293748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
294748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
295748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).pause();
296748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
297748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
298748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
299748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void playStateReceivePause() {
300748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
301748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        playMockAdapter();
302748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
303748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PAUSE,
304748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE));
305748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).pause();
306748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
307748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
308748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
309748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void playStateReceivePlay() {
310748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
311748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        playMockAdapter();
312748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
313748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY,
314748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
315748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, never()).pause();
316748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
317748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
318748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
319748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void pauseStateReceivePlayPause() {
320748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
321748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
322748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
323748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
324748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).play();
325748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
326748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
327748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
328748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void pauseStateReceivePause() {
329748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
330748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
331748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PAUSE,
332748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE));
333748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, never()).play();
334748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
335748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
336748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    @Test
337748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    public void pauseStateReceivePlay() {
338748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        setupWithMockAdapterAndViewHolder();
339748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu
340748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        mGlue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY,
341748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
342748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu        Mockito.verify(mAdapter, times(1)).play();
343748e59a062f5d0bf5a8696abff09c649fca0775aDake Gu    }
3443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu}
345