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
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
package com.audiobrowser.http
|
|
2
2
|
|
|
3
|
+
import com.audiobrowser.TestFixtures
|
|
3
4
|
import com.audiobrowser.TestFixtures.mediaConfig
|
|
4
5
|
import com.audiobrowser.TestFixtures.transformableConfig
|
|
6
|
+
import com.audiobrowser.util.JsPromise
|
|
7
|
+
import com.margelo.nitro.audiobrowser.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_RequestConfig_____RequestConfig_std__optional_std__unordered_map_std__string__std__string__
|
|
5
8
|
import com.margelo.nitro.audiobrowser.HttpMethod
|
|
6
9
|
import com.margelo.nitro.audiobrowser.RequestConfig
|
|
10
|
+
import com.margelo.nitro.core.Promise
|
|
11
|
+
import kotlin.time.Duration.Companion.seconds
|
|
12
|
+
import kotlinx.coroutines.launch
|
|
13
|
+
import kotlinx.coroutines.test.runCurrent
|
|
7
14
|
import kotlinx.coroutines.test.runTest
|
|
8
15
|
import org.junit.Assert.*
|
|
9
16
|
import org.junit.Test
|
|
@@ -301,4 +308,59 @@ class RequestConfigBuilderTest {
|
|
|
301
308
|
val merged = RequestConfigBuilder.mergeConfig(base, media)
|
|
302
309
|
assertEquals("/stream/123", merged.path)
|
|
303
310
|
}
|
|
311
|
+
|
|
312
|
+
@Test
|
|
313
|
+
fun `a transform cancelled while JS is silent hands back no config`() =
|
|
314
|
+
runTest(timeout = 5.seconds) {
|
|
315
|
+
val bridge =
|
|
316
|
+
JsPromise<Promise<RequestConfig>>().apply { resolve(JsPromise<RequestConfig>().promise) }
|
|
317
|
+
val override =
|
|
318
|
+
transformableConfig()
|
|
319
|
+
.copy(
|
|
320
|
+
transform =
|
|
321
|
+
Func_std__shared_ptr_Promise_std__shared_ptr_Promise_RequestConfig_____RequestConfig_std__optional_std__unordered_map_std__string__std__string__ {
|
|
322
|
+
_,
|
|
323
|
+
_ ->
|
|
324
|
+
bridge.promise
|
|
325
|
+
}
|
|
326
|
+
)
|
|
327
|
+
var returned = false
|
|
328
|
+
val caller =
|
|
329
|
+
backgroundScope.launch {
|
|
330
|
+
RequestConfigBuilder.mergeConfig(
|
|
331
|
+
RequestConfigBuilder.toRequestConfig(transformableConfig()),
|
|
332
|
+
override,
|
|
333
|
+
)
|
|
334
|
+
returned = true
|
|
335
|
+
}
|
|
336
|
+
runCurrent()
|
|
337
|
+
|
|
338
|
+
caller.cancel()
|
|
339
|
+
runCurrent()
|
|
340
|
+
|
|
341
|
+
assertTrue(caller.isCompleted)
|
|
342
|
+
assertFalse(returned)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
@Test
|
|
346
|
+
fun `a media resolve cancelled while JS is silent hands back no config`() =
|
|
347
|
+
runTest(timeout = 5.seconds) {
|
|
348
|
+
val silent = JsPromise<RequestConfig>()
|
|
349
|
+
var returned = false
|
|
350
|
+
val caller =
|
|
351
|
+
backgroundScope.launch {
|
|
352
|
+
RequestConfigBuilder.applyMediaResolve(
|
|
353
|
+
mediaConfig(resolveSync = { silent.promise }),
|
|
354
|
+
TestFixtures.track(),
|
|
355
|
+
)
|
|
356
|
+
returned = true
|
|
357
|
+
}
|
|
358
|
+
runCurrent()
|
|
359
|
+
|
|
360
|
+
caller.cancel()
|
|
361
|
+
runCurrent()
|
|
362
|
+
|
|
363
|
+
assertTrue(caller.isCompleted)
|
|
364
|
+
assertFalse(returned)
|
|
365
|
+
}
|
|
304
366
|
}
|
|
@@ -14,18 +14,19 @@ import com.audiobrowser.formattedOrDefault
|
|
|
14
14
|
import com.audiobrowser.navigationErrorFor
|
|
15
15
|
import com.audiobrowser.util.Automotive
|
|
16
16
|
import com.audiobrowser.util.BrowserPathHelper
|
|
17
|
+
import com.audiobrowser.util.JsPromise
|
|
18
|
+
import com.audiobrowser.util.awaitOrTimeout
|
|
17
19
|
import com.margelo.nitro.audiobrowser.FormattedNavigationError
|
|
18
20
|
import com.margelo.nitro.audiobrowser.NavigationError
|
|
19
21
|
import com.margelo.nitro.audiobrowser.NavigationErrorType
|
|
20
|
-
import kotlin.
|
|
21
|
-
import
|
|
22
|
-
import
|
|
22
|
+
import kotlin.time.Duration.Companion.seconds
|
|
23
|
+
import kotlinx.coroutines.launch
|
|
24
|
+
import kotlinx.coroutines.test.runCurrent
|
|
23
25
|
import kotlinx.coroutines.test.runTest
|
|
24
26
|
import org.junit.After
|
|
25
27
|
import org.junit.Assert.assertEquals
|
|
26
28
|
import org.junit.Assert.assertFalse
|
|
27
29
|
import org.junit.Assert.assertNotEquals
|
|
28
|
-
import org.junit.Assert.assertNotNull
|
|
29
30
|
import org.junit.Assert.assertNull
|
|
30
31
|
import org.junit.Assert.assertSame
|
|
31
32
|
import org.junit.Assert.assertTrue
|
|
@@ -215,23 +216,41 @@ class BrowseTilesTest {
|
|
|
215
216
|
}
|
|
216
217
|
|
|
217
218
|
@Test
|
|
218
|
-
fun `the formatter
|
|
219
|
+
fun `the formatter keeps the app's answer`() = runTest {
|
|
219
220
|
val custom = FormattedNavigationError("No favorites yet", "Tap the heart.")
|
|
220
221
|
assertEquals(
|
|
221
222
|
custom,
|
|
222
|
-
formattedOrDefault(
|
|
223
|
+
formattedOrDefault(FormattedNavigationError("Nothing here", null)) { custom },
|
|
223
224
|
)
|
|
224
225
|
}
|
|
225
226
|
|
|
226
227
|
@Test
|
|
227
|
-
fun `
|
|
228
|
+
fun `a formatter's cancelled caller gets the cancellation, not the default`() =
|
|
229
|
+
runTest(timeout = 5.seconds) {
|
|
230
|
+
val js = JsPromise<FormattedNavigationError?>()
|
|
231
|
+
var returned = false
|
|
232
|
+
val result =
|
|
233
|
+
backgroundScope.launch {
|
|
234
|
+
formattedOrDefault(FormattedNavigationError("Nothing here", null)) {
|
|
235
|
+
js.promise.awaitOrTimeout()
|
|
236
|
+
}
|
|
237
|
+
returned = true
|
|
238
|
+
}
|
|
239
|
+
runCurrent()
|
|
240
|
+
|
|
241
|
+
result.cancel()
|
|
242
|
+
runCurrent()
|
|
243
|
+
|
|
244
|
+
assertTrue(result.isCompleted)
|
|
245
|
+
assertFalse(returned)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
@Test
|
|
249
|
+
fun `the formatter falls back to the default on null and on a throw`() = runTest {
|
|
228
250
|
val default = FormattedNavigationError("Nothing here", null)
|
|
229
251
|
|
|
230
|
-
assertEquals(default, formattedOrDefault(
|
|
231
|
-
assertEquals(
|
|
232
|
-
default,
|
|
233
|
-
formattedOrDefault(backgroundScope, default) { throw IllegalStateException("boom") },
|
|
234
|
-
)
|
|
252
|
+
assertEquals(default, formattedOrDefault(default) { null })
|
|
253
|
+
assertEquals(default, formattedOrDefault(default) { throw IllegalStateException("boom") })
|
|
235
254
|
}
|
|
236
255
|
|
|
237
256
|
@Test
|
|
@@ -437,24 +456,4 @@ class BrowseTilesTest {
|
|
|
437
456
|
assertNotEquals(BrowserPathHelper.EMPTY_PATH, BrowserPathHelper.ERROR_PATH)
|
|
438
457
|
assertTrue(BrowserPathHelper.isDeadEndPath(BrowserPathHelper.ERROR_PATH))
|
|
439
458
|
}
|
|
440
|
-
|
|
441
|
-
@Test
|
|
442
|
-
fun `a formatter parked in a non-cancellable wait falls back without hanging`() = runTest {
|
|
443
|
-
val default = FormattedNavigationError("Nothing here", null)
|
|
444
|
-
var parked: Continuation<FormattedNavigationError?>? = null
|
|
445
|
-
|
|
446
|
-
try {
|
|
447
|
-
// Nitro's Promise.await parks like this, in suspendCoroutine, which cancellation never
|
|
448
|
-
// unparks; the hop must be abandoned, not waited on.
|
|
449
|
-
val formatted =
|
|
450
|
-
formattedOrDefault(backgroundScope, default) {
|
|
451
|
-
suspendCoroutine { continuation -> parked = continuation }
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
assertNotNull(parked)
|
|
455
|
-
assertEquals(default, formatted)
|
|
456
|
-
} finally {
|
|
457
|
-
parked?.resume(null)
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
459
|
}
|
|
@@ -28,14 +28,7 @@ class MediaFactoryTest {
|
|
|
28
28
|
|
|
29
29
|
private fun mediaFactory(
|
|
30
30
|
getRequestConfig: (String, Track) -> MediaRequestConfig? = { _, _ -> null }
|
|
31
|
-
) =
|
|
32
|
-
MediaFactory(
|
|
33
|
-
context,
|
|
34
|
-
cache = null,
|
|
35
|
-
RetryPolicy.Default,
|
|
36
|
-
shouldRetry = { false },
|
|
37
|
-
getRequestConfig = getRequestConfig,
|
|
38
|
-
)
|
|
31
|
+
) = MediaFactory(context, cache = null, RetryPolicy.Default, getRequestConfig = getRequestConfig)
|
|
39
32
|
|
|
40
33
|
/** Starts loading [track]'s media from position 0, runs [block], then releases the source. */
|
|
41
34
|
private fun loading(factory: MediaFactory, track: Track, block: () -> Unit) {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.TestFixtures
|
|
4
|
+
import com.audiobrowser.model.PlayerSetupOptions
|
|
5
|
+
import com.audiobrowser.util.FALLBACK_TIMEOUT
|
|
6
|
+
import com.audiobrowser.util.JsPromise
|
|
7
|
+
import com.margelo.nitro.audiobrowser.FormatNowPlayingParams
|
|
8
|
+
import com.margelo.nitro.audiobrowser.Func_std__shared_ptr_Promise_std__optional_NowPlayingUpdate____FormatNowPlayingParams
|
|
9
|
+
import com.margelo.nitro.audiobrowser.NowPlayingUpdate
|
|
10
|
+
import java.util.concurrent.TimeoutException
|
|
11
|
+
import kotlin.time.Duration.Companion.seconds
|
|
12
|
+
import kotlinx.coroutines.async
|
|
13
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
14
|
+
import kotlinx.coroutines.test.runCurrent
|
|
15
|
+
import kotlinx.coroutines.test.runTest
|
|
16
|
+
import org.junit.After
|
|
17
|
+
import org.junit.Assert.assertEquals
|
|
18
|
+
import org.junit.Assert.assertFalse
|
|
19
|
+
import org.junit.Assert.assertTrue
|
|
20
|
+
import org.junit.Test
|
|
21
|
+
import org.junit.runner.RunWith
|
|
22
|
+
import org.robolectric.RobolectricTestRunner
|
|
23
|
+
import org.robolectric.RuntimeEnvironment
|
|
24
|
+
|
|
25
|
+
/** The now-playing formatter as setup hands it to the updater, unwrapped from the Nitro Func. */
|
|
26
|
+
@RunWith(RobolectricTestRunner::class)
|
|
27
|
+
class NowPlayingFormatterTest {
|
|
28
|
+
private val js = JsPromise<NowPlayingUpdate?>()
|
|
29
|
+
private val player =
|
|
30
|
+
Player(RuntimeEnvironment.getApplication()).apply {
|
|
31
|
+
setup(
|
|
32
|
+
PlayerSetupOptions(
|
|
33
|
+
nowPlayingMetadataFormatter =
|
|
34
|
+
Func_std__shared_ptr_Promise_std__optional_NowPlayingUpdate____FormatNowPlayingParams {
|
|
35
|
+
js.promise
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@After fun release() = player.exoPlayer.release()
|
|
42
|
+
|
|
43
|
+
private val params = FormatNowPlayingParams(TestFixtures.track("Station"), null, true, null, null)
|
|
44
|
+
|
|
45
|
+
@Test
|
|
46
|
+
fun `a formatter answering inside the timeout lands its answer`() =
|
|
47
|
+
runTest(timeout = 5.seconds) {
|
|
48
|
+
val answer = NowPlayingUpdate(title = null, artist = "Song - Artist", album = null)
|
|
49
|
+
val formatted = backgroundScope.async { player.nowPlaying.formatter!!(params) }
|
|
50
|
+
runCurrent()
|
|
51
|
+
|
|
52
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
53
|
+
js.resolve(answer)
|
|
54
|
+
runCurrent()
|
|
55
|
+
|
|
56
|
+
assertTrue(formatted.isCompleted)
|
|
57
|
+
assertEquals(answer, formatted.await())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Test
|
|
61
|
+
fun `a formatter that never answers fails at the timeout`() =
|
|
62
|
+
runTest(timeout = 5.seconds) {
|
|
63
|
+
val formatted =
|
|
64
|
+
backgroundScope.async { runCatching { player.nowPlaying.formatter!!(params) } }
|
|
65
|
+
|
|
66
|
+
advanceTimeBy(FALLBACK_TIMEOUT - 1.seconds)
|
|
67
|
+
runCurrent()
|
|
68
|
+
assertFalse(formatted.isCompleted)
|
|
69
|
+
advanceTimeBy(2.seconds)
|
|
70
|
+
runCurrent()
|
|
71
|
+
|
|
72
|
+
assertTrue(formatted.isCompleted)
|
|
73
|
+
assertTrue(formatted.await().exceptionOrNull() is TimeoutException)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
package com.audiobrowser.player
|
|
2
2
|
|
|
3
3
|
import com.audiobrowser.TestFixtures.track
|
|
4
|
+
import com.audiobrowser.util.JsPromise
|
|
5
|
+
import com.audiobrowser.util.awaitCancellable
|
|
4
6
|
import com.margelo.nitro.audiobrowser.FormatNowPlayingParams
|
|
5
7
|
import com.margelo.nitro.audiobrowser.NowPlayingMetadata
|
|
6
8
|
import com.margelo.nitro.audiobrowser.NowPlayingUpdate
|
|
@@ -9,6 +11,8 @@ import com.margelo.nitro.audiobrowser.PlaybackState
|
|
|
9
11
|
import com.margelo.nitro.audiobrowser.StallReason
|
|
10
12
|
import com.margelo.nitro.audiobrowser.Track
|
|
11
13
|
import kotlinx.coroutines.CompletableDeferred
|
|
14
|
+
import kotlinx.coroutines.Job
|
|
15
|
+
import kotlinx.coroutines.plus
|
|
12
16
|
import kotlinx.coroutines.test.advanceTimeBy
|
|
13
17
|
import kotlinx.coroutines.test.runCurrent
|
|
14
18
|
import kotlinx.coroutines.test.runTest
|
|
@@ -244,6 +248,27 @@ class NowPlayingUpdaterTest {
|
|
|
244
248
|
assertEquals(listOf("Place", "Song - Artist"), surface.stamps.map { it.secondary })
|
|
245
249
|
}
|
|
246
250
|
|
|
251
|
+
@Test
|
|
252
|
+
fun `a destroyed updater stamps nothing from a formatter still waiting on JS`() = runTest {
|
|
253
|
+
val surface = FakeSurface().apply { currentTrack = track("Station", artist = "Place") }
|
|
254
|
+
val updater = NowPlayingUpdater(surface, backgroundScope + Job())
|
|
255
|
+
val silent = JsPromise<NowPlayingUpdate?>()
|
|
256
|
+
var calls = 0
|
|
257
|
+
updater.formatter = {
|
|
258
|
+
if (++calls == 1) update(artist = "Song - Artist") else silent.promise.awaitCancellable()
|
|
259
|
+
}
|
|
260
|
+
updater.render()
|
|
261
|
+
runCurrent()
|
|
262
|
+
updater.render()
|
|
263
|
+
runCurrent()
|
|
264
|
+
val stamped = surface.stamps.toList()
|
|
265
|
+
|
|
266
|
+
updater.destroy()
|
|
267
|
+
runCurrent()
|
|
268
|
+
|
|
269
|
+
assertEquals(stamped, surface.stamps)
|
|
270
|
+
}
|
|
271
|
+
|
|
247
272
|
@Test
|
|
248
273
|
fun `a formatter that stops customizing restores the default`() = runTest {
|
|
249
274
|
val surface = FakeSurface().apply { currentTrack = track("Station", artist = "Place") }
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.AudioBrowser
|
|
4
|
+
import com.audiobrowser.TestFixtures
|
|
5
|
+
import com.audiobrowser.browser.BrowserConfig
|
|
6
|
+
import com.audiobrowser.browser.BrowserManager
|
|
7
|
+
import com.audiobrowser.model.PlayerSetupOptions
|
|
8
|
+
import com.audiobrowser.util.JsPromise
|
|
9
|
+
import com.margelo.nitro.audiobrowser.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__vector_Track______SearchParams
|
|
10
|
+
import com.margelo.nitro.audiobrowser.MediaReference
|
|
11
|
+
import com.margelo.nitro.audiobrowser.SearchParams
|
|
12
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
13
|
+
import com.margelo.nitro.core.Promise
|
|
14
|
+
import kotlin.time.Duration.Companion.seconds
|
|
15
|
+
import kotlinx.coroutines.async
|
|
16
|
+
import kotlinx.coroutines.test.runCurrent
|
|
17
|
+
import kotlinx.coroutines.test.runTest
|
|
18
|
+
import org.junit.After
|
|
19
|
+
import org.junit.Assert.assertEquals
|
|
20
|
+
import org.junit.Assert.assertFalse
|
|
21
|
+
import org.junit.Assert.assertTrue
|
|
22
|
+
import org.junit.Test
|
|
23
|
+
import org.junit.runner.RunWith
|
|
24
|
+
import org.mockito.Mockito.RETURNS_DEFAULTS
|
|
25
|
+
import org.mockito.Mockito.mock
|
|
26
|
+
import org.mockito.stubbing.Answer
|
|
27
|
+
import org.robolectric.RobolectricTestRunner
|
|
28
|
+
import org.robolectric.RuntimeEnvironment
|
|
29
|
+
|
|
30
|
+
@RunWith(RobolectricTestRunner::class)
|
|
31
|
+
class PlayFromSearchTest {
|
|
32
|
+
private val player =
|
|
33
|
+
Player(RuntimeEnvironment.getApplication()).apply { setup(PlayerSetupOptions()) }
|
|
34
|
+
|
|
35
|
+
@After fun release() = player.exoPlayer.release()
|
|
36
|
+
|
|
37
|
+
private val params =
|
|
38
|
+
SearchParams(
|
|
39
|
+
mode = null,
|
|
40
|
+
query = "a",
|
|
41
|
+
genre = null,
|
|
42
|
+
artist = null,
|
|
43
|
+
album = null,
|
|
44
|
+
title = null,
|
|
45
|
+
playlist = null,
|
|
46
|
+
reference = MediaReference.UNKNOWN,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
/** A browser whose search callback hands back [answer], behind Nitro's bridge promise. */
|
|
50
|
+
private fun browserWhoseSearchAnswers(answer: JsPromise<Array<Track>>) {
|
|
51
|
+
val bridge = JsPromise<Promise<Array<Track>>>().apply { resolve(answer.promise) }
|
|
52
|
+
val browserManager =
|
|
53
|
+
BrowserManager().apply {
|
|
54
|
+
config =
|
|
55
|
+
BrowserConfig(
|
|
56
|
+
routes =
|
|
57
|
+
arrayOf(
|
|
58
|
+
TestFixtures.staticRoute(
|
|
59
|
+
BrowserManager.SEARCH_ROUTE_PATH,
|
|
60
|
+
TestFixtures.resolvedTrack(path = BrowserManager.SEARCH_ROUTE_PATH),
|
|
61
|
+
)
|
|
62
|
+
.copy(
|
|
63
|
+
browseStatic = null,
|
|
64
|
+
searchCallback =
|
|
65
|
+
Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__vector_Track______SearchParams {
|
|
66
|
+
bridge.promise
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
player.browser =
|
|
73
|
+
mock(
|
|
74
|
+
AudioBrowser::class.java,
|
|
75
|
+
Answer {
|
|
76
|
+
when {
|
|
77
|
+
it.method.name == "gateDecision" -> AudioBrowser.GateOutcome(false, null)
|
|
78
|
+
it.method.name.startsWith("getBrowserManager") -> browserManager
|
|
79
|
+
else -> RETURNS_DEFAULTS.answer(it)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
player.notifyBrowserConfigurationReady()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Test
|
|
87
|
+
fun `a search cancelled while its callback is silent ends and plays nothing`() =
|
|
88
|
+
runTest(timeout = 5.seconds) {
|
|
89
|
+
val answer = JsPromise<Array<Track>>()
|
|
90
|
+
browserWhoseSearchAnswers(answer)
|
|
91
|
+
val caller = backgroundScope.async { player.playFromSearch(params) }
|
|
92
|
+
runCurrent()
|
|
93
|
+
|
|
94
|
+
caller.cancel()
|
|
95
|
+
runCurrent()
|
|
96
|
+
answer.resolve(arrayOf(TestFixtures.track(title = "a")))
|
|
97
|
+
runCurrent()
|
|
98
|
+
|
|
99
|
+
assertTrue(caller.isCompleted)
|
|
100
|
+
assertTrue(caller.isCancelled)
|
|
101
|
+
assertEquals(emptyList<String>(), player.tracks.map { it.title })
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Test
|
|
105
|
+
fun `a search that times out waiting for AudioBrowser reports failure`() = runTest {
|
|
106
|
+
assertFalse(player.playFromSearch(params))
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -138,17 +138,55 @@ class PlaybackStateMachineTest {
|
|
|
138
138
|
fun `a transition to a track synthesizes the loading burst`() {
|
|
139
139
|
assertEquals(
|
|
140
140
|
listOf(PlaybackState.LOADING),
|
|
141
|
-
on(
|
|
141
|
+
on(
|
|
142
|
+
PlaybackEvent.MediaItemTransition(
|
|
143
|
+
hasTrack = true,
|
|
144
|
+
isPlaying = false,
|
|
145
|
+
loads = true,
|
|
146
|
+
shouldPlayWhenReady = false,
|
|
147
|
+
)
|
|
148
|
+
),
|
|
142
149
|
)
|
|
143
150
|
assertEquals(
|
|
144
151
|
listOf(PlaybackState.LOADING, PlaybackState.READY, PlaybackState.PLAYING),
|
|
145
|
-
on(
|
|
152
|
+
on(
|
|
153
|
+
PlaybackEvent.MediaItemTransition(
|
|
154
|
+
hasTrack = true,
|
|
155
|
+
isPlaying = true,
|
|
156
|
+
loads = true,
|
|
157
|
+
shouldPlayWhenReady = false,
|
|
158
|
+
)
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
@Test
|
|
164
|
+
fun `a transition nothing loads is paused, except from stopped`() {
|
|
165
|
+
val idle =
|
|
166
|
+
PlaybackEvent.MediaItemTransition(
|
|
167
|
+
hasTrack = true,
|
|
168
|
+
isPlaying = false,
|
|
169
|
+
loads = false,
|
|
170
|
+
shouldPlayWhenReady = false,
|
|
171
|
+
)
|
|
172
|
+
assertEquals(listOf(PlaybackState.PAUSED), on(idle, from = PlaybackState.ERROR))
|
|
173
|
+
assertEquals(listOf(PlaybackState.PAUSED), on(idle, from = PlaybackState.PAUSED))
|
|
174
|
+
assertEquals(emptyList<PlaybackState>(), on(idle, from = PlaybackState.STOPPED))
|
|
175
|
+
assertEquals(
|
|
176
|
+
emptyList<PlaybackState>(),
|
|
177
|
+
on(idle.copy(shouldPlayWhenReady = true), from = PlaybackState.ERROR),
|
|
146
178
|
)
|
|
147
179
|
}
|
|
148
180
|
|
|
149
181
|
@Test
|
|
150
182
|
fun `a transition without a track is none, except from stopped`() {
|
|
151
|
-
val emptied =
|
|
183
|
+
val emptied =
|
|
184
|
+
PlaybackEvent.MediaItemTransition(
|
|
185
|
+
hasTrack = false,
|
|
186
|
+
isPlaying = false,
|
|
187
|
+
loads = false,
|
|
188
|
+
shouldPlayWhenReady = false,
|
|
189
|
+
)
|
|
152
190
|
assertEquals(listOf(PlaybackState.NONE), on(emptied, from = PlaybackState.ERROR))
|
|
153
191
|
assertEquals(listOf(PlaybackState.NONE), on(emptied, from = PlaybackState.ENDED))
|
|
154
192
|
assertEquals(emptyList<PlaybackState>(), on(emptied, from = PlaybackState.STOPPED))
|
|
@@ -17,15 +17,20 @@ import com.audiobrowser.util.TrackFactory
|
|
|
17
17
|
import com.margelo.nitro.audiobrowser.Playback
|
|
18
18
|
import com.margelo.nitro.audiobrowser.PlaybackState
|
|
19
19
|
import java.io.File
|
|
20
|
+
import java.net.ServerSocket
|
|
20
21
|
import java.nio.ByteBuffer
|
|
21
22
|
import java.nio.ByteOrder
|
|
22
23
|
import java.time.Duration
|
|
24
|
+
import java.util.concurrent.CountDownLatch
|
|
25
|
+
import java.util.concurrent.TimeUnit
|
|
26
|
+
import kotlin.concurrent.thread
|
|
23
27
|
import kotlinx.coroutines.CoroutineScope
|
|
24
28
|
import kotlinx.coroutines.Dispatchers
|
|
25
29
|
import kotlinx.coroutines.cancel
|
|
26
30
|
import org.junit.After
|
|
27
31
|
import org.junit.Assert.assertEquals
|
|
28
32
|
import org.junit.Assert.assertFalse
|
|
33
|
+
import org.junit.Assert.assertNotEquals
|
|
29
34
|
import org.junit.Assert.assertTrue
|
|
30
35
|
import org.junit.Assert.fail
|
|
31
36
|
import org.junit.Rule
|
|
@@ -138,6 +143,11 @@ class PlayerRetryTest {
|
|
|
138
143
|
awaitRetry("a pending retry")
|
|
139
144
|
|
|
140
145
|
pause()
|
|
146
|
+
assertStaysPausedAndIdle()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Runs past every backoff a retry would have taken. */
|
|
150
|
+
private fun assertStaysPausedAndIdle() {
|
|
141
151
|
repeat(10) {
|
|
142
152
|
ShadowSystemClock.advanceBy(Duration.ofSeconds(1))
|
|
143
153
|
Thread.sleep(20)
|
|
@@ -181,13 +191,128 @@ class PlayerRetryTest {
|
|
|
181
191
|
}
|
|
182
192
|
|
|
183
193
|
@Test
|
|
184
|
-
fun `removing the current track after a cancelled retry stays idle`() {
|
|
194
|
+
fun `removing the current track after a cancelled retry stays idle and paused`() {
|
|
185
195
|
pauseDuringRetry(Uri.fromFile(silentWav()).toString()) { player.pause() }
|
|
186
196
|
|
|
187
197
|
player.remove(listOf(0))
|
|
188
198
|
shadowOf(Looper.getMainLooper()).idle()
|
|
189
199
|
|
|
190
200
|
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
201
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
202
|
+
assertEquals(MediaPlayer.STATE_READY, player.forwardingPlayer.playbackState)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@Test
|
|
206
|
+
fun `removing a failed current track while paused is paused`() {
|
|
207
|
+
failTerminally(Uri.fromFile(silentWav()).toString())
|
|
208
|
+
player.pause()
|
|
209
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
210
|
+
|
|
211
|
+
player.remove(listOf(0))
|
|
212
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
213
|
+
|
|
214
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
215
|
+
assertEquals(Playback(PlaybackState.PAUSED, null), player.getPlayback())
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
@Test
|
|
219
|
+
fun `adding a track after a cancelled retry stays idle and paused`() {
|
|
220
|
+
pauseDuringRetry { player.pause() }
|
|
221
|
+
|
|
222
|
+
player.add(TestFixtures.track(src = Uri.fromFile(silentWav()).toString()))
|
|
223
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
224
|
+
|
|
225
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
226
|
+
assertEquals(PlaybackState.PAUSED, player.playbackState)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
@Test
|
|
230
|
+
fun `adding to an empty queue loads`() {
|
|
231
|
+
player.setup(PlayerSetupOptions())
|
|
232
|
+
|
|
233
|
+
player.add(TestFixtures.track(src = Uri.fromFile(silentWav()).toString()))
|
|
234
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
235
|
+
|
|
236
|
+
assertNotEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@Test
|
|
240
|
+
fun `a stop after a cancelled retry tells the session`() {
|
|
241
|
+
pauseDuringRetry { player.pause() }
|
|
242
|
+
val states = mutableListOf<Int>()
|
|
243
|
+
player.forwardingPlayer.addListener(
|
|
244
|
+
object : MediaPlayer.Listener {
|
|
245
|
+
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
246
|
+
states += playbackState
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
player.stop()
|
|
252
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
253
|
+
|
|
254
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.forwardingPlayer.playbackState)
|
|
255
|
+
assertEquals(listOf(MediaPlayer.STATE_IDLE), states)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
@Test
|
|
259
|
+
fun `a session stop after a cancelled retry is a stop the session sees`() {
|
|
260
|
+
pauseDuringRetry { player.pause() }
|
|
261
|
+
val states = mutableListOf<Int>()
|
|
262
|
+
player.forwardingPlayer.addListener(
|
|
263
|
+
object : MediaPlayer.Listener {
|
|
264
|
+
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
265
|
+
states += playbackState
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
player.forwardingPlayer.stop()
|
|
271
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
272
|
+
|
|
273
|
+
assertEquals(PlaybackState.STOPPED, player.playbackState)
|
|
274
|
+
assertEquals(MediaPlayer.STATE_IDLE, player.forwardingPlayer.playbackState)
|
|
275
|
+
assertEquals(listOf(MediaPlayer.STATE_IDLE), states)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@Test
|
|
279
|
+
fun `a load failing while paused stays paused and the next play reloads`() {
|
|
280
|
+
val server = ServerSocket(0)
|
|
281
|
+
val wav = silentWav().readBytes()
|
|
282
|
+
val sent = CountDownLatch(1)
|
|
283
|
+
val drop = CountDownLatch(1)
|
|
284
|
+
// Serves five seconds of the minute, holds the connection until the drop, then refuses.
|
|
285
|
+
thread(isDaemon = true) {
|
|
286
|
+
server.accept().use { socket ->
|
|
287
|
+
val request = socket.getInputStream().bufferedReader()
|
|
288
|
+
while (!request.readLine().isNullOrEmpty()) Unit
|
|
289
|
+
socket.getOutputStream().run {
|
|
290
|
+
write("HTTP/1.1 200 OK\r\nContent-Length: ${wav.size}\r\n\r\n".toByteArray())
|
|
291
|
+
write(wav, 0, 40_000)
|
|
292
|
+
flush()
|
|
293
|
+
}
|
|
294
|
+
sent.countDown()
|
|
295
|
+
drop.await(10, TimeUnit.SECONDS)
|
|
296
|
+
}
|
|
297
|
+
server.close()
|
|
298
|
+
}
|
|
299
|
+
play("http://127.0.0.1:${server.localPort}/stream.wav", retryPolicy = RetryPolicy.Infinite())
|
|
300
|
+
// A source with no track enabled stops loading, and the load must still run when it fails.
|
|
301
|
+
player.exoPlayer.trackSelectionParameters =
|
|
302
|
+
player.exoPlayer.trackSelectionParameters
|
|
303
|
+
.buildUpon()
|
|
304
|
+
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, false)
|
|
305
|
+
.build()
|
|
306
|
+
idleUntil("the first bytes") { sent.count == 0L }
|
|
307
|
+
|
|
308
|
+
player.pause()
|
|
309
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
310
|
+
drop.countDown()
|
|
311
|
+
idleUntil("the load given up") { player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE }
|
|
312
|
+
assertStaysPausedAndIdle()
|
|
313
|
+
|
|
314
|
+
player.play()
|
|
315
|
+
awaitRetry("the reload's retry")
|
|
191
316
|
}
|
|
192
317
|
|
|
193
318
|
@Test
|
|
@@ -73,7 +73,6 @@ class PlayerSetupTest {
|
|
|
73
73
|
assertEquals(0.5f, rebuilt.volume)
|
|
74
74
|
assertTrue(rebuilt.playbackState != MediaPlayer.STATE_IDLE)
|
|
75
75
|
assertTrue(rebuilt.playWhenReady)
|
|
76
|
-
assertTrue(player.playWhenReadyCache)
|
|
77
76
|
// The new engine buffers again from the kept position.
|
|
78
77
|
assertEquals(PlaybackState.BUFFERING, player.playbackState)
|
|
79
78
|
assertEquals(
|