aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/dvr/recorder/InputTaskSchedulerTest.java
blob: e5c27e2c251cc216e69a27a1d90a1ff13c6e4868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.tv.dvr.recorder;

import static android.support.test.InstrumentationRegistry.getContext;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.AlarmManager;
import android.media.tv.TvInputInfo;
import android.os.Build;
import android.os.Looper;
import android.os.SystemClock;
import android.support.test.filters.SdkSuppress;
import android.support.test.filters.SmallTest;

import com.android.tv.InputSessionManager;
import com.android.tv.data.Channel;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.dvr.DvrManager;
import com.android.tv.dvr.WritableDvrDataManager;
import com.android.tv.dvr.data.ScheduledRecording;
import com.android.tv.dvr.recorder.InputTaskScheduler.RecordingTaskFactory;
import com.android.tv.testing.FakeClock;
import com.android.tv.testing.dvr.RecordingTestUtils;
import com.android.tv.util.Clock;
import com.android.tv.util.TestUtils;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Tests for {@link InputTaskScheduler}.
 */
@SmallTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
public class InputTaskSchedulerTest {
    private static final String INPUT_ID = "input_id";
    private static final int CHANNEL_ID = 1;
    private static final long LISTENER_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(1);
    private static final int TUNER_COUNT_ONE = 1;
    private static final int TUNER_COUNT_TWO = 2;
    private static final long LOW_PRIORITY = 1;
    private static final long HIGH_PRIORITY = 2;

    private FakeClock mFakeClock;
    private InputTaskScheduler mScheduler;
    @Mock private DvrManager mDvrManager;
    @Mock private WritableDvrDataManager mDataManager;
    @Mock private InputSessionManager mSessionManager;
    @Mock private AlarmManager mMockAlarmManager;
    @Mock private ChannelDataManager mChannelDataManager;
    private List<RecordingTask> mRecordingTasks;

    @Before
    public void setUp() throws Exception {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        mRecordingTasks = new ArrayList();
        MockitoAnnotations.initMocks(this);
        mFakeClock = FakeClock.createWithCurrentTime();
        TvInputInfo input = createTvInputInfo(TUNER_COUNT_ONE);
        mScheduler = new InputTaskScheduler(getContext(), input, Looper.myLooper(),
                mChannelDataManager, mDvrManager, mDataManager, mSessionManager, mFakeClock,
                new RecordingTaskFactory() {
                    @Override
                    public RecordingTask createRecordingTask(ScheduledRecording scheduledRecording,
                            Channel channel, DvrManager dvrManager,
                            InputSessionManager sessionManager, WritableDvrDataManager dataManager,
                            Clock clock) {
                        RecordingTask task = mock(RecordingTask.class);
                        when(task.getPriority()).thenReturn(scheduledRecording.getPriority());
                        when(task.getEndTimeMs()).thenReturn(scheduledRecording.getEndTimeMs());
                        mRecordingTasks.add(task);
                        return task;
                    }
                });
    }

    @Test
    public void testAddSchedule_past() {
        ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
                CHANNEL_ID, 0L, 1L);
        when(mDataManager.getScheduledRecording(anyLong())).thenReturn(r);
        mScheduler.handleAddSchedule(r);
        mScheduler.handleBuildSchedule();
        verify(mDataManager, timeout((int) LISTENER_TIMEOUT_MS).times(1))
                .changeState(any(ScheduledRecording.class),
                        eq(ScheduledRecording.STATE_RECORDING_FAILED));
    }

    @Test
    public void testAddSchedule_start() {
        mScheduler.handleAddSchedule(RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
                CHANNEL_ID, mFakeClock.currentTimeMillis(),
                mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)));
        mScheduler.handleBuildSchedule();
        verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
    }

    @Test
    public void testAddSchedule_consecutiveNoStop() {
        long startTimeMs = mFakeClock.currentTimeMillis();
        long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        long id = 0;
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        LOW_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        startTimeMs = endTimeMs;
        endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        HIGH_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
        // The first schedule should not be stopped because the second one should wait for the end
        // of the first schedule.
        SystemClock.sleep(LISTENER_TIMEOUT_MS);
        verify(mRecordingTasks.get(0), never()).stop();
    }

    @Test
    public void testAddSchedule_consecutiveNoFail() {
        long startTimeMs = mFakeClock.currentTimeMillis();
        long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        long id = 0;
        when(mDataManager.getScheduledRecording(anyLong())).thenReturn(ScheduledRecording
                .builder(INPUT_ID, CHANNEL_ID, 0L, 0L).build());
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        HIGH_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        startTimeMs = endTimeMs;
        endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        LOW_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
        SystemClock.sleep(LISTENER_TIMEOUT_MS);
        verify(mRecordingTasks.get(0), never()).stop();
        // The second schedule should not fail because it can starts after the first one finishes.
        SystemClock.sleep(LISTENER_TIMEOUT_MS);
        verify(mDataManager, never())
                .changeState(any(ScheduledRecording.class),
                        eq(ScheduledRecording.STATE_RECORDING_FAILED));
    }

    @Test
    public void testAddSchedule_consecutiveUseLessSession() throws Exception {
        TvInputInfo input = createTvInputInfo(TUNER_COUNT_TWO);
        mScheduler.updateTvInputInfo(input);
        long startTimeMs = mFakeClock.currentTimeMillis();
        long endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        long id = 0;
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        LOW_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        startTimeMs = endTimeMs;
        endTimeMs = startTimeMs + TimeUnit.SECONDS.toMillis(1);
        mScheduler.handleAddSchedule(
                RecordingTestUtils.createTestRecordingWithIdAndPriorityAndPeriod(++id, CHANNEL_ID,
                        HIGH_PRIORITY, startTimeMs, endTimeMs));
        mScheduler.handleBuildSchedule();
        verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).start();
        SystemClock.sleep(LISTENER_TIMEOUT_MS);
        verify(mRecordingTasks.get(0), never()).stop();
        // The second schedule should wait until the first one finishes rather than creating a new
        // session even though there are available tuners.
        assertTrue(mRecordingTasks.size() == 1);
    }

    @Test
    public void testUpdateSchedule_noCancel() {
        ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
                CHANNEL_ID, mFakeClock.currentTimeMillis(),
                mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1));
        mScheduler.handleAddSchedule(r);
        mScheduler.handleBuildSchedule();
        mScheduler.handleUpdateSchedule(r);
        SystemClock.sleep(LISTENER_TIMEOUT_MS);
        verify(mRecordingTasks.get(0), never()).cancel();
    }

    @Test
    public void testUpdateSchedule_cancel() {
        ScheduledRecording r = RecordingTestUtils.createTestRecordingWithPeriod(INPUT_ID,
                CHANNEL_ID, mFakeClock.currentTimeMillis(),
                mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(2));
        mScheduler.handleAddSchedule(r);
        mScheduler.handleBuildSchedule();
        mScheduler.handleUpdateSchedule(ScheduledRecording.buildFrom(r)
                .setStartTimeMs(mFakeClock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1))
                .build());
        verify(mRecordingTasks.get(0), timeout((int) LISTENER_TIMEOUT_MS).times(1)).cancel();
    }

    private TvInputInfo createTvInputInfo(int tunerCount) throws Exception {
        return TestUtils.createTvInputInfo(null, null, null, 0, false, true, tunerCount);
    }
}