aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/dvr/ScheduledRecordingTest.java
blob: 96036418d4c091c3761b044ac2d8e3a40348e3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.tv.dvr;

import static com.android.tv.testing.dvr.RecordingTestUtils
        .createTestRecordingWithIdAndPeriod;
import static com.android.tv.testing.dvr.RecordingTestUtils.normalizePriority;

import android.support.test.filters.SmallTest;
import android.test.MoreAsserts;
import android.util.Range;

import com.android.tv.data.Channel;
import com.android.tv.data.Program;
import com.android.tv.testing.dvr.RecordingTestUtils;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Tests for {@link ScheduledRecordingTest}
 */
@SmallTest
public class ScheduledRecordingTest extends TestCase {
    private static final String INPUT_ID = "input_id";
    private static final int CHANNEL_ID = 273;

    public void testIsOverLapping() throws Exception {
        ScheduledRecording r = createTestRecordingWithIdAndPeriod(1, INPUT_ID, CHANNEL_ID,
                10L, 20L);
        assertOverLapping(false, 1L, 9L, r);

        assertOverLapping(true, 1L, 20L, r);
        assertOverLapping(false, 1L, 10L, r);
        assertOverLapping(true, 10L, 19L, r);
        assertOverLapping(true, 10L, 20L, r);
        assertOverLapping(true, 11L, 20L, r);
        assertOverLapping(true, 11L, 21L, r);
        assertOverLapping(false, 20L, 21L, r);

        assertOverLapping(false, 21L, 29L, r);
    }

    public void testBuildProgram() {
        Channel c = new Channel.Builder().build();
        Program p = new Program.Builder().build();
        ScheduledRecording actual = ScheduledRecording.builder(INPUT_ID, p)
                .setChannelId(c.getId()).build();
        assertEquals("type", ScheduledRecording.TYPE_PROGRAM, actual.getType());
    }

    public void testBuildTime() {
        ScheduledRecording actual = createTestRecordingWithIdAndPeriod(1, INPUT_ID, CHANNEL_ID,
                10L, 20L);
        assertEquals("type", ScheduledRecording.TYPE_TIMED, actual.getType());
    }

    public void testBuildFrom() {
        ScheduledRecording expected = createTestRecordingWithIdAndPeriod(1, INPUT_ID, CHANNEL_ID,
                10L, 20L);
        ScheduledRecording actual = ScheduledRecording.buildFrom(expected).build();
        RecordingTestUtils.assertRecordingEquals(expected, actual);
    }

    public void testBuild_priority() {
        ScheduledRecording a = normalizePriority(
                createTestRecordingWithIdAndPeriod(1, INPUT_ID, CHANNEL_ID, 10L, 20L));
        ScheduledRecording b = normalizePriority(
                createTestRecordingWithIdAndPeriod(2, INPUT_ID, CHANNEL_ID, 10L, 20L));
        ScheduledRecording c = normalizePriority(
                createTestRecordingWithIdAndPeriod(3, INPUT_ID, CHANNEL_ID, 10L, 20L));

        // default priority
        MoreAsserts.assertContentsInOrder(sortByPriority(c, b, a), a, b, c);

        // make A preferred over B
        a = ScheduledRecording.buildFrom(a).setPriority(b.getPriority() + 2).build();
        MoreAsserts.assertContentsInOrder(sortByPriority(a, b, c), b, c, a);
    }

    public Collection<ScheduledRecording> sortByPriority(ScheduledRecording a, ScheduledRecording b,
            ScheduledRecording c) {
        List<ScheduledRecording> list = Arrays.asList(a, b, c);
        Collections.sort(list, ScheduledRecording.PRIORITY_COMPARATOR);
        return list;
    }

    private void assertOverLapping(boolean expected, long lower, long upper, ScheduledRecording r) {
        assertEquals("isOverlapping(Range(" + lower + "," + upper + "), recording " + r, expected,
                r.isOverLapping(new Range<>(lower, upper)));
    }
}