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 com.google.common.truth.Truth.assertWithMessage;
22 
23 import androidx.test.annotation.UiThreadTest;
24 import androidx.test.filters.MediumTest;
25 import androidx.test.runner.AndroidJUnit4;
26 import com.android.tv.testing.activities.BaseMainActivityTestCase;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 @MediumTest
32 @RunWith(AndroidJUnit4.class)
33 public class CurrentPositionMediatorTest extends BaseMainActivityTestCase {
34     private TimeShiftManager.CurrentPositionMediator mMediator;
35 
36     @Override
37     @Before
setUp()38     public void setUp() {
39         super.setUp();
40         mMediator = mActivity.getTimeShiftManager().mCurrentPositionMediator;
41     }
42 
43     @UiThreadTest
44     @Test
testInitialize()45     public void testInitialize() {
46         long currentTimeMs = System.currentTimeMillis();
47         mMediator.initialize(currentTimeMs);
48         assertCurrentPositionMediator(INVALID_TIME, currentTimeMs);
49     }
50 
51     @UiThreadTest
52     @Test
testOnSeekRequested()53     public void testOnSeekRequested() {
54         long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3;
55         mMediator.onSeekRequested(seekToTimeMs);
56     assertWithMessage("Seek request time")
57         .that(mMediator.mSeekRequestTimeMs)
58         .isNotSameInstanceAs(INVALID_TIME);
59         assertWithMessage("Current position")
60                 .that(mMediator.mCurrentPositionMs)
61                 .isEqualTo(seekToTimeMs);
62     }
63 
64     @UiThreadTest
65     @Test
testOnCurrentPositionChangedInvalidInput()66     public void testOnCurrentPositionChangedInvalidInput() {
67         long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3;
68         long newCurrentTimeMs = seekToTimeMs + REQUEST_TIMEOUT_MS;
69         mMediator.onSeekRequested(seekToTimeMs);
70         mMediator.onCurrentPositionChanged(newCurrentTimeMs);
71     assertWithMessage("Seek request time")
72         .that(mMediator.mSeekRequestTimeMs)
73         .isNotSameInstanceAs(INVALID_TIME);
74     assertWithMessage("Current position")
75         .that(mMediator.mCurrentPositionMs)
76         .isNotSameInstanceAs(seekToTimeMs);
77     assertWithMessage("Current position")
78         .that(mMediator.mCurrentPositionMs)
79         .isNotSameInstanceAs(newCurrentTimeMs);
80     }
81 
82     @UiThreadTest
83     @Test
testOnCurrentPositionChangedValidInput()84     public void testOnCurrentPositionChangedValidInput() {
85         long seekToTimeMs = System.currentTimeMillis() - REQUEST_TIMEOUT_MS * 3;
86         long newCurrentTimeMs = seekToTimeMs + REQUEST_TIMEOUT_MS - 1;
87         mMediator.onSeekRequested(seekToTimeMs);
88         mMediator.onCurrentPositionChanged(newCurrentTimeMs);
89         assertCurrentPositionMediator(INVALID_TIME, newCurrentTimeMs);
90     }
91 
assertCurrentPositionMediator( long expectedSeekRequestTimeMs, long expectedCurrentPositionMs)92     private void assertCurrentPositionMediator(
93             long expectedSeekRequestTimeMs, long expectedCurrentPositionMs) {
94         assertWithMessage("Seek request time")
95                 .that(mMediator.mSeekRequestTimeMs)
96                 .isEqualTo(expectedSeekRequestTimeMs);
97         assertWithMessage("Current position")
98                 .that(mMediator.mCurrentPositionMs)
99                 .isEqualTo(expectedCurrentPositionMs);
100     }
101 }
102