1/*
2 * Copyright (C) 2016 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
17package com.android.tv.dvr.data;
18
19import static org.junit.Assert.assertEquals;
20
21import android.os.Build;
22import android.os.Parcel;
23import android.support.test.filters.SdkSuppress;
24import android.support.test.filters.SmallTest;
25
26import com.android.tv.data.Program;
27
28import org.junit.Test;
29
30/**
31 * Tests for {@link SeriesRecording}.
32 */
33@SmallTest
34@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
35public class SeriesRecordingTest {
36    private static final String PROGRAM_TITLE = "MyProgram";
37    private static final long CHANNEL_ID = 123;
38    private static final long OTHER_CHANNEL_ID = 321;
39    private static final String SERIES_ID = "SERIES_ID";
40    private static final String OTHER_SERIES_ID = "OTHER_SERIES_ID";
41
42    private final SeriesRecording mBaseSeriesRecording = new SeriesRecording.Builder()
43            .setTitle(PROGRAM_TITLE).setChannelId(CHANNEL_ID).setSeriesId(SERIES_ID).build();
44    private final SeriesRecording mSeriesRecordingSeason2 = SeriesRecording
45            .buildFrom(mBaseSeriesRecording).setStartFromSeason(2).build();
46    private final SeriesRecording mSeriesRecordingSeason2Episode5 = SeriesRecording
47            .buildFrom(mSeriesRecordingSeason2).setStartFromEpisode(5).build();
48    private final Program mBaseProgram = new Program.Builder().setTitle(PROGRAM_TITLE)
49            .setChannelId(CHANNEL_ID).setSeriesId(SERIES_ID).build();
50
51    @Test
52    public void testParcelable() {
53        SeriesRecording r1 = new SeriesRecording.Builder()
54                .setId(1)
55                .setChannelId(2)
56                .setPriority(3)
57                .setTitle("4")
58                .setDescription("5")
59                .setLongDescription("5-long")
60                .setSeriesId("6")
61                .setStartFromEpisode(7)
62                .setStartFromSeason(8)
63                .setChannelOption(SeriesRecording.OPTION_CHANNEL_ALL)
64                .setCanonicalGenreIds(new int[] {9, 10})
65                .setPosterUri("11")
66                .setPhotoUri("12")
67                .build();
68        Parcel p1 = Parcel.obtain();
69        Parcel p2 = Parcel.obtain();
70        try {
71            r1.writeToParcel(p1, 0);
72            byte[] bytes = p1.marshall();
73            p2.unmarshall(bytes, 0, bytes.length);
74            p2.setDataPosition(0);
75            SeriesRecording r2 = SeriesRecording.fromParcel(p2);
76            assertEquals(r1, r2);
77        } finally {
78            p1.recycle();
79            p2.recycle();
80        }
81    }
82
83    @Test
84    public void testDoesProgramMatch_simpleMatch() {
85        assertDoesProgramMatch(mBaseProgram, mBaseSeriesRecording, true);
86    }
87
88    @Test
89    public void testDoesProgramMatch_differentSeriesId() {
90        Program program = new Program.Builder(mBaseProgram).setSeriesId(OTHER_SERIES_ID).build();
91        assertDoesProgramMatch(program, mBaseSeriesRecording, false);
92    }
93
94    @Test
95    public void testDoesProgramMatch_differentChannel() {
96        Program program = new Program.Builder(mBaseProgram).setChannelId(OTHER_CHANNEL_ID).build();
97        assertDoesProgramMatch(program, mBaseSeriesRecording, false);
98    }
99
100    @Test
101    public void testDoesProgramMatch_startFromSeason2() {
102        Program program = mBaseProgram;
103        assertDoesProgramMatch(program, mSeriesRecordingSeason2, true);
104        program = new Program.Builder(program).setSeasonNumber("1").build();
105        assertDoesProgramMatch(program, mSeriesRecordingSeason2, false);
106        program = new Program.Builder(program).setSeasonNumber("2").build();
107        assertDoesProgramMatch(program, mSeriesRecordingSeason2, true);
108        program = new Program.Builder(program).setSeasonNumber("3").build();
109        assertDoesProgramMatch(program, mSeriesRecordingSeason2, true);
110    }
111
112    @Test
113    public void testDoesProgramMatch_startFromSeason2episode5() {
114        Program program = mBaseProgram;
115        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, true);
116        program = new Program.Builder(program).setSeasonNumber("2").build();
117        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, true);
118        program = new Program.Builder(program).setEpisodeNumber("4").build();
119        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, false);
120        program = new Program.Builder(program).setEpisodeNumber("5").build();
121        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, true);
122        program = new Program.Builder(program).setEpisodeNumber("6").build();
123        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, true);
124        program = new Program.Builder(program).setSeasonNumber("3").setEpisodeNumber("1").build();
125        assertDoesProgramMatch(program, mSeriesRecordingSeason2Episode5, true);
126    }
127
128    private void assertDoesProgramMatch(Program p, SeriesRecording seriesRecording,
129            boolean expected) {
130        assertEquals(seriesRecording + " doesProgramMatch " + p, expected,
131                seriesRecording.matchProgram(p));
132    }
133}
134