1 /* 2 * Copyright (C) 2015 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 com.android.tv.util; 18 19 import android.content.pm.ResolveInfo; 20 import android.content.pm.ServiceInfo; 21 import android.graphics.drawable.Icon; 22 import android.hardware.hdmi.HdmiDeviceInfo; 23 import android.media.tv.TvInputInfo; 24 import android.os.Build; 25 import android.os.Bundle; 26 27 import java.lang.reflect.Constructor; 28 29 /** 30 * A class that includes convenience methods for testing. 31 */ 32 public class TestUtils { 33 /** 34 * Creates a {@link TvInputInfo}. 35 */ 36 public static TvInputInfo createTvInputInfo(ResolveInfo service, String id, String parentId, 37 int type, boolean isHardwareInput) throws Exception { 38 return createTvInputInfo(service, id, parentId, type, isHardwareInput, false, 0); 39 } 40 41 /** 42 * Creates a {@link TvInputInfo}. 43 * <p> 44 * If this is called on MNC, {@code canRecord} and {@code tunerCount} are ignored. 45 */ 46 public static TvInputInfo createTvInputInfo(ResolveInfo service, String id, String parentId, 47 int type, boolean isHardwareInput, boolean canRecord, int tunerCount) throws Exception { 48 // Create a mock TvInputInfo by using private constructor 49 // Note that mockito doesn't support mock/spy on final object. 50 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 51 return createTvInputInfoForO(service, id, parentId, type, isHardwareInput, canRecord, 52 tunerCount); 53 54 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 55 return createTvInputInfoForNyc(service, id, parentId, type, isHardwareInput, canRecord, 56 tunerCount); 57 } 58 return createTvInputInfoForMnc(service, id, parentId, type, isHardwareInput); 59 } 60 61 /** 62 * private TvInputInfo(ResolveInfo service, String id, int type, boolean isHardwareInput, 63 * CharSequence label, int labelResId, Icon icon, Icon iconStandby, Icon iconDisconnected, 64 * String setupActivity, boolean canRecord, int tunerCount, HdmiDeviceInfo hdmiDeviceInfo, 65 * boolean isConnectedToHdmiSwitch, String parentId, Bundle extras) { 66 */ 67 private static TvInputInfo createTvInputInfoForO(ResolveInfo service, String id, 68 String parentId, int type, boolean isHardwareInput, boolean canRecord, int tunerCount) 69 throws Exception { 70 Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor( 71 ResolveInfo.class, String.class, int.class, boolean.class, CharSequence.class, 72 int.class, Icon.class, Icon.class, Icon.class, String.class, boolean.class, 73 int.class, HdmiDeviceInfo.class, boolean.class, String.class, Bundle.class); 74 constructor.setAccessible(true); 75 return constructor.newInstance(service, id, type, isHardwareInput, null, 0, null, null, 76 null, null, canRecord, tunerCount, null, false, parentId, null); 77 } 78 79 /** 80 * private TvInputInfo(ResolveInfo service, String id, int type, boolean isHardwareInput, 81 * CharSequence label, int labelResId, Icon icon, Icon iconStandby, Icon iconDisconnected, 82 * String setupActivity, String settingsActivity, boolean canRecord, int tunerCount, 83 * HdmiDeviceInfo hdmiDeviceInfo, boolean isConnectedToHdmiSwitch, String parentId, 84 * Bundle extras) { 85 */ 86 private static TvInputInfo createTvInputInfoForNyc(ResolveInfo service, String id, 87 String parentId, int type, boolean isHardwareInput, boolean canRecord, int tunerCount) 88 throws Exception { 89 Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor( 90 ResolveInfo.class, String.class, int.class, boolean.class, CharSequence.class, 91 int.class, Icon.class, Icon.class, Icon.class, String.class, String.class, 92 boolean.class, int.class, HdmiDeviceInfo.class, boolean.class, String.class, 93 Bundle.class); 94 constructor.setAccessible(true); 95 return constructor.newInstance(service, id, type, isHardwareInput, null, 0, null, null, 96 null, null, null, canRecord, tunerCount, null, false, parentId, null); 97 } 98 99 private static TvInputInfo createTvInputInfoForMnc(ResolveInfo service, String id, 100 String parentId, int type, boolean isHardwareInput) throws Exception { 101 Constructor<TvInputInfo> constructor = TvInputInfo.class.getDeclaredConstructor( 102 ResolveInfo.class, String.class, String.class, int.class, boolean.class); 103 constructor.setAccessible(true); 104 return constructor.newInstance(service, id, parentId, type, isHardwareInput); 105 } 106 107 public static ResolveInfo createResolveInfo(String packageName, String name) { 108 ResolveInfo resolveInfo = new ResolveInfo(); 109 resolveInfo.serviceInfo = new ServiceInfo(); 110 resolveInfo.serviceInfo.packageName = packageName; 111 resolveInfo.serviceInfo.name = name; 112 resolveInfo.serviceInfo.metaData = new Bundle(); 113 return resolveInfo; 114 } 115 } 116