RecordingTaskTest.java revision 1abddd9f6225298066094e20a6c29061b6af4590
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 17package com.android.tv.dvr; 18 19import static org.mockito.Mockito.verify; 20import static org.mockito.Mockito.verifyNoMoreInteractions; 21import static org.mockito.Mockito.when; 22 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import com.android.tv.common.dvr.DvrSessionClient; 27import com.android.tv.data.Channel; 28import com.android.tv.testing.FakeClock; 29import com.android.tv.testing.dvr.RecordingTestUtils; 30 31import org.mockito.Mock; 32import org.mockito.MockitoAnnotations; 33 34/** 35 * Tests for {@link RecordingTask}. 36 */ 37@SmallTest 38public class RecordingTaskTest extends AndroidTestCase { 39 private FakeClock mFakeClock; 40 private DvrDataManagerInMemoryImpl mDataManager; 41 @Mock 42 DvrSessionManager mMockSessionManager; 43 @Mock 44 DvrSessionClient mMockDvrSessionClient; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 MockitoAnnotations.initMocks(this); 50 mFakeClock = FakeClock.createWithTimeOne(); 51 mDataManager = new DvrDataManagerInMemoryImpl(getContext()); 52 } 53 54 public void testRun_sleepUntil() { 55 long startTime = mFakeClock.currentTimeMillis(); 56 long endTime = startTime + 1; 57 Recording r = RecordingTestUtils.createTestRecordingWithPeriod(1, startTime, endTime); 58 RecordingTask task = new RecordingTask(r, mMockSessionManager, mDataManager, 59 mFakeClock); 60 61 Channel channel = r.getChannel(); 62 String inputId = channel.getInputId(); 63 when(mMockSessionManager.canAcquireDvrSession(inputId, channel)) 64 .thenReturn(true); 65 when(mMockSessionManager.acquireDvrSession(inputId, channel)) 66 .thenReturn(mMockDvrSessionClient); 67 task.run(); 68 assertEquals("Recording " + r + "finish time", endTime + RecordingTask.MS_AFTER_END, 69 mFakeClock.currentTimeMillis()); 70 } 71 72 public void testRun_connectAndRelease() { 73 long startTime = mFakeClock.currentTimeMillis(); 74 long endTime = startTime + 1; 75 Recording r = RecordingTestUtils.createTestRecordingWithPeriod(1, startTime, endTime); 76 RecordingTask task = new RecordingTask(r, mMockSessionManager, mDataManager, 77 mFakeClock); 78 79 Channel channel = r.getChannel(); 80 String inputId = channel.getInputId(); 81 when(mMockSessionManager.canAcquireDvrSession(inputId, channel)) 82 .thenReturn(true); 83 when(mMockSessionManager.acquireDvrSession(inputId, channel)) 84 .thenReturn(mMockDvrSessionClient); 85 task.run(); 86 87 verify(mMockSessionManager).canAcquireDvrSession(inputId, channel); 88 verify(mMockSessionManager).acquireDvrSession(inputId, channel); 89 verify(mMockDvrSessionClient).connect(inputId, task); 90 verify(mMockDvrSessionClient).startRecord(channel.getUri(), 91 RecordingTask.getIdAsMediaUri(r)); 92 verify(mMockDvrSessionClient).stopRecord(); 93 verify(mMockSessionManager).releaseDvrSession(mMockDvrSessionClient); 94 verifyNoMoreInteractions(mMockDvrSessionClient, mMockSessionManager); 95 } 96 97 98 public void testRun_cannotAcquireSession() { 99 long startTime = mFakeClock.currentTimeMillis(); 100 long endTime = startTime + 1; 101 Recording r = RecordingTestUtils.createTestRecordingWithPeriod(1, startTime, endTime); 102 mDataManager.addRecording(r); 103 RecordingTask task = new RecordingTask(r, mMockSessionManager, mDataManager, 104 mFakeClock); 105 106 when(mMockSessionManager.canAcquireDvrSession(r.getChannel().getInputId(), r.getChannel())) 107 .thenReturn(false); 108 task.run(); 109 Recording updatedRecording = mDataManager.getRecording(r.getId()); 110 assertEquals("status",Recording.STATE_RECORDING_FAILED, updatedRecording.getState() ); 111 } 112}