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 
17 package com.android.tv.dvr;
18 
19 import android.test.MoreAsserts;
20 
21 import com.android.tv.testing.FakeClock;
22 import com.android.tv.testing.dvr.RecordingTestUtils;
23 
24 import junit.framework.TestCase;
25 
26 import java.util.concurrent.TimeUnit;
27 
28 /**
29  * Tests for {@link ScheduledProgramReaper}.
30  */
31 public class ScheduledProgramReaperTest extends TestCase {
32     public static final int CHANNEL_ID = 273;
33     public static final long DURATION = TimeUnit.HOURS.toMillis(1);
34 
35     private ScheduledProgramReaper mReaper;
36     private FakeClock mFakeClock;
37     private DvrDataManagerInMemoryImpl mDvrDataManager;
38     private ScheduledRecording mScheduledRecordingDay1;
39 
40 
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44         mFakeClock = FakeClock.createWithTimeOne();
45         mDvrDataManager = new DvrDataManagerInMemoryImpl(null, mFakeClock);
46         mReaper = new ScheduledProgramReaper(mDvrDataManager, mFakeClock);
47     }
48 
49 
testRun_noRecordings()50     public void testRun_noRecordings() {
51         MoreAsserts.assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings());
52         mReaper.run();
53         MoreAsserts.assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings());
54     }
55 
testRun_oneRecordingsTomorrow()56     public void testRun_oneRecordingsTomorrow() {
57         ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
58         MoreAsserts
59                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
60         mReaper.run();
61         MoreAsserts
62                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
63     }
64 
testRun_oneRecordingsStarted()65     public void testRun_oneRecordingsStarted() {
66         ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
67         MoreAsserts
68                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
69         mFakeClock.increment(TimeUnit.DAYS);
70         mReaper.run();
71         MoreAsserts
72                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
73     }
74 
testRun_oneRecordingsFinished()75     public void testRun_oneRecordingsFinished() {
76         ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
77         MoreAsserts
78                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
79         mFakeClock.increment(TimeUnit.DAYS);
80         mFakeClock.increment(TimeUnit.MINUTES, 2);
81         mReaper.run();
82         MoreAsserts
83                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
84     }
85 
testRun_oneRecordingsExpired()86     public void testRun_oneRecordingsExpired() {
87         ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
88         MoreAsserts
89                 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
90         mFakeClock.increment(TimeUnit.DAYS, 1 + ScheduledProgramReaper.DAYS);
91         mFakeClock.increment(TimeUnit.MILLISECONDS, DURATION);
92         // After the cutoff and enough so we can see on the clock
93         mFakeClock.increment(TimeUnit.SECONDS, 1);
94 
95         mReaper.run();
96         MoreAsserts.assertContentsInAnyOrder(
97                 "Recordings after reaper at " + com.android.tv.util.Utils
98                         .toIsoDateTimeString(mFakeClock.currentTimeMillis()),
99                 mDvrDataManager.getAllScheduledRecordings());
100     }
101 
addNewScheduledRecordingForTomorrow()102     private ScheduledRecording addNewScheduledRecordingForTomorrow() {
103         long startTime = mFakeClock.currentTimeMillis() + TimeUnit.DAYS.toMillis(1);
104         return RecordingTestUtils.addScheduledRecording(mDvrDataManager, CHANNEL_ID, startTime,
105                 startTime + DURATION);
106     }
107 }
108