-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*** Original commit *** Remove set timeout on release() and setSurface() Removes the experimental methods to set a timeout when releasing the player and setting the surface. *** PiperOrigin-RevId: 312647457
- Loading branch information
1 parent
634634f
commit 1154e80
Showing
5 changed files
with
286 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
library/core/src/test/java/com/google/android/exoplayer2/PlayerMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (C) 2019 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.android.exoplayer2; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
import android.os.Handler; | ||
import android.os.HandlerThread; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.google.android.exoplayer2.util.Clock; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
|
||
/** Unit test for {@link PlayerMessage}. */ | ||
@RunWith(AndroidJUnit4.class) | ||
public class PlayerMessageTest { | ||
|
||
private static final long TIMEOUT_MS = 10; | ||
|
||
@Mock Clock clock; | ||
private HandlerThread handlerThread; | ||
private PlayerMessage message; | ||
|
||
@Before | ||
public void setUp() { | ||
initMocks(this); | ||
PlayerMessage.Sender sender = (message) -> {}; | ||
PlayerMessage.Target target = (messageType, payload) -> {}; | ||
handlerThread = new HandlerThread("TestHandler"); | ||
handlerThread.start(); | ||
Handler handler = new Handler(handlerThread.getLooper()); | ||
message = | ||
new PlayerMessage(sender, target, Timeline.EMPTY, /* defaultWindowIndex= */ 0, handler); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
handlerThread.quit(); | ||
} | ||
|
||
@Test | ||
public void experimental_blockUntilDelivered_timesOut() throws Exception { | ||
when(clock.elapsedRealtime()).thenReturn(0L).thenReturn(TIMEOUT_MS * 2); | ||
|
||
try { | ||
message.send().experimental_blockUntilDelivered(TIMEOUT_MS, clock); | ||
fail(); | ||
} catch (TimeoutException expected) { | ||
} | ||
|
||
// Ensure experimental_blockUntilDelivered() entered the blocking loop | ||
verify(clock, Mockito.times(2)).elapsedRealtime(); | ||
} | ||
|
||
@Test | ||
public void experimental_blockUntilDelivered_onAlreadyProcessed_succeeds() throws Exception { | ||
when(clock.elapsedRealtime()).thenReturn(0L); | ||
|
||
message.send().markAsProcessed(/* isDelivered= */ true); | ||
|
||
assertThat(message.experimental_blockUntilDelivered(TIMEOUT_MS, clock)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void experimental_blockUntilDelivered_markAsProcessedWhileBlocked_succeeds() | ||
throws Exception { | ||
message.send(); | ||
|
||
// Use a separate Thread to mark the message as processed. | ||
CountDownLatch prepareLatch = new CountDownLatch(1); | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
Future<Boolean> future = | ||
executorService.submit( | ||
() -> { | ||
prepareLatch.await(); | ||
message.markAsProcessed(true); | ||
return true; | ||
}); | ||
|
||
when(clock.elapsedRealtime()) | ||
.thenReturn(0L) | ||
.then( | ||
(invocation) -> { | ||
// Signal the background thread to call PlayerMessage#markAsProcessed. | ||
prepareLatch.countDown(); | ||
return TIMEOUT_MS - 1; | ||
}); | ||
|
||
try { | ||
assertThat(message.experimental_blockUntilDelivered(TIMEOUT_MS, clock)).isTrue(); | ||
// Ensure experimental_blockUntilDelivered() entered the blocking loop. | ||
verify(clock, Mockito.atLeast(2)).elapsedRealtime(); | ||
future.get(1, TimeUnit.SECONDS); | ||
} finally { | ||
executorService.shutdown(); | ||
} | ||
} | ||
} |