1a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin/*
2a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * Copyright (C) 2017 The Android Open Source Project
3a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin *
4a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * Licensed under the Apache License, Version 2.0 (the "License");
5a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * you may not use this file except in compliance with the License.
6a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * You may obtain a copy of the License at
7a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin *
8a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin *      http://www.apache.org/licenses/LICENSE-2.0
9a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin *
10a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * Unless required by applicable law or agreed to in writing, software
11a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * distributed under the License is distributed on an "AS IS" BASIS,
12a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * See the License for the specific language governing permissions and
14a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin * limitations under the License.
15a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin */
16a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.leanback.media;
18a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
19fa39e2bca1b284ad7c931d9194287770b7b507faAurimas Liutikasimport static org.junit.Assert.assertEquals;
20a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport static org.junit.Assert.assertSame;
21fa39e2bca1b284ad7c931d9194287770b7b507faAurimas Liutikasimport static org.junit.Assert.assertTrue;
22a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport static org.mockito.Matchers.anyInt;
23a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport static org.mockito.Matchers.anyString;
24a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport static org.mockito.Mockito.times;
25a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport static org.mockito.Mockito.when;
26a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
27a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.content.Context;
28a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.support.test.InstrumentationRegistry;
29a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.support.test.filters.SmallTest;
30a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.view.ContextThemeWrapper;
31a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.view.ViewGroup;
32a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.widget.FrameLayout;
33a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport android.widget.LinearLayout;
34a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
358619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackControlsRow;
368619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackControlsRowPresenter;
378619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.PlaybackRowPresenter;
388619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport androidx.leanback.widget.RowPresenter;
398619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikas
40a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport org.junit.Test;
41a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinimport org.mockito.Mockito;
42a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
43a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin@SmallTest
44a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsinpublic class PlaybackBannerControlGlueTest {
45a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
46a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public static class PlayerAdapterSample extends PlayerAdapter {
47a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        @Override
48a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        public void play() {
49a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        }
50a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
51a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        @Override
52a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        public void pause() {
53a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        }
54a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
55a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
56a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public static class PlaybackBannerControlGlueImpl
57a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            extends PlaybackBannerControlGlue {
58a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        public PlaybackBannerControlGlueImpl(Context context) {
59a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            super(context, new int[] {1, 2 , 3, 4, 5}, new PlayerAdapterSample());
60a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        }
61a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
62a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        public PlaybackBannerControlGlueImpl(Context context, PlayerAdapter impl) {
63a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            super(context, new int[] {1, 2 , 3, 4, 5}, impl);
64a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        }
65a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
66a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
67a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    Context mContext;
68a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    PlaybackBannerControlGlueImpl mGlue;
69a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    PlaybackControlsRowPresenter.ViewHolder mViewHolder;
70a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
71a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
72a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void usingDefaultRowAndPresenter() {
73a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
74a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
75a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
76a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
77a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext);
78a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
79a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
80a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
81a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
82a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setHost(host);
83a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(mGlue, host.mGlue);
84a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host, mGlue.getHost());
85a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackControlsRowPresenter);
86a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertTrue(host.mRow instanceof PlaybackControlsRow);
87a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
88a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
89a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
90a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void customRowPresenter() {
91a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
92a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
93a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
94a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
95a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext);
96a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
97a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
98a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
99a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
100a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
101a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
102a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
103a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        };
104a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setPlaybackRowPresenter(presenter);
105a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
106a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
107a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setHost(host);
108a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(mGlue, host.mGlue);
109a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host, mGlue.getHost());
110a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host.mPlaybackRowPresenter, presenter);
111a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertTrue(host.mRow instanceof PlaybackControlsRow);
112a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
113a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
114a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
115a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
116a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void customControlsRow() {
117a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
118a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
119a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
120a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
121a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext);
122a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
123a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
124a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
125a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setControlsRow(row);
126a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
127a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
128a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setHost(host);
129a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(mGlue, host.mGlue);
130a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host, mGlue.getHost());
131a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackControlsRowPresenter);
132a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host.mRow, row);
133a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
134a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
135a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
136a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
137a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void customRowAndPresenter() {
138a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
139a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
140a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
141a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
142a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext);
143a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
144a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
145a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
146a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setControlsRow(row);
147a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
148a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
149a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
150a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
151a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
152a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        };
153a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setPlaybackRowPresenter(presenter);
154a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
155a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
156a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.setHost(host);
157a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(mGlue, host.mGlue);
158a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host, mGlue.getHost());
159a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host.mPlaybackRowPresenter, presenter);
160a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(host.mRow, row);
161a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
162a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
163a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
164a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
165a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void playerAdapterTest() {
166a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = new ContextThemeWrapper(
167a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                InstrumentationRegistry.getInstrumentation().getTargetContext(),
168ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                androidx.leanback.test.R.style.Theme_Leanback);
169a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
170a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        final PlayerAdapter impl = Mockito.mock(PlayerAdapter.class);
171a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.isPrepared()).thenReturn(true);
172a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getCurrentPosition()).thenReturn(123L);
173a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getDuration()).thenReturn(20000L);
174a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getBufferedPosition()).thenReturn(321L);
175a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
176a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
177a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
178a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext, impl);
179a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
180a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue.setHost(host);
181a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
182a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackControlsRowPresenter presenter = (PlaybackControlsRowPresenter)
183a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                        mGlue.getPlaybackRowPresenter();
184a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                FrameLayout parent = new FrameLayout(mContext);
185a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mViewHolder = (PlaybackControlsRowPresenter.ViewHolder)
186a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                        presenter.onCreateViewHolder(parent);
187a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                presenter.onBindViewHolder(mViewHolder, mGlue.getControlsRow());
188a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
189a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
190a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
191a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
192a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.play();
193a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(impl, times(1)).play();
194a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.pause();
195a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(impl, times(1)).pause();
196a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mGlue.seekTo(123L);
197a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // one call for play() and one call for seekTo()
198a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(impl, times(2)).seekTo(123L);
199a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals(123L, mGlue.getCurrentPosition());
200a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals(20000L, mGlue.getDuration());
201a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals(321L, mGlue.getBufferedPosition());
202a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
203a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertSame(mGlue.mAdapterCallback, impl.getCallback());
204a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
205a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getCurrentPosition()).thenReturn(124L);
206a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        impl.getCallback().onCurrentPositionChanged(impl);
207a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals(124L, mGlue.getControlsRow().getCurrentPosition());
208a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
209a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getBufferedPosition()).thenReturn(333L);
210a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        impl.getCallback().onBufferedPositionChanged(impl);
211a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals(333L, mGlue.getControlsRow().getBufferedPosition());
212a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
213a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getDuration()).thenReturn((long) (Integer.MAX_VALUE) * 2);
214a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        impl.getCallback().onDurationChanged(impl);
215a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        assertEquals((long) (Integer.MAX_VALUE) * 2, mGlue.getControlsRow().getDuration());
216a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
217a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
218a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
219a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    @Test
220a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    public void savePlayerAdapterEventBeforeAttachToHost() {
221a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        mContext = new ContextThemeWrapper(
222a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                InstrumentationRegistry.getInstrumentation().getTargetContext(),
223ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                androidx.leanback.test.R.style.Theme_Leanback);
224a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
225a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        final PlayerAdapter impl = Mockito.mock(PlayerAdapter.class);
226a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.isPrepared()).thenReturn(true);
227a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getCurrentPosition()).thenReturn(123L);
228a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getDuration()).thenReturn(20000L);
229a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        when(impl.getBufferedPosition()).thenReturn(321L);
230a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        final PlaybackGlueHost.PlayerCallback hostCallback = Mockito.mock(
231a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackGlueHost.PlayerCallback.class);
232a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
233a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
234a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
235a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue = new PlaybackBannerControlGlueImpl(mContext, impl);
236a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                // fire events before attach to host.
237a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                impl.getCallback().onBufferingStateChanged(impl, true);
238a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                impl.getCallback().onVideoSizeChanged(impl, 200, 150);
239a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                impl.getCallback().onError(impl, 12, "abc");
240a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
241a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                host.setPlayerCallback(hostCallback);
242a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue.setHost(host);
243a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
244a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
245a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
246a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // when attach to host, should pass the buffering state, video size and last error message
247a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // to the host.
248a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(true);
249a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(1)).onVideoSizeChanged(200, 150);
250a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(1)).onError(12, "abc");
251a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.reset(hostCallback);
252a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
253a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        final PlaybackGlueHost.PlayerCallback hostCallback2 = Mockito.mock(
254a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackGlueHost.PlayerCallback.class);
255a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
256a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            @Override
257a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            public void run() {
258a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
259a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                host.setPlayerCallback(hostCallback2);
260a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin                mGlue.setHost(host);
261a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin            }
262a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        });
263a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
264a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // when detach from host, should have host stop buffering.
265a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(1)).onBufferingStateChanged(false);
266a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(0)).onVideoSizeChanged(anyInt(), anyInt());
267a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback, times(0)).onError(anyInt(), anyString());
268a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
269a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // attach to a different host, buffering state and video size should be saved, one time
270a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        // error state is not saved.
271a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback2, times(1)).onBufferingStateChanged(true);
272a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback2, times(1)).onVideoSizeChanged(200, 150);
273a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin        Mockito.verify(hostCallback2, times(0)).onError(anyInt(), anyString());
274a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin    }
275a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin
276a4952dd9e1ddd678743f49d9d728b9b8c5fc7232Wei-Hsin}
277