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 */
16package com.android.tv.data;
17
18import static android.media.tv.TvContract.Programs.Genres.COMEDY;
19import static android.media.tv.TvContract.Programs.Genres.FAMILY_KIDS;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertNull;
23
24import android.media.tv.TvContentRating;
25import android.media.tv.TvContract.Programs.Genres;
26import android.os.Parcel;
27import android.support.test.filters.SmallTest;
28
29import com.android.tv.data.Program.CriticScore;
30
31import org.junit.Test;
32
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
36
37/**
38 * Tests for {@link Program}.
39 */
40@SmallTest
41public class ProgramTest {
42    private static final int NOT_FOUND_GENRE = 987;
43
44    private static final int FAMILY_GENRE_ID = GenreItems.getId(FAMILY_KIDS);
45
46    private static final int COMEDY_GENRE_ID = GenreItems.getId(COMEDY);
47
48    @Test
49    public void testBuild() {
50        Program program = new Program.Builder().build();
51        assertEquals("isValid", false, program.isValid());
52    }
53
54    @Test
55    public void testNoGenres() {
56        Program program = new Program.Builder()
57                .setCanonicalGenres("")
58                .build();
59        assertNullCanonicalGenres(program);
60        assertHasGenre(program, NOT_FOUND_GENRE, false);
61        assertHasGenre(program, FAMILY_GENRE_ID, false);
62        assertHasGenre(program, COMEDY_GENRE_ID, false);
63        assertHasGenre(program, GenreItems.ID_ALL_CHANNELS, true);
64    }
65
66    @Test
67    public void testFamilyGenre() {
68        Program program = new Program.Builder()
69                .setCanonicalGenres(FAMILY_KIDS)
70                .build();
71        assertCanonicalGenres(program, FAMILY_KIDS);
72        assertHasGenre(program, NOT_FOUND_GENRE, false);
73        assertHasGenre(program, FAMILY_GENRE_ID, true);
74        assertHasGenre(program, COMEDY_GENRE_ID, false);
75        assertHasGenre(program, GenreItems.ID_ALL_CHANNELS, true);
76    }
77
78    @Test
79    public void testFamilyComedyGenre() {
80        Program program = new Program.Builder()
81                .setCanonicalGenres(FAMILY_KIDS + ", " + COMEDY)
82                .build();
83        assertCanonicalGenres(program, FAMILY_KIDS, COMEDY);
84        assertHasGenre(program, NOT_FOUND_GENRE, false);
85        assertHasGenre(program, FAMILY_GENRE_ID, true);
86        assertHasGenre(program, COMEDY_GENRE_ID, true);
87        assertHasGenre(program, GenreItems.ID_ALL_CHANNELS, true);
88    }
89
90    @Test
91    public void testOtherGenre() {
92        Program program = new Program.Builder()
93                .setCanonicalGenres("other")
94                .build();
95        assertCanonicalGenres(program);
96        assertHasGenre(program, NOT_FOUND_GENRE, false);
97        assertHasGenre(program, FAMILY_GENRE_ID, false);
98        assertHasGenre(program, COMEDY_GENRE_ID, false);
99        assertHasGenre(program, GenreItems.ID_ALL_CHANNELS, true);
100    }
101
102    @Test
103    public void testParcelable() {
104        List<CriticScore> criticScores = new ArrayList<>();
105        criticScores.add(new CriticScore("1", "2", "3"));
106        criticScores.add(new CriticScore("4", "5", "6"));
107        TvContentRating[] ratings = new TvContentRating[2];
108        ratings[0] = TvContentRating.unflattenFromString("1/2/3");
109        ratings[1] = TvContentRating.unflattenFromString("4/5/6");
110        Program p = new Program.Builder()
111                .setId(1)
112                .setPackageName("2")
113                .setChannelId(3)
114                .setTitle("4")
115                .setSeriesId("5")
116                .setEpisodeTitle("6")
117                .setSeasonNumber("7")
118                .setSeasonTitle("8")
119                .setEpisodeNumber("9")
120                .setStartTimeUtcMillis(10)
121                .setEndTimeUtcMillis(11)
122                .setDescription("12")
123                .setLongDescription("12-long")
124                .setVideoWidth(13)
125                .setVideoHeight(14)
126                .setCriticScores(criticScores)
127                .setPosterArtUri("15")
128                .setThumbnailUri("16")
129                .setCanonicalGenres(Genres.encode(Genres.SPORTS, Genres.SHOPPING))
130                .setContentRatings(ratings)
131                .setRecordingProhibited(true)
132                .build();
133        Parcel p1 = Parcel.obtain();
134        Parcel p2 = Parcel.obtain();
135        try {
136            p.writeToParcel(p1, 0);
137            byte[] bytes = p1.marshall();
138            p2.unmarshall(bytes, 0, bytes.length);
139            p2.setDataPosition(0);
140            Program r2 = Program.fromParcel(p2);
141            assertEquals(p, r2);
142        } finally {
143            p1.recycle();
144            p2.recycle();
145        }
146    }
147
148    @Test
149    public void testParcelableWithCriticScore() {
150        Program program = new Program.Builder()
151                .setTitle("MyTitle")
152                .addCriticScore(new CriticScore(
153                        "default source",
154                        "5/10",
155                        "https://testurl/testimage.jpg"))
156                .build();
157        Parcel parcel = Parcel.obtain();
158        program.writeToParcel(parcel, 0);
159        parcel.setDataPosition(0);
160        Program programFromParcel = Program.CREATOR.createFromParcel(parcel);
161
162        assertNotNull(programFromParcel.getCriticScores());
163        assertEquals(programFromParcel.getCriticScores().get(0).source, "default source");
164        assertEquals(programFromParcel.getCriticScores().get(0).score, "5/10");
165        assertEquals(programFromParcel.getCriticScores().get(0).logoUrl,
166                "https://testurl/testimage.jpg");
167    }
168
169    private static void assertNullCanonicalGenres(Program program) {
170        String[] actual = program.getCanonicalGenres();
171        assertNull("Expected null canonical genres but was " + Arrays.toString(actual), actual);
172    }
173
174    private static void assertCanonicalGenres(Program program, String... expected) {
175        assertEquals("canonical genres", Arrays.asList(expected),
176                Arrays.asList(program.getCanonicalGenres()));
177    }
178
179    private static void assertHasGenre(Program program, int genreId, boolean expected) {
180        assertEquals("hasGenre(" + genreId + ")", expected, program.hasGenre(genreId));
181    }
182}
183