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 android.support.v17.leanback.media; 18 19import static org.junit.Assert.assertNull; 20import static org.mockito.Mockito.atLeastOnce; 21import static org.mockito.Mockito.never; 22import static org.mockito.Mockito.times; 23 24import android.content.Context; 25import android.net.Uri; 26import android.os.Build; 27import android.os.SystemClock; 28import android.support.test.InstrumentationRegistry; 29import android.support.test.filters.MediumTest; 30import android.support.test.filters.SdkSuppress; 31import android.support.test.runner.AndroidJUnit4; 32import android.support.v17.leanback.testutils.PollingCheck; 33 34import org.junit.Test; 35import org.junit.runner.RunWith; 36import org.mockito.Mockito; 37 38@RunWith(AndroidJUnit4.class) 39@MediumTest 40public class MediaPlayerGlueTest { 41 42 /** 43 * Mockito spy not working on API 19 if class has package private method (b/35387610) 44 */ 45 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP) 46 @Test 47 public void mediaPlayer() { 48 // create a MediaPlayerGlue with updatePeriod = 100ms 49 final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 50 final MediaPlayerGlue[] result = new MediaPlayerGlue[1]; 51 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 52 @Override 53 public void run() { 54 result[0] = new MediaPlayerGlue(context); 55 } 56 }); 57 final MediaPlayerGlue glue = Mockito.spy(result[0]); 58 Mockito.when(glue.getUpdatePeriod()).thenReturn(100); 59 60 final PlaybackGlueHostImpl host = new PlaybackGlueHostImpl(); 61 62 glue.setHost(host); 63 glue.setMode(MediaPlayerGlue.REPEAT_ALL); 64 final boolean[] ready = new boolean[] {false}; 65 glue.setPlayerCallback(new PlaybackGlue.PlayerCallback() { 66 @Override 67 public void onReadyForPlayback() { 68 glue.play(); 69 ready[0] = true; 70 71 } 72 }); 73 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 74 @Override 75 public void run() { 76 glue.setMediaSource(Uri.parse( 77 "android.resource://android.support.v17.leanback.test/raw/track_01")); 78 } 79 }); 80 PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() { 81 @Override 82 public boolean canProceed() { 83 return ready[0]; 84 } 85 }); 86 87 // Test setProgressUpdatingEnabled(true) and setProgressUpdatingEnabled(false); 88 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 89 @Override 90 public void run() { 91 glue.enableProgressUpdating(true); 92 } 93 }); 94 Mockito.reset(glue); 95 SystemClock.sleep(1000); 96 Mockito.verify(glue, atLeastOnce()).updateProgress(); 97 98 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 99 @Override 100 public void run() { 101 glue.enableProgressUpdating(false); 102 } 103 }); 104 Mockito.reset(glue); 105 SystemClock.sleep(1000); 106 Mockito.verify(glue, never()).updateProgress(); 107 108 // Test onStart()/onStop() will pause the updateProgress. 109 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 110 @Override 111 public void run() { 112 host.notifyOnStart(); 113 } 114 }); 115 Mockito.reset(glue); 116 SystemClock.sleep(1000); 117 Mockito.verify(glue, atLeastOnce()).updateProgress(); 118 119 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 120 @Override 121 public void run() { 122 host.notifyOnStop(); 123 } 124 }); 125 Mockito.reset(glue); 126 SystemClock.sleep(1000); 127 Mockito.verify(glue, never()).updateProgress(); 128 129 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 130 @Override 131 public void run() { 132 host.notifyOnDestroy(); 133 } 134 }); 135 assertNull(glue.getHost()); 136 Mockito.verify(glue, times(1)).onDetachedFromHost(); 137 Mockito.verify(glue, times(1)).release(); 138 } 139 140} 141