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.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.media.tv.TvContract;
24 import android.media.tv.TvInputInfo;
25 import android.media.tv.TvInputManager;
26 import android.os.Bundle;
27 import android.os.Parcel;
28 import android.platform.test.annotations.Presubmit;
29 import android.test.AndroidTestCase;
30 import android.text.TextUtils;
31 
32 /**
33  * Test for {@link android.media.tv.TvInputInfo}.
34  */
35 @Presubmit
36 public class TvInputInfoTest extends AndroidTestCase {
37     private TvInputInfo mStubInfo;
38     private PackageManager mPackageManager;
39 
compareTvInputInfos(Context context, TvInputInfo info1, TvInputInfo info2)40     public static boolean compareTvInputInfos(Context context, TvInputInfo info1,
41             TvInputInfo info2) {
42         return TextUtils.equals(info1.getId(), info2.getId())
43                 && TextUtils.equals(info1.getParentId(), info2.getParentId())
44                 && TextUtils.equals(info1.getServiceInfo().packageName,
45                         info2.getServiceInfo().packageName)
46                 && TextUtils.equals(info1.getServiceInfo().name, info2.getServiceInfo().name)
47                 && TextUtils.equals(info1.createSetupIntent().toString(),
48                          info2.createSetupIntent().toString())
49                 && info1.getType() == info2.getType()
50                 && info1.getTunerCount() == info2.getTunerCount()
51                 && info1.canRecord() == info2.canRecord()
52                 && info1.canPauseRecording() == info2.canPauseRecording()
53                 && info1.isPassthroughInput() == info2.isPassthroughInput()
54                 && TextUtils.equals(info1.loadLabel(context), info2.loadLabel(context));
55     }
56 
57     @Override
setUp()58     public void setUp() throws Exception {
59         if (!Utils.hasTvInputFramework(getContext())) {
60             return;
61         }
62         TvInputManager manager =
63                 (TvInputManager) getContext().getSystemService(Context.TV_INPUT_SERVICE);
64         for (TvInputInfo info : manager.getTvInputList()) {
65             if (info.getServiceInfo().name.equals(
66                     StubTunerTvInputService.class.getName())) {
67                 mStubInfo = info;
68                 break;
69             }
70         }
71         mPackageManager = getContext().getPackageManager();
72     }
73 
testTvInputInfoOp()74     public void testTvInputInfoOp() throws Exception {
75         if (!Utils.hasTvInputFramework(getContext())) {
76             return;
77         }
78         // Test describeContents
79         assertEquals(0, mStubInfo.describeContents());
80 
81         // Test equals
82         assertTrue(mStubInfo.equals(mStubInfo));
83 
84         // Test getId
85         final ComponentName componentName =
86                 new ComponentName(getContext(), StubTunerTvInputService.class);
87         final String id = TvContract.buildInputId(componentName);
88         assertEquals(id, mStubInfo.getId());
89 
90         // Test getServiceInfo
91         assertEquals(getContext().getPackageManager().getServiceInfo(componentName, 0).name,
92                 mStubInfo.getServiceInfo().name);
93 
94         // Test hashCode
95         assertEquals(id.hashCode(), mStubInfo.hashCode());
96 
97         // Test writeToParcel
98         Parcel p = Parcel.obtain();
99         mStubInfo.writeToParcel(p, 0);
100         p.setDataPosition(0);
101         TvInputInfo infoFromParcel = TvInputInfo.CREATOR.createFromParcel(p);
102         assertEquals(mStubInfo.createSetupIntent().getComponent(),
103                 infoFromParcel.createSetupIntent().getComponent());
104         assertEquals(mStubInfo.describeContents(), infoFromParcel.describeContents());
105         assertTrue("expected=" + mStubInfo + " actual=" + infoFromParcel,
106                 TvInputInfoTest.compareTvInputInfos(getContext(), mStubInfo, infoFromParcel));
107         assertEquals(mStubInfo.getId(), infoFromParcel.getId());
108         assertEquals(mStubInfo.getParentId(), infoFromParcel.getParentId());
109         assertEquals(mStubInfo.getServiceInfo().name, infoFromParcel.getServiceInfo().name);
110         assertEquals(mStubInfo.getType(), infoFromParcel.getType());
111         assertEquals(mStubInfo.hashCode(), infoFromParcel.hashCode());
112         assertEquals(mStubInfo.isPassthroughInput(), infoFromParcel.isPassthroughInput());
113         assertEquals(mStubInfo.loadIcon(getContext()).getConstantState(),
114                 infoFromParcel.loadIcon(getContext()).getConstantState());
115         assertEquals(mStubInfo.loadLabel(getContext()), infoFromParcel.loadLabel(getContext()));
116         assertEquals(mStubInfo.toString(), infoFromParcel.toString());
117         p.recycle();
118     }
119 
testGetIntentForSetupActivity()120     public void testGetIntentForSetupActivity() throws Exception {
121         if (!Utils.hasTvInputFramework(getContext())) {
122             return;
123         }
124         Intent intent = mStubInfo.createSetupIntent();
125 
126         assertEquals(intent.getComponent(), new ComponentName(getContext(),
127                 TvInputSetupActivityStub.class));
128         String inputId = intent.getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
129         assertEquals(mStubInfo.getId(), inputId);
130     }
131 
testTunerHasNoParentId()132     public void testTunerHasNoParentId() throws Exception {
133         if (!Utils.hasTvInputFramework(getContext())) {
134             return;
135         }
136         assertNull(mStubInfo.getParentId());
137     }
138 
testGetTypeForTuner()139     public void testGetTypeForTuner() throws Exception {
140         if (!Utils.hasTvInputFramework(getContext())) {
141             return;
142         }
143         assertEquals(mStubInfo.getType(), TvInputInfo.TYPE_TUNER);
144     }
145 
testTunerIsNotPassthroughInput()146     public void testTunerIsNotPassthroughInput() throws Exception {
147         if (!Utils.hasTvInputFramework(getContext())) {
148             return;
149         }
150         assertFalse(mStubInfo.isPassthroughInput());
151     }
152 
testLoadIcon()153     public void testLoadIcon() throws Exception {
154         if (!Utils.hasTvInputFramework(getContext())) {
155             return;
156         }
157         assertEquals(mStubInfo.loadIcon(getContext()).getConstantState(),
158                 mStubInfo.getServiceInfo().loadIcon(mPackageManager).getConstantState());
159     }
160 
testLoadLabel()161     public void testLoadLabel() throws Exception {
162         if (!Utils.hasTvInputFramework(getContext())) {
163             return;
164         }
165         assertEquals(mStubInfo.loadLabel(getContext()),
166                 mStubInfo.getServiceInfo().loadLabel(mPackageManager));
167     }
168 
testIsHidden()169     public void testIsHidden() throws Exception {
170         if (!Utils.hasTvInputFramework(getContext())) {
171             return;
172         }
173         assertFalse(mStubInfo.isHidden(getContext()));
174     }
175 
testLoadCustomLabel()176     public void testLoadCustomLabel() throws Exception {
177         if (!Utils.hasTvInputFramework(getContext())) {
178             return;
179         }
180         assertNull(mStubInfo.loadCustomLabel(getContext()));
181     }
182 
testBuilder()183     public void testBuilder() throws Exception {
184         if (!Utils.hasTvInputFramework(getContext())) {
185             return;
186         }
187         TvInputInfo defaultInfo = new TvInputInfo.Builder(getContext(),
188                 new ComponentName(getContext(), StubTunerTvInputService.class)).build();
189         assertEquals(1, defaultInfo.getTunerCount());
190         assertFalse(defaultInfo.canRecord());
191         assertTrue(defaultInfo.canPauseRecording());
192         assertEquals(mStubInfo.getId(), defaultInfo.getId());
193         assertEquals(mStubInfo.getTunerCount(), defaultInfo.getTunerCount());
194         assertEquals(mStubInfo.canRecord(), defaultInfo.canRecord());
195         assertEquals(mStubInfo.canPauseRecording(), defaultInfo.canPauseRecording());
196 
197         Bundle extras = new Bundle();
198         final String TEST_KEY = "android.media.tv.cts.TEST_KEY";
199         final String TEST_VALUE = "android.media.tv.cts.TEST_VALUE";
200         extras.putString(TEST_KEY, TEST_VALUE);
201         TvInputInfo updatedInfo = new TvInputInfo.Builder(getContext(),
202                 new ComponentName(getContext(), StubTunerTvInputService.class)).setTunerCount(10)
203                 .setCanRecord(true).setCanPauseRecording(false).setExtras(extras).build();
204         assertEquals(mStubInfo.getId(), updatedInfo.getId());
205         assertEquals(10, updatedInfo.getTunerCount());
206         assertTrue(updatedInfo.canRecord());
207         assertFalse(updatedInfo.canPauseRecording());
208         assertEquals(TEST_VALUE, updatedInfo.getExtras().getString(TEST_KEY));
209     }
210 }
211