diff options
Diffstat (limited to 'tests/unit/src/com/android/tv/dvr/recorder')
4 files changed, 411 insertions, 0 deletions
diff --git a/tests/unit/src/com/android/tv/dvr/recorder/DvrRecordingServiceTest.java b/tests/unit/src/com/android/tv/dvr/recorder/DvrRecordingServiceTest.java new file mode 100644 index 00000000..7ad8d55d --- /dev/null +++ b/tests/unit/src/com/android/tv/dvr/recorder/DvrRecordingServiceTest.java @@ -0,0 +1,68 @@ +/* + * 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.recorder; + +import static org.mockito.Mockito.verify; + +import android.os.Build; +import android.support.test.filters.SdkSuppress; +import android.support.test.filters.SmallTest; +import android.test.ServiceTestCase; + +import com.android.tv.common.feature.CommonFeatures; +import com.android.tv.common.feature.TestableFeature; +import com.android.tv.testing.FakeClock; + +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +/** + * Tests for {@link DvrRecordingService}. + */ +@SmallTest +@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) +public class DvrRecordingServiceTest extends ServiceTestCase<DvrRecordingService> { + @Mock Scheduler mMockScheduler; + private final TestableFeature mDvrFeature = CommonFeatures.DVR; + private final FakeClock mFakeClock = FakeClock.createWithCurrentTime(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + mDvrFeature.enableForTest(); + MockitoAnnotations.initMocks(this); + setupService(); + DvrRecordingService service = getService(); + service.setScheduler(mMockScheduler); + } + + @Override + protected void tearDown() throws Exception { + mDvrFeature.resetForTests(); + super.tearDown(); + } + + public DvrRecordingServiceTest() { + super(DvrRecordingService.class); + } + + public void testStartService_null() throws Exception { + startService(null); + verify(mMockScheduler, Mockito.only()).update(); + } +}
\ No newline at end of file diff --git a/tests/unit/src/com/android/tv/dvr/recorder/ScheduledProgramReaperTest.java b/tests/unit/src/com/android/tv/dvr/recorder/ScheduledProgramReaperTest.java new file mode 100644 index 00000000..d434a34e --- /dev/null +++ b/tests/unit/src/com/android/tv/dvr/recorder/ScheduledProgramReaperTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2016 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.recorder; + +import android.os.Build; +import android.support.test.filters.SdkSuppress; +import android.support.test.filters.SmallTest; +import android.test.AndroidTestCase; +import android.test.MoreAsserts; + +import com.android.tv.dvr.DvrDataManagerInMemoryImpl; +import com.android.tv.dvr.DvrManager; +import com.android.tv.dvr.data.ScheduledRecording; +import com.android.tv.testing.FakeClock; +import com.android.tv.testing.dvr.RecordingTestUtils; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.TimeUnit; + +/** + * Tests for {@link ScheduledProgramReaper}. + */ +@SmallTest +@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) +public class ScheduledProgramReaperTest extends AndroidTestCase { + private static final String INPUT_ID = "input_id"; + private static final int CHANNEL_ID = 273; + private static final long DURATION = TimeUnit.HOURS.toMillis(1); + + private ScheduledProgramReaper mReaper; + private FakeClock mFakeClock; + private DvrDataManagerInMemoryImpl mDvrDataManager; + @Mock private DvrManager mDvrManager; + + @Override + protected void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + mFakeClock = FakeClock.createWithTimeOne(); + mDvrDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock); + mReaper = new ScheduledProgramReaper(mDvrDataManager, mFakeClock); + } + + public void testRun_noRecordings() { + MoreAsserts.assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings()); + mReaper.run(); + MoreAsserts.assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings()); + } + + public void testRun_oneRecordingsTomorrow() { + ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + mReaper.run(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + } + + public void testRun_oneRecordingsStarted() { + ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + mFakeClock.increment(TimeUnit.DAYS); + mReaper.run(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + } + + public void testRun_oneRecordingsFinished() { + ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + mFakeClock.increment(TimeUnit.DAYS); + mFakeClock.increment(TimeUnit.MINUTES, 2); + mReaper.run(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + } + + public void testRun_oneRecordingsExpired() { + ScheduledRecording recording = addNewScheduledRecordingForTomorrow(); + MoreAsserts + .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording); + mFakeClock.increment(TimeUnit.DAYS, 1 + ScheduledProgramReaper.DAYS); + mFakeClock.increment(TimeUnit.MILLISECONDS, DURATION); + // After the cutoff and enough so we can see on the clock + mFakeClock.increment(TimeUnit.SECONDS, 1); + + mReaper.run(); + MoreAsserts.assertContentsInAnyOrder( + "Recordings after reaper at " + com.android.tv.util.Utils + .toIsoDateTimeString(mFakeClock.currentTimeMillis()), + mDvrDataManager.getAllScheduledRecordings()); + } + + private ScheduledRecording addNewScheduledRecordingForTomorrow() { + long startTime = mFakeClock.currentTimeMillis() + TimeUnit.DAYS.toMillis(1); + ScheduledRecording recording = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID, + CHANNEL_ID, startTime, startTime + DURATION); + return mDvrDataManager.addScheduledRecordingInternal( + ScheduledRecording.buildFrom(recording) + .setState(ScheduledRecording.STATE_RECORDING_FINISHED).build()); + } +} diff --git a/tests/unit/src/com/android/tv/dvr/recorder/SchedulerTest.java b/tests/unit/src/com/android/tv/dvr/recorder/SchedulerTest.java new file mode 100644 index 00000000..94cfaac1 --- /dev/null +++ b/tests/unit/src/com/android/tv/dvr/recorder/SchedulerTest.java @@ -0,0 +1,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.recorder; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.os.Build; +import android.os.Looper; +import android.support.test.filters.SdkSuppress; +import android.support.test.filters.SmallTest; +import android.test.AndroidTestCase; + +import com.android.tv.InputSessionManager; +import com.android.tv.data.ChannelDataManager; +import com.android.tv.dvr.DvrDataManagerInMemoryImpl; +import com.android.tv.dvr.DvrManager; +import com.android.tv.dvr.data.ScheduledRecording; +import com.android.tv.testing.FakeClock; +import com.android.tv.testing.dvr.RecordingTestUtils; +import com.android.tv.util.TvInputManagerHelper; + +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.TimeUnit; + +/** + * Tests for {@link Scheduler}. + */ +@SmallTest +@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) +public class SchedulerTest extends AndroidTestCase { + private static final String INPUT_ID = "input_id"; + private static final int CHANNEL_ID = 273; + + private FakeClock mFakeClock; + private DvrDataManagerInMemoryImpl mDataManager; + private Scheduler mScheduler; + @Mock DvrManager mDvrManager; + @Mock InputSessionManager mSessionManager; + @Mock AlarmManager mMockAlarmManager; + @Mock ChannelDataManager mChannelDataManager; + @Mock TvInputManagerHelper mInputManager; + + @Override + protected void setUp() throws Exception { + super.setUp(); + MockitoAnnotations.initMocks(this); + mFakeClock = FakeClock.createWithCurrentTime(); + mDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock); + Mockito.when(mChannelDataManager.isDbLoadFinished()).thenReturn(true); + mScheduler = new Scheduler(Looper.myLooper(), mDvrManager, mSessionManager, mDataManager, + mChannelDataManager, mInputManager, getContext(), mFakeClock, mMockAlarmManager); + } + + public void testUpdate_none() throws Exception { + mScheduler.start(); + mScheduler.update(); + verifyZeroInteractions(mMockAlarmManager); + } + + public void testUpdate_nextIn12Hours() throws Exception { + long now = mFakeClock.currentTimeMillis(); + long startTime = now + TimeUnit.HOURS.toMillis(12); + ScheduledRecording r = RecordingTestUtils + .createTestRecordingWithPeriod(INPUT_ID, CHANNEL_ID, startTime, + startTime + TimeUnit.HOURS.toMillis(1)); + mDataManager.addScheduledRecording(r); + mScheduler.start(); + verify(mMockAlarmManager).setExactAndAllowWhileIdle( + eq(AlarmManager.RTC_WAKEUP), + eq(startTime - Scheduler.MS_TO_WAKE_BEFORE_START), + any(PendingIntent.class)); + Mockito.reset(mMockAlarmManager); + mScheduler.update(); + verify(mMockAlarmManager).setExactAndAllowWhileIdle( + eq(AlarmManager.RTC_WAKEUP), + eq(startTime - Scheduler.MS_TO_WAKE_BEFORE_START), + any(PendingIntent.class)); + } + + public void testStartsWithin() throws Exception { + long now = mFakeClock.currentTimeMillis(); + long startTime = now + 3; + ScheduledRecording r = RecordingTestUtils + .createTestRecordingWithPeriod(INPUT_ID, CHANNEL_ID, startTime, startTime + 100); + assertFalse(mScheduler.startsWithin(r, 2)); + assertTrue(mScheduler.startsWithin(r, 3)); + } +}
\ No newline at end of file diff --git a/tests/unit/src/com/android/tv/dvr/recorder/SeriesRecordingSchedulerTest.java b/tests/unit/src/com/android/tv/dvr/recorder/SeriesRecordingSchedulerTest.java new file mode 100644 index 00000000..afb9c042 --- /dev/null +++ b/tests/unit/src/com/android/tv/dvr/recorder/SeriesRecordingSchedulerTest.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2016 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.recorder; + +import android.os.Build; +import android.support.test.filters.SdkSuppress; +import android.support.test.filters.SmallTest; +import android.test.AndroidTestCase; +import android.test.MoreAsserts; +import android.util.LongSparseArray; + +import com.android.tv.data.Program; +import com.android.tv.dvr.DvrDataManagerInMemoryImpl; +import com.android.tv.dvr.data.SeriesRecording; +import com.android.tv.testing.FakeClock; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Tests for {@link SeriesRecordingScheduler} + */ +@SmallTest +@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) +public class SeriesRecordingSchedulerTest extends AndroidTestCase { + private static final String PROGRAM_TITLE = "MyProgram"; + private static final long CHANNEL_ID = 123; + private static final long SERIES_RECORDING_ID1 = 1; + private static final String SERIES_ID = "SERIES_ID"; + private static final String SEASON_NUMBER1 = "SEASON NUMBER1"; + private static final String SEASON_NUMBER2 = "SEASON NUMBER2"; + private static final String EPISODE_NUMBER1 = "EPISODE NUMBER1"; + private static final String EPISODE_NUMBER2 = "EPISODE NUMBER2"; + + private final SeriesRecording mBaseSeriesRecording = new SeriesRecording.Builder() + .setTitle(PROGRAM_TITLE).setChannelId(CHANNEL_ID).setSeriesId(SERIES_ID).build(); + private final Program mBaseProgram = new Program.Builder().setTitle(PROGRAM_TITLE) + .setChannelId(CHANNEL_ID).setSeriesId(SERIES_ID).build(); + + private DvrDataManagerInMemoryImpl mDataManager; + + @Override + protected void setUp() throws Exception { + super.setUp(); + FakeClock fakeClock = FakeClock.createWithCurrentTime(); + mDataManager = new DvrDataManagerInMemoryImpl(getContext(), fakeClock); + } + + public void testPickOneProgramPerEpisode_onePerEpisode() { + SeriesRecording seriesRecording = SeriesRecording.buildFrom(mBaseSeriesRecording) + .setId(SERIES_RECORDING_ID1).build(); + mDataManager.addSeriesRecording(seriesRecording); + List<Program> programs = new ArrayList<>(); + Program program1 = new Program.Builder(mBaseProgram).setSeasonNumber(SEASON_NUMBER1) + .setEpisodeNumber(EPISODE_NUMBER1).build(); + programs.add(program1); + Program program2 = new Program.Builder(mBaseProgram).setSeasonNumber(SEASON_NUMBER2) + .setEpisodeNumber(EPISODE_NUMBER2).build(); + programs.add(program2); + LongSparseArray<List<Program>> result = SeriesRecordingScheduler.pickOneProgramPerEpisode( + mDataManager, Collections.singletonList(seriesRecording), programs); + MoreAsserts.assertContentsInAnyOrder(result.get(SERIES_RECORDING_ID1), program1, program2); + } + + public void testPickOneProgramPerEpisode_manyPerEpisode() { + SeriesRecording seriesRecording = SeriesRecording.buildFrom(mBaseSeriesRecording) + .setId(SERIES_RECORDING_ID1).build(); + mDataManager.addSeriesRecording(seriesRecording); + List<Program> programs = new ArrayList<>(); + Program program1 = new Program.Builder(mBaseProgram).setSeasonNumber(SEASON_NUMBER1) + .setEpisodeNumber(EPISODE_NUMBER1).setStartTimeUtcMillis(0).build(); + programs.add(program1); + Program program2 = new Program.Builder(program1).setStartTimeUtcMillis(1).build(); + programs.add(program2); + Program program3 = new Program.Builder(mBaseProgram).setSeasonNumber(SEASON_NUMBER2) + .setEpisodeNumber(EPISODE_NUMBER2).build(); + programs.add(program3); + Program program4 = new Program.Builder(program1).setStartTimeUtcMillis(1).build(); + programs.add(program4); + LongSparseArray<List<Program>> result = SeriesRecordingScheduler.pickOneProgramPerEpisode( + mDataManager, Collections.singletonList(seriesRecording), programs); + MoreAsserts.assertContentsInAnyOrder(result.get(SERIES_RECORDING_ID1), program1, program3); + } + + public void testPickOneProgramPerEpisode_nullEpisode() { + SeriesRecording seriesRecording = SeriesRecording.buildFrom(mBaseSeriesRecording) + .setId(SERIES_RECORDING_ID1).build(); + mDataManager.addSeriesRecording(seriesRecording); + List<Program> programs = new ArrayList<>(); + Program program1 = new Program.Builder(mBaseProgram).setStartTimeUtcMillis(0).build(); + programs.add(program1); + Program program2 = new Program.Builder(mBaseProgram).setStartTimeUtcMillis(1).build(); + programs.add(program2); + LongSparseArray<List<Program>> result = SeriesRecordingScheduler.pickOneProgramPerEpisode( + mDataManager, Collections.singletonList(seriesRecording), programs); + MoreAsserts.assertContentsInAnyOrder(result.get(SERIES_RECORDING_ID1), program1, program2); + } +} |