react-native-audio-browser 0.2.0-next.76 → 0.2.0-next.78

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.
Files changed (50) hide show
  1. package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +1 -1
  2. package/android/src/main/java/com/audiobrowser/player/EnginePlayback.kt +9 -5
  3. package/android/src/main/java/com/audiobrowser/player/InterceptingPlayer.kt +71 -39
  4. package/android/src/main/java/com/audiobrowser/player/LiveStreamExtractor.kt +65 -0
  5. package/android/src/main/java/com/audiobrowser/player/MediaFactory.kt +12 -3
  6. package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +5 -0
  7. package/android/src/main/java/com/audiobrowser/player/Player.kt +78 -36
  8. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +6 -0
  9. package/android/src/main/java/com/audiobrowser/player/RetryLoadErrorHandlingPolicy.kt +1 -1
  10. package/android/src/main/java/com/audiobrowser/remote/RemoteSwitchingPlayer.kt +48 -2
  11. package/android/src/test/java/androidx/media3/session/SessionViews.kt +10 -0
  12. package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +372 -0
  13. package/android/src/test/java/com/audiobrowser/player/PlayerSetupEndedTest.kt +221 -0
  14. package/android/src/test/java/com/audiobrowser/player/PlayerSetupTest.kt +17 -7
  15. package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +183 -7
  16. package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +48 -0
  17. package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +236 -0
  18. package/android/src/test/java/com/audiobrowser/player/SessionViewRulesTest.kt +62 -0
  19. package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +188 -0
  20. package/android/src/test/java/com/audiobrowser/remote/FakeRemote.kt +23 -0
  21. package/android/src/test/java/com/audiobrowser/remote/RemoteSwitchingPlayerTest.kt +74 -0
  22. package/ios/AVPlayerEngine.swift +5 -1
  23. package/ios/Browser/BrowserManager.swift +2 -2
  24. package/ios/Browser/PageResolver.swift +14 -19
  25. package/ios/Browser/RequestPipeline.swift +6 -4
  26. package/ios/CarPlay/CarPlayNowPlayingManager.swift +1 -1
  27. package/ios/CarPlay/CarPlaySession+PageLoader.swift +1 -0
  28. package/ios/CarPlay/CarPlaySession+TabBar.swift +2 -0
  29. package/ios/Extension/Promise+AwaitCallback.swift +33 -0
  30. package/ios/HybridAudioBrowser+Player.swift +1 -1
  31. package/ios/HybridAudioBrowser.swift +1 -1
  32. package/ios/Player/MediaLoader.swift +1 -1
  33. package/ios/Player/PlaybackFailure.swift +3 -3
  34. package/ios/Player/Player.swift +67 -49
  35. package/ios/Player/PlayerEngine.swift +2 -0
  36. package/ios/Player/RetryLoop.swift +72 -12
  37. package/ios/Tests/AwaitCallbackTests.swift +210 -0
  38. package/ios/Tests/GenerationMemoTests.swift +57 -0
  39. package/ios/Tests/PlaybackFailureTests.swift +7 -0
  40. package/ios/Tests/PlayerPolicyTests.swift +346 -6
  41. package/ios/Tests/PlayerTests.swift +79 -1
  42. package/ios/Tests/RetryLoopTests.swift +30 -5
  43. package/ios/Tests/Support/TestTypes.swift +15 -7
  44. package/ios/Tests/Support/TimeLimit.swift +43 -0
  45. package/ios/Util/AwaitCallback.swift +80 -0
  46. package/ios/Util/GenerationMemo.swift +11 -4
  47. package/package.json +1 -1
  48. package/android/src/test/java/com/audiobrowser/player/SessionErrorMaskTest.kt +0 -63
  49. package/ios/Player/EndOfTrackJudgement.swift +0 -25
  50. package/ios/Tests/EndOfTrackJudgementTests.swift +0 -30
@@ -121,15 +121,25 @@ class PlayerSetupTest {
121
121
  }
122
122
 
123
123
  @Test
