• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.tv.util;
17 
18 import static com.android.tv.util.TvTrackInfoUtils.getBestTrackInfo;
19 
20 import android.media.tv.TvTrackInfo;
21 import android.support.test.filters.SmallTest;
22 
23 import com.android.tv.testing.ComparatorTester;
24 
25 import junit.framework.TestCase;
26 
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
31 
32 /**
33  * Tests for {@link com.android.tv.util.TvTrackInfoUtils}.
34  */
35 @SmallTest
36 public class TvTrackInfoUtilsTest extends TestCase {
37 
38     private static final String UN_MATCHED_ID = "no matching ID";
39 
40     private static final TvTrackInfo INFO_1_EN_1 = create("1", "en", 1);
41 
42     private static final TvTrackInfo INFO_2_EN_5 = create("2", "en", 5);
43 
44     private static final TvTrackInfo INFO_3_FR_8 = create("3", "fr", 8);
45 
46     private static final TvTrackInfo INFO_4_NULL_2 = create("4", null, 2);
47 
48     private static final TvTrackInfo INFO_5_NULL_6 = create("5", null, 6);
49 
create(String id, String fr, int audioChannelCount)50     private static TvTrackInfo create(String id, String fr, int audioChannelCount) {
51         return new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, id)
52                 .setLanguage(fr)
53                 .setAudioChannelCount(audioChannelCount)
54                 .build();
55     }
56 
57     private static final List<TvTrackInfo> ALL = Arrays.asList(INFO_1_EN_1, INFO_2_EN_5,
58             INFO_3_FR_8, INFO_4_NULL_2, INFO_5_NULL_6);
59     private static final List<TvTrackInfo> NULL_LANGUAGE_TRACKS = Arrays.asList(INFO_4_NULL_2,
60             INFO_5_NULL_6);
61 
testGetBestTrackInfo_empty()62     public void testGetBestTrackInfo_empty() {
63         TvTrackInfo result = getBestTrackInfo(Collections.emptyList(), UN_MATCHED_ID, "en", 1);
64         assertEquals("best track ", null, result);
65     }
66 
testGetBestTrackInfo_exactMatch()67     public void testGetBestTrackInfo_exactMatch() {
68         TvTrackInfo result = getBestTrackInfo(ALL, "1", "en", 1);
69         assertEquals("best track ", INFO_1_EN_1, result);
70     }
71 
testGetBestTrackInfo_langAndChannelCountMatch()72     public void testGetBestTrackInfo_langAndChannelCountMatch() {
73         TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "en", 5);
74         assertEquals("best track ", INFO_2_EN_5, result);
75     }
76 
testGetBestTrackInfo_languageOnlyMatch()77     public void testGetBestTrackInfo_languageOnlyMatch() {
78         TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "fr", 1);
79         assertEquals("best track ", INFO_3_FR_8, result);
80     }
81 
testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage()82     public void testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage() {
83         TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, null, 8);
84         assertEquals("best track ", INFO_3_FR_8, result);
85     }
86 
testGetBestTrackInfo_noMatches()87     public void testGetBestTrackInfo_noMatches() {
88         TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "kr", 1);
89         assertEquals("best track ", INFO_1_EN_1, result);
90     }
91 
testGetBestTrackInfo_noMatchesWithNullLanguage()92     public void testGetBestTrackInfo_noMatchesWithNullLanguage() {
93         TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, null, 0);
94         assertEquals("best track ", INFO_1_EN_1, result);
95     }
96 
testGetBestTrackInfo_channelCountAndIdMatch()97     public void testGetBestTrackInfo_channelCountAndIdMatch() {
98         TvTrackInfo result = getBestTrackInfo(NULL_LANGUAGE_TRACKS, "5", null, 6);
99         assertEquals("best track ", INFO_5_NULL_6, result);
100     }
101 
testComparator()102     public void testComparator() {
103         Comparator<TvTrackInfo> comparator = TvTrackInfoUtils.createComparator("1", "en", 1);
104         ComparatorTester.withoutEqualsTest(comparator)
105                 // lang not match
106                 .addComparableGroup(create("1", "kr", 1), create("2", "kr", 2),
107                         create("1", "ja", 1),
108                         create("1", "ch", 1))
109                  // lang match not count match
110                 .addComparableGroup(create("2", "en", 2), create("3", "en", 3),
111                         create("1", "en", 2))
112                  // lang and count match
113                 .addComparableGroup(create("2", "en", 1), create("3", "en", 1))
114                  // all match
115                 .addComparableGroup(create("1", "en", 1), create("1", "en", 1))
116                 .test();
117     }
118 }
119