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 static android.support.test.InstrumentationRegistry.getContext; 20 21 import android.content.pm.ResolveInfo; 22 import android.media.tv.TvInputInfo; 23 import android.support.test.filters.SmallTest; 24 25 import com.android.tv.testing.ComparatorTester; 26 27 import org.junit.Test; 28 import org.mockito.Mockito; 29 import org.mockito.invocation.InvocationOnMock; 30 import org.mockito.stubbing.Answer; 31 32 import java.util.ArrayList; 33 import java.util.HashMap; 34 import java.util.List; 35 36 /** 37 * Test for {@link TvInputManagerHelper} 38 */ 39 @SmallTest 40 public class TvInputManagerHelperTest { 41 final HashMap<String, TvInputInfoWrapper> TEST_INPUT_MAP = new HashMap<>(); 42 43 @Test testComparatorInternal()44 public void testComparatorInternal() { 45 ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test"); 46 47 List<TvInputInfo> inputs = new ArrayList<>(); 48 inputs.add(createTvInputInfo(resolveInfo, "2_partner_input", null, 0, false, 49 "2_partner_input", null, true)); 50 inputs.add(createTvInputInfo(resolveInfo, "3_partner_input", null, 0, false, 51 "3_partner_input", null, true)); 52 inputs.add(createTvInputInfo(resolveInfo, "1_3rd_party_input", null, 0, false, 53 "1_3rd_party_input", null, false)); 54 inputs.add(createTvInputInfo(resolveInfo, "4_3rd_party_input", null, 0, false, 55 "4_3rd_party_input", null, false)); 56 57 TvInputManagerHelper manager = createMockTvInputManager(); 58 59 ComparatorTester<TvInputInfo> comparatorTester = ComparatorTester.withoutEqualsTest( 60 new TvInputManagerHelper.InputComparatorInternal(manager)); 61 for (TvInputInfo input : inputs) { 62 comparatorTester.addComparableGroup(input); 63 } 64 comparatorTester.test(); 65 } 66 67 @Test testHardwareInputComparatorHdmi()68 public void testHardwareInputComparatorHdmi() { 69 ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test"); 70 71 TvInputInfo hdmi1 = createTvInputInfo(resolveInfo, "HDMI1", null, TvInputInfo.TYPE_HDMI, 72 true, "HDMI1", null, false); 73 TvInputInfo hdmi2 = createTvInputInfo(resolveInfo, "HDMI2", null, TvInputInfo.TYPE_HDMI, 74 true, "HDMI2", "DVD", false); 75 TvInputInfo hdmi3 = createTvInputInfo(resolveInfo, "HDMI3", null, TvInputInfo.TYPE_HDMI, 76 true, "HDMI3", "Cable", false); 77 TvInputInfo hdmi4 = createTvInputInfo(resolveInfo, "HDMI4", null, TvInputInfo.TYPE_HDMI, 78 true, "HDMI4", null, false); 79 80 TvInputManagerHelper manager = createMockTvInputManager(); 81 82 ComparatorTester<TvInputInfo> comparatorTester = ComparatorTester.withoutEqualsTest( 83 new TvInputManagerHelper.HardwareInputComparator(getContext(), manager)); 84 comparatorTester.addComparableGroup(hdmi3) 85 .addComparableGroup(hdmi2) 86 .addComparableGroup(hdmi1) 87 .addComparableGroup(hdmi4) 88 .test(); 89 } 90 91 @Test testHardwareInputComparatorCec()92 public void testHardwareInputComparatorCec() { 93 ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test"); 94 95 TvInputInfo hdmi1 = createTvInputInfo(resolveInfo, "HDMI1", null, TvInputInfo.TYPE_HDMI, 96 true, "HDMI1", null, false); 97 TvInputInfo hdmi2 = createTvInputInfo(resolveInfo, "HDMI2", null, TvInputInfo.TYPE_HDMI, 98 true, "HDMI2", null, false); 99 100 TvInputInfo cec1 = createTvInputInfo(resolveInfo, "2_cec", "HDMI1", TvInputInfo.TYPE_HDMI, 101 true, "2_cec", null, false); 102 TvInputInfo cec2 = createTvInputInfo(resolveInfo, "1_cec", "HDMI2", TvInputInfo.TYPE_HDMI, 103 true, "1_cec", null, false); 104 105 TvInputManagerHelper manager = createMockTvInputManager(); 106 107 ComparatorTester<TvInputInfo> comparatorTester = ComparatorTester.withoutEqualsTest( 108 new TvInputManagerHelper.HardwareInputComparator(getContext(), manager)); 109 comparatorTester.addComparableGroup(cec1) 110 .addComparableGroup(cec2) 111 .test(); 112 } 113 createMockTvInputManager()114 private TvInputManagerHelper createMockTvInputManager() { 115 TvInputManagerHelper manager = Mockito.mock(TvInputManagerHelper.class); 116 Mockito.doAnswer(new Answer<Boolean>() { 117 @Override 118 public Boolean answer(InvocationOnMock invocation) throws Throwable { 119 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0]; 120 return TEST_INPUT_MAP.get(info.getId()).mIsPartnerInput; 121 } 122 }).when(manager).isPartnerInput(Mockito.<TvInputInfo>any()); 123 Mockito.doAnswer(new Answer<String>() { 124 @Override 125 public String answer(InvocationOnMock invocation) throws Throwable { 126 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0]; 127 return TEST_INPUT_MAP.get(info.getId()).mLabel; 128 } 129 }).when(manager).loadLabel(Mockito.<TvInputInfo>any()); 130 Mockito.doAnswer(new Answer<String>() { 131 @Override 132 public String answer(InvocationOnMock invocation) throws Throwable { 133 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0]; 134 return TEST_INPUT_MAP.get(info.getId()).mCustomLabel; 135 } 136 }).when(manager).loadCustomLabel(Mockito.<TvInputInfo>any()); 137 Mockito.doAnswer(new Answer<TvInputInfo>() { 138 @Override 139 public TvInputInfo answer(InvocationOnMock invocation) throws Throwable { 140 String inputId = (String) invocation.getArguments()[0]; 141 TvInputInfoWrapper inputWrapper = TEST_INPUT_MAP.get(inputId); 142 return inputWrapper == null ? null : inputWrapper.mInput; 143 } 144 }).when(manager).getTvInputInfo(Mockito.<String>any()); 145 return manager; 146 } 147 createTvInputInfo(ResolveInfo service, String id, String parentId, int type, boolean isHardwareInput, String label, String customLabel, boolean isPartnerInput)148 private TvInputInfo createTvInputInfo(ResolveInfo service, String id, 149 String parentId, int type, boolean isHardwareInput, String label, String customLabel, 150 boolean isPartnerInput) { 151 TvInputInfoWrapper inputWrapper = new TvInputInfoWrapper(); 152 try { 153 inputWrapper.mInput = 154 TestUtils.createTvInputInfo(service, id, parentId, type, isHardwareInput); 155 } catch (Exception e) { 156 } 157 inputWrapper.mLabel = label; 158 inputWrapper.mIsPartnerInput = isPartnerInput; 159 inputWrapper.mCustomLabel = customLabel; 160 TEST_INPUT_MAP.put(id, inputWrapper); 161 return inputWrapper.mInput; 162 } 163 164 private static class TvInputInfoWrapper { 165 TvInputInfo mInput; 166 String mLabel; 167 String mCustomLabel; 168 boolean mIsPartnerInput; 169 } 170 } 171