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
17package com.android.tv.dvr;
18
19import static com.android.tv.testing.dvr.RecordingTestUtils.createTestRecordingWithIdAndPeriod;
20import static com.android.tv.testing.dvr.RecordingTestUtils.normalizePriority;
21
22import android.test.MoreAsserts;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.util.Range;
25
26import com.android.tv.data.Channel;
27import com.android.tv.data.Program;
28import com.android.tv.testing.dvr.RecordingTestUtils;
29
30import junit.framework.TestCase;
31
32import java.util.Arrays;
33import java.util.Collection;
34import java.util.Collections;
35import java.util.List;
36
37/**
38 * Tests for {@link ScheduledRecordingTest}
39 */
40@SmallTest
41public class ScheduledRecordingTest extends TestCase {
42    private static final int CHANNEL_ID = 273;
43
44    public void testIsOverLapping() throws Exception {
45        ScheduledRecording r = createTestRecordingWithIdAndPeriod(1, CHANNEL_ID, 10L, 20L);
46        assertOverLapping(false, 1L, 9L, r);
47
48        assertOverLapping(true, 1L, 20L, r);
49        assertOverLapping(true, 1L, 10L, r);
50        assertOverLapping(true, 10L, 19L, r);
51        assertOverLapping(true, 10L, 20L, r);
52        assertOverLapping(true, 11L, 20L, r);
53        assertOverLapping(true, 11L, 21L, r);
54        assertOverLapping(true, 20L, 21L, r);
55
56        assertOverLapping(false, 21L, 29L, r);
57    }
58
59    public void testBuildProgram() {
60        Channel c = new Channel.Builder().build();
61        Program p = new Program.Builder().build();
62        ScheduledRecording actual = ScheduledRecording.builder(p).setChannelId(c.getId()).build();
63        assertEquals("type", ScheduledRecording.TYPE_PROGRAM, actual.getType());
64    }
65
66    public void testBuildTime() {
67        ScheduledRecording actual = createTestRecordingWithIdAndPeriod(1, CHANNEL_ID, 10L, 20L);
68        assertEquals("type", ScheduledRecording.TYPE_TIMED, actual.getType());
69    }
70
71    public void testBuildFrom() {
72        ScheduledRecording expected = createTestRecordingWithIdAndPeriod(1, CHANNEL_ID, 10L, 20L);
73        ScheduledRecording actual = ScheduledRecording.buildFrom(expected).build();
74        RecordingTestUtils.assertRecordingEquals(expected, actual);
75    }
76
77    public void testBuild_priority() {
78        ScheduledRecording a = normalizePriority(
79                createTestRecordingWithIdAndPeriod(1, CHANNEL_ID, 10L, 20L));
80        ScheduledRecording b = normalizePriority(
81                createTestRecordingWithIdAndPeriod(2, CHANNEL_ID, 10L, 20L));
82        ScheduledRecording c = normalizePriority(
83                createTestRecordingWithIdAndPeriod(3, CHANNEL_ID, 10L, 20L));
84
85        // default priority
86        MoreAsserts.assertContentsInOrder(sortByPriority(c,b,a), a, b, c);
87
88        // make C preferred over B
89        c = ScheduledRecording.buildFrom(c).setPriority(b.getPriority() - 1).build();
90        MoreAsserts.assertContentsInOrder(sortByPriority(c,b,a), a, c, b);
91    }
92
93    public Collection<ScheduledRecording> sortByPriority(ScheduledRecording a, ScheduledRecording b, ScheduledRecording c) {
94        List<ScheduledRecording> list = Arrays.asList(a, b, c);
95        Collections.sort(list, ScheduledRecording.PRIORITY_COMPARATOR);
96        return list;
97    }
98
99    private void assertOverLapping(boolean expected, long lower, long upper, ScheduledRecording r) {
100        assertEquals("isOverlapping(Range(" + lower + "," + upper + "), recording " + r, expected,
101                r.isOverLapping(new Range<Long>(lower, upper)));
102    }
103}
104