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.recorder; 18 19 import static android.support.test.InstrumentationRegistry.getContext; 20 import static org.junit.Assert.assertTrue; 21 22 import android.os.Build; 23 import android.support.test.filters.SdkSuppress; 24 import android.support.test.filters.SmallTest; 25 import android.test.MoreAsserts; 26 27 import com.android.tv.common.feature.CommonFeatures; 28 import com.android.tv.common.feature.TestableFeature; 29 import com.android.tv.dvr.DvrDataManagerInMemoryImpl; 30 import com.android.tv.dvr.DvrManager; 31 import com.android.tv.dvr.data.ScheduledRecording; 32 import com.android.tv.testing.FakeClock; 33 import com.android.tv.testing.dvr.RecordingTestUtils; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 import java.util.concurrent.TimeUnit; 42 43 /** 44 * Tests for {@link ScheduledProgramReaper}. 45 */ 46 @SmallTest 47 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) 48 public class ScheduledProgramReaperTest { 49 private static final String INPUT_ID = "input_id"; 50 private static final int CHANNEL_ID = 273; 51 private static final long DURATION = TimeUnit.HOURS.toMillis(1); 52 53 private ScheduledProgramReaper mReaper; 54 private FakeClock mFakeClock; 55 private DvrDataManagerInMemoryImpl mDvrDataManager; 56 @Mock private DvrManager mDvrManager; 57 private final TestableFeature mDvrFeature = CommonFeatures.DVR; 58 59 @Before 60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mDvrFeature.enableForTest(); 63 mFakeClock = FakeClock.createWithTimeOne(); 64 mDvrDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock); 65 mReaper = new ScheduledProgramReaper(mDvrDataManager, mFakeClock); 66 } 67 68 @After 69 public void tearDown() { 70 mDvrFeature.resetForTests(); 71 } 72 73 @Test 74 public void testRun_noRecordings() { 75 assertTrue(mDvrDataManager.getAllScheduledRecordings().isEmpty()); 76 mReaper.run(); 77 assertTrue(mDvrDataManager.getAllScheduledRecordings().isEmpty()); 78 } 79 80 @Test 81 public void testRun_oneRecordingsTomorrow() { 82 ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); 83 MoreAsserts 84 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 85 mReaper.run(); 86 MoreAsserts 87 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 88 } 89 90 @Test 91 public void testRun_oneRecordingsStarted() { 92 ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); 93 MoreAsserts 94 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 95 mFakeClock.increment(TimeUnit.DAYS); 96 mReaper.run(); 97 MoreAsserts 98 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 99 } 100 101 @Test 102 public void testRun_oneRecordingsFinished() { 103 ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); 104 MoreAsserts 105 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 106 mFakeClock.increment(TimeUnit.DAYS); 107 mFakeClock.increment(TimeUnit.MINUTES, 2); 108 mReaper.run(); 109 MoreAsserts 110 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 111 } 112 113 @Test 114 public void testRun_oneRecordingsExpired() { 115 ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); 116 MoreAsserts 117 .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); 118 mFakeClock.increment(TimeUnit.DAYS, 1 + ScheduledProgramReaper.DAYS); 119 mFakeClock.increment(TimeUnit.MILLISECONDS, DURATION); 120 // After the cutoff and enough so we can see on the clock 121 mFakeClock.increment(TimeUnit.SECONDS, 1); 122 123 mReaper.run(); 124 assertTrue("Recordings after reaper at " + com.android.tv.util.Utils 125 .toIsoDateTimeString(mFakeClock.currentTimeMillis()), 126 mDvrDataManager.getAllScheduledRecordings().isEmpty()); 127 } 128 129 private ScheduledRecording addNewScheduledRecordingForTomorrow() { 130 long startTime = mFakeClock.currentTimeMillis() + TimeUnit.DAYS.toMillis(1); 131 ScheduledRecording recording = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID, 132 CHANNEL_ID, startTime, startTime + DURATION); 133 return mDvrDataManager.addScheduledRecordingInternal( 134 ScheduledRecording.buildFrom(recording) 135 .setState(ScheduledRecording.STATE_RECORDING_FINISHED).build()); 136 } 137 } 138