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