aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/dvr/recorder
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/src/com/android/tv/dvr/recorder')
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/DvrRecordingServiceTest.java183
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/InputTaskSchedulerTest.java231
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/RecordingTaskTest.java149
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/ScheduledProgramReaperTest.java137
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/SchedulerTest.java125
-rw-r--r--tests/unit/src/com/android/tv/dvr/recorder/SeriesRecordingSchedulerTest.java129
6 files changed, 954 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..8f7dcaf2
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/DvrRecordingServiceTest.java
@@ -0,0 +1,183 @@
+/*
+ * 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.content.Intent;
+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 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<DvrRecordingServiceTest.MockDvrRecordingService> {
+ private final TestableFeature mDvrFeature = CommonFeatures.DVR;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mDvrFeature.enableForTest();
+ MockitoAnnotations.initMocks(this);
+ setupService();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ mDvrFeature.resetForTests();
+ super.tearDown();
+ }
+
+ public DvrRecordingServiceTest() {
+ super(MockDvrRecordingService.class);
+ }
+
+ public void testStartService_null() throws Exception {
+ // Not recording
+ startService(null);
+ assertFalse(getService().mInForeground);
+
+ // Recording
+ getService().startRecording();
+ startService(null);
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mIsRecording);
+ getService().reset();
+ }
+
+ public void testStartService_noUpcomingRecording() throws Exception {
+ Intent intent = new Intent(getContext(), DvrRecordingServiceTest.class);
+ intent.putExtra(DvrRecordingService.EXTRA_START_FOR_RECORDING, false);
+
+ // Not recording
+ startService(intent);
+ assertTrue(getService().mInForeground);
+ assertFalse(getService().mForegroundForUpcomingRecording);
+ getService().stopForegroundIfNotRecordingInternal();
+ assertFalse(getService().mInForeground);
+
+ // Recording, ended quickly
+ getService().startRecording();
+ startService(intent);
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopRecording();
+ assertFalse(getService().mInForeground);
+ assertFalse(getService().mIsRecording);
+ getService().stopForegroundIfNotRecordingInternal();
+ assertFalse(getService().mInForeground);
+ assertFalse(getService().mIsRecording);
+ getService().reset();
+
+ // Recording, ended later
+ getService().startRecording();
+ startService(intent);
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopForegroundIfNotRecordingInternal();
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopRecording();
+ assertFalse(getService().mInForeground);
+ assertFalse(getService().mIsRecording);
+ getService().reset();
+ }
+
+ public void testStartService_hasUpcomingRecording() throws Exception {
+ Intent intent = new Intent(getContext(), DvrRecordingServiceTest.class);
+ intent.putExtra(DvrRecordingService.EXTRA_START_FOR_RECORDING, true);
+
+ // Not recording
+ startService(intent);
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertFalse(getService().mIsRecording);
+ getService().startRecording();
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopRecording();
+ assertFalse(getService().mInForeground);
+ assertFalse(getService().mIsRecording);
+ getService().reset();
+
+ // Recording
+ getService().startRecording();
+ startService(intent);
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().startRecording();
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopRecording();
+ assertTrue(getService().mInForeground);
+ assertTrue(getService().mForegroundForUpcomingRecording);
+ assertTrue(getService().mIsRecording);
+ getService().stopRecording();
+ assertFalse(getService().mInForeground);
+ assertFalse(getService().mIsRecording);
+ getService().reset();
+ }
+
+ public static class MockDvrRecordingService extends DvrRecordingService {
+ private int mRecordingCount = 0;
+ private boolean mInForeground;
+ private boolean mForegroundForUpcomingRecording;
+
+ @Override
+ protected void startForegroundInternal(boolean hasUpcomingRecording) {
+ mForegroundForUpcomingRecording = hasUpcomingRecording;
+ mInForeground = true;
+ }
+
+ @Override
+ protected void stopForegroundInternal() {
+ mInForeground = false;
+ }
+
+ private void startRecording() {
+ mOnRecordingSessionChangeListener.onRecordingSessionChange(true, ++mRecordingCount);
+ }
+
+ private void stopRecording() {
+ mOnRecordingSessionChangeListener.onRecordingSessionChange(false, --mRecordingCount);
+ }
+
+ private void reset() {
+ mRecordingCount = 0;
+ mInForeground = false;
+ mIsRecording = false;
+ }
+ }
+} \ No newline at end of file
diff --git a/tests/unit/src/com/android/tv/dvr/recorder/InputTaskSchedulerTest.java b/tests/unit/src/com/android/tv/dvr/recorder/InputTaskSchedulerTest.java
new file mode 100644
index 00000000..e5c27e2c
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/InputTaskSchedulerTest.java
@@ -0,0 +1,231 @@
+/*
+ * 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 static android.support.test.InstrumentationRegistry.getContext;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.AlarmManager;
+import android.media.tv.TvInputInfo;
+import android.os.Build;
+import android.os.Looper;
+import android.os.SystemClock;
+import android.support.test.filters.SdkSuppress;
+import android.support.test.filters.SmallTest;
+
+import com.android.tv.InputSessionManager;
+import com.android.tv.data.Channel;
+import com.android.tv.data.ChannelDataManager;
+import com.android.tv.dvr.DvrManager;
+import com.android.tv.dvr.WritableDvrDataManager;
+import com.android.tv.dvr.data.ScheduledRecording;
+import com.android.tv.dvr.recorder.InputTaskScheduler.RecordingTaskFactory;
+import com.android.tv.testing.FakeClock;
+import com.android.tv.testing.dvr.RecordingTestUtils;
+import com.android.tv.util.Clock;
+import com.android.tv.util.TestUtils;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Tests for {@link InputTaskScheduler}.
+ */
+@SmallTest
+@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
+public class InputTaskSchedulerTest {
+ private static final String INPUT_ID = "input_id";
+ private static final int CHANNEL_ID = 1;
+ private static final long LISTENER_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(1);
+ private static final int TUNER_COUNT_ONE = 1;
+ private static final int TUNER_COUNT_TWO = 2;
+ private static final long LOW_PRIORITY = 1;
+ private static final long HIGH_PRIORITY = 2;
+
+ private FakeClock mFakeClock;
+ private InputTaskScheduler mScheduler;
+ @Mock private DvrManager mDvrManager;
+ @Mock private WritableDvrDataManager mDataManager;
+ @Mock private InputSessionManager mSessionManager;
+ @Mock private AlarmManager mMockAlarmManager;
+ @Mock private ChannelDataManager mChannelDataManager;
+ private List<RecordingTask> mRecordingTasks;
+
+ @Before
+ public void setUp() throws Exception {
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+ mRecordingTasks = new ArrayList();
+ MockitoAnnotations.initMocks(this);
+ mFakeClock = FakeClock.createWithCurrentTime();
+ TvInputInfo input = createTvInputInfo(TUNER_COUNT_ONE);
+ mScheduler = new InputTaskScheduler(getContext(), input, Looper.myLooper(),
+ mChannelDataManager, mDvrManager, mDataManager, mSessionManager, mFakeClock,
+ new RecordingTaskFactory() {
+ @Override
+ public RecordingTask createRecordingTask(ScheduledRecording scheduledRecording,
+ Channel channel, DvrManager dvrManager,
+ InputSessionManager sessionManager, WritableDvrDataManager dataManager,
+ Clock clock) {
+ RecordingTask task = mock(RecordingTask.class);
+ when(task.getPriority()).thenReturn(scheduledRecording.getPriority());
+ when(task.getEndTimeMs()).thenReturn(scheduledRecording.getEndTimeMs());
+ mRecordingTasks.add(task);
+ return task;
+ }
+ });
+ }
+
+ @Test
+ public void testAddSchedule_past() {
+ ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
+ CHANNEL_ID, 0L, 1L);
+ when(mDataManager.getScheduledRecording(anyLong())).thenReturn(r);
+ mScheduler.handleAddSchedule(r);
+ mScheduler.handleBuildSchedule();
+ verify(mDataManager, timeout((int) LISTENER_TIMEOUT_MS).times(1))
+ .changeState(any(ScheduledRecording.class),
+ eq(ScheduledRecording.STATE_RECORDING_FAILED));
+ }
+
+ @Test
+ public void testAddSchedule_start() {
+ mScheduler.handleAddSchedule(RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
+ CHANNEL_ID, mFakeClock.currentTimeMillis(),
+ mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)));
+ mScheduler.handleBuildSchedule();
+ verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
+ }
+
+ @Test
+ public void testAddSchedule_consecutiveNoStop() {
+ long startTimeMs = mFakeClock.currentTimeMillis();
+ long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ long id = 0;
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ LOW_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ startTimeMs = endTimeMs;
+ endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ HIGH_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
+ // The first schedule should not be stopped because the second one should wait for the end
+ // of the first schedule.
+ SystemClock.sleep(LISTENER_TIMEOUT_MS);
+ verify(mRecordingTasks.get(0), never()).stop();
+ }
+
+ @Test
+ public void testAddSchedule_consecutiveNoFail() {
+ long startTimeMs = mFakeClock.currentTimeMillis();
+ long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ long id = 0;
+ when(mDataManager.getScheduledRecording(anyLong())).thenReturn(ScheduledRecording
+ .builder(INPUT_ID, CHANNEL_ID, 0L, 0L).build());
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ HIGH_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ startTimeMs = endTimeMs;
+ endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ LOW_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
+ SystemClock.sleep(LISTENER_TIMEOUT_MS);
+ verify(mRecordingTasks.get(0), never()).stop();
+ // The second schedule should not fail because it can starts after the first one finishes.
+ SystemClock.sleep(LISTENER_TIMEOUT_MS);
+ verify(mDataManager, never())
+ .changeState(any(ScheduledRecording.class),
+ eq(ScheduledRecording.STATE_RECORDING_FAILED));
+ }
+
+ @Test
+ public void testAddSchedule_consecutiveUseLessSession() throws Exception {
+ TvInputInfo input = createTvInputInfo(TUNER_COUNT_TWO);
+ mScheduler.updateTvInputInfo(input);
+ long startTimeMs = mFakeClock.currentTimeMillis();
+ long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ long id = 0;
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ LOW_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ startTimeMs = endTimeMs;
+ endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
+ mScheduler.handleAddSchedule(
+ RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
+ HIGH_PRIORITY, startTimeMs, endTimeMs));
+ mScheduler.handleBuildSchedule();
+ verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
+ SystemClock.sleep(LISTENER_TIMEOUT_MS);
+ verify(mRecordingTasks.get(0), never()).stop();
+ // The second schedule should wait until the first one finishes rather than creating a new
+ // session even though there are available tuners.
+ assertTrue(mRecordingTasks.size() == 1);
+ }
+
+ @Test
+ public void testUpdateSchedule_noCancel() {
+ ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
+ CHANNEL_ID, mFakeClock.currentTimeMillis(),
+ mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1));
+ mScheduler.handleAddSchedule(r);
+ mScheduler.handleBuildSchedule();
+ mScheduler.handleUpdateSchedule(r);
+ SystemClock.sleep(LISTENER_TIMEOUT_MS);
+ verify(mRecordingTasks.get(0), never()).cancel();
+ }
+
+ @Test
+ public void testUpdateSchedule_cancel() {
+ ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
+ CHANNEL_ID, mFakeClock.currentTimeMillis(),
+ mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(2));
+ mScheduler.handleAddSchedule(r);
+ mScheduler.handleBuildSchedule();
+ mScheduler.handleUpdateSchedule(ScheduledRecording.buildFrom(r)
+ .setStartTimeMs(mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1))
+ .build());
+ verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).cancel();
+ }
+
+ private TvInputInfo createTvInputInfo(int tunerCount) throws Exception {
+ return TestUtils.createTvInputInfo(null, null, null, 0, false, true, tunerCount);
+ }
+}
diff --git a/tests/unit/src/com/android/tv/dvr/recorder/RecordingTaskTest.java b/tests/unit/src/com/android/tv/dvr/recorder/RecordingTaskTest.java
new file mode 100644
index 00000000..37561a42
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/RecordingTaskTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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 android.support.test.InstrumentationRegistry.getContext;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.support.test.filters.SdkSuppress;
+import android.support.test.filters.SmallTest;
+
+import com.android.tv.InputSessionManager;
+import com.android.tv.InputSessionManager.RecordingSession;
+import com.android.tv.common.feature.CommonFeatures;
+import com.android.tv.common.feature.TestableFeature;
+import com.android.tv.data.Channel;
+import com.android.tv.dvr.DvrDataManagerInMemoryImpl;
+import com.android.tv.dvr.DvrManager;
+import com.android.tv.dvr.data.ScheduledRecording;
+import com.android.tv.dvr.recorder.RecordingTask.State;
+import com.android.tv.testing.FakeClock;
+import com.android.tv.testing.dvr.RecordingTestUtils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Tests for {@link RecordingTask}.
+ */
+@SmallTest
+@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
+public class RecordingTaskTest {
+ private static final long DURATION = TimeUnit.MINUTES.toMillis(30);
+ private static final long START_OFFSET_MS = RecordingScheduler.MS_TO_WAKE_BEFORE_START;
+ private static final String INPUT_ID = "input_id";
+ private static final int CHANNEL_ID = 273;
+
+ private FakeClock mFakeClock;
+ private DvrDataManagerInMemoryImpl mDataManager;
+ @Mock Handler mMockHandler;
+ @Mock DvrManager mDvrManager;
+ @Mock InputSessionManager mMockSessionManager;
+ @Mock RecordingSession mMockRecordingSession;
+ private final TestableFeature mDvrFeature = CommonFeatures.DVR;
+
+ @Before
+ public void setUp() {
+ mDvrFeature.enableForTest();
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+ MockitoAnnotations.initMocks(this);
+ mFakeClock = FakeClock.createWithCurrentTime();
+ mDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock);
+ }
+
+ @After
+ public void tearDown() {
+ mDvrFeature.resetForTests();
+ }
+
+ @Test
+ public void testHandle_init() {
+ Channel channel = createTestChannel();
+ ScheduledRecording r = createRecording(channel);
+ RecordingTask task = createRecordingTask(r, channel);
+ String inputId = channel.getInputId();
+ when(mMockSessionManager.createRecordingSession(eq(inputId), anyString(), eq(task),
+ eq(mMockHandler), anyLong())).thenReturn(mMockRecordingSession);
+ when(mMockHandler.sendMessageAtTime(anyObject(), anyLong())).thenReturn(true);
+ assertTrue(task.handleMessage(createMessage(RecordingTask.MSG_INITIALIZE)));
+ assertEquals(State.CONNECTION_PENDING, task.getState());
+ verify(mMockSessionManager).createRecordingSession(eq(inputId), anyString(), eq(task),
+ eq(mMockHandler), anyLong());
+ verify(mMockRecordingSession).tune(eq(inputId), eq(channel.getUri()));
+ verifyNoMoreInteractions(mMockHandler, mMockRecordingSession, mMockSessionManager);
+ }
+
+ private static Channel createTestChannel() {
+ return new Channel.Builder().setInputId(INPUT_ID).setId(CHANNEL_ID)
+ .setDisplayName("Test Ch " + CHANNEL_ID).build();
+ }
+
+ @Test
+ public void testOnConnected() {
+ Channel channel = createTestChannel();
+ ScheduledRecording r = createRecording(channel);
+ mDataManager.addScheduledRecording(r);
+ RecordingTask task = createRecordingTask(r, channel);
+ String inputId = channel.getInputId();
+ when(mMockSessionManager.createRecordingSession(eq(inputId), anyString(), eq(task),
+ eq(mMockHandler), anyLong())).thenReturn(mMockRecordingSession);
+ when(mMockHandler.sendMessageAtTime(anyObject(), anyLong())).thenReturn(true);
+ task.handleMessage(createMessage(RecordingTask.MSG_INITIALIZE));
+ task.onTuned(channel.getUri());
+ assertEquals(State.CONNECTED, task.getState());
+ }
+
+ private ScheduledRecording createRecording(Channel c) {
+ long startTime = mFakeClock.currentTimeMillis() + START_OFFSET_MS;
+ long endTime = startTime + DURATION;
+ return RecordingTestUtils.createTestRecordingWithPeriod(c.getInputId(), c.getId(),
+ startTime, endTime);
+ }
+
+ private RecordingTask createRecordingTask(ScheduledRecording r, Channel channel) {
+ RecordingTask recordingTask = new RecordingTask(getContext(), r, channel, mDvrManager,
+ mMockSessionManager, mDataManager, mFakeClock);
+ recordingTask.setHandler(mMockHandler);
+ return recordingTask;
+ }
+
+ private Message createMessage(int what) {
+ Message msg = new Message();
+ msg.setTarget(mMockHandler);
+ msg.what = what;
+ return msg;
+ }
+} \ 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..ca72e13f
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/ScheduledProgramReaperTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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 static android.support.test.InstrumentationRegistry.getContext;
+import static org.junit.Assert.assertTrue;
+
+import android.os.Build;
+import android.support.test.filters.SdkSuppress;
+import android.support.test.filters.SmallTest;
+import android.test.MoreAsserts;
+
+import com.android.tv.common.feature.CommonFeatures;
+import com.android.tv.common.feature.TestableFeature;
+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.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+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 {
+ 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;
+ private final TestableFeature mDvrFeature = CommonFeatures.DVR;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mDvrFeature.enableForTest();
+ mFakeClock = FakeClock.createWithTimeOne();
+ mDvrDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock);
+ mReaper = new ScheduledProgramReaper(mDvrDataManager, mFakeClock);
+ }
+
+ @After
+ public void tearDown() {
+ mDvrFeature.resetForTests();
+ }
+
+ @Test
+ public void testRun_noRecordings() {
+ assertTrue(mDvrDataManager.getAllScheduledRecordings().isEmpty());
+ mReaper.run();
+ assertTrue(mDvrDataManager.getAllScheduledRecordings().isEmpty());
+ }
+
+ @Test
+ public void testRun_oneRecordingsTomorrow() {
+ ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
+ MoreAsserts
+ .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
+ mReaper.run();
+ MoreAsserts
+ .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
+ }
+
+ @Test
+ public void testRun_oneRecordingsStarted() {
+ ScheduledRecording recording = addNewScheduledRecordingForTomorrow();
+ MoreAsserts
+ .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
+ mFakeClock.increment(TimeUnit.DAYS);
+ mReaper.run();
+ MoreAsserts
+ .assertContentsInAnyOrder(mDvrDataManager.getAllScheduledRecordings(), recording);
+ }
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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();
+ assertTrue("Recordings after reaper at " + com.android.tv.util.Utils
+ .toIsoDateTimeString(mFakeClock.currentTimeMillis()),
+ mDvrDataManager.getAllScheduledRecordings().isEmpty());
+ }
+
+ 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..a5154729
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/SchedulerTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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 android.support.test.InstrumentationRegistry.getTargetContext;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+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 com.android.tv.InputSessionManager;
+import com.android.tv.common.feature.CommonFeatures;
+import com.android.tv.common.feature.TestableFeature;
+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.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Tests for {@link RecordingScheduler}.
+ */
+@SmallTest
+@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
+public class SchedulerTest {
+ private static final String INPUT_ID = "input_id";
+ private static final int CHANNEL_ID = 273;
+
+ private FakeClock mFakeClock;
+ private DvrDataManagerInMemoryImpl mDataManager;
+ private RecordingScheduler mScheduler;
+ @Mock DvrManager mDvrManager;
+ @Mock InputSessionManager mSessionManager;
+ @Mock AlarmManager mMockAlarmManager;
+ @Mock ChannelDataManager mChannelDataManager;
+ @Mock TvInputManagerHelper mInputManager;
+ private final TestableFeature mDvrFeature = CommonFeatures.DVR;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mDvrFeature.enableForTest();
+ mFakeClock = FakeClock.createWithCurrentTime();
+ mDataManager = new DvrDataManagerInMemoryImpl(getTargetContext(), mFakeClock);
+ Mockito.when(mChannelDataManager.isDbLoadFinished()).thenReturn(true);
+ mScheduler = new RecordingScheduler(Looper.myLooper(), mDvrManager, mSessionManager, mDataManager,
+ mChannelDataManager, mInputManager, getTargetContext(), mFakeClock,
+ mMockAlarmManager);
+ }
+
+ @After
+ public void tearDown() {
+ mDvrFeature.resetForTests();
+ }
+
+ @Test
+ public void testUpdate_none() {
+ mScheduler.updateAndStartServiceIfNeeded();
+ verifyZeroInteractions(mMockAlarmManager);
+ }
+
+ @Test
+ public void testUpdate_nextIn12Hours() {
+ 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);
+ verify(mMockAlarmManager).setExactAndAllowWhileIdle(
+ eq(AlarmManager.RTC_WAKEUP),
+ eq(startTime - RecordingScheduler.MS_TO_WAKE_BEFORE_START),
+ any(PendingIntent.class));
+ Mockito.reset(mMockAlarmManager);
+ mScheduler.updateAndStartServiceIfNeeded();
+ verify(mMockAlarmManager).setExactAndAllowWhileIdle(
+ eq(AlarmManager.RTC_WAKEUP),
+ eq(startTime - RecordingScheduler.MS_TO_WAKE_BEFORE_START),
+ any(PendingIntent.class));
+ }
+
+ @Test
+ public void testStartsWithin() {
+ 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..16fa1baf
--- /dev/null
+++ b/tests/unit/src/com/android/tv/dvr/recorder/SeriesRecordingSchedulerTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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 static android.support.test.InstrumentationRegistry.getContext;
+
+import android.os.Build;
+import android.support.test.filters.SdkSuppress;
+import android.support.test.filters.SmallTest;
+import android.test.MoreAsserts;
+import android.util.LongSparseArray;
+
+import com.android.tv.common.feature.CommonFeatures;
+import com.android.tv.common.feature.TestableFeature;
+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 org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+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 {
+ 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 final TestableFeature mDvrFeature = CommonFeatures.DVR;
+
+ private DvrDataManagerInMemoryImpl mDataManager;
+
+ @Before
+ public void setUp() {
+ mDvrFeature.enableForTest();
+ FakeClock fakeClock = FakeClock.createWithCurrentTime();
+ mDataManager = new DvrDataManagerInMemoryImpl(getContext(), fakeClock);
+ }
+
+ @After
+ public void tearDown() {
+ mDvrFeature.resetForTests();
+ }
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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);
+ }
+}