1 /* 2 * Copyright (C) 2014 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 17 package android.media.tv.cts; 18 19 import android.app.Activity; 20 import android.app.Instrumentation; 21 import android.content.Context; 22 import android.media.tv.TvContract; 23 import android.media.tv.TvInputInfo; 24 import android.media.tv.TvInputManager; 25 import android.media.tv.TvInputService; 26 import android.media.tv.TvView; 27 import android.media.tv.cts.HardwareSessionTest.HardwareProxyTvInputService.CountingSession; 28 import android.net.Uri; 29 import android.test.ActivityInstrumentationTestCase2; 30 31 import android.tv.cts.R; 32 33 import com.android.compatibility.common.util.PollingCheck; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Test {@link android.media.tv.TvInputService.HardwareSession}. 40 */ 41 public class HardwareSessionTest extends ActivityInstrumentationTestCase2<TvViewStubActivity> { 42 /** The maximum time to wait for an operation. */ 43 private static final long TIME_OUT = 15000L; 44 45 private TvView mTvView; 46 private Activity mActivity; 47 private Instrumentation mInstrumentation; 48 private TvInputManager mManager; 49 private TvInputInfo mStubInfo; 50 private final List<TvInputInfo> mPassthroughInputList = new ArrayList<>(); 51 52 public HardwareSessionTest() { 53 super(TvViewStubActivity.class); 54 } 55 56 @Override 57 protected void setUp() throws Exception { 58 super.setUp(); 59 if (!Utils.hasTvInputFramework(getActivity())) { 60 return; 61 } 62 mActivity = getActivity(); 63 mInstrumentation = getInstrumentation(); 64 mTvView = (TvView) mActivity.findViewById(R.id.tvview); 65 mManager = (TvInputManager) mActivity.getSystemService(Context.TV_INPUT_SERVICE); 66 for (TvInputInfo info : mManager.getTvInputList()) { 67 if (info.getServiceInfo().name.equals(HardwareProxyTvInputService.class.getName())) { 68 mStubInfo = info; 69 } 70 if (info.isPassthroughInput()) { 71 mPassthroughInputList.add(info); 72 } 73 } 74 assertNotNull(mStubInfo); 75 } 76 77 public void testHardwareProxyTvInputService() throws Throwable { 78 if (!Utils.hasTvInputFramework(getActivity())) { 79 return; 80 } 81 for (final TvInputInfo info : mPassthroughInputList) { 82 verifyCommandTuneAndHardwareVideoAvailability(info); 83 } 84 } 85 86 public void verifyCommandTuneAndHardwareVideoAvailability(TvInputInfo passthroughInfo) throws 87 Throwable { 88 HardwareProxyTvInputService.sHardwareInputId = passthroughInfo.getId(); 89 Uri fakeChannelUri = TvContract.buildChannelUri(0); 90 mTvView.tune(mStubInfo.getId(), fakeChannelUri); 91 mInstrumentation.waitForIdleSync(); 92 new PollingCheck(TIME_OUT) { 93 @Override 94 protected boolean check() { 95 CountingSession session = HardwareProxyTvInputService.sSession; 96 return session != null && session.mTuneCount > 0 97 && (session.mHardwareVideoAvailableCount > 0 98 || session.mHardwareVideoUnavailableCount > 0); 99 } 100 }.run(); 101 runTestOnUiThread(new Runnable() { 102 @Override 103 public void run() { 104 mTvView.reset(); 105 } 106 }); 107 mInstrumentation.waitForIdleSync(); 108 HardwareProxyTvInputService.sSession = null; 109 } 110 111 public static class HardwareProxyTvInputService extends TvInputService { 112 static String sHardwareInputId; 113 static CountingSession sSession; 114 115 @Override 116 public Session onCreateSession(String inputId) { 117 sSession = new CountingSession(this); 118 return sSession; 119 } 120 121 public static class CountingSession extends HardwareSession { 122 public volatile int mTuneCount; 123 public volatile int mHardwareVideoAvailableCount; 124 public volatile int mHardwareVideoUnavailableCount; 125 126 CountingSession(Context context) { 127 super(context); 128 } 129 130 @Override 131 public void onRelease() { 132 } 133 134 @Override 135 public void onSetCaptionEnabled(boolean enabled) { 136 } 137 138 @Override 139 public String getHardwareInputId() { 140 return sHardwareInputId; 141 } 142 143 @Override 144 public void onSetStreamVolume(float volume) { 145 } 146 147 @Override 148 public boolean onTune(Uri channelUri) { 149 mTuneCount++; 150 return true; 151 } 152 153 @Override 154 public void onHardwareVideoAvailable() { 155 mHardwareVideoAvailableCount++; 156 } 157 158 @Override 159 public void onHardwareVideoUnavailable(int reason) { 160 mHardwareVideoUnavailableCount++; 161 } 162 } 163 } 164 } 165