forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is required in the previewer to stop a coroutine from blocking Issue 14693: Video AutoPlay
- Loading branch information
1 parent
ac58b92
commit ed49388
Showing
8 changed files
with
164 additions
and
5 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
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
122 changes: 122 additions & 0 deletions
122
AnkiDroid/src/test/java/com/ichi2/anki/cardviewer/VideoPlayerTest.kt
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,122 @@ | ||
/* | ||
* Copyright (c) 2024 David Allison <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.cardviewer | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.ichi2.anki.RobolectricTest | ||
import com.ichi2.libanki.SoundOrVideoTag | ||
import kotlinx.coroutines.CancellableContinuation | ||
import kotlinx.coroutines.CompletionHandler | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class VideoPlayerTest : RobolectricTest() { | ||
|
||
@Test | ||
fun `stops audio playback when paused`() { | ||
val v = VideoPlayer { JavascriptEvaluator { } } | ||
|
||
val m = MockContinuation() | ||
v.playVideo(m, SoundOrVideoTag("a.mp4")) | ||
|
||
assertNull(m.result) | ||
v.onVideoPaused() | ||
|
||
val result = assertNotNull(m.result) | ||
assertThat("failure", result.isFailure) | ||
val exception = result.exceptionOrNull() as? SoundException | ||
assertThat("Audio is stopped", exception != null && exception.continuationBehavior == SoundErrorBehavior.STOP_AUDIO) | ||
} | ||
|
||
// TODO: use a mock - couldn't get mockk working here | ||
class MockContinuation : CancellableContinuation<Unit> { | ||
var result: Result<Unit>? = null | ||
|
||
override val context: CoroutineContext | ||
get() = TODO("Not yet implemented") | ||
override val isActive: Boolean | ||
get() = TODO("Not yet implemented") | ||
override val isCancelled: Boolean | ||
get() = TODO("Not yet implemented") | ||
override val isCompleted: Boolean | ||
get() = TODO("Not yet implemented") | ||
|
||
override fun cancel(cause: Throwable?): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun completeResume(token: Any) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun initCancellability() { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun invokeOnCancellation(handler: CompletionHandler) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun tryResumeWithException(exception: Throwable): Any? { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun CoroutineDispatcher.resumeUndispatchedWithException(exception: Throwable) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun CoroutineDispatcher.resumeUndispatched(value: Unit) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun tryResume( | ||
value: Unit, | ||
idempotent: Any?, | ||
onCancellation: ((cause: Throwable) -> Unit)? | ||
): Any? { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun tryResume(value: Unit, idempotent: Any?): Any? { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun resume(value: Unit, onCancellation: ((cause: Throwable) -> Unit)?) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun resumeWith(result: Result<Unit>) { | ||
this.result = result | ||
} | ||
} | ||
} |