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; 18 19 import static com.android.tv.TimeShiftManager.INVALID_TIME; 20 import static com.android.tv.TimeShiftManager.REQUEST_TIMEOUT_MS; 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNotSame; 23 24 import android.support.test.annotation.UiThreadTest; 25 import android.support.test.filters.MediumTest; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 30 @MediumTest 31 public class CurrentPositionMediatorTest extends BaseMainActivityTestCase { 32 private TimeShiftManager.CurrentPositionMediator mMediator; 33 34 @Override 35 @Before 36 public void setUp() { 37 super.setUp(); 38 mMediator = mActivity.getTimeShiftManager().mCurrentPositionMediator; 39 } 40 41 @UiThreadTest 42 @Test 43 public void testInitialize() { 44 long currentTimeMs = System.currentTimeMillis(); 45 mMediator.initialize(currentTimeMs); 46 assertCurrentPositionMediator(INVALID_TIME, currentTimeMs); 47 } 48 49 @UiThreadTest 50 @Test 51 public void testOnSeekRequested() { 52 long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3; 53 mMediator.onSeekRequested(seekToTimeMs); 54 assertNotSame("Seek request time", INVALID_TIME, mMediator.mSeekRequestTimeMs); 55 assertEquals("Current position", seekToTimeMs, mMediator.mCurrentPositionMs); 56 } 57 58 @UiThreadTest 59 @Test 60 public void testOnCurrentPositionChangedInvalidInput() { 61 long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3; 62 long newCurrentTimeMs = seekToTimeMs + REQUEST_TIMEOUT_MS; 63 mMediator.onSeekRequested(seekToTimeMs); 64 mMediator.onCurrentPositionChanged(newCurrentTimeMs); 65 assertNotSame("Seek request time", INVALID_TIME, mMediator.mSeekRequestTimeMs); 66 assertNotSame("Current position", seekToTimeMs, mMediator.mCurrentPositionMs); 67 assertNotSame("Current position", newCurrentTimeMs, mMediator.mCurrentPositionMs); 68 } 69 70 @UiThreadTest 71 @Test 72 public void testOnCurrentPositionChangedValidInput() { 73 long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3; 74 long newCurrentTimeMs = seekToTimeMs + REQUEST_TIMEOUT_MS - 1; 75 mMediator.onSeekRequested(seekToTimeMs); 76 mMediator.onCurrentPositionChanged(newCurrentTimeMs); 77 assertCurrentPositionMediator(INVALID_TIME, newCurrentTimeMs); 78 } 79 80 private void assertCurrentPositionMediator(long expectedSeekRequestTimeMs, 81 long expectedCurrentPositionMs) { 82 assertEquals("Seek request time", expectedSeekRequestTimeMs, mMediator.mSeekRequestTimeMs); 83 assertEquals("Current position", expectedCurrentPositionMs, mMediator.mCurrentPositionMs); 84 } 85 } 86