react-native-audio-browser 0.2.0-next.74 → 0.2.0-next.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +19 -26
- package/android/src/main/java/com/audiobrowser/Service.kt +15 -30
- package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -8
- package/android/src/main/java/com/audiobrowser/browser/TrackLoadHandler.kt +5 -3
- package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +10 -2
- package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +21 -1
- package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +0 -3
- package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +3 -0
- package/android/src/main/java/com/audiobrowser/player/PlaybackStateMachine.kt +15 -1
- package/android/src/main/java/com/audiobrowser/player/Player.kt +43 -33
- package/android/src/main/java/com/audiobrowser/player/PlayerEngineFactory.kt +0 -2
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +38 -15
- package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +0 -7
- package/android/src/main/java/com/audiobrowser/util/AwaitCancellable.kt +45 -0
- package/android/src/test/java/com/audiobrowser/GateDecisionTest.kt +128 -0
- package/android/src/test/java/com/audiobrowser/ResolveFormattedErrorTest.kt +93 -0
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +35 -0
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerLayerResolutionTest.kt +75 -5
- package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +42 -0
- package/android/src/test/java/com/audiobrowser/browser/TrackLoadHandlerTest.kt +43 -0
- package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +62 -0
- package/android/src/test/java/com/audiobrowser/player/BrowseTilesTest.kt +31 -32
- package/android/src/test/java/com/audiobrowser/player/MediaFactoryTest.kt +1 -8
- package/android/src/test/java/com/audiobrowser/player/NowPlayingFormatterTest.kt +75 -0
- package/android/src/test/java/com/audiobrowser/player/NowPlayingUpdaterTest.kt +25 -0
- package/android/src/test/java/com/audiobrowser/player/PlayFromSearchTest.kt +108 -0
- package/android/src/test/java/com/audiobrowser/player/PlaybackStateMachineTest.kt +41 -3
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +126 -1
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupTest.kt +0 -1
- package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +105 -0
- package/android/src/test/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicyTest.kt +0 -7
- package/android/src/test/java/com/audiobrowser/util/AwaitCancellableTest.kt +136 -0
- package/android/src/test/java/com/audiobrowser/util/JsPromise.kt +44 -0
- package/ios/AVPlayerEngine.swift +2 -1
- package/ios/HybridAudioBrowser+Player.swift +14 -4
- package/ios/Player/Player.swift +15 -10
- package/ios/Player/PlayerEngine.swift +1 -1
- package/ios/Tests/PlayerPolicyTests.swift +71 -0
- package/ios/Tests/Support/TestTypes.swift +6 -1
- package/package.json +2 -1
|
@@ -29,7 +29,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
29
29
|
private val stuckRecoveryPolicy = StuckRecoveryPolicy()
|
|
30
30
|
|
|
31
31
|
// Main looper: Player.Listener callbacks arrive on the main thread.
|
|
32
|
-
private val
|
|
32
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
33
33
|
private val healthyPlaybackRunnable =
|
|
34
34
|
object : Runnable {
|
|
35
35
|
override fun run() {
|
|
@@ -39,12 +39,34 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
39
39
|
player.resetRetryTimer()
|
|
40
40
|
// Re-arm while playing: errors the buffer absorbs never flip isPlaying.
|
|
41
41
|
if (player.isPlaying) {
|
|
42
|
-
|
|
42
|
+
mainHandler.postDelayed(this, HEALTHY_PLAYBACK_MS)
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Set from an app-started track change until the playback thread has handled it. Until then every
|
|
49
|
+
* metadata delivery is the outgoing item's: its renderer posted them to the main looper before
|
|
50
|
+
* the change, and nothing removes them.
|
|
51
|
+
*/
|
|
52
|
+
private var metadataFence: Any? = null
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Its target runs on the playback thread right after the command that changed the item. A
|
|
56
|
+
* main-looper target goes to the back of that thread's queue, where a release would drop it.
|
|
57
|
+
*/
|
|
58
|
+
private fun fenceOutgoingMetadata() {
|
|
59
|
+
val fence = Any()
|
|
60
|
+
metadataFence = fence
|
|
61
|
+
player.exoPlayer
|
|
62
|
+
.createMessage { _, _ ->
|
|
63
|
+
mainHandler.post { if (metadataFence === fence) metadataFence = null }
|
|
64
|
+
}
|
|
65
|
+
.send()
|
|
66
|
+
}
|
|
67
|
+
|
|
47
68
|
override fun onMetadata(metadata: Metadata) {
|
|
69
|
+
if (metadataFence != null) return
|
|
48
70
|
val chapters = MetadataAdapter.extractChapters(metadata)
|
|
49
71
|
if (chapters.isNotEmpty()) {
|
|
50
72
|
player.callbacks?.onChapterMetadata(chapters)
|
|
@@ -81,15 +103,14 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
81
103
|
index = player.currentIndex?.toDouble(),
|
|
82
104
|
track = player.currentTrack,
|
|
83
105
|
)
|
|
106
|
+
val ended =
|
|
107
|
+
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_AUTO ||
|
|
108
|
+
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_REPEAT
|
|
84
109
|
player.nowPlaying.clearOverride()
|
|
110
|
+
if (!ended) fenceOutgoingMetadata()
|
|
85
111
|
player.callbacks?.onPlaybackActiveTrackChanged(event)
|
|
86
112
|
|
|
87
|
-
if (
|
|
88
|
-
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_AUTO ||
|
|
89
|
-
reason == MediaPlayer.MEDIA_ITEM_TRANSITION_REASON_REPEAT
|
|
90
|
-
) {
|
|
91
|
-
player.sleepTimer.onTrackEnd()
|
|
92
|
-
}
|
|
113
|
+
if (ended) player.sleepTimer.onTrackEnd()
|
|
93
114
|
|
|
94
115
|
player.lastTrack = player.currentTrack
|
|
95
116
|
player.lastIndex = player.currentIndex
|
|
@@ -107,7 +128,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
107
128
|
// Emits on its own: the incoming track may hold LOADING/BUFFERING and emit no state change.
|
|
108
129
|
player.clearRetryingError()
|
|
109
130
|
|
|
110
|
-
|
|
131
|
+
mainHandler.removeCallbacks(healthyPlaybackRunnable)
|
|
111
132
|
stuckRecoveryPolicy.reset()
|
|
112
133
|
|
|
113
134
|
player.playbackStateStore.save()
|
|
@@ -115,9 +136,9 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
115
136
|
}
|
|
116
137
|
|
|
117
138
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
|
118
|
-
|
|
139
|
+
mainHandler.removeCallbacks(healthyPlaybackRunnable)
|
|
119
140
|
if (isPlaying) {
|
|
120
|
-
|
|
141
|
+
mainHandler.postDelayed(healthyPlaybackRunnable, HEALTHY_PLAYBACK_MS)
|
|
121
142
|
}
|
|
122
143
|
}
|
|
123
144
|
|
|
@@ -128,9 +149,6 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
128
149
|
player.sleepTimer.clear()
|
|
129
150
|
}
|
|
130
151
|
|
|
131
|
-
// The second writer: Player.playWhenReady's setter writes first so loader-thread readers see
|
|
132
|
-
// the intent early; this syncs changes ExoPlayer makes itself (e.g. audio focus).
|
|
133
|
-
player.playWhenReadyCache = playWhenReady
|
|
134
152
|
// Here rather than in Player.pause(): a session pause, a noisy-audio pause and a permanent
|
|
135
153
|
// focus loss all drop the intent without it.
|
|
136
154
|
if (!playWhenReady) player.cancelRetry()
|
|
@@ -164,7 +182,12 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
164
182
|
player.shouldPlayWhenReady,
|
|
165
183
|
)
|
|
166
184
|
MediaPlayer.EVENT_MEDIA_ITEM_TRANSITION ->
|
|
167
|
-
PlaybackEvent.MediaItemTransition(
|
|
185
|
+
PlaybackEvent.MediaItemTransition(
|
|
186
|
+
player.currentTrack != null,
|
|
187
|
+
player.isPlaying,
|
|
188
|
+
loads = player.sessionPlayer.playbackState != MediaPlayer.STATE_IDLE,
|
|
189
|
+
player.shouldPlayWhenReady,
|
|
190
|
+
)
|
|
168
191
|
MediaPlayer.EVENT_PLAY_WHEN_READY_CHANGED,
|
|
169
192
|
MediaPlayer.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED ->
|
|
170
193
|
PlaybackEvent.ShouldPlayWhenReadyChanged(
|
|
@@ -29,7 +29,6 @@ import timber.log.Timber
|
|
|
29
29
|
* @param maxRetryDurationMs the full recovery budget; null for 2 minutes
|
|
30
30
|
* @param firstConnectMaxRetryDurationMs the online budget of a load that has never produced audio;
|
|
31
31
|
* null for 12 seconds
|
|
32
|
-
* @param shouldRetry false gives up at once (e.g. the player is paused)
|
|
33
32
|
* @param hasPlayed whether the current load has produced audio; called on ExoPlayer's loader thread
|
|
34
33
|
* @param onRetryPending called on ExoPlayer's loader thread each time a retry is scheduled, with
|
|
35
34
|
* the error being retried
|
|
@@ -38,7 +37,6 @@ class RetryLoadErrorHandlingPolicy(
|
|
|
38
37
|
private val maxRetries: Int? = null,
|
|
39
38
|
maxRetryDurationMs: Long? = null,
|
|
40
39
|
firstConnectMaxRetryDurationMs: Long? = null,
|
|
41
|
-
private val shouldRetry: () -> Boolean = { true },
|
|
42
40
|
private val isOnline: () -> Boolean = { true },
|
|
43
41
|
private val hasPlayed: () -> Boolean = { false },
|
|
44
42
|
private val onRetryPending: ((exception: IOException) -> Unit)? = null,
|
|
@@ -103,11 +101,6 @@ class RetryLoadErrorHandlingPolicy(
|
|
|
103
101
|
val errorCount = loadErrorInfo.errorCount
|
|
104
102
|
val currentTime = SystemClock.elapsedRealtime()
|
|
105
103
|
|
|
106
|
-
if (!shouldRetry()) {
|
|
107
|
-
Timber.d("Retry skipped: shouldRetry() returned false (player likely paused)")
|
|
108
|
-
return C.TIME_UNSET
|
|
109
|
-
}
|
|
110
|
-
|
|
111
104
|
if (maxRetries != null && errorCount > maxRetries) {
|
|
112
105
|
Timber.d("Max retries ($maxRetries) exceeded, not retrying")
|
|
113
106
|
return C.TIME_UNSET
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package com.audiobrowser.util
|
|
2
|
+
|
|
3
|
+
import com.margelo.nitro.core.Promise
|
|
4
|
+
import java.util.concurrent.TimeoutException
|
|
5
|
+
import kotlin.coroutines.resume
|
|
6
|
+
import kotlin.coroutines.resumeWithException
|
|
7
|
+
import kotlin.time.Duration.Companion.seconds
|
|
8
|
+
import kotlinx.coroutines.TimeoutCancellationException
|
|
9
|
+
import kotlinx.coroutines.currentCoroutineContext
|
|
10
|
+
import kotlinx.coroutines.ensureActive
|
|
11
|
+
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
12
|
+
import kotlinx.coroutines.withTimeout
|
|
13
|
+
|
|
14
|
+
/** How long a JS callback may take where its caller falls back on a failure. */
|
|
15
|
+
internal val FALLBACK_TIMEOUT = 10.seconds
|
|
16
|
+
|
|
17
|
+
/** Awaits this JS promise. Unlike Nitro's `await`, a cancelled caller resumes at once. */
|
|
18
|
+
internal suspend fun <T> Promise<T>.awaitCancellable(): T = settled()
|
|
19
|
+
|
|
20
|
+
/** [awaitCancellable] for a JS callback that returns a promise, behind Nitro's bridge promise. */
|
|
21
|
+
internal suspend fun <T> Promise<Promise<T>>.awaitNested(): T = settled().settled()
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* [awaitCancellable], failing with a [TimeoutException], which is not a cancellation, after
|
|
25
|
+
* [FALLBACK_TIMEOUT].
|
|
26
|
+
*/
|
|
27
|
+
internal suspend fun <T> Promise<T>.awaitOrTimeout(): T = withTimeoutAsFailure { settled() }
|
|
28
|
+
|
|
29
|
+
/** [awaitNested], failing as [awaitOrTimeout] does; the timeout covers both promises. */
|
|
30
|
+
internal suspend fun <T> Promise<Promise<T>>.awaitNestedOrTimeout(): T = withTimeoutAsFailure {
|
|
31
|
+
settled().settled()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private suspend fun <T> withTimeoutAsFailure(block: suspend () -> T): T =
|
|
35
|
+
try {
|
|
36
|
+
withTimeout(FALLBACK_TIMEOUT) { block() }
|
|
37
|
+
} catch (e: TimeoutCancellationException) {
|
|
38
|
+
currentCoroutineContext().ensureActive()
|
|
39
|
+
throw TimeoutException("JS callback outran $FALLBACK_TIMEOUT")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private suspend fun <T> Promise<T>.settled(): T = suspendCancellableCoroutine { c ->
|
|
43
|
+
then { c.resume(it) }
|
|
44
|
+
catch { c.resumeWithException(it) }
|
|
45
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.util.FALLBACK_TIMEOUT
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
5
|
+
import com.margelo.nitro.audiobrowser.Gate
|
|
6
|
+
import com.margelo.nitro.audiobrowser.GateDecision
|
|
7
|
+
import com.margelo.nitro.audiobrowser.GateReason
|
|
8
|
+
import com.margelo.nitro.audiobrowser.NativeGateRequest
|
|
9
|
+
import com.margelo.nitro.core.Promise
|
|
10
|
+
import kotlin.time.Duration.Companion.seconds
|
|
11
|
+
import kotlinx.coroutines.CancellationException
|
|
12
|
+
import kotlinx.coroutines.Job
|
|
13
|
+
import kotlinx.coroutines.async
|
|
14
|
+
import kotlinx.coroutines.launch
|
|
15
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
16
|
+
import kotlinx.coroutines.test.runCurrent
|
|
17
|
+
import kotlinx.coroutines.test.runTest
|
|
18
|
+
import org.junit.Assert.assertFalse
|
|
19
|
+
import org.junit.Assert.assertNull
|
|
20
|
+
import org.junit.Assert.assertTrue
|
|
21
|
+
import org.junit.Test
|
|
22
|
+
import org.mockito.Mockito.CALLS_REAL_METHODS
|
|
23
|
+
import org.mockito.Mockito.mock
|
|
24
|
+
|
|
25
|
+
class GateDecisionTest {
|
|
26
|
+
|
|
27
|
+
// AudioBrowser's constructor needs JNI, so the browser is a mock that runs its real methods,
|
|
28
|
+
// with an active gate written to the field setGate writes.
|
|
29
|
+
private fun gatedBrowser(
|
|
30
|
+
resolveGate: (NativeGateRequest) -> Promise<Promise<GateDecision>>
|
|
31
|
+
): AudioBrowser {
|
|
32
|
+
val browser = mock(AudioBrowser::class.java, CALLS_REAL_METHODS)
|
|
33
|
+
val gateState =
|
|
34
|
+
Class.forName("com.audiobrowser.AudioBrowser\$GateState")
|
|
35
|
+
.getDeclaredConstructor(Boolean::class.java, Gate::class.java, Boolean::class.java)
|
|
36
|
+
.apply { isAccessible = true }
|
|
37
|
+
.newInstance(true, Gate("Gated", null), true)
|
|
38
|
+
AudioBrowser::class
|
|
39
|
+
.java
|
|
40
|
+
.getDeclaredField("gateState")
|
|
41
|
+
.apply { isAccessible = true }
|
|
42
|
+
.set(browser, gateState)
|
|
43
|
+
browser.resolveGate = resolveGate
|
|
44
|
+
return browser
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private val request = NativeGateRequest(GateReason.SEARCH, null, null)
|
|
48
|
+
|
|
49
|
+
@Test
|
|
50
|
+
fun `a decision whose caller is cancelled while it waits returns nothing`() = runTest {
|
|
51
|
+
var outcome: AudioBrowser.GateOutcome? = null
|
|
52
|
+
lateinit var caller: Job
|
|
53
|
+
val browser = gatedBrowser {
|
|
54
|
+
caller.cancel()
|
|
55
|
+
// Cancelled while the resolver runs, which then fails.
|
|
56
|
+
throw RuntimeException("Rejected")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
caller = launch { outcome = browser.gateDecision(request) }
|
|
60
|
+
caller.join()
|
|
61
|
+
|
|
62
|
+
assertNull(outcome)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Test
|
|
66
|
+
fun `a resolver's own cancellation fails closed while its caller is active`() = runTest {
|
|
67
|
+
val browser = gatedBrowser { throw CancellationException("Timed out") }
|
|
68
|
+
|
|
69
|
+
assertTrue(browser.gateDecision(request).gated)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A gated browser whose resolver's JS promise the test settles. */
|
|
73
|
+
private fun pendingResolver(): Pair<AudioBrowser, JsPromise<GateDecision>> {
|
|
74
|
+
val decision = JsPromise<GateDecision>()
|
|
75
|
+
val bridge = JsPromise<Promise<GateDecision>>().apply { resolve(decision.promise) }
|
|
76
|
+
return gatedBrowser { bridge.promise } to decision
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Test
|
|
80
|
+
fun `a resolver answering inside the timeout decides`() =
|
|
81
|
+
runTest(timeout = 5.seconds) {
|
|
82
|
+
val (browser, decision) = pendingResolver()
|
|
83
|
+
val outcome = backgroundScope.async { browser.gateDecision(request) }
|
|
84
|
+
runCurrent()
|
|
85
|
+
|
|
86
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
87
|
+
decision.resolve(GateDecision(false, null))
|
|
88
|
+
runCurrent()
|
|
89
|
+
|
|
90
|
+
assertTrue(outcome.isCompleted)
|
|
91
|
+
assertFalse(outcome.await().gated)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Test
|
|
95
|
+
fun `a resolver that never answers fails closed at the timeout`() =
|
|
96
|
+
runTest(timeout = 5.seconds) {
|
|
97
|
+
val (browser, _) = pendingResolver()
|
|
98
|
+
val outcome = backgroundScope.async { browser.gateDecision(request) }
|
|
99
|
+
|
|
100
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
101
|
+
runCurrent()
|
|
102
|
+
assertFalse(outcome.isCompleted)
|
|
103
|
+
advanceTimeBy(2.seconds)
|
|
104
|
+
runCurrent()
|
|
105
|
+
|
|
106
|
+
assertTrue(outcome.isCompleted)
|
|
107
|
+
assertTrue(outcome.await().gated)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Test
|
|
111
|
+
fun `a caller cancelled while the resolver is silent stops waiting`() =
|
|
112
|
+
runTest(timeout = 5.seconds) {
|
|
113
|
+
val (browser, _) = pendingResolver()
|
|
114
|
+
var returned = false
|
|
115
|
+
val outcome =
|
|
116
|
+
backgroundScope.launch {
|
|
117
|
+
browser.gateDecision(request)
|
|
118
|
+
returned = true
|
|
119
|
+
}
|
|
120
|
+
runCurrent()
|
|
121
|
+
|
|
122
|
+
outcome.cancel()
|
|
123
|
+
runCurrent()
|
|
124
|
+
|
|
125
|
+
assertTrue(outcome.isCompleted)
|
|
126
|
+
assertFalse(returned)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.util.FALLBACK_TIMEOUT
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
5
|
+
import com.margelo.nitro.audiobrowser.FormattedNavigationError
|
|
6
|
+
import com.margelo.nitro.audiobrowser.Func_std__shared_ptr_Promise_std__optional_FormattedNavigationError____FormatNavigationErrorParams
|
|
7
|
+
import com.margelo.nitro.audiobrowser.NativeBrowserConfiguration
|
|
8
|
+
import com.margelo.nitro.audiobrowser.NavigationError
|
|
9
|
+
import com.margelo.nitro.audiobrowser.NavigationErrorType
|
|
10
|
+
import kotlin.time.Duration.Companion.seconds
|
|
11
|
+
import kotlinx.coroutines.async
|
|
12
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
13
|
+
import kotlinx.coroutines.test.runCurrent
|
|
14
|
+
import kotlinx.coroutines.test.runTest
|
|
15
|
+
import org.junit.Assert.assertEquals
|
|
16
|
+
import org.junit.Assert.assertFalse
|
|
17
|
+
import org.junit.Assert.assertTrue
|
|
18
|
+
import org.junit.Test
|
|
19
|
+
import org.mockito.Mockito.CALLS_REAL_METHODS
|
|
20
|
+
import org.mockito.Mockito.mock
|
|
21
|
+
|
|
22
|
+
class ResolveFormattedErrorTest {
|
|
23
|
+
|
|
24
|
+
// AudioBrowser's constructor needs JNI: a mock running its real methods, with the configuration
|
|
25
|
+
// written to the field configuration's setter writes.
|
|
26
|
+
private fun browserFormattingWith(js: JsPromise<FormattedNavigationError?>): AudioBrowser {
|
|
27
|
+
val browser = mock(AudioBrowser::class.java, CALLS_REAL_METHODS)
|
|
28
|
+
val configuration =
|
|
29
|
+
NativeBrowserConfiguration(
|
|
30
|
+
path = null,
|
|
31
|
+
request = null,
|
|
32
|
+
requestResolver = null,
|
|
33
|
+
browse = null,
|
|
34
|
+
browseResolver = null,
|
|
35
|
+
media = null,
|
|
36
|
+
artwork = null,
|
|
37
|
+
nowPlayingArtwork = null,
|
|
38
|
+
routes = null,
|
|
39
|
+
singleTrack = null,
|
|
40
|
+
handleTrackLoad = null,
|
|
41
|
+
androidControllerOfflineError = null,
|
|
42
|
+
viewMoreTitle = null,
|
|
43
|
+
carPlayLoadingTitle = null,
|
|
44
|
+
resolveAlbumPath = null,
|
|
45
|
+
formatNavigationError =
|
|
46
|
+
Func_std__shared_ptr_Promise_std__optional_FormattedNavigationError____FormatNavigationErrorParams {
|
|
47
|
+
js.promise
|
|
48
|
+
},
|
|
49
|
+
)
|
|
50
|
+
AudioBrowser::class
|
|
51
|
+
.java
|
|
52
|
+
.getDeclaredField("_configuration")
|
|
53
|
+
.apply { isAccessible = true }
|
|
54
|
+
.set(browser, configuration)
|
|
55
|
+
return browser
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private val error = NavigationError(NavigationErrorType.EMPTY_CONTENT, "", null, null)
|
|
59
|
+
private val default = defaultFormattedError(error)
|
|
60
|
+
|
|
61
|
+
@Test
|
|
62
|
+
fun `a formatter answering inside the timeout keeps the app's answer`() =
|
|
63
|
+
runTest(timeout = 5.seconds) {
|
|
64
|
+
val custom = FormattedNavigationError("No favorites yet", null)
|
|
65
|
+
val js = JsPromise<FormattedNavigationError?>()
|
|
66
|
+
val browser = browserFormattingWith(js)
|
|
67
|
+
val formatted = backgroundScope.async { browser.resolveFormattedError(error, "/favorites") }
|
|
68
|
+
runCurrent()
|
|
69
|
+
|
|
70
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
71
|
+
js.resolve(custom)
|
|
72
|
+
runCurrent()
|
|
73
|
+
|
|
74
|
+
assertTrue(formatted.isCompleted)
|
|
75
|
+
assertEquals(custom, formatted.await())
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
fun `a formatter that never answers falls back to the default at the timeout`() =
|
|
80
|
+
runTest(timeout = 5.seconds) {
|
|
81
|
+
val browser = browserFormattingWith(JsPromise())
|
|
82
|
+
val formatted = backgroundScope.async { browser.resolveFormattedError(error, "/favorites") }
|
|
83
|
+
|
|
84
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
85
|
+
runCurrent()
|
|
86
|
+
assertFalse(formatted.isCompleted)
|
|
87
|
+
advanceTimeBy(2.seconds)
|
|
88
|
+
runCurrent()
|
|
89
|
+
|
|
90
|
+
assertTrue(formatted.isCompleted)
|
|
91
|
+
assertEquals(default, formatted.await())
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
|
+
import android.app.SearchManager
|
|
3
4
|
import android.content.ComponentName
|
|
4
5
|
import android.content.ContextWrapper
|
|
5
6
|
import android.content.Intent
|
|
@@ -276,6 +277,35 @@ class ServiceTaskRemovedTest {
|
|
|
276
277
|
assertEquals(listOf("a"), player.tracks.map { it.title })
|
|
277
278
|
}
|
|
278
279
|
|
|
280
|
+
private fun removeTaskDuringASearchWaitingForAudioBrowser() {
|
|
281
|
+
controller
|
|
282
|
+
.withIntent(
|
|
283
|
+
Intent("android.media.action.MEDIA_PLAY_FROM_SEARCH").putExtra(SearchManager.QUERY, "a")
|
|
284
|
+
)
|
|
285
|
+
.startCommand(0, 4)
|
|
286
|
+
|
|
287
|
+
removeTask()
|
|
288
|
+
// Past AudioBrowser's registration timeout.
|
|
289
|
+
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(11))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
@Test
|
|
293
|
+
fun `a search waiting for AudioBrowser ends with the removed task`() {
|
|
294
|
+
removeTaskDuringASearchWaitingForAudioBrowser()
|
|
295
|
+
|
|
296
|
+
assertEquals(NEVER_STOPPED, shadowOf(service).stopSelfId)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
@Test
|
|
300
|
+
fun `a search waiting for AudioBrowser ends with the removed task while AudioBrowser is bound`() {
|
|
301
|
+
controller.bind()
|
|
302
|
+
|
|
303
|
+
removeTaskDuringASearchWaitingForAudioBrowser()
|
|
304
|
+
|
|
305
|
+
assertEquals(NEVER_STOPPED, shadowOf(service).stopSelfId)
|
|
306
|
+
assertFalse(player.exoPlayer.isReleased)
|
|
307
|
+
}
|
|
308
|
+
|
|
279
309
|
@Test
|
|
280
310
|
fun `a media browser binding after the task is removed gets no session`() {
|
|
281
311
|
playTrack("a")
|
|
@@ -284,4 +314,9 @@ class ServiceTaskRemovedTest {
|
|
|
284
314
|
|
|
285
315
|
assertNull(service.onBind(Intent("android.media.browse.MediaBrowserService")))
|
|
286
316
|
}
|
|
317
|
+
|
|
318
|
+
private companion object {
|
|
319
|
+
// ShadowService's stopSelfId before any stopSelf(startId); Android's start ids begin at 1.
|
|
320
|
+
const val NEVER_STOPPED = 0
|
|
321
|
+
}
|
|
287
322
|
}
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
package com.audiobrowser.browser
|
|
2
2
|
|
|
3
|
+
import com.audiobrowser.util.FALLBACK_TIMEOUT
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
3
5
|
import com.margelo.nitro.audiobrowser.TransformableRequestConfig
|
|
6
|
+
import com.margelo.nitro.core.Promise
|
|
7
|
+
import kotlin.time.Duration.Companion.seconds
|
|
8
|
+
import kotlinx.coroutines.async
|
|
9
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
10
|
+
import kotlinx.coroutines.test.runCurrent
|
|
4
11
|
import kotlinx.coroutines.test.runTest
|
|
5
12
|
import org.junit.Assert.assertEquals
|
|
13
|
+
import org.junit.Assert.assertFalse
|
|
6
14
|
import org.junit.Assert.assertNotNull
|
|
7
15
|
import org.junit.Assert.assertNull
|
|
8
16
|
import org.junit.Assert.assertSame
|
|
17
|
+
import org.junit.Assert.assertTrue
|
|
9
18
|
import org.junit.Before
|
|
10
19
|
import org.junit.Test
|
|
11
20
|
import org.junit.runner.RunWith
|
|
12
21
|
import org.robolectric.RobolectricTestRunner
|
|
13
22
|
|
|
14
|
-
/**
|
|
15
|
-
* Covers the generation guard through static layers only. A resolver thunk or viewMoreTitle
|
|
16
|
-
* callback returns a Nitro Promise, which needs the JNI bridge a JVM test lacks; the guard is the
|
|
17
|
-
* same for both paths.
|
|
18
|
-
*/
|
|
23
|
+
/** Covers the generation guard, through static layers, and the viewMoreTitle callback. */
|
|
19
24
|
// Robolectric provides the android.util.LruCache whose evictAll() clearContentCache calls.
|
|
20
25
|
@RunWith(RobolectricTestRunner::class)
|
|
21
26
|
class BrowserManagerLayerResolutionTest {
|
|
@@ -130,4 +135,69 @@ class BrowserManagerLayerResolutionTest {
|
|
|
130
135
|
|
|
131
136
|
assertEquals(BrowserManager.DEFAULT_VIEW_MORE_TITLE, browserManager.viewMoreTitle())
|
|
132
137
|
}
|
|
138
|
+
|
|
139
|
+
/** A viewMoreTitle callback answering from the next of [promises], counting its calls. */
|
|
140
|
+
private class TitleCallback(vararg promises: JsPromise<String>) {
|
|
141
|
+
private val queue = ArrayDeque(promises.toList())
|
|
142
|
+
var calls = 0
|
|
143
|
+
val callback: () -> Promise<String> = {
|
|
144
|
+
calls += 1
|
|
145
|
+
queue.removeFirst().promise
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@Test
|
|
150
|
+
fun `a viewMoreTitle answering inside the timeout is kept for the generation`() =
|
|
151
|
+
runTest(timeout = 5.seconds) {
|
|
152
|
+
val js = JsPromise<String>()
|
|
153
|
+
val title = TitleCallback(js)
|
|
154
|
+
browserManager.config = BrowserConfig(viewMoreTitle = title.callback)
|
|
155
|
+
val first = backgroundScope.async { browserManager.viewMoreTitle() }
|
|
156
|
+
runCurrent()
|
|
157
|
+
|
|
158
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
159
|
+
js.resolve("More")
|
|
160
|
+
runCurrent()
|
|
161
|
+
|
|
162
|
+
assertTrue(first.isCompleted)
|
|
163
|
+
assertEquals("More", first.await())
|
|
164
|
+
assertEquals("More", browserManager.viewMoreTitle())
|
|
165
|
+
assertEquals(1, title.calls)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@Test
|
|
169
|
+
fun `a viewMoreTitle that times out falls back for the generation`() =
|
|
170
|
+
runTest(timeout = 5.seconds) {
|
|
171
|
+
val title = TitleCallback(JsPromise())
|
|
172
|
+
browserManager.config = BrowserConfig(viewMoreTitle = title.callback)
|
|
173
|
+
val first = backgroundScope.async { browserManager.viewMoreTitle() }
|
|
174
|
+
|
|
175
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
176
|
+
runCurrent()
|
|
177
|
+
assertFalse(first.isCompleted)
|
|
178
|
+
advanceTimeBy(2.seconds)
|
|
179
|
+
runCurrent()
|
|
180
|
+
|
|
181
|
+
assertTrue(first.isCompleted)
|
|
182
|
+
assertEquals(BrowserManager.DEFAULT_VIEW_MORE_TITLE, first.await())
|
|
183
|
+
assertEquals(BrowserManager.DEFAULT_VIEW_MORE_TITLE, browserManager.viewMoreTitle())
|
|
184
|
+
assertEquals(1, title.calls)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
@Test
|
|
188
|
+
fun `a viewMoreTitle whose caller is cancelled caches nothing`() =
|
|
189
|
+
runTest(timeout = 5.seconds) {
|
|
190
|
+
val silent = JsPromise<String>()
|
|
191
|
+
val next = JsPromise<String>().apply { resolve("More") }
|
|
192
|
+
val title = TitleCallback(silent, next)
|
|
193
|
+
browserManager.config = BrowserConfig(viewMoreTitle = title.callback)
|
|
194
|
+
val first = backgroundScope.async { browserManager.viewMoreTitle() }
|
|
195
|
+
runCurrent()
|
|
196
|
+
|
|
197
|
+
first.cancel()
|
|
198
|
+
runCurrent()
|
|
199
|
+
|
|
200
|
+
assertTrue(first.isCompleted)
|
|
201
|
+
assertEquals("More", browserManager.viewMoreTitle())
|
|
202
|
+
}
|
|
133
203
|
}
|
|
@@ -8,9 +8,18 @@ import com.audiobrowser.TestFixtures.sectionStyle
|
|
|
8
8
|
import com.audiobrowser.TestFixtures.staticRoute
|
|
9
9
|
import com.audiobrowser.TestFixtures.track
|
|
10
10
|
import com.audiobrowser.util.BrowserPathHelper
|
|
11
|
+
import com.audiobrowser.util.JsPromise
|
|
12
|
+
import com.margelo.nitro.audiobrowser.Func_std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError__std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError_______BrowserSourceCallbackParam
|
|
13
|
+
import com.margelo.nitro.audiobrowser.Variant_ResolvedTrack_BrowseError_Promise_BrowseResult_
|
|
14
|
+
import kotlin.time.Duration.Companion.seconds
|
|
15
|
+
import kotlinx.coroutines.launch
|
|
11
16
|
import kotlinx.coroutines.runBlocking
|
|
17
|
+
import kotlinx.coroutines.test.runCurrent
|
|
18
|
+
import kotlinx.coroutines.test.runTest
|
|
12
19
|
import org.junit.Assert.assertEquals
|
|
20
|
+
import org.junit.Assert.assertFalse
|
|
13
21
|
import org.junit.Assert.assertNull
|
|
22
|
+
import org.junit.Assert.assertTrue
|
|
14
23
|
import org.junit.Assert.fail
|
|
15
24
|
import org.junit.Before
|
|
16
25
|
import org.junit.Test
|
|
@@ -116,6 +125,39 @@ class BrowserManagerTest {
|
|
|
116
125
|
|
|
117
126
|
// MARK: - expandQueueFromContextualPath
|
|
118
127
|
|
|
128
|
+
@Test
|
|
129
|
+
fun `expandQueueFromContextualPath hands a cancelled caller the cancellation, not a fallback`() =
|
|
130
|
+
runTest(timeout = 5.seconds) {
|
|
131
|
+
val silent = JsPromise<Variant_ResolvedTrack_BrowseError_Promise_BrowseResult_>()
|
|
132
|
+
browserManager.config =
|
|
133
|
+
BrowserConfig(
|
|
134
|
+
routes =
|
|
135
|
+
arrayOf(
|
|
136
|
+
staticRoute("/home", resolvedTrack(path = "/home"))
|
|
137
|
+
.copy(
|
|
138
|
+
browseStatic = null,
|
|
139
|
+
browseCallback =
|
|
140
|
+
Func_std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError__std__shared_ptr_Promise_std__variant_ResolvedTrack__BrowseError_______BrowserSourceCallbackParam {
|
|
141
|
+
silent.promise
|
|
142
|
+
},
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
var returned = false
|
|
147
|
+
val expanded =
|
|
148
|
+
backgroundScope.launch {
|
|
149
|
+
browserManager.expandQueueFromContextualPath(contextual("/home", "c"))
|
|
150
|
+
returned = true
|
|
151
|
+
}
|
|
152
|
+
runCurrent()
|
|
153
|
+
|
|
154
|
+
expanded.cancel()
|
|
155
|
+
runCurrent()
|
|
156
|
+
|
|
157
|
+
assertTrue(expanded.isCompleted)
|
|
158
|
+
assertFalse(returned)
|
|
159
|
+
}
|
|
160
|
+
|
|
119
161
|
@Test
|
|
120
162
|
fun `expandQueueFromContextualPath scopes the queue to the tapped section`() = runBlocking {
|
|
121
163
|
serveSections(
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
package com.audiobrowser.browser
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.TestFixtures.track
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
5
|
+
import com.margelo.nitro.audiobrowser.TrackLoadEvent
|
|
6
|
+
import com.margelo.nitro.core.Promise
|
|
7
|
+
import kotlin.time.Duration.Companion.seconds
|
|
8
|
+
import kotlinx.coroutines.async
|
|
9
|
+
import kotlinx.coroutines.test.runCurrent
|
|
10
|
+
import kotlinx.coroutines.test.runTest
|
|
11
|
+
import org.junit.Assert.assertFalse
|
|
12
|
+
import org.junit.Assert.assertTrue
|
|
13
|
+
import org.junit.Test
|
|
14
|
+
|
|
15
|
+
class TrackLoadHandlerTest {
|
|
16
|
+
|
|
17
|
+
@Test
|
|
18
|
+
fun `a load cancelled while the handler waits on JS runs neither branch`() =
|
|
19
|
+
runTest(timeout = 5.seconds) {
|
|
20
|
+
val bridge = JsPromise<Promise<Unit>>().apply { resolve(JsPromise<Unit>().promise) }
|
|
21
|
+
val handler: (TrackLoadEvent) -> Promise<Promise<Unit>> = { bridge.promise }
|
|
22
|
+
var ran = false
|
|
23
|
+
val station = track("Station")
|
|
24
|
+
val load =
|
|
25
|
+
backgroundScope.async {
|
|
26
|
+
handleTrackLoad(
|
|
27
|
+
handler,
|
|
28
|
+
station,
|
|
29
|
+
arrayOf(station),
|
|
30
|
+
0.0,
|
|
31
|
+
intercepted = { ran = true },
|
|
32
|
+
defaultBehavior = { ran = true },
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
runCurrent()
|
|
36
|
+
|
|
37
|
+
load.cancel()
|
|
38
|
+
runCurrent()
|
|
39
|
+
|
|
40
|
+
assertTrue(load.isCompleted)
|
|
41
|
+
assertFalse(ran)
|
|
42
|
+
}
|
|
43
|
+
}
|