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