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.os.Build; 20 import android.support.annotation.NonNull; 21 22 import com.android.tv.common.feature.CommonFeatures; 23 import com.android.tv.common.feature.TestableFeature; 24 import com.android.tv.dvr.data.ScheduledRecording; 25 import com.android.tv.testing.TestSingletonApp; 26 import com.android.tv.testing.dvr.DvrDataManagerInMemoryImpl; 27 import com.android.tv.testing.dvr.RecordingTestUtils; 28 import com.android.tv.testing.fakes.FakeClock; 29 30 import com.google.common.truth.Truth; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 import java.util.List; 41 import java.util.concurrent.TimeUnit; 42 43 /** Tests for {@link BaseDvrDataManager} using {@link DvrDataManagerInMemoryImpl}. */ 44 @RunWith(RobolectricTestRunner.class) 45 @Config(sdk = Build.VERSION_CODES.N, application = TestSingletonApp.class) 46 public class BaseDvrDataManagerTest { 47 private static final String INPUT_ID = "input_id"; 48 private static final int CHANNEL_ID = 273; 49 50 private final TestableFeature mDvrFeature = CommonFeatures.DVR; 51 private DvrDataManagerInMemoryImpl mDvrDataManager; 52 private FakeClock mFakeClock; 53 54 @Before setUp()55 public void setUp() { 56 mDvrFeature.enableForTest(); 57 mFakeClock = FakeClock.createWithCurrentTime(); 58 mDvrDataManager = 59 new DvrDataManagerInMemoryImpl(RuntimeEnvironment.application, mFakeClock); 60 } 61 62 @After tearDown()63 public void tearDown() { 64 mDvrFeature.resetForTests(); 65 } 66 67 @Test testGetNonStartedScheduledRecordings()68 public void testGetNonStartedScheduledRecordings() { 69 ScheduledRecording recording = 70 mDvrDataManager.addScheduledRecordingInternal( 71 createNewScheduledRecordingStartingNow()); 72 List<ScheduledRecording> result = mDvrDataManager.getNonStartedScheduledRecordings(); 73 Truth.assertThat(result).containsExactly(recording); 74 } 75 76 @Test testGetNonStartedScheduledRecordings_past()77 public void testGetNonStartedScheduledRecordings_past() { 78 mDvrDataManager.addScheduledRecordingInternal(createNewScheduledRecordingStartingNow()); 79 mFakeClock.increment(TimeUnit.MINUTES, 6); 80 List<ScheduledRecording> result = mDvrDataManager.getNonStartedScheduledRecordings(); 81 Truth.assertThat(result).isEmpty(); 82 } 83 84 @NonNull createNewScheduledRecordingStartingNow()85 private ScheduledRecording createNewScheduledRecordingStartingNow() { 86 return ScheduledRecording.buildFrom( 87 RecordingTestUtils.createTestRecordingWithIdAndPeriod( 88 ScheduledRecording.ID_NOT_SET, 89 INPUT_ID, 90 CHANNEL_ID, 91 mFakeClock.currentTimeMillis(), 92 mFakeClock.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5))) 93 .setState(ScheduledRecording.STATE_RECORDING_NOT_STARTED) 94 .build(); 95 } 96 } 97