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 static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assert.assertEquals; 23 24 import android.media.tv.TvTrackInfo; 25 import android.os.Build.VERSION_CODES; 26 import android.os.LocaleList; 27 28 import com.android.tv.testing.ComparatorTester; 29 import com.android.tv.testing.TvRobolectricTestRunner; 30 import com.android.tv.testing.constants.ConfigConstants; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 37 import java.util.Arrays; 38 import java.util.Collections; 39 import java.util.Comparator; 40 import java.util.List; 41 import java.util.Locale; 42 43 /** Tests for {@link com.android.tv.util.TvTrackInfoUtils}. */ 44 @RunWith(TvRobolectricTestRunner.class) 45 @Config(minSdk = ConfigConstants.MIN_SDK, maxSdk = ConfigConstants.MAX_SDK) 46 public class TvTrackInfoUtilsTest { 47 48 /** Tests for {@link TvTrackInfoUtils#getBestTrackInfo}. */ 49 private static final String UN_MATCHED_ID = "no matching ID"; 50 51 private final TvTrackInfo info1En1 = createTvTrackInfo("track_1", "en", 1); 52 53 private final TvTrackInfo info2En5 = createTvTrackInfo("track_2", "en", 5); 54 55 private final TvTrackInfo info3Fr8 = createTvTrackInfo("track_3", "fr", 8); 56 57 private final TvTrackInfo info4Null2 = createTvTrackInfo("track_4", null, 2); 58 59 private final TvTrackInfo info5Null6 = createTvTrackInfo("track_5", null, 6); 60 createTvTrackInfo(String trackId, String language, int audioChannelCount)61 private TvTrackInfo createTvTrackInfo(String trackId, String language, int audioChannelCount) { 62 return new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, trackId) 63 .setLanguage(language) 64 .setAudioChannelCount(audioChannelCount) 65 .build(); 66 } 67 68 private final List<TvTrackInfo> allTracks = 69 Arrays.asList(info1En1, info2En5, info3Fr8, info4Null2, info5Null6); 70 private final List<TvTrackInfo> nullLanguageTracks = Arrays.asList(info4Null2, info5Null6); 71 72 @Test testGetBestTrackInfo_empty()73 public void testGetBestTrackInfo_empty() { 74 TvTrackInfo result = getBestTrackInfo(Collections.emptyList(), UN_MATCHED_ID, "en", 1); 75 assertWithMessage("best track ").that(result).isEqualTo(null); 76 } 77 78 @Test testGetBestTrackInfo_exactMatch()79 public void testGetBestTrackInfo_exactMatch() { 80 TvTrackInfo result = getBestTrackInfo(allTracks, "track_1", "en", 1); 81 assertWithMessage("best track ").that(result).isEqualTo(info1En1); 82 } 83 84 @Test testGetBestTrackInfo_langAndChannelCountMatch()85 public void testGetBestTrackInfo_langAndChannelCountMatch() { 86 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, "en", 5); 87 assertWithMessage("best track ").that(result).isEqualTo(info2En5); 88 } 89 90 @Test testGetBestTrackInfo_languageOnlyMatch()91 public void testGetBestTrackInfo_languageOnlyMatch() { 92 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, "fr", 1); 93 assertWithMessage("best track ").that(result).isEqualTo(info3Fr8); 94 } 95 96 @Test 97 @Config(minSdk = ConfigConstants.MIN_SDK, maxSdk = VERSION_CODES.M) testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage_23()98 public void testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage_23() { 99 Locale localPreference = Locale.forLanguageTag("es"); 100 Locale.setDefault(localPreference); 101 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, null, 8); 102 assertWithMessage("best track ").that(result).isEqualTo(info3Fr8); 103 } 104 105 @Test 106 @Config(minSdk = VERSION_CODES.N, maxSdk = ConfigConstants.MAX_SDK) testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage()107 public void testGetBestTrackInfo_channelCountOnlyMatchWithNullLanguage() { 108 // Setting LoacaleList to a language which is not in the test set. 109 LocaleList localPreferenceList = LocaleList.forLanguageTags("es"); 110 LocaleList.setDefault(localPreferenceList); 111 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, null, 8); 112 assertWithMessage("best track ").that(result).isEqualTo(info3Fr8); 113 } 114 115 @Test testGetBestTrackInfo_noMatches()116 public void testGetBestTrackInfo_noMatches() { 117 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, "kr", 1); 118 assertWithMessage("best track ").that(result).isEqualTo(info1En1); 119 } 120 121 @Test 122 @Config(minSdk = ConfigConstants.MIN_SDK, maxSdk = VERSION_CODES.M) testGetBestTrackInfo_noMatchesWithNullLanguage_23()123 public void testGetBestTrackInfo_noMatchesWithNullLanguage_23() { 124 Locale localPreference = Locale.forLanguageTag("es"); 125 Locale.setDefault(localPreference); 126 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, null, 0); 127 assertWithMessage("best track ").that(result).isEqualTo(info3Fr8); 128 } 129 130 @Test 131 @Config(minSdk = VERSION_CODES.N, maxSdk = ConfigConstants.MAX_SDK) testGetBestTrackInfo_noMatchesWithNullLanguage()132 public void testGetBestTrackInfo_noMatchesWithNullLanguage() { 133 // Setting LoacaleList to a language which is not in the test set. 134 LocaleList localPreferenceList = LocaleList.forLanguageTags("es"); 135 LocaleList.setDefault(localPreferenceList); 136 TvTrackInfo result = getBestTrackInfo(allTracks, UN_MATCHED_ID, null, 0); 137 assertWithMessage("best track ").that(result).isEqualTo(info3Fr8); 138 } 139 140 @Test testGetBestTrackInfo_channelCountAndIdMatch()141 public void testGetBestTrackInfo_channelCountAndIdMatch() { 142 TvTrackInfo result = getBestTrackInfo(nullLanguageTracks, "track_5", null, 6); 143 assertWithMessage("best track ").that(result).isEqualTo(info5Null6); 144 } 145 146 @Test testComparator()147 public void testComparator() { 148 List<String> languages = Arrays.asList("en", "spa", "hi"); 149 Comparator<TvTrackInfo> comparator = 150 TvTrackInfoUtils.createComparator("track_1", languages, 1); 151 new ComparatorTester(comparator) 152 .permitInconsistencyWithEquals() 153 // lang not match 154 .addEqualityGroup( 155 createTvTrackInfo("track_1", "kr", 2), 156 createTvTrackInfo("track_1", "ja", 2), 157 createTvTrackInfo("track_1", "ch", 2)) 158 // lang not match, better count 159 .addEqualityGroup( 160 createTvTrackInfo("track_1", "kr", 3), 161 createTvTrackInfo("track_1", "ch", 3)) 162 // lang not match, count match 163 .addEqualityGroup( 164 createTvTrackInfo("track_1", "ch", 1), 165 createTvTrackInfo("track_1", "ja", 1)) 166 // lang match in order of increasing priority 167 .addEqualityGroup(createTvTrackInfo("track_1", "hi", 3)) 168 .addEqualityGroup(createTvTrackInfo("track_2", "hi", 7)) 169 .addEqualityGroup(createTvTrackInfo("track_1", "hi", 1)) 170 .addEqualityGroup(createTvTrackInfo("track_1", "spa", 5)) 171 .addEqualityGroup(createTvTrackInfo("track_2", "spa", 1)) 172 .addEqualityGroup(createTvTrackInfo("track_1", "spa", 1)) 173 .addEqualityGroup(createTvTrackInfo("track_2", "en", 3)) 174 .addEqualityGroup( 175 createTvTrackInfo("track_1", "en", 5), 176 createTvTrackInfo("track_2", "en", 5)) 177 // best lang match and count match 178 .addEqualityGroup( 179 createTvTrackInfo("track_2", "en", 1), 180 createTvTrackInfo("track_3", "en", 1)) 181 // all match 182 .addEqualityGroup( 183 createTvTrackInfo("track_1", "en", 1), 184 createTvTrackInfo("track_1", "en", 1)) 185 .testCompare(); 186 } 187 188 /** Tests for {@link TvTrackInfoUtils#needToShowSampleRate}. */ 189 private final TvTrackInfo info6En1 = createTvTrackInfo("track_6", "en", 1); 190 191 private final TvTrackInfo info7En0 = createTvTrackInfo("track_7", "en", 0); 192 193 private final TvTrackInfo info8En0 = createTvTrackInfo("track_8", "en", 0); 194 195 private List<TvTrackInfo> trackList; 196 197 @Test needToShowSampleRate_false()198 public void needToShowSampleRate_false() { 199 trackList = Arrays.asList(info1En1, info2En5); 200 assertEquals( 201 false, 202 TvTrackInfoUtils.needToShowSampleRate(RuntimeEnvironment.application, trackList)); 203 } 204 205 @Test needToShowSampleRate_sameLanguageAndChannelCount()206 public void needToShowSampleRate_sameLanguageAndChannelCount() { 207 trackList = Arrays.asList(info1En1, info6En1); 208 assertEquals( 209 true, 210 TvTrackInfoUtils.needToShowSampleRate(RuntimeEnvironment.application, trackList)); 211 } 212 213 @Test needToShowSampleRate_sameLanguageNoChannelCount()214 public void needToShowSampleRate_sameLanguageNoChannelCount() { 215 trackList = Arrays.asList(info7En0, info8En0); 216 assertEquals( 217 true, 218 TvTrackInfoUtils.needToShowSampleRate(RuntimeEnvironment.application, trackList)); 219 } 220 221 /** Tests for {@link TvTrackInfoUtils#getMultiAudioString}. */ 222 private static final String TRACK_ID = "test_track_id"; 223 224 private static final int AUDIO_SAMPLE_RATE = 48000; 225 226 @Test testAudioTrackLanguage()227 public void testAudioTrackLanguage() { 228 assertEquals( 229 "Korean", 230 TvTrackInfoUtils.getMultiAudioString( 231 RuntimeEnvironment.application, createAudioTrackInfo("kor"), false)); 232 assertEquals( 233 "English", 234 TvTrackInfoUtils.getMultiAudioString( 235 RuntimeEnvironment.application, createAudioTrackInfo("eng"), false)); 236 assertEquals( 237 "Unknown language", 238 TvTrackInfoUtils.getMultiAudioString( 239 RuntimeEnvironment.application, createAudioTrackInfo(null), false)); 240 assertEquals( 241 "Unknown language", 242 TvTrackInfoUtils.getMultiAudioString( 243 RuntimeEnvironment.application, createAudioTrackInfo(""), false)); 244 assertEquals( 245 "abc", 246 TvTrackInfoUtils.getMultiAudioString( 247 RuntimeEnvironment.application, createAudioTrackInfo("abc"), false)); 248 } 249 250 @Test testAudioTrackCount()251 public void testAudioTrackCount() { 252 assertEquals( 253 "English", 254 TvTrackInfoUtils.getMultiAudioString( 255 RuntimeEnvironment.application, createAudioTrackInfo("eng", -1), false)); 256 assertEquals( 257 "English", 258 TvTrackInfoUtils.getMultiAudioString( 259 RuntimeEnvironment.application, createAudioTrackInfo("eng", 0), false)); 260 assertEquals( 261 "English (mono)", 262 TvTrackInfoUtils.getMultiAudioString( 263 RuntimeEnvironment.application, createAudioTrackInfo("eng", 1), false)); 264 assertEquals( 265 "English (stereo)", 266 TvTrackInfoUtils.getMultiAudioString( 267 RuntimeEnvironment.application, createAudioTrackInfo("eng", 2), false)); 268 assertEquals( 269 "English (3 channels)", 270 TvTrackInfoUtils.getMultiAudioString( 271 RuntimeEnvironment.application, createAudioTrackInfo("eng", 3), false)); 272 assertEquals( 273 "English (4 channels)", 274 TvTrackInfoUtils.getMultiAudioString( 275 RuntimeEnvironment.application, createAudioTrackInfo("eng", 4), false)); 276 assertEquals( 277 "English (5 channels)", 278 TvTrackInfoUtils.getMultiAudioString( 279 RuntimeEnvironment.application, createAudioTrackInfo("eng", 5), false)); 280 assertEquals( 281 "English (5.1 surround)", 282 TvTrackInfoUtils.getMultiAudioString( 283 RuntimeEnvironment.application, createAudioTrackInfo("eng", 6), false)); 284 assertEquals( 285 "English (7 channels)", 286 TvTrackInfoUtils.getMultiAudioString( 287 RuntimeEnvironment.application, createAudioTrackInfo("eng", 7), false)); 288 assertEquals( 289 "English (7.1 surround)", 290 TvTrackInfoUtils.getMultiAudioString( 291 RuntimeEnvironment.application, createAudioTrackInfo("eng", 8), false)); 292 } 293 294 @Test testShowSampleRate()295 public void testShowSampleRate() { 296 assertEquals( 297 "Korean (48kHz)", 298 TvTrackInfoUtils.getMultiAudioString( 299 RuntimeEnvironment.application, createAudioTrackInfo("kor", 0), true)); 300 assertEquals( 301 "Korean (7.1 surround, 48kHz)", 302 TvTrackInfoUtils.getMultiAudioString( 303 RuntimeEnvironment.application, createAudioTrackInfo("kor", 8), true)); 304 } 305 createAudioTrackInfo(String language)306 private static TvTrackInfo createAudioTrackInfo(String language) { 307 return createAudioTrackInfo(language, 0); 308 } 309 createAudioTrackInfo(String language, int channelCount)310 private static TvTrackInfo createAudioTrackInfo(String language, int channelCount) { 311 return new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, TRACK_ID) 312 .setLanguage(language) 313 .setAudioChannelCount(channelCount) 314 .setAudioSampleRate(AUDIO_SAMPLE_RATE) 315 .build(); 316 } 317 318 /** Tests for {@link TvTrackInfoUtils#toString */ 319 @Test toString_audioWithDetails()320 public void toString_audioWithDetails() { 321 assertEquals( 322 "TvTrackInfo{type=Audio, id=track_1, language=en, " 323 + "description=test, audioChannelCount=1, audioSampleRate=5}", 324 TvTrackInfoUtils.toString( 325 new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, "track_1") 326 .setLanguage("en") 327 .setAudioChannelCount(1) 328 .setDescription("test") 329 .setAudioSampleRate(5) 330 .build())); 331 } 332 333 @Test toString_audioWithDefaults()334 public void toString_audioWithDefaults() { 335 assertEquals( 336 "TvTrackInfo{type=Audio, id=track_2, language=null, " 337 + "description=null, audioChannelCount=0, audioSampleRate=0}", 338 TvTrackInfoUtils.toString( 339 new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, "track_2").build())); 340 } 341 342 @Test toString_videoWithDetails()343 public void toString_videoWithDetails() { 344 assertEquals( 345 "TvTrackInfo{type=Video, id=track_3, language=en, description=test, videoWidth=1," 346 + " videoHeight=1, videoFrameRate=1.0, videoPixelAspectRatio=2.0}", 347 TvTrackInfoUtils.toString( 348 new TvTrackInfo.Builder(TvTrackInfo.TYPE_VIDEO, "track_3") 349 .setLanguage("en") 350 .setDescription("test") 351 .setVideoWidth(1) 352 .setVideoHeight(1) 353 .setVideoFrameRate(1) 354 .setVideoPixelAspectRatio(2) 355 .build())); 356 } 357 358 @Test toString_videoWithDefaults()359 public void toString_videoWithDefaults() { 360 assertEquals( 361 "TvTrackInfo{type=Video, id=track_4, language=null, description=null, videoWidth=0," 362 + " videoHeight=0, videoFrameRate=0.0, videoPixelAspectRatio=1.0}", 363 TvTrackInfoUtils.toString( 364 new TvTrackInfo.Builder(TvTrackInfo.TYPE_VIDEO, "track_4").build())); 365 } 366 367 @Test toString_subtitleWithDetails()368 public void toString_subtitleWithDetails() { 369 assertEquals( 370 "TvTrackInfo{type=Subtitle, id=track_5, language=en, description=test}", 371 TvTrackInfoUtils.toString( 372 new TvTrackInfo.Builder(TvTrackInfo.TYPE_SUBTITLE, "track_5") 373 .setLanguage("en") 374 .setDescription("test") 375 .build())); 376 } 377 378 @Test toString_subtitleWithDefaults()379 public void toString_subtitleWithDefaults() { 380 assertEquals( 381 "TvTrackInfo{type=Subtitle, id=track_6, language=null, description=null}", 382 TvTrackInfoUtils.toString( 383 new TvTrackInfo.Builder(TvTrackInfo.TYPE_SUBTITLE, "track_6").build())); 384 } 385 } 386