124
- fun setupWithChangedOptionsKeepsAnUnpreparedQueueStopped() {
124
+ fun setupWithChangedOptionsKeepsAnUnpreparedQueueAndItsState() {
125
125
  player.setup(PlayerSetupOptions())
126
126
  player.exoPlayer.setMediaItems(listOf(item("a")))
127
-
128
- player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
129
-
130
- assertEquals(listOf("a"), player.tracks.map { it.title })
131
- assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
132
- assertEquals(PlaybackState.NONE, player.playbackState)
127
+ listOf(PlaybackState.PAUSED, PlaybackState.STOPPED, PlaybackState.ERROR).forEachIndexed {
128
+ i,
129
+ state ->
130
+ player.setPlaybackState(state)
131
+
132
+ // A different maxBuffer each time, so each setup() rebuilds.
133
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0 + i))
134
+
135
+ assertEquals(listOf("a"), player.tracks.map { it.title })
136
+ assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
137
+ assertEquals(state, player.playbackState)
138
+ assertEquals(
139
+ if (state == PlaybackState.STOPPED) MediaPlayer.STATE_IDLE else MediaPlayer.STATE_READY,
140
+ player.forwardingPlayer.playbackState,
141
+ )
142
+ }
133
143
  }
134
144
 
135
145
  private fun item(title: String) =
@@ -9,16 +9,20 @@ import androidx.media3.common.Player as MediaPlayer
9
9
  import com.audiobrowser.Callbacks
10
10
  import com.audiobrowser.TestFixtures
11
11
  import com.audiobrowser.model.PlayerSetupOptions
12
+ import com.audiobrowser.remote.FakeRemote
12
13
  import com.audiobrowser.util.TrackFactory
13
14
  import com.margelo.nitro.audiobrowser.Playback
14
15
  import com.margelo.nitro.audiobrowser.PlaybackState
15
16
  import com.margelo.nitro.audiobrowser.PlayingState
17
+ import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
16
18
  import java.io.File
17
19
  import java.nio.ByteBuffer
18
20
  import java.nio.ByteOrder
19
21
  import org.junit.After
20
22
  import org.junit.Assert.assertEquals
21
23
  import org.junit.Assert.assertFalse
24
+ import org.junit.Assert.assertNotSame
25
+ import org.junit.Assert.assertSame
22
26
  import org.junit.Assert.assertTrue
23
27
  import org.junit.Assert.fail
24
28
  import org.junit.Before
