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 17 package com.android.tv.dvr; 18 19 20 import static org.mockito.Matchers.any; 21 import static org.mockito.Matchers.eq; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.verifyZeroInteractions; 24 25 import android.app.AlarmManager; 26 import android.app.PendingIntent; 27 import android.os.Build; 28 import android.os.Looper; 29 import android.support.test.filters.SdkSuppress; 30 import android.test.AndroidTestCase; 31 import android.test.suitebuilder.annotation.SmallTest; 32 33 import com.android.tv.data.ChannelDataManager; 34 import com.android.tv.testing.FakeClock; 35 import com.android.tv.testing.dvr.RecordingTestUtils; 36 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Tests for {@link Scheduler}. 44 */ 45 @SmallTest 46 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) 47 public class SchedulerTest extends AndroidTestCase { 48 private static final int CHANNEL_ID = 273; 49 50 private FakeClock mFakeClock; 51 private DvrDataManagerInMemoryImpl mDataManager; 52 private Scheduler mScheduler; 53 @Mock DvrManager mDvrManager; 54 @Mock DvrSessionManager mSessionManager; 55 @Mock AlarmManager mMockAlarmManager; 56 @Mock 57 ChannelDataManager mChannelDataManager; 58 59 @Override setUp()60 protected void setUp() throws Exception { 61 super.setUp(); 62 MockitoAnnotations.initMocks(this); 63 mFakeClock = FakeClock.createWithCurrentTime(); 64 mDataManager = new DvrDataManagerInMemoryImpl(getContext(), mFakeClock); 65 mScheduler = new Scheduler(Looper.myLooper(), mDvrManager, mSessionManager, mDataManager, 66 mChannelDataManager, getContext(), mFakeClock, mMockAlarmManager); 67 } 68 testUpdate_none()69 public void testUpdate_none() throws Exception { 70 mScheduler.update(); 71 verifyZeroInteractions(mMockAlarmManager); 72 } 73 testUpdate_nextIn12Hours()74 public void testUpdate_nextIn12Hours() throws Exception { 75 long now = mFakeClock.currentTimeMillis(); 76 long startTime = now + TimeUnit.HOURS.toMillis(12); 77 ScheduledRecording r = RecordingTestUtils 78 .createTestRecordingWithPeriod(CHANNEL_ID, startTime, 79 startTime + TimeUnit.HOURS.toMillis(1)); 80 mDataManager.addScheduledRecording(r); 81 mScheduler.update(); 82 verify(mMockAlarmManager).set( 83 eq(AlarmManager.RTC_WAKEUP), 84 eq(startTime - Scheduler.MS_TO_WAKE_BEFORE_START), 85 any(PendingIntent.class)); 86 } 87 testStartsWithin()88 public void testStartsWithin() throws Exception { 89 long now = mFakeClock.currentTimeMillis(); 90 long startTime = now + 3; 91 ScheduledRecording r = RecordingTestUtils 92 .createTestRecordingWithPeriod(273, startTime, startTime + 100); 93 assertFalse(mScheduler.startsWithin(r, 2)); 94 assertTrue(mScheduler.startsWithin(r, 3)); 95 } 96 }