mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Remove the multi-threading from DrmSessionManagerTest
I don't need to keep a separate playback looper, I can just use ShadowLooper.idleMainLooper(). PiperOrigin-RevId: 318823190
This commit is contained in:
parent
314bc65d62
commit
311d21bf8d
1 changed files with 25 additions and 76 deletions
|
|
@ -17,25 +17,17 @@ package com.google.android.exoplayer2.drm;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Looper;
|
||||||
import android.os.HandlerThread;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import com.google.android.exoplayer2.source.MediaSource;
|
|
||||||
import com.google.android.exoplayer2.testutil.FakeExoMediaDrm;
|
import com.google.android.exoplayer2.testutil.FakeExoMediaDrm;
|
||||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||||
import com.google.android.exoplayer2.util.ConditionVariable;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import com.google.android.exoplayer2.util.Function;
|
|
||||||
import com.google.android.exoplayer2.util.MediaSourceEventDispatcher;
|
|
||||||
import com.google.android.exoplayer2.util.MimeTypes;
|
import com.google.android.exoplayer2.util.MimeTypes;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.shadows.ShadowLooper;
|
||||||
|
|
||||||
/** Tests for {@link DefaultDrmSessionManager} and {@link DefaultDrmSession}. */
|
/** Tests for {@link DefaultDrmSessionManager} and {@link DefaultDrmSession}. */
|
||||||
// TODO: Test more branches:
|
// TODO: Test more branches:
|
||||||
|
|
@ -46,8 +38,6 @@ import org.junit.runner.RunWith;
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class DefaultDrmSessionManagerTest {
|
public class DefaultDrmSessionManagerTest {
|
||||||
|
|
||||||
private static final int TIMEOUT_MS = 1_000;
|
|
||||||
|
|
||||||
private static final UUID DRM_SCHEME_UUID =
|
private static final UUID DRM_SCHEME_UUID =
|
||||||
UUID.nameUUIDFromBytes(TestUtil.createByteArray(7, 8, 9));
|
UUID.nameUUIDFromBytes(TestUtil.createByteArray(7, 8, 9));
|
||||||
private static final ImmutableList<DrmInitData.SchemeData> DRM_SCHEME_DATAS =
|
private static final ImmutableList<DrmInitData.SchemeData> DRM_SCHEME_DATAS =
|
||||||
|
|
@ -56,76 +46,35 @@ public class DefaultDrmSessionManagerTest {
|
||||||
DRM_SCHEME_UUID, MimeTypes.VIDEO_MP4, /* data= */ TestUtil.createByteArray(1, 2, 3)));
|
DRM_SCHEME_UUID, MimeTypes.VIDEO_MP4, /* data= */ TestUtil.createByteArray(1, 2, 3)));
|
||||||
private static final DrmInitData DRM_INIT_DATA = new DrmInitData(DRM_SCHEME_DATAS);
|
private static final DrmInitData DRM_INIT_DATA = new DrmInitData(DRM_SCHEME_DATAS);
|
||||||
|
|
||||||
private HandlerThread playbackThread;
|
@Test(timeout = 10_000)
|
||||||
private Handler playbackThreadHandler;
|
|
||||||
private MediaSourceEventDispatcher eventDispatcher;
|
|
||||||
private ConditionVariable keysLoaded;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
playbackThread = new HandlerThread("Test playback thread");
|
|
||||||
playbackThread.start();
|
|
||||||
playbackThreadHandler = new Handler(playbackThread.getLooper());
|
|
||||||
eventDispatcher = new MediaSourceEventDispatcher();
|
|
||||||
keysLoaded = TestUtil.createRobolectricConditionVariable();
|
|
||||||
eventDispatcher.addEventListener(
|
|
||||||
playbackThreadHandler,
|
|
||||||
new DrmSessionEventListener() {
|
|
||||||
@Override
|
|
||||||
public void onDrmKeysLoaded(
|
|
||||||
int windowIndex, @Nullable MediaSource.MediaPeriodId mediaPeriodId) {
|
|
||||||
keysLoaded.open();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
DrmSessionEventListener.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
playbackThread.quitSafely();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void acquireSessionTriggersKeyLoadAndSessionIsOpened() throws Exception {
|
public void acquireSessionTriggersKeyLoadAndSessionIsOpened() throws Exception {
|
||||||
FakeExoMediaDrm.LicenseServer licenseServer =
|
FakeExoMediaDrm.LicenseServer licenseServer =
|
||||||
FakeExoMediaDrm.LicenseServer.allowingSchemeDatas(DRM_SCHEME_DATAS);
|
FakeExoMediaDrm.LicenseServer.allowingSchemeDatas(DRM_SCHEME_DATAS);
|
||||||
|
|
||||||
keysLoaded.close();
|
DefaultDrmSessionManager drmSessionManager =
|
||||||
AtomicReference<DrmSession> drmSession = new AtomicReference<>();
|
new DefaultDrmSessionManager.Builder()
|
||||||
playbackThreadHandler.post(
|
.setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm())
|
||||||
() -> {
|
.build(/* mediaDrmCallback= */ licenseServer);
|
||||||
DefaultDrmSessionManager drmSessionManager =
|
drmSessionManager.prepare();
|
||||||
new DefaultDrmSessionManager.Builder()
|
DrmSession drmSession =
|
||||||
.setUuidAndExoMediaDrmProvider(DRM_SCHEME_UUID, uuid -> new FakeExoMediaDrm())
|
drmSessionManager.acquireSession(
|
||||||
.build(/* mediaDrmCallback= */ licenseServer);
|
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
|
||||||
|
/* eventDispatcher= */ null,
|
||||||
|
DRM_INIT_DATA);
|
||||||
|
waitForOpenedWithKeys(drmSession);
|
||||||
|
|
||||||
drmSessionManager.prepare();
|
assertThat(drmSession.getState()).isEqualTo(DrmSession.STATE_OPENED_WITH_KEYS);
|
||||||
drmSession.set(
|
assertThat(drmSession.queryKeyStatus())
|
||||||
drmSessionManager.acquireSession(
|
|
||||||
playbackThread.getLooper(), eventDispatcher, DRM_INIT_DATA));
|
|
||||||
});
|
|
||||||
|
|
||||||
keysLoaded.block(TIMEOUT_MS);
|
|
||||||
|
|
||||||
@DrmSession.State int state = post(drmSession.get(), DrmSession::getState);
|
|
||||||
assertThat(state).isEqualTo(DrmSession.STATE_OPENED_WITH_KEYS);
|
|
||||||
Map<String, String> keyStatus = post(drmSession.get(), DrmSession::queryKeyStatus);
|
|
||||||
assertThat(keyStatus)
|
|
||||||
.containsExactly(FakeExoMediaDrm.KEY_STATUS_KEY, FakeExoMediaDrm.KEY_STATUS_AVAILABLE);
|
.containsExactly(FakeExoMediaDrm.KEY_STATUS_KEY, FakeExoMediaDrm.KEY_STATUS_AVAILABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Call a function on {@code drmSession} on the playback thread and return the result. */
|
private static void waitForOpenedWithKeys(DrmSession drmSession) {
|
||||||
private <T> T post(DrmSession drmSession, Function<DrmSession, T> fn)
|
// Check the error first, so we get a meaningful failure if there's been an error.
|
||||||
throws InterruptedException {
|
assertThat(drmSession.getError()).isNull();
|
||||||
AtomicReference<T> result = new AtomicReference<>();
|
assertThat(drmSession.getState()).isEqualTo(DrmSession.STATE_OPENED);
|
||||||
ConditionVariable resultReady = TestUtil.createRobolectricConditionVariable();
|
while (drmSession.getState() != DrmSession.STATE_OPENED_WITH_KEYS) {
|
||||||
resultReady.close();
|
// Allow the key response to be handled.
|
||||||
playbackThreadHandler.post(
|
ShadowLooper.idleMainLooper();
|
||||||
() -> {
|
}
|
||||||
result.set(fn.apply(drmSession));
|
|
||||||
resultReady.open();
|
|
||||||
});
|
|
||||||
resultReady.block(TIMEOUT_MS);
|
|
||||||
return result.get();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue