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 
17 package com.android.tv.dvr;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import android.os.Build;
22 
23 import com.android.tv.dvr.data.ScheduledRecording;
24 import com.android.tv.testing.TestSingletonApp;
25 import com.android.tv.testing.dvr.RecordingTestUtils;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.annotation.Config;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /** Tests for {@link DvrDataManagerImpl} */
36 @RunWith(RobolectricTestRunner.class)
37 @Config(sdk = Build.VERSION_CODES.N, application = TestSingletonApp.class)
38 public class DvrDataManagerImplTest {
39     private static final String INPUT_ID = "input_id";
40     private static final int CHANNEL_ID = 273;
41 
42     @Test
testGetNextScheduledStartTimeAfter()43     public void testGetNextScheduledStartTimeAfter() {
44         long id = 1;
45         List<ScheduledRecording> scheduledRecordings = new ArrayList<>();
46         assertNextStartTime(scheduledRecordings, 0L, DvrDataManager.NEXT_START_TIME_NOT_FOUND);
47         scheduledRecordings.add(
48                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
49                         id++, INPUT_ID, CHANNEL_ID, 10L, 20L));
50         assertNextStartTime(scheduledRecordings, 9L, 10L);
51         assertNextStartTime(scheduledRecordings, 10L, DvrDataManager.NEXT_START_TIME_NOT_FOUND);
52         scheduledRecordings.add(
53                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
54                         id++, INPUT_ID, CHANNEL_ID, 20L, 30L));
55         assertNextStartTime(scheduledRecordings, 9L, 10L);
56         assertNextStartTime(scheduledRecordings, 10L, 20L);
57         assertNextStartTime(scheduledRecordings, 20L, DvrDataManager.NEXT_START_TIME_NOT_FOUND);
58         scheduledRecordings.add(
59                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
60                         id++, INPUT_ID, CHANNEL_ID, 30L, 40L));
61         assertNextStartTime(scheduledRecordings, 9L, 10L);
62         assertNextStartTime(scheduledRecordings, 10L, 20L);
63         assertNextStartTime(scheduledRecordings, 20L, 30L);
64         assertNextStartTime(scheduledRecordings, 30L, DvrDataManager.NEXT_START_TIME_NOT_FOUND);
65         scheduledRecordings.clear();
66         scheduledRecordings.add(
67                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
68                         id++, INPUT_ID, CHANNEL_ID, 10L, 20L));
69         scheduledRecordings.add(
70                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
71                         id++, INPUT_ID, CHANNEL_ID, 10L, 20L));
72         scheduledRecordings.add(
73                 RecordingTestUtils.createTestRecordingWithIdAndPeriod(
74                         id++, INPUT_ID, CHANNEL_ID, 10L, 20L));
75         assertNextStartTime(scheduledRecordings, 9L, 10L);
76         assertNextStartTime(scheduledRecordings, 10L, DvrDataManager.NEXT_START_TIME_NOT_FOUND);
77     }
78 
assertNextStartTime( List<ScheduledRecording> scheduledRecordings, long startTime, long expected)79     private void assertNextStartTime(
80             List<ScheduledRecording> scheduledRecordings, long startTime, long expected) {
81         assertWithMessage("getNextScheduledStartTimeAfter()")
82                 .that(DvrDataManagerImpl.getNextStartTimeAfter(scheduledRecordings, startTime))
83                 .isEqualTo(expected);
84     }
85 }
86