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.recorder; 18 19 import static org.mockito.Mockito.verify; 20 21 import android.content.Intent; 22 import android.os.Build; 23 import android.support.test.filters.SdkSuppress; 24 import android.support.test.filters.SmallTest; 25 import android.test.ServiceTestCase; 26 27 import com.android.tv.common.feature.CommonFeatures; 28 import com.android.tv.common.feature.TestableFeature; 29 30 import org.mockito.Mock; 31 import org.mockito.Mockito; 32 import org.mockito.MockitoAnnotations; 33 34 /** 35 * Tests for {@link DvrRecordingService}. 36 */ 37 @SmallTest 38 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.N) 39 public class DvrRecordingServiceTest 40 extends ServiceTestCase<DvrRecordingServiceTest.MockDvrRecordingService> { 41 private final TestableFeature mDvrFeature = CommonFeatures.DVR; 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 mDvrFeature.enableForTest(); 47 MockitoAnnotations.initMocks(this); 48 setupService(); 49 } 50 51 @Override 52 protected void tearDown() throws Exception { 53 mDvrFeature.resetForTests(); 54 super.tearDown(); 55 } 56 57 public DvrRecordingServiceTest() { 58 super(MockDvrRecordingService.class); 59 } 60 61 public void testStartService_null() throws Exception { 62 // Not recording 63 startService(null); 64 assertFalse(getService().mInForeground); 65 66 // Recording 67 getService().startRecording(); 68 startService(null); 69 assertTrue(getService().mInForeground); 70 assertTrue(getService().mIsRecording); 71 getService().reset(); 72 } 73 74 public void testStartService_noUpcomingRecording() throws Exception { 75 Intent intent = new Intent(getContext(), DvrRecordingServiceTest.class); 76 intent.putExtra(DvrRecordingService.EXTRA_START_FOR_RECORDING, false); 77 78 // Not recording 79 startService(intent); 80 assertTrue(getService().mInForeground); 81 assertFalse(getService().mForegroundForUpcomingRecording); 82 getService().stopForegroundIfNotRecordingInternal(); 83 assertFalse(getService().mInForeground); 84 85 // Recording, ended quickly 86 getService().startRecording(); 87 startService(intent); 88 assertTrue(getService().mInForeground); 89 assertTrue(getService().mForegroundForUpcomingRecording); 90 assertTrue(getService().mIsRecording); 91 getService().stopRecording(); 92 assertFalse(getService().mInForeground); 93 assertFalse(getService().mIsRecording); 94 getService().stopForegroundIfNotRecordingInternal(); 95 assertFalse(getService().mInForeground); 96 assertFalse(getService().mIsRecording); 97 getService().reset(); 98 99 // Recording, ended later 100 getService().startRecording(); 101 startService(intent); 102 assertTrue(getService().mInForeground); 103 assertTrue(getService().mForegroundForUpcomingRecording); 104 assertTrue(getService().mIsRecording); 105 getService().stopForegroundIfNotRecordingInternal(); 106 assertTrue(getService().mInForeground); 107 assertTrue(getService().mForegroundForUpcomingRecording); 108 assertTrue(getService().mIsRecording); 109 getService().stopRecording(); 110 assertFalse(getService().mInForeground); 111 assertFalse(getService().mIsRecording); 112 getService().reset(); 113 } 114 115 public void testStartService_hasUpcomingRecording() throws Exception { 116 Intent intent = new Intent(getContext(), DvrRecordingServiceTest.class); 117 intent.putExtra(DvrRecordingService.EXTRA_START_FOR_RECORDING, true); 118 119 // Not recording 120 startService(intent); 121 assertTrue(getService().mInForeground); 122 assertTrue(getService().mForegroundForUpcomingRecording); 123 assertFalse(getService().mIsRecording); 124 getService().startRecording(); 125 assertTrue(getService().mInForeground); 126 assertTrue(getService().mForegroundForUpcomingRecording); 127 assertTrue(getService().mIsRecording); 128 getService().stopRecording(); 129 assertFalse(getService().mInForeground); 130 assertFalse(getService().mIsRecording); 131 getService().reset(); 132 133 // Recording 134 getService().startRecording(); 135 startService(intent); 136 assertTrue(getService().mInForeground); 137 assertTrue(getService().mForegroundForUpcomingRecording); 138 assertTrue(getService().mIsRecording); 139 getService().startRecording(); 140 assertTrue(getService().mInForeground); 141 assertTrue(getService().mForegroundForUpcomingRecording); 142 assertTrue(getService().mIsRecording); 143 getService().stopRecording(); 144 assertTrue(getService().mInForeground); 145 assertTrue(getService().mForegroundForUpcomingRecording); 146 assertTrue(getService().mIsRecording); 147 getService().stopRecording(); 148 assertFalse(getService().mInForeground); 149 assertFalse(getService().mIsRecording); 150 getService().reset(); 151 } 152 153 public static class MockDvrRecordingService extends DvrRecordingService { 154 private int mRecordingCount = 0; 155 private boolean mInForeground; 156 private boolean mForegroundForUpcomingRecording; 157 158 @Override 159 protected void startForegroundInternal(boolean hasUpcomingRecording) { 160 mForegroundForUpcomingRecording = hasUpcomingRecording; 161 mInForeground = true; 162 } 163 164 @Override 165 protected void stopForegroundInternal() { 166 mInForeground = false; 167 } 168 169 private void startRecording() { 170 mOnRecordingSessionChangeListener.onRecordingSessionChange(true, ++mRecordingCount); 171 } 172 173 private void stopRecording() { 174 mOnRecordingSessionChangeListener.onRecordingSessionChange(false, --mRecordingCount); 175 } 176 177 private void reset() { 178 mRecordingCount = 0; 179 mInForeground = false; 180 mIsRecording = false; 181 } 182 } 183 }