PlaybackGlueTest.java revision ac5fe7c617c66850fff75a9fce9979c6e5674b0f
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.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertSame;
23import static org.junit.Assert.assertTrue;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.times;
26import static org.mockito.Mockito.when;
27
28import android.content.Context;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mockito;
36
37@RunWith(AndroidJUnit4.class)
38@SmallTest
39public class PlaybackGlueTest {
40
41
42    public static class PlaybackGlueImpl extends PlaybackGlue {
43
44        public PlaybackGlueImpl(Context context) {
45            super(context);
46        }
47    }
48
49    @Test
50    public void glueAndHostInteraction() {
51        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
52        PlaybackGlue glue = Mockito.spy(new PlaybackGlueImpl(context));
53        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
54
55        glue.setHost(host);
56        Mockito.verify(glue, times(1)).onAttachedToHost(host);
57        assertSame(glue, host.mGlue);
58        assertSame(host, glue.getHost());
59
60        host.notifyOnStart();
61        Mockito.verify(glue, times(1)).onHostStart();
62
63        host.notifyOnResume();
64        Mockito.verify(glue, times(1)).onHostResume();
65
66        host.notifyOnPause();
67        Mockito.verify(glue, times(1)).onHostPause();
68
69        host.notifyOnStop();
70        Mockito.verify(glue, times(1)).onHostStop();
71
72        PlaybackGlue glue2 = Mockito.spy(new PlaybackGlueImpl(context));
73        glue2.setHost(host);
74        Mockito.verify(glue, times(1)).onDetachedFromHost();
75        Mockito.verify(glue2, times(1)).onAttachedToHost(host);
76        assertSame(glue2, host.mGlue);
77        assertSame(host, glue2.getHost());
78        assertNull(glue.getHost());
79
80        host.notifyOnDestroy();
81        assertNull(glue2.getHost());
82        assertNull(host.mGlue);
83        Mockito.verify(glue2, times(1)).onDetachedFromHost();
84    }
85
86    @Test
87    public void listenerModification() {
88        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
89        PlaybackGlue glue = Mockito.spy(new PlaybackGlueImpl(context));
90        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
91
92        glue.setHost(host);
93        final boolean[] called = new boolean[] {false, false};
94        glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
95            @Override
96            public void onPreparedStateChanged(PlaybackGlue glue) {
97                called[0] = true;
98            }
99        });
100        glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
101            @Override
102            public void onPreparedStateChanged(PlaybackGlue glue) {
103                glue.removePlayerCallback(this);
104            }
105        });
106        glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
107            @Override
108            public void onPreparedStateChanged(PlaybackGlue glue) {
109                called[1] = true;
110            }
111        });
112
113        for (PlaybackGlue.PlayerCallback callback: glue.getPlayerCallbacks()) {
114            callback.onPreparedStateChanged(glue);
115        }
116        assertTrue(called[0]);
117        assertTrue(called[1]);
118        assertEquals(2, glue.getPlayerCallbacks().size());
119    }
120
121    @Test
122    public void playWhenPrepared() {
123        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
124        PlaybackGlue glue = Mockito.spy(new PlaybackGlueImpl(context));
125        PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
126
127        when(glue.isPrepared()).thenReturn(false);
128        glue.setHost(host);
129        glue.playWhenPrepared();
130        assertFalse(glue.isPrepared());
131        Mockito.verify(glue, never()).play();
132
133        when(glue.isPrepared()).thenReturn(true);
134        for (PlaybackGlue.PlayerCallback callback: glue.getPlayerCallbacks()) {
135            callback.onPreparedStateChanged(glue);
136        }
137        assertTrue(glue.isPrepared());
138        Mockito.verify(glue, times(1)).play();
139        assertEquals(0, glue.getPlayerCallbacks().size());
140    }
141}
142