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