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