@@ -46,7 +50,13 @@ class PlayerSuppressionTest {
46
50
  @get:Rule val folder = TemporaryFolder()
47
51
 
48
52
  private val context = RuntimeEnvironment.getApplication()
49
- private val player = Player(context)
53
+ private val remote = FakeRemote(Looper.getMainLooper())
54
+ private val player =
55
+ Player(
56
+ context,
57
+ castRemote = { _, _ -> remote },
58
+ sonosRemote = { _, _ -> FakeRemote(Looper.getMainLooper()) },
59
+ )
50
60
  private val callbacks = mock(Callbacks::class.java)
51
61
  private val audioManager =
52
62
  shadowOf(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
@@ -144,16 +154,182 @@ class PlayerSuppressionTest {
144
154
  }
145
155
 
146
156
  @Test
147
- fun `a rebuild during a transient focus loss reports the new engine's intent`() {
157
+ fun `a rebuild during a transient focus loss stays suppressed until focus returns`() {
148
158
  focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
149
- // As after a skip while suppressed.
150
- player.setPlaybackState(PlaybackState.BUFFERING)
151
- assertEquals(PlayingState(false, false), player.getPlayingState())
159
+ val engine = player.exoPlayer
160
+
161
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
162
+ shadowOf(Looper.getMainLooper()).idle()
163
+
164
+ assertSame(engine, player.exoPlayer)
165
+ assertSuppressed()
166
+ focus(AudioManager.AUDIOFOCUS_GAIN)
167
+ idleUntil("playing on a new engine") {
168
+ player.playbackState == PlaybackState.PLAYING && player.exoPlayer !== engine
169
+ }
170
+ }
171
+
172
+ @Test
173
+ fun `a rebuild during a call keeps the intent`() {
174
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
175
+ audioManager.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_FAILED)
176
+
177
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
178
+ // ExoPlayer requests focus on its playback thread and posts a refusal back.
179
+ Thread.sleep(200)
180
+ shadowOf(Looper.getMainLooper()).idle()
181
+
182
+ assertSuppressed()
183
+ audioManager.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
184
+ focus(AudioManager.AUDIOFOCUS_GAIN)
185
+ idleUntil("playing again") { player.playbackState == PlaybackState.PLAYING }
186
+ }
187
+
188
+ @Test
189
+ fun `a rebuild held during a transient focus loss runs when the intent drops`() {
190
+ listOf<() -> Unit>(
191
+ { focus(AudioManager.AUDIOFOCUS_LOSS) },
192
+ { player.pause() },
193
+ { player.stop() },
194
+ )
195
+ .forEachIndexed { i, drop ->
196
+ player.play()
197
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
198
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
199
+ val engine = player.exoPlayer
200
+ // A different maxBuffer each time, so each setup() rebuilds.
201
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0 + i))
202
+ assertSame(engine, player.exoPlayer)
203
+ drop()
204
+
205
+ shadowOf(Looper.getMainLooper()).idle()
206
+
207
+ assertNotSame(engine, player.exoPlayer)
208
+ assertFalse(player.exoPlayer.playWhenReady)
209
+ }
210
+ assertEquals(PlaybackState.STOPPED, player.playbackState)
211
+ }
212
+
213
+ @Test
214
+ fun `a play during a transient focus loss runs a held rebuild after it`() {
215
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
216
+ val engine = player.exoPlayer
217
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
218
+ assertSame(engine, player.exoPlayer)
219
+
220
+ // ExoPlayer's play() asks for focus again, so it plays over the transient loss.
221
+ player.play()
222
+ // The rebuild follows the play: it is posted, never run inside it.
223
+ assertSame(engine, player.exoPlayer)
224
+ shadowOf(Looper.getMainLooper()).idle()
225
+
226
+ assertNotSame(engine, player.exoPlayer)
227
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
228
+ }
229
+
230
+ @Test
231
+ fun `an engine stopped during a transient focus loss holds no rebuild`() {
232
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
233
+ // As a handover to a remote output does: the engine stops but keeps the intent.
234
+ player.exoPlayer.stop()
235
+ shadowOf(Looper.getMainLooper()).idle()
236
+ val engine = player.exoPlayer
237
+
238
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
239
+
240
+ assertNotSame(engine, player.exoPlayer)
241
+ }
242
+
243
+ @Test
244
+ fun `a held rebuild runs when a remote output takes over`() {
245
+ player.setup(
246
+ PlayerSetupOptions(
247
+ remoteOutputs =
248
+ RemoteOutputsOptions(cast = true, castReceiverApplicationId = null, sonos = null)
249
+ )
250
+ )
251
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
252
+ val engine = player.exoPlayer
253
+ player.setup(
254
+ PlayerSetupOptions(
255
+ maxBuffer = 120_000.0,
256
+ remoteOutputs =
257
+ RemoteOutputsOptions(cast = true, castReceiverApplicationId = null, sonos = null),
258
+ )
259
+ )
260
+ assertSame(engine, player.exoPlayer)
261
+
262
+ remote.startSession()
263
+ idleUntil("remote") { !player.localEngine.isActive }
264
+ shadowOf(Looper.getMainLooper()).idle()
152
265
 
266
+ assertNotSame(engine, player.exoPlayer)
267
+ assertFalse(player.localEngine.isActive)
268
+ }
269
+
270
+ @Test
271
+ fun `a held rebuild runs when the engine idles`() {
272
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
273
+ val engine = player.exoPlayer
153
274
  player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
275
+ assertSame(engine, player.exoPlayer)
276
+
277
+ // As a terminal error does: the engine idles with the intent and the suppression unchanged.
278
+ player.exoPlayer.stop()
279
+ shadowOf(Looper.getMainLooper()).idle()
154
280
 
155
- assertEquals(PlayingState(true, true), player.getPlayingState())
156
- verify(callbacks).onPlaybackPlayingState(PlayingState(true, true))
281
+ assertNotSame(engine, player.exoPlayer)
282
+ }
283
+
284
+ @Test
285
+ fun `the last setup during a transient focus loss wins`() {
286
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
287
+ val engines = mutableSetOf(player.exoPlayer)
288
+
289
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
290
+ engines += player.exoPlayer
291
+ player.setup(PlayerSetupOptions(maxBuffer = 130_000.0))
292
+ engines += player.exoPlayer
293
+ focus(AudioManager.AUDIOFOCUS_GAIN)
294
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
295
+ val engine = player.exoPlayer
296
+ engines += engine
297
+
298
+ assertEquals("one rebuild", 2, engines.size)
299
+
300
+ player.setup(PlayerSetupOptions(maxBuffer = 130_000.0))
301
+ assertSame(engine, player.exoPlayer)
302
+ }
303
+
304
+ @Test
305
+ fun `a setup back to the current options during a transient focus loss cancels the rebuild`() {
306
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
307
+ val engine = player.exoPlayer
308
+
309
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
310
+ player.setup(PlayerSetupOptions())
311
+ focus(AudioManager.AUDIOFOCUS_GAIN)
312
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
313
+
314
+ assertSame(engine, player.exoPlayer)
315
+ }
316
+
317
+ @Test
318
+ fun `destroying the player drops a held rebuild`() {
319
+ focus(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
320
+ val engine = player.exoPlayer
321
+ player.setup(PlayerSetupOptions(maxBuffer = 120_000.0))
322
+
323
+ player.destroy()
324
+ shadowOf(Looper.getMainLooper()).idle()
325
+
326
+ assertSame(engine, player.exoPlayer)
327
+ }
328
+
329
+ private fun assertSuppressed() {
330
+ assertTrue(player.exoPlayer.playWhenReady)
331
+ assertEquals(PlaybackState.PAUSED, player.playbackState)
332
+ assertEquals(PlayingState(false, false), player.getPlayingState())
157
333
  }
158
334
 
159
335
  private fun focus(change: Int) {
@@ -9,6 +9,7 @@ import com.audiobrowser.Callbacks
9
9
  import com.audiobrowser.TestFixtures
10
10
  import com.audiobrowser.model.PlayerSetupOptions
11
11
  import com.audiobrowser.util.TrackFactory
12
+ import com.margelo.nitro.audiobrowser.NowPlayingUpdate
12
13
  import com.margelo.nitro.audiobrowser.TimedMetadata
13
14
  import kotlin.concurrent.thread
14
15
  import org.junit.After
@@ -148,4 +149,51 @@ class PlayerTrackChangeTest {
148
149
 
149
150
  assertEquals("B song 1", player.nowPlaying.timedMetadata?.title)
150
151
  }
152
+
153
+ /** Plays a station whose formatter shows the stream title as the second line, as an app does. */
154
+ private fun playStationWithTitle() {
155
+ player.setup(PlayerSetupOptions())
156
+ player.nowPlaying.formatter = { params ->
157
+ NowPlayingUpdate(title = null, artist = params.timedMetadata?.title, album = null)
158
+ }
159
+ player.sessionPlayer.setMediaItems(
160
+ listOf(TrackFactory.toMedia3(TestFixtures.track(title = "A", artist = "A's city")))
161
+ )
162
+ idle()
163
+ player.playerListener.onMetadata(icy("A song 1"))
164
+ idle()
165
+ }
166
+
167
+ private fun shownSecondLines() =
168
+ listOf(
169
+ player.sessionPlayer.mediaMetadata.artist?.toString(),
170
+ player.nowPlaying.getNowPlaying()?.artist,
171
+ )
172
+
173
+ @Test
174
+ fun `a stop clears the stream title the session and the now-playing info show`() {
175
+ playStationWithTitle()
176
+ assertEquals(listOf("A song 1", "A song 1"), shownSecondLines())
177
+
178
+ player.stop()
179
+ idle()
180
+
181
+ assertNull(player.nowPlaying.timedMetadata)
182
+ assertEquals(listOf("A's city", "A's city"), shownSecondLines())
183
+ }
184
+
185
+ @Test
186
+ fun `a play after a stop shows the track's own lines until the stream sends a title`() {
187
+ playStationWithTitle()
188
+ player.stop()
189
+ idle()
190
+
191
+ player.play()
192
+ idle()
193
+ assertEquals(listOf("A's city", "A's city"), shownSecondLines())
194
+
195
+ player.playerListener.onMetadata(icy("A song 2"))
196
+ idle()
197
+ assertEquals(listOf("A song 2", "A song 2"), shownSecondLines())
198
+ }
151
199
  }
@@ -0,0 +1,236 @@
1
+ package com.audiobrowser.player
2
+
3
+ import android.net.Uri
4
+ import android.os.Looper
5
+ import androidx.media3.common.C
6
+ import androidx.media3.common.Player as MediaPlayer
7
+ import androidx.media3.common.util.Util
8
+ import com.audiobrowser.Callbacks
9
+ import com.audiobrowser.TestFixtures
10
+ import com.audiobrowser.model.PlayerSetupOptions
11
+ import com.audiobrowser.model.RetryPolicy
12
+ import com.audiobrowser.util.TrackFactory
13
+ import com.margelo.nitro.audiobrowser.Playback
14
+ import com.margelo.nitro.audiobrowser.PlaybackState
15
+ import java.io.File
16
+ import java.nio.ByteBuffer
17
+ import java.nio.ByteOrder
18
+ import java.time.Duration
19
+ import org.junit.After
20
+ import org.junit.Assert.assertEquals
21
+ import org.junit.Assert.assertFalse
22
+ import org.junit.Assert.fail
23
+ import org.junit.Rule
24
+ import org.junit.Test
25
+ import org.junit.rules.TemporaryFolder
26
+ import org.junit.runner.RunWith
27
+ import org.mockito.Mockito.mock
28
+ import org.mockito.Mockito.mockingDetails
29
+ import org.mockito.Mockito.times
30
+ import org.mockito.Mockito.verify
31
+ import org.mockito.Mockito.`when`
32
+ import org.robolectric.RobolectricTestRunner
33
+ import org.robolectric.RuntimeEnvironment
34
+ import org.robolectric.Shadows.shadowOf
35
+ import org.robolectric.annotation.Config
36
+ import org.robolectric.shadows.ShadowSystemClock
37
+
38
+ /** A play from the session, or JS's setPlayWhenReady(true), plays as play() does. */
39
+ @RunWith(RobolectricTestRunner::class)
40
+ @Config(sdk = [28])
41
+ class SessionPlayTest {
42
+
43
+ @get:Rule val folder = TemporaryFolder()
44
+
45
+ private val context = RuntimeEnvironment.getApplication()
46
+ private val player = Player(context)
47
+ private val callbacks = mock(Callbacks::class.java)
48
+
49
+ @After
50
+ fun release() {
51
+ player.networkMonitor.destroy()
52
+ player.exoPlayer.release()
53
+ }
54
+
55
+ @Test
56
+ fun `an app's play handler takes a session play once and nothing plays`() {
57
+ load(Uri.fromFile(silentWav()).toString())
58
+ player.stop()
59
+ `when`(callbacks.handleRemotePlay()).thenReturn(true)
60
+
61
+ // What media3 runs for a controller's play.
62
+ Util.handlePlayButtonAction(player.forwardingPlayer)
63
+ idleFor(1)
64
+
65
+ verify(callbacks, times(1)).handleRemotePlay()
66
+ assertFalse(player.exoPlayer.playWhenReady)
67
+ assertFalse(player.isPlaying)
68
+ }
69
+
70
+ @Test
71
+ fun `a session play after a stop loads once and plays`() {
72
+ load(Uri.fromFile(silentWav()).toString())
73
+ player.stop()
74
+ val before = states().size
75
+
76
+ Util.handlePlayButtonAction(player.forwardingPlayer)
77
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
78
+
79
+ assertEquals(listOf(PlaybackState.BUFFERING, PlaybackState.PLAYING), states().drop(before))
80
+ }
81
+
82
+ @Test
83
+ fun `setPlayWhenReady(true) after a stop loads`() {
84
+ load(Uri.fromFile(silentWav()).toString())
85
+ player.stop()
86
+
87
+ // As JS setPlayWhenReady(true) does: the setter alone.
88
+ player.playWhenReady = true
89
+
90
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
91
+ }
92
+
93
+ @Test
94
+ fun `setPlayWhenReady(true) after a cancelled retry reloads`() {
95
+ load("https://127.0.0.1:9/stream", RetryPolicy.Infinite())
96
+ player.play()
97
+ idleUntil("a pending retry") { player.playbackError?.retrying == true }
98
+ player.pause()
99
+ idleUntil("an idle engine") { player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE }
100
+
101
+ player.playWhenReady = true
102
+
103
+ idleUntil("the reload's retry") { player.playbackError?.retrying == true }
104
+ }
105
+
106
+ @Test
107
+ fun `a setup's raised intent over a stop loads nothing`() {
108
+ load(Uri.fromFile(silentWav()).toString())
109
+ player.stop()
110
+
111
+ val before = states().size
112
+
113
+ player.applySetupPlayWhenReady(true)
114
+ idleFor(1)
115
+
116
+ assertEquals(emptyList<PlaybackState>(), states().drop(before))
117
+
118
+ assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
119
+ assertEquals(PlaybackState.STOPPED, player.playbackState)
120
+ }
121
+
122
+ @Test
123
+ fun `a setup's raised intent over a terminal error loads nothing`() {
124
+ load("https://127.0.0.1:9/stream", RetryPolicy.Limited(maxRetries = 0))
125
+ val beforePlay = states().size
126
+ player.play()
127
+ idleUntil("the play's load to fail") { PlaybackState.ERROR in states().drop(beforePlay) }
128
+
129
+ val before = states().size
130
+
131
+ player.applySetupPlayWhenReady(true)
132
+ idleFor(1)
133
+
134
+ assertEquals(emptyList<PlaybackState>(), states().drop(before))
135
+
136
+ assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
137
+ assertEquals(PlaybackState.ERROR, player.playbackState)
138
+ }
139
+
140
+ @Test
141
+ fun `a setup's raised intent over a cancelled retry loads nothing`() {
142
+ load("https://127.0.0.1:9/stream", RetryPolicy.Infinite())
143
+ player.play()
144
+ idleUntil("a pending retry") { player.playbackError?.retrying == true }
145
+ player.pause()
146
+ idleUntil("an idle engine") { player.exoPlayer.playbackState == MediaPlayer.STATE_IDLE }
147
+
148
+ val before = states().size
149
+
150
+ player.applySetupPlayWhenReady(true)
151
+ idleFor(1)
152
+
153
+ assertEquals(emptyList<PlaybackState>(), states().drop(before))
154
+
155
+ assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
156
+ assertEquals(PlaybackState.PAUSED, player.playbackState)
157
+ }
158
+
159
+ @Test
160
+ fun `a setup's raised intent plays a queue set afterwards`() {
161
+ player.setup(PlayerSetupOptions())
162
+ player.setCallbacks(callbacks)
163
+ player.exoPlayer.trackSelectionParameters =
164
+ player.exoPlayer.trackSelectionParameters
165
+ .buildUpon()
166
+ .setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
167
+ .build()
168
+ player.applySetupPlayWhenReady(true)
169
+
170
+ player.setQueue(arrayOf(TestFixtures.track(src = Uri.fromFile(silentWav()).toString())))
171
+
172
+ idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
173
+ }
174
+
175
+ private fun load(src: String, retryPolicy: RetryPolicy = RetryPolicy.Default) {
176
+ player.setup(PlayerSetupOptions(retryPolicy = retryPolicy))
177
+ player.setCallbacks(callbacks)
178
+ player.exoPlayer.trackSelectionParameters =
179
+ player.exoPlayer.trackSelectionParameters
180
+ .buildUpon()
181
+ .setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
182
+ .build()
183
+ player.exoPlayer.setMediaItems(listOf(TrackFactory.toMedia3(TestFixtures.track(src = src))))
184
+ player.exoPlayer.prepare()
185
+ idleFor(1)
186
+ }
187
+
188
+ private fun states() =
189
+ mockingDetails(callbacks)
190
+ .invocations
191
+ .filter { it.method.name == "onPlaybackChanged" }
192
+ .map { (it.arguments[0] as Playback).state }
193
+
194
+ private fun idleFor(seconds: Int) =
195
+ repeat(seconds * 10) {
196
+ ShadowSystemClock.advanceBy(Duration.ofMillis(100))
197
+ Thread.sleep(10)
198
+ shadowOf(Looper.getMainLooper()).idle()
199
+ }
200
+
201
+ private fun idleUntil(what: String, condition: () -> Boolean) {
202
+ val deadline = System.currentTimeMillis() + 5_000
203
+ while (!condition()) {
204
+ if (System.currentTimeMillis() > deadline)
205
+ fail(
206
+ "timed out waiting for $what: ${player.playbackState}, exo ${player.exoPlayer.playbackState}"
207
+ )
208
+ ShadowSystemClock.advanceBy(Duration.ofMillis(100))
209
+ Thread.sleep(10)
210
+ shadowOf(Looper.getMainLooper()).idle()
211
+ }
212
+ }
213
+
214
+ /** Half a second of 8 kHz mono 8-bit silence. */
215
+ private fun silentWav(): File {
216
+ val samples = 4_000
217
+ val header =
218
+ ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
219
+ put("RIFF".toByteArray())
220
+ putInt(36 + samples)
221
+ put("WAVEfmt ".toByteArray())
222
+ putInt(16)
223
+ putShort(1)
224
+ putShort(1)
225
+ putInt(8_000)
226
+ putInt(8_000)
227
+ putShort(1)
228
+ putShort(8)
229
+ put("data".toByteArray())
230
+ putInt(samples)
231
+ }
232
+ return folder.newFile("silence.wav").apply {
233
+ writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
234
+ }
235
+ }
236
+ }
@@ -0,0 +1,62 @@
1
+ package com.audiobrowser.player
2
+
3
+ import androidx.media3.common.Player as MediaPlayer
4
+ import com.margelo.nitro.audiobrowser.PlaybackState
5
+ import org.junit.Assert.assertEquals
6
+ import org.junit.Test
7
+
8
+ class SessionViewRulesTest {
9
+
10
+ private fun view(
11
+ engineState: Int = MediaPlayer.STATE_IDLE,
12
+ state: PlaybackState = PlaybackState.NONE,
13
+ engineFailed: Boolean = false,
14
+ keepAlive: Boolean = true,
15
+ ) = SessionView.playbackState(engineState, state, engineFailed, keepAlive)
16
+
17
+ @Test
18
+ fun `an idle engine with an error, in ERROR or in PAUSED reads ready while keep-alive is on`() {
19
+ assertEquals(MediaPlayer.STATE_READY, view(state = PlaybackState.PLAYING, engineFailed = true))
20
+ assertEquals(MediaPlayer.STATE_READY, view(state = PlaybackState.ERROR))
21
+ assertEquals(MediaPlayer.STATE_READY, view(state = PlaybackState.PAUSED))
22
+ }
23
+
24
+ @Test
25
+ fun `an ended queue on an idle engine reads ended, keep-alive or not`() {
26
+ assertEquals(MediaPlayer.STATE_ENDED, view(state = PlaybackState.ENDED))
27
+ assertEquals(MediaPlayer.STATE_ENDED, view(state = PlaybackState.ENDED, keepAlive = false))
28
+ }
29
+
30
+ @Test
31
+ fun `no masking without keep-alive`() {
32
+ assertEquals(MediaPlayer.STATE_IDLE, view(engineFailed = true, keepAlive = false))
33
+ assertEquals(MediaPlayer.STATE_IDLE, view(state = PlaybackState.ERROR, keepAlive = false))
34
+ }
35
+
36
+ @Test
37
+ fun `no masking in other states or outside idle`() {
38
+ assertEquals(MediaPlayer.STATE_IDLE, view(state = PlaybackState.STOPPED))
39
+ assertEquals(MediaPlayer.STATE_IDLE, view(state = PlaybackState.STOPPED, engineFailed = true))
40
+ assertEquals(MediaPlayer.STATE_IDLE, view(state = PlaybackState.NONE))
41
+ assertEquals(
42
+ MediaPlayer.STATE_BUFFERING,
43
+ view(engineState = MediaPlayer.STATE_BUFFERING, engineFailed = true),
44
+ )
45
+ }
46
+
47
+ @Test
48
+ fun `an idle engine read as anything else isn't about to play`() {
49
+ assertEquals(
50
+ false,
51
+ SessionView.playWhenReady(true, MediaPlayer.STATE_IDLE, MediaPlayer.STATE_READY),
52
+ )
53
+ assertEquals(
54
+ true,
55
+ SessionView.playWhenReady(true, MediaPlayer.STATE_IDLE, MediaPlayer.STATE_IDLE),
56
+ )
57
+ assertEquals(
58
+ true,
59
+ SessionView.playWhenReady(true, MediaPlayer.STATE_READY, MediaPlayer.STATE_READY),
60
+ )
61
+ }
62
+ }