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.data;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.media.tv.TvContract.Programs.Genres;
22 import android.os.Build;
23 
24 import com.android.tv.testing.TvRobolectricTestRunner;
25 import com.android.tv.testing.constants.ConfigConstants;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RuntimeEnvironment;
30 import org.robolectric.annotation.Config;
31 
32 /** Tests for {@link GenreItems}. */
33 @RunWith(TvRobolectricTestRunner.class)
34 @Config(sdk = ConfigConstants.SDK)
35 public class GenreItemTest {
36     private static final String INVALID_GENRE = "INVALID GENRE";
37 
38     @Test
testGetLabels()39     public void testGetLabels() {
40         // Checks if no exception is thrown.
41         GenreItems.getLabels(RuntimeEnvironment.application);
42     }
43 
44     @Test
testGetCanonicalGenre()45     public void testGetCanonicalGenre() {
46         int count = GenreItems.getGenreCount();
47         assertThat(GenreItems.getCanonicalGenre(GenreItems.ID_ALL_CHANNELS)).isNull();
48         for (int i = 1; i < count; ++i) {
49             assertThat(GenreItems.getCanonicalGenre(i)).isNotNull();
50         }
51     }
52 
53     @Test
testGetId_base()54     public void testGetId_base() {
55         int count = GenreItems.getGenreCount();
56         assertThat(GenreItems.getId(null)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
57         assertThat(GenreItems.getId(INVALID_GENRE)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
58         assertInRange(GenreItems.getId(Genres.FAMILY_KIDS), 1, count - 1);
59         assertInRange(GenreItems.getId(Genres.SPORTS), 1, count - 1);
60         assertInRange(GenreItems.getId(Genres.SHOPPING), 1, count - 1);
61         assertInRange(GenreItems.getId(Genres.MOVIES), 1, count - 1);
62         assertInRange(GenreItems.getId(Genres.COMEDY), 1, count - 1);
63         assertInRange(GenreItems.getId(Genres.TRAVEL), 1, count - 1);
64         assertInRange(GenreItems.getId(Genres.DRAMA), 1, count - 1);
65         assertInRange(GenreItems.getId(Genres.EDUCATION), 1, count - 1);
66         assertInRange(GenreItems.getId(Genres.ANIMAL_WILDLIFE), 1, count - 1);
67         assertInRange(GenreItems.getId(Genres.NEWS), 1, count - 1);
68         assertInRange(GenreItems.getId(Genres.GAMING), 1, count - 1);
69     }
70 
71     @Test
testGetId_lmp_mr1()72     public void testGetId_lmp_mr1() {
73         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
74             assertThat(GenreItems.getId(Genres.ARTS)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
75             assertThat(GenreItems.getId(Genres.ENTERTAINMENT))
76                     .isEqualTo(GenreItems.ID_ALL_CHANNELS);
77             assertThat(GenreItems.getId(Genres.LIFE_STYLE)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
78             assertThat(GenreItems.getId(Genres.MUSIC)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
79             assertThat(GenreItems.getId(Genres.PREMIER)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
80             assertThat(GenreItems.getId(Genres.TECH_SCIENCE)).isEqualTo(GenreItems.ID_ALL_CHANNELS);
81         } else {
82             int count = GenreItems.getGenreCount();
83             assertInRange(GenreItems.getId(Genres.ARTS), 1, count - 1);
84             assertInRange(GenreItems.getId(Genres.ENTERTAINMENT), 1, count - 1);
85             assertInRange(GenreItems.getId(Genres.LIFE_STYLE), 1, count - 1);
86             assertInRange(GenreItems.getId(Genres.MUSIC), 1, count - 1);
87             assertInRange(GenreItems.getId(Genres.PREMIER), 1, count - 1);
88             assertInRange(GenreItems.getId(Genres.TECH_SCIENCE), 1, count - 1);
89         }
90     }
91 
assertInRange(int value, int lower, int upper)92     private void assertInRange(int value, int lower, int upper) {
93         assertThat(value >= lower && value <= upper).isTrue();
94     }
95 }
96