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.recorder;
18
19import static android.support.test.InstrumentationRegistry.getTargetContext;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22import static org.mockito.Matchers.any;
23import static org.mockito.Matchers.eq;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.verifyZeroInteractions;
26
27import android.app.AlarmManager;
28import android.app.PendingIntent;
29import android.os.Build;
30import android.os.Looper;
31import android.support.test.filters.SdkSuppress;
32import android.support.test.filters.SmallTest;
33
34import com.android.tv.InputSessionManager;
35import com.android.tv.common.feature.CommonFeatures;
36import com.android.tv.common.feature.TestableFeature;
37import com.android.tv.data.ChannelDataManager;
38import com.android.tv.dvr.DvrDataManagerInMemoryImpl;
39import com.android.tv.dvr.DvrManager;
40import com.android.tv.dvr.data.ScheduledRecording;
41import com.android.tv.testing.FakeClock;
42import com.android.tv.testing.dvr.RecordingTestUtils;
43import com.android.tv.util.TvInputManagerHelper;
44
45import org.junit.After;
46import org.junit.Before;
47import org.junit.Test;
48import org.mockito.Mock;
49import org.mockito.Mockito;
50import org.mockito.MockitoAnnotations;
51
52import java.util.concurrent.TimeUnit;
53
54/**
55 * Tests for {@link RecordingScheduler}.
56 */
57@SmallTest
58@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
59public class SchedulerTest {
60    private static final String INPUT_ID = "input_id";
61    private static final int CHANNEL_ID = 273;
62
63    private FakeClock mFakeClock;
64    private DvrDataManagerInMemoryImpl mDataManager;
65    private RecordingScheduler mScheduler;
66    @Mock DvrManager mDvrManager;
67    @Mock InputSessionManager mSessionManager;
68    @Mock AlarmManager mMockAlarmManager;
69    @Mock ChannelDataManager mChannelDataManager;
70    @Mock TvInputManagerHelper mInputManager;
71    private final TestableFeature mDvrFeature = CommonFeatures.DVR;
72
73    @Before
74    public void setUp() {
75        MockitoAnnotations.initMocks(this);
76        mDvrFeature.enableForTest();
77        mFakeClock = FakeClock.createWithCurrentTime();
78        mDataManager = new DvrDataManagerInMemoryImpl(getTargetContext(), mFakeClock);
79        Mockito.when(mChannelDataManager.isDbLoadFinished()).thenReturn(true);
80        mScheduler = new RecordingScheduler(Looper.myLooper(), mDvrManager, mSessionManager, mDataManager,
81                mChannelDataManager, mInputManager, getTargetContext(), mFakeClock,
82                mMockAlarmManager);
83    }
84
85    @After
86    public void tearDown() {
87        mDvrFeature.resetForTests();
88    }
89
90    @Test
91    public void testUpdate_none() {
92        mScheduler.updateAndStartServiceIfNeeded();
93        verifyZeroInteractions(mMockAlarmManager);
94    }
95
96    @Test
97    public void testUpdate_nextIn12Hours() {
98        long now = mFakeClock.currentTimeMillis();
99        long startTime = now + TimeUnit.HOURS.toMillis(12);
100        ScheduledRecording r = RecordingTestUtils
101                .createTestRecordingWithPeriod(INPUT_ID, CHANNEL_ID, startTime,
102                startTime + TimeUnit.HOURS.toMillis(1));
103        mDataManager.addScheduledRecording(r);
104        verify(mMockAlarmManager).setExactAndAllowWhileIdle(
105                eq(AlarmManager.RTC_WAKEUP),
106                eq(startTime - RecordingScheduler.MS_TO_WAKE_BEFORE_START),
107                any(PendingIntent.class));
108        Mockito.reset(mMockAlarmManager);
109        mScheduler.updateAndStartServiceIfNeeded();
110        verify(mMockAlarmManager).setExactAndAllowWhileIdle(
111                eq(AlarmManager.RTC_WAKEUP),
112                eq(startTime - RecordingScheduler.MS_TO_WAKE_BEFORE_START),
113                any(PendingIntent.class));
114    }
115
116    @Test
117    public void testStartsWithin() {
118        long now = mFakeClock.currentTimeMillis();
119        long startTime = now + 3;
120        ScheduledRecording r = RecordingTestUtils
121                .createTestRecordingWithPeriod(INPUT_ID, CHANNEL_ID, startTime, startTime + 100);
122        assertFalse(mScheduler.startsWithin(r, 2));
123        assertTrue(mScheduler.startsWithin(r, 3));
124    }
125}