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 androidx.test.InstrumentationRegistry.getContext;
20 
21 import android.content.pm.ResolveInfo;
22 import android.media.tv.TvInputInfo;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.tv.testing.ComparatorTester;
28 import com.android.tv.testing.utils.TestUtils;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentMatchers;
33 import org.mockito.Mockito;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36 
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 
41 /** Test for {@link TvInputManagerHelper} */
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public class TvInputManagerHelperTest {
45     final HashMap<String, TvInputInfoWrapper> TEST_INPUT_MAP = new HashMap<>();
46 
47     @Test
testComparatorInternal()48     public void testComparatorInternal() {
49         ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test");
50 
51         List<TvInputInfo> inputs = new ArrayList<>();
52         inputs.add(
53                 createTvInputInfo(
54                         resolveInfo,
55                         "2_partner_input",
56                         null,
57                         0,
58                         false,
59                         "2_partner_input",
60                         null,
61                         true));
62         inputs.add(
63                 createTvInputInfo(
64                         resolveInfo,
65                         "3_partner_input",
66                         null,
67                         0,
68                         false,
69                         "3_partner_input",
70                         null,
71                         true));
72         inputs.add(
73                 createTvInputInfo(
74                         resolveInfo,
75                         "1_3rd_party_input",
76                         null,
77                         0,
78                         false,
79                         "1_3rd_party_input",
80                         null,
81                         false));
82         inputs.add(
83                 createTvInputInfo(
84                         resolveInfo,
85                         "4_3rd_party_input",
86                         null,
87                         0,
88                         false,
89                         "4_3rd_party_input",
90                         null,
91                         false));
92 
93         TvInputManagerHelper manager = createMockTvInputManager();
94 
95         ComparatorTester comparatorTester =
96                 new ComparatorTester(new TvInputManagerHelper.InputComparatorInternal(manager))
97                         .permitInconsistencyWithEquals();
98         for (TvInputInfo input : inputs) {
99             comparatorTester.addEqualityGroup(input);
100         }
101         comparatorTester.testCompare();
102     }
103 
104     @Test
testHardwareInputComparatorHdmi()105     public void testHardwareInputComparatorHdmi() {
106         ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test");
107 
108         TvInputInfo hdmi1 =
109                 createTvInputInfo(
110                         resolveInfo,
111                         "HDMI1",
112                         null,
113                         TvInputInfo.TYPE_HDMI,
114                         true,
115                         "HDMI1",
116                         null,
117                         false);
118         TvInputInfo hdmi2 =
119                 createTvInputInfo(
120                         resolveInfo,
121                         "HDMI2",
122                         null,
123                         TvInputInfo.TYPE_HDMI,
124                         true,
125                         "HDMI2",
126                         "DVD",
127                         false);
128         TvInputInfo hdmi3 =
129                 createTvInputInfo(
130                         resolveInfo,
131                         "HDMI3",
132                         null,
133                         TvInputInfo.TYPE_HDMI,
134                         true,
135                         "HDMI3",
136                         "Cable",
137                         false);
138         TvInputInfo hdmi4 =
139                 createTvInputInfo(
140                         resolveInfo,
141                         "HDMI4",
142                         null,
143                         TvInputInfo.TYPE_HDMI,
144                         true,
145                         "HDMI4",
146                         null,
147                         false);
148 
149         TvInputManagerHelper manager = createMockTvInputManager();
150 
151         ComparatorTester comparatorTester =
152                 new ComparatorTester(
153                                 new TvInputManagerHelper.HardwareInputComparator(
154                                         getContext(), manager))
155                         .permitInconsistencyWithEquals();
156         comparatorTester
157                 .addEqualityGroup(hdmi3)
158                 .addEqualityGroup(hdmi2)
159                 .addEqualityGroup(hdmi1)
160                 .addEqualityGroup(hdmi4)
161                 .testCompare();
162     }
163 
164     @Test
testHardwareInputComparatorCec()165     public void testHardwareInputComparatorCec() {
166         ResolveInfo resolveInfo = TestUtils.createResolveInfo("test", "test");
167 
168         TvInputInfo hdmi1 =
169                 createTvInputInfo(
170                         resolveInfo,
171                         "HDMI1",
172                         null,
173                         TvInputInfo.TYPE_HDMI,
174                         true,
175                         "HDMI1",
176                         null,
177                         false);
178         TvInputInfo hdmi2 =
179                 createTvInputInfo(
180                         resolveInfo,
181                         "HDMI2",
182                         null,
183                         TvInputInfo.TYPE_HDMI,
184                         true,
185                         "HDMI2",
186                         null,
187                         false);
188 
189         TvInputInfo cec1 =
190                 createTvInputInfo(
191                         resolveInfo,
192                         "2_cec",
193                         "HDMI1",
194                         TvInputInfo.TYPE_HDMI,
195                         true,
196                         "2_cec",
197                         null,
198                         false);
199         TvInputInfo cec2 =
200                 createTvInputInfo(
201                         resolveInfo,
202                         "1_cec",
203                         "HDMI2",
204                         TvInputInfo.TYPE_HDMI,
205                         true,
206                         "1_cec",
207                         null,
208                         false);
209 
210         TvInputManagerHelper manager = createMockTvInputManager();
211 
212         ComparatorTester comparatorTester =
213                 new ComparatorTester(
214                                 new TvInputManagerHelper.HardwareInputComparator(
215                                         getContext(), manager))
216                         .permitInconsistencyWithEquals();
217         comparatorTester.addEqualityGroup(cec1).addEqualityGroup(cec2).testCompare();
218     }
219 
createMockTvInputManager()220     private TvInputManagerHelper createMockTvInputManager() {
221         TvInputManagerHelper manager = Mockito.mock(TvInputManagerHelper.class);
222         Mockito.doAnswer(
223                         new Answer<Boolean>() {
224                             @Override
225                             public Boolean answer(InvocationOnMock invocation) throws Throwable {
226                                 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0];
227                                 return TEST_INPUT_MAP.get(info.getId()).mIsPartnerInput;
228                             }
229                         })
230                 .when(manager)
231                 .isPartnerInput(ArgumentMatchers.<TvInputInfo>any());
232         Mockito.doAnswer(
233                         new Answer<String>() {
234                             @Override
235                             public String answer(InvocationOnMock invocation) throws Throwable {
236                                 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0];
237                                 return TEST_INPUT_MAP.get(info.getId()).mLabel;
238                             }
239                         })
240                 .when(manager)
241                 .loadLabel(ArgumentMatchers.<TvInputInfo>any());
242         Mockito.doAnswer(
243                         new Answer<String>() {
244                             @Override
245                             public String answer(InvocationOnMock invocation) throws Throwable {
246                                 TvInputInfo info = (TvInputInfo) invocation.getArguments()[0];
247                                 return TEST_INPUT_MAP.get(info.getId()).mCustomLabel;
248                             }
249                         })
250                 .when(manager)
251                 .loadCustomLabel(ArgumentMatchers.<TvInputInfo>any());
252         Mockito.doAnswer(
253                         new Answer<TvInputInfo>() {
254                             @Override
255                             public TvInputInfo answer(InvocationOnMock invocation)
256                                     throws Throwable {
257                                 String inputId = (String) invocation.getArguments()[0];
258                                 TvInputInfoWrapper inputWrapper = TEST_INPUT_MAP.get(inputId);
259                                 return inputWrapper == null ? null : inputWrapper.mInput;
260                             }
261                         })
262                 .when(manager)
263                 .getTvInputInfo(ArgumentMatchers.<String>any());
264         return manager;
265     }
266 
createTvInputInfo( ResolveInfo service, String id, String parentId, int type, boolean isHardwareInput, String label, String customLabel, boolean isPartnerInput)267     private TvInputInfo createTvInputInfo(
268             ResolveInfo service,
269             String id,
270             String parentId,
271             int type,
272             boolean isHardwareInput,
273             String label,
274             String customLabel,
275             boolean isPartnerInput) {
276         TvInputInfoWrapper inputWrapper = new TvInputInfoWrapper();
277         try {
278             inputWrapper.mInput =
279                     TestUtils.createTvInputInfo(service, id, parentId, type, isHardwareInput);
280         } catch (Exception e) {
281         }
282         inputWrapper.mLabel = label;
283         inputWrapper.mIsPartnerInput = isPartnerInput;
284         inputWrapper.mCustomLabel = customLabel;
285         TEST_INPUT_MAP.put(id, inputWrapper);
286         return inputWrapper.mInput;
287     }
288 
289     private static class TvInputInfoWrapper {
290         TvInputInfo mInput;
291         String mLabel;
292         String mCustomLabel;
293         boolean mIsPartnerInput;
294     }
295 }
296