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