blob: d84a90d43a380d6ec6f45fb2659cb6ec422b3f5a [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.tv.util;
import static com.android.tv.util.TvTrackInfoUtils.getBestTrackInfo;
import static com.google.common.truth.Truth.assertWithMessage;
import android.media.tv.TvTrackInfo;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.android.tv.testing.ComparatorTester;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests for {@link com.android.tv.util.TvTrackInfoUtils}. */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class TvTrackInfoUtilsTest {
private static final String UN_MATCHED_ID = "no matching ID";
private static final TvTrackInfo INFO_1_EN_1 = create("1", "en", 1);
private static final TvTrackInfo INFO_2_EN_5 = create("2", "en", 5);
private static final TvTrackInfo INFO_3_FR_8 = create("3", "fr", 8);
private static final TvTrackInfo INFO_4_NULL_2 = create("4", null, 2);
private static final TvTrackInfo INFO_5_NULL_6 = create("5", null, 6);
private static TvTrackInfo create(String id, String fr, int audioChannelCount) {
return new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, id)
.setLanguage(fr)
.setAudioChannelCount(audioChannelCount)
.build();
}
private static final List<TvTrackInfo> ALL =
Arrays.asList(INFO_1_EN_1, INFO_2_EN_5, INFO_3_FR_8, INFO_4_NULL_2, INFO_5_NULL_6);
private static final List<TvTrackInfo> NULL_LANGUAGE_TRACKS =
Arrays.asList(INFO_4_NULL_2, INFO_5_NULL_6);
@Test
public void testGetBestTrackInfo_empty() {
TvTrackInfo result = getBestTrackInfo(Collections.emptyList(), UN_MATCHED_ID, "en", 1);
assertWithMessage("best track ").that(result).isEqualTo(null);
}
@Test
public void testGetBestTrackInfo_exactMatch() {
TvTrackInfo result = getBestTrackInfo(ALL, "1", "en", 1);
assertWithMessage("best track ").that(result).isEqualTo(INFO_1_EN_1);
}
@Test
public void testGetBestTrackInfo_langAndChannelCountMatch() {
TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "en", 5);
assertWithMessage("best track ").that(result).isEqualTo(INFO_2_EN_5);
}
@Test
public void testGetBestTrackInfo_languageOnlyMatch() {
TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "fr", 1);
assertWithMessage("best track ").that(result).isEqualTo(INFO_3_FR_8);
}
@Test
public void testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage() {
TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, null, 8);
assertWithMessage("best track ").that(result).isEqualTo(INFO_3_FR_8);
}
@Test
public void testGetBestTrackInfo_noMatches() {
TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, "kr", 1);
assertWithMessage("best track ").that(result).isEqualTo(INFO_1_EN_1);
}
@Test
public void testGetBestTrackInfo_noMatchesWithNullLanguage() {
TvTrackInfo result = getBestTrackInfo(ALL, UN_MATCHED_ID, null, 0);
assertWithMessage("best track ").that(result).isEqualTo(INFO_1_EN_1);
}
@Test
public void testGetBestTrackInfo_channelCountAndIdMatch() {
TvTrackInfo result = getBestTrackInfo(NULL_LANGUAGE_TRACKS, "5", null, 6);
assertWithMessage("best track ").that(result).isEqualTo(INFO_5_NULL_6);
}
@Test
public void testComparator() {
Comparator<TvTrackInfo> comparator = TvTrackInfoUtils.createComparator("1", "en", 1);
ComparatorTester.withoutEqualsTest(comparator)
// lang not match
.addComparableGroup(
create("1", "kr", 1),
create("2", "kr", 2),
create("1", "ja", 1),
create("1", "ch", 1))
// lang match not count match
.addComparableGroup(
create("2", "en", 2), create("3", "en", 3), create("1", "en", 2))
// lang and count match
.addComparableGroup(create("2", "en", 1), create("3", "en", 1))
// all match
.addComparableGroup(create("1", "en", 1), create("1", "en", 1))
.test();
}
}