1 package com.android.tv.common.support.tis; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.content.Intent; 6 import android.media.tv.TvContentRating; 7 import android.media.tv.TvInputManager; 8 import android.net.Uri; 9 import android.os.Build; 10 import android.support.annotation.Nullable; 11 import android.view.Surface; 12 13 import com.android.tv.common.support.tis.TifSession.TifSessionCallbacks; 14 import com.android.tv.common.support.tis.TifSession.TifSessionFactory; 15 16 import org.junit.Before; 17 import org.junit.Test; 18 import org.junit.runner.RunWith; 19 import org.robolectric.Robolectric; 20 import org.robolectric.RobolectricTestRunner; 21 import org.robolectric.RuntimeEnvironment; 22 import org.robolectric.android.controller.ServiceController; 23 import org.robolectric.annotation.Config; 24 25 /** Tests for {@link BaseTvInputService}. */ 26 @RunWith(RobolectricTestRunner.class) 27 @Config(minSdk = Build.VERSION_CODES.LOLLIPOP, maxSdk = Build.VERSION_CODES.P) 28 public class BaseTvInputServiceTest { 29 30 private static class TestTvInputService extends BaseTvInputService { 31 32 private final SessionManager sessionManager = new SimpleSessionManager(1); 33 34 private int parentalControlsChangedCount = 0; 35 private final TifSessionFactory sessionFactory; 36 TestTvInputService()37 private TestTvInputService() { 38 super(); 39 this.sessionFactory = 40 new TifSessionFactory() { 41 @Override 42 public TifSession create(TifSessionCallbacks callbacks, String inputId) { 43 return new TifSession(callbacks) { 44 @Override 45 public boolean onSetSurface(@Nullable Surface surface) { 46 return false; 47 } 48 49 @Override 50 public void onSurfaceChanged(int format, int width, int height) {} 51 52 @Override 53 public void onSetStreamVolume(float volume) {} 54 55 @Override 56 public boolean onTune(Uri channelUri) { 57 return false; 58 } 59 60 @Override 61 public void onSetCaptionEnabled(boolean enabled) {} 62 63 @Override 64 public void onUnblockContent(TvContentRating unblockedRating) {} 65 66 @Override 67 public void onParentalControlsChanged() { 68 parentalControlsChangedCount++; 69 } 70 }; 71 } 72 }; 73 } 74 75 @Override getTifSessionFactory()76 protected TifSessionFactory getTifSessionFactory() { 77 return sessionFactory; 78 } 79 80 @Override getSessionManager()81 protected SessionManager getSessionManager() { 82 return sessionManager; 83 } 84 getParentalControlsChangedCount()85 private int getParentalControlsChangedCount() { 86 return parentalControlsChangedCount; 87 } 88 } 89 90 TestTvInputService tvInputService; 91 ServiceController<TestTvInputService> controller; 92 93 @Before setUp()94 public void setUp() { 95 controller = Robolectric.buildService(TestTvInputService.class); 96 tvInputService = controller.create().get(); 97 } 98 99 @Test createSession_once()100 public void createSession_once() { 101 assertThat(tvInputService.onCreateSession("test")).isNotNull(); 102 } 103 104 @Test createSession_twice()105 public void createSession_twice() { 106 WrappedSession first = tvInputService.onCreateSession("test"); 107 assertThat(first).isNotNull(); 108 WrappedSession second = tvInputService.onCreateSession("test"); 109 assertThat(second).isNull(); 110 } 111 112 @Test createSession_release()113 public void createSession_release() { 114 WrappedSession first = tvInputService.onCreateSession("test"); 115 assertThat(first).isNotNull(); 116 first.onRelease(); 117 WrappedSession second = tvInputService.onCreateSession("test"); 118 assertThat(second).isNotNull(); 119 assertThat(second).isNotSameInstanceAs(first); 120 } 121 122 @Test testReceiver_actionEnabledChanged()123 public void testReceiver_actionEnabledChanged() { 124 tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); 125 tvInputService.broadcastReceiver.onReceive( 126 RuntimeEnvironment.application, 127 new Intent(TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED)); 128 assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(1); 129 } 130 131 @Test testReceiver_actionBlockedChanged()132 public void testReceiver_actionBlockedChanged() { 133 tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); 134 tvInputService.broadcastReceiver.onReceive( 135 RuntimeEnvironment.application, 136 new Intent(TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED)); 137 assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(1); 138 } 139 140 @Test testReceiver_invalidAction()141 public void testReceiver_invalidAction() { 142 tvInputService.getSessionManager().addSession(tvInputService.onCreateSession("test")); 143 tvInputService.broadcastReceiver.onReceive( 144 RuntimeEnvironment.application, new Intent("test")); 145 assertThat(tvInputService.getParentalControlsChangedCount()).isEqualTo(0); 146 } 147 } 148