Add tests for SampleQueue isReady

PiperOrigin-RevId: 277691935
This commit is contained in:
aquilescanta 2019-10-31 10:58:24 +00:00 committed by Oliver Woodman
parent 2d530478ee
commit df251ad1be

View file

@ -22,6 +22,7 @@ import static com.google.android.exoplayer2.source.SampleQueue.ADVANCE_FAILED;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.lang.Long.MIN_VALUE; import static java.lang.Long.MIN_VALUE;
import static java.util.Arrays.copyOfRange; import static java.util.Arrays.copyOfRange;
import static org.mockito.Mockito.when;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
@ -141,8 +142,7 @@ public final class SampleQueueTest {
mockDrmSessionManager = mockDrmSessionManager =
(DrmSessionManager<ExoMediaCrypto>) Mockito.mock(DrmSessionManager.class); (DrmSessionManager<ExoMediaCrypto>) Mockito.mock(DrmSessionManager.class);
mockDrmSession = (DrmSession<ExoMediaCrypto>) Mockito.mock(DrmSession.class); mockDrmSession = (DrmSession<ExoMediaCrypto>) Mockito.mock(DrmSession.class);
Mockito.when( when(mockDrmSessionManager.acquireSession(ArgumentMatchers.any(), ArgumentMatchers.any()))
mockDrmSessionManager.acquireSession(ArgumentMatchers.any(), ArgumentMatchers.any()))
.thenReturn(mockDrmSession); .thenReturn(mockDrmSession);
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager); sampleQueue = new SampleQueue(allocator, mockDrmSessionManager);
formatHolder = new FormatHolder(); formatHolder = new FormatHolder();
@ -307,21 +307,53 @@ public final class SampleQueueTest {
assertReadTestData(); assertReadTestData();
} }
@Test
public void testEmptyQueueReturnsLoadingFinished() {
sampleQueue.sampleData(new ParsableByteArray(DATA), DATA.length);
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isFalse();
assertThat(sampleQueue.isReady(/* loadingFinished= */ true)).isTrue();
}
@Test
public void testIsReadyWithUpstreamFormatOnlyReturnsTrue() {
sampleQueue.format(FORMAT_ENCRYPTED);
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isTrue();
}
@Test
public void testIsReadyReturnsTrueForValidDrmSession() {
writeTestDataWithEncryptedSections();
assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED);
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isFalse();
when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isTrue();
}
@Test
public void testIsReadyReturnsTrueForClearSampleAndPlayClearSamplesWithoutKeysIsTrue() {
when(mockDrmSessionManager.getFlags())
.thenReturn(DrmSessionManager.FLAG_PLAY_CLEAR_SAMPLES_WITHOUT_KEYS);
// We recreate the queue to ensure the mock DRM session manager flags are taken into account.
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager);
writeTestDataWithEncryptedSections();
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isTrue();
}
@Test @Test
public void testReadEncryptedSectionsWaitsForKeys() { public void testReadEncryptedSectionsWaitsForKeys() {
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED);
writeEncryptedTestData(); writeTestDataWithEncryptedSections();
assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED); assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED);
assertReadNothing(/* formatRequired= */ false); assertReadNothing(/* formatRequired= */ false);
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
assertReadEncryptedSample(/* sampleIndex= */ 0); assertReadEncryptedSample(/* sampleIndex= */ 0);
} }
@Test @Test
public void testReadEncryptedSectionsPopulatesDrmSession() { public void testReadEncryptedSectionsPopulatesDrmSession() {
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
writeEncryptedTestData(); writeTestDataWithEncryptedSections();
int result = int result =
sampleQueue.read( sampleQueue.read(
@ -360,14 +392,13 @@ public final class SampleQueueTest {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testAllowPlaceholderSessionPopulatesDrmSession() { public void testAllowPlaceholderSessionPopulatesDrmSession() {
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
DrmSession<ExoMediaCrypto> mockPlaceholderDrmSession = DrmSession<ExoMediaCrypto> mockPlaceholderDrmSession =
(DrmSession<ExoMediaCrypto>) Mockito.mock(DrmSession.class); (DrmSession<ExoMediaCrypto>) Mockito.mock(DrmSession.class);
Mockito.when(mockPlaceholderDrmSession.getState()) when(mockPlaceholderDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
.thenReturn(DrmSession.STATE_OPENED_WITH_KEYS); when(mockDrmSessionManager.acquirePlaceholderSession(ArgumentMatchers.any()))
Mockito.when(mockDrmSessionManager.acquirePlaceholderSession(ArgumentMatchers.any()))
.thenReturn(mockPlaceholderDrmSession); .thenReturn(mockPlaceholderDrmSession);
writeEncryptedTestData(); writeTestDataWithEncryptedSections();
int result = int result =
sampleQueue.read( sampleQueue.read(
@ -405,15 +436,14 @@ public final class SampleQueueTest {
@Test @Test
public void testReadWithErrorSessionReadsNothingAndThrows() throws IOException { public void testReadWithErrorSessionReadsNothingAndThrows() throws IOException {
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED);
writeEncryptedTestData(); writeTestDataWithEncryptedSections();
assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED); assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED);
assertReadNothing(/* formatRequired= */ false); assertReadNothing(/* formatRequired= */ false);
sampleQueue.maybeThrowError(); sampleQueue.maybeThrowError();
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_ERROR); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_ERROR);
Mockito.when(mockDrmSession.getError()) when(mockDrmSession.getError()).thenReturn(new DrmSession.DrmSessionException(new Exception()));
.thenReturn(new DrmSession.DrmSessionException(new Exception()));
assertReadNothing(/* formatRequired= */ false); assertReadNothing(/* formatRequired= */ false);
try { try {
sampleQueue.maybeThrowError(); sampleQueue.maybeThrowError();
@ -421,18 +451,18 @@ public final class SampleQueueTest {
} catch (IOException e) { } catch (IOException e) {
// Expected. // Expected.
} }
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED_WITH_KEYS);
assertReadEncryptedSample(/* sampleIndex= */ 0); assertReadEncryptedSample(/* sampleIndex= */ 0);
} }
@Test @Test
public void testAllowPlayClearSamplesWithoutKeysReadsClearSamples() { public void testAllowPlayClearSamplesWithoutKeysReadsClearSamples() {
Mockito.when(mockDrmSessionManager.getFlags()) when(mockDrmSessionManager.getFlags())
.thenReturn(DrmSessionManager.FLAG_PLAY_CLEAR_SAMPLES_WITHOUT_KEYS); .thenReturn(DrmSessionManager.FLAG_PLAY_CLEAR_SAMPLES_WITHOUT_KEYS);
// We recreate the queue to ensure the mock DRM session manager flags are taken into account. // We recreate the queue to ensure the mock DRM session manager flags are taken into account.
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager); sampleQueue = new SampleQueue(allocator, mockDrmSessionManager);
Mockito.when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED); when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED);
writeEncryptedTestData(); writeTestDataWithEncryptedSections();
assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED); assertReadFormat(/* formatRequired= */ false, FORMAT_ENCRYPTED);
assertReadEncryptedSample(/* sampleIndex= */ 0); assertReadEncryptedSample(/* sampleIndex= */ 0);
@ -794,7 +824,7 @@ public final class SampleQueueTest {
DATA, SAMPLE_SIZES, SAMPLE_OFFSETS, SAMPLE_TIMESTAMPS, SAMPLE_FORMATS, SAMPLE_FLAGS); DATA, SAMPLE_SIZES, SAMPLE_OFFSETS, SAMPLE_TIMESTAMPS, SAMPLE_FORMATS, SAMPLE_FLAGS);
} }
private void writeEncryptedTestData() { private void writeTestDataWithEncryptedSections() {
writeTestData( writeTestData(
ENCRYPTED_SAMPLES_DATA, ENCRYPTED_SAMPLES_DATA,
ENCRYPTED_SAMPLES_SIZES, ENCRYPTED_SAMPLES_SIZES,