1a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu/*
2a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * Copyright (C) 2017 The Android Open Source Project
3a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu *
4a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * Licensed under the Apache License, Version 2.0 (the "License");
5a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * you may not use this file except in compliance with the License.
6a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * You may obtain a copy of the License at
7a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu *
8a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu *      http://www.apache.org/licenses/LICENSE-2.0
9a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu *
10a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * Unless required by applicable law or agreed to in writing, software
11a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * distributed under the License is distributed on an "AS IS" BASIS,
12a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * See the License for the specific language governing permissions and
14a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu * limitations under the License.
15a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu */
16a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.leanback.media;
18a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
19a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport static org.junit.Assert.assertSame;
20fa39e2bca1b284ad7c931d9194287770b7b507faAurimas Liutikasimport static org.junit.Assert.assertTrue;
21a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport static org.mockito.Mockito.times;
22a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
23a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport android.content.Context;
24a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport android.graphics.drawable.Drawable;
25a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport android.support.test.InstrumentationRegistry;
2604c9e28ea0be0cedcee58e4d29343ac08733d58aAurimas Liutikasimport android.support.test.filters.LargeTest;
278619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.view.ViewGroup;
288619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.widget.LinearLayout;
298619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikas
30ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.PlaybackControlsRow;
31ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.PlaybackControlsRowPresenter;
32ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.PlaybackRowPresenter;
33ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.RowPresenter;
34a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
35a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport org.junit.Test;
36a9a5c79da5845ec44d6eb129c590b68606260b15Dake Guimport org.mockito.Mockito;
37a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
3804c9e28ea0be0cedcee58e4d29343ac08733d58aAurimas Liutikas@LargeTest
39a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gupublic class PlaybackControlGlueTest {
40a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
41a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    public static class PlaybackControlGlueImpl extends PlaybackControlGlue {
42a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
43a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public PlaybackControlGlueImpl(Context context) {
44a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            super(context, new int[] {PLAYBACK_SPEED_FAST_L0, PLAYBACK_SPEED_FAST_L1});
45a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
46a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
47a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
48a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public boolean hasValidMedia() {
49a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return true;
50a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
51a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
52a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
53a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public boolean isMediaPlaying() {
54a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return false;
55a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
56a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
57a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
58a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public CharSequence getMediaTitle() {
59a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return null;
60a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
61a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
62a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
63a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public CharSequence getMediaSubtitle() {
64a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return null;
65a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
66a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
67a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
68a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public int getMediaDuration() {
69a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return 0;
70a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
71a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
72a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
73a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public Drawable getMediaArt() {
74a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return null;
75a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
76a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
77a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
78a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public long getSupportedActions() {
79a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return 0;
80a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
81a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
82a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
83a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public int getCurrentSpeedId() {
84a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return 0;
85a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
86a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
87a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        @Override
88a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        public int getCurrentPosition() {
89a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            return 0;
90a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        }
91a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    }
92a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
93a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    Context mContext;
94a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    PlaybackControlGlue mGlue;
95a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
96a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    @Test
97a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    public void usingDefaultRowAndPresenter() {
98a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
99a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
100a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
101a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            public void run() {
102a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                mGlue = Mockito.spy(new PlaybackControlGlueImpl(mContext));
103a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
104a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        });
105a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
106a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
107a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setHost(host);
108a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        Mockito.verify(mGlue, times(1)).onAttachedToHost(host);
109a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(mGlue, host.mGlue);
110a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host, mGlue.getHost());
111a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackControlsRowPresenter);
112a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertTrue(host.mRow instanceof PlaybackControlsRow);
113a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
114a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    }
115a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    @Test
116a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    public void customRowPresenter() {
117a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
118a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
119a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
120a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            public void run() {
121a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                mGlue = Mockito.spy(new PlaybackControlGlueImpl(mContext));
122a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
123a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        });
124a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
125a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
126a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
127a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
128a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
129a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        };
130a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setPlaybackRowPresenter(presenter);
131a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
132a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
133a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setHost(host);
134a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        Mockito.verify(mGlue, times(1)).onAttachedToHost(host);
135a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(mGlue, host.mGlue);
136a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host, mGlue.getHost());
137a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host.mPlaybackRowPresenter, presenter);
138a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertTrue(host.mRow instanceof PlaybackControlsRow);
139a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
140a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    }
141a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
142a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    @Test
143a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    public void customControlsRow() {
144a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
145a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
146a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
147a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            public void run() {
148a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                mGlue = Mockito.spy(new PlaybackControlGlueImpl(mContext));
149a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
150a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        });
151a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
152a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setControlsRow(row);
153a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
154a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
155a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setHost(host);
156a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        Mockito.verify(mGlue, times(1)).onAttachedToHost(host);
157a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(mGlue, host.mGlue);
158a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host, mGlue.getHost());
159a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertTrue(host.mPlaybackRowPresenter instanceof PlaybackControlsRowPresenter);
160a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host.mRow, row);
161a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
162a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    }
163a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
164a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    @Test
165a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    public void customRowAndPresenter() {
166a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
167a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
168a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
169a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            public void run() {
170a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                mGlue = Mockito.spy(new PlaybackControlGlueImpl(mContext));
171a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
172a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        });
173a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackControlsRow row = new PlaybackControlsRow(mContext);
174a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setControlsRow(row);
175a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackRowPresenter presenter = new PlaybackRowPresenter() {
176a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            @Override
177a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
178a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu                return new RowPresenter.ViewHolder(new LinearLayout(parent.getContext()));
179a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu            }
180a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        };
181a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setPlaybackRowPresenter(presenter);
182a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
183a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
184a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        mGlue.setHost(host);
185a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        Mockito.verify(mGlue, times(1)).onAttachedToHost(host);
186a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(mGlue, host.mGlue);
187a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host, mGlue.getHost());
188a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host.mPlaybackRowPresenter, presenter);
189a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu        assertSame(host.mRow, row);
190a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu
191a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu    }
192a9a5c79da5845ec44d6eb129c590b68606260b15Dake Gu}
193