react-native-audio-browser 0.2.0-next.86 → 0.2.0-next.88

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.
@@ -17,11 +17,10 @@ import androidx.media3.exoplayer.upstream.DefaultAllocator
17
17
  import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter
18
18
  import com.audiobrowser.TestFixtures.track
19
19
  import com.audiobrowser.model.RetryPolicy
20
+ import com.audiobrowser.silentWavBytes
20
21
  import com.audiobrowser.util.TrackFactory
21
22
  import java.io.IOException
22
23
  import java.net.ServerSocket
23
- import java.nio.ByteBuffer
24
- import java.nio.ByteOrder
25
24
  import java.time.Duration
26
25
  import java.util.concurrent.atomic.AtomicInteger
27
26
  import kotlin.concurrent.thread
@@ -97,7 +96,11 @@ class LiveStreamEndTest {
97
96
 
98
97
  @Test
99
98
  fun `a live stream whose container declares its end ends there`() {
100
- serve(framesFor = { 0 }, contentType = "audio/wav", body = { silentWav() })
99
+ serve(
100
+ framesFor = { 0 },
101
+ contentType = "audio/wav",
102
+ body = { silentWavBytes(Duration.ofSeconds(1)) },
103
+ )
101
104
 
102
105
  val outcome = play(live = true, RetryPolicy.Infinite())
103
106
 
@@ -287,27 +290,6 @@ class LiveStreamEndTest {
287
290
  const val SECONDS_1_5 = 57
288
291
  const val SECONDS_60 = 2_297
289
292
 
290
- /** A WAV of a second of 8 kHz mono 8-bit silence, its data size in its header. */
291
- fun silentWav(): ByteArray {
292
- val samples = 8_000
293
- val header =
294
- ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
295
- put("RIFF".toByteArray())
296
- putInt(36 + samples)
297
- put("WAVEfmt ".toByteArray())
298
- putInt(16)
299
- putShort(1)
300
- putShort(1)
301
- putInt(8_000)
302
- putInt(8_000)
303
- putShort(1)
304
- putShort(8)
305
- put("data".toByteArray())
306
- putInt(samples)
307
- }
308
- return header.array() + ByteArray(samples) { 0x80.toByte() }
309
- }
310
-
311
293
  /** [frames] ADTS frames of AAC LC at 44.1 kHz, mono, with 100 zero bytes of payload each. */
312
294
  fun silentAdts(frames: Int): ByteArray {
313
295
  val length = 7 + 100
@@ -4,7 +4,6 @@ import android.content.Context
4
4
  import android.media.AudioManager
5
5
  import android.net.ConnectivityManager
6
6
  import android.net.NetworkCapabilities
7
- import android.net.Uri
8
7
  import android.os.Looper
9
8
  import androidx.media3.common.C
10
9
  import androidx.media3.common.Metadata
@@ -17,15 +16,14 @@ import com.audiobrowser.TestFixtures
17
16
  import com.audiobrowser.goOnline
18
17
  import com.audiobrowser.model.PlayerSetupOptions
19
18
  import com.audiobrowser.model.RetryPolicy
19
+ import com.audiobrowser.silentWavBytes
20
+ import com.audiobrowser.silentWavUri
20
21
  import com.audiobrowser.util.TrackFactory
21
22
  import com.margelo.nitro.audiobrowser.AndroidPlayerWakeMode
22
23
  import com.margelo.nitro.audiobrowser.Playback
23
24
  import com.margelo.nitro.audiobrowser.PlaybackState
24
- import java.io.File
25
25
  import java.io.IOException
26
26
  import java.net.ServerSocket
27
- import java.nio.ByteBuffer
28
- import java.nio.ByteOrder
29
27
  import java.time.Duration
30
28
  import java.util.concurrent.CopyOnWriteArrayList
31
29
  import java.util.concurrent.CountDownLatch
@@ -85,7 +83,7 @@ class PlayerRetryTest {
85
83
 
86
84
  @Test
87
85
  fun `a terminal error ends the load`() {
88
- play(Uri.fromFile(silentWav()).toString())
86
+ play(folder.silentWavUri(Duration.ofMinutes(1)))
89
87
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
90
88
 
91
89
  player.playerListener.onPlayerError(
@@ -170,7 +168,7 @@ class PlayerRetryTest {
170
168
 
171
169
  @Test
172
170
  fun `removing a failed current track plays the next`() {
173
- failTerminally(Uri.fromFile(silentWav()).toString())
171
+ failTerminally(folder.silentWavUri(Duration.ofMinutes(1)))
174
172
 
175
173
  player.remove(listOf(0))
176
174
 
@@ -179,7 +177,7 @@ class PlayerRetryTest {
179
177
 
180
178
  @Test
181
179
  fun `removing another track keeps a terminal error`() {
182
- failTerminally(Uri.fromFile(silentWav()).toString())
180
+ failTerminally(folder.silentWavUri(Duration.ofMinutes(1)))
183
181
 
184
182
  player.remove(listOf(1))
185
183
  shadowOf(Looper.getMainLooper()).idle()
@@ -190,7 +188,7 @@ class PlayerRetryTest {
190
188
 
191
189
  @Test
192
190
  fun `removing the current track after a cancelled retry stays idle and paused`() {
193
- pauseDuringRetry(Uri.fromFile(silentWav()).toString()) { player.pause() }
191
+ pauseDuringRetry(folder.silentWavUri(Duration.ofMinutes(1))) { player.pause() }
194
192
 
195
193
  player.remove(listOf(0))
196
194
  shadowOf(Looper.getMainLooper()).idle()
@@ -202,7 +200,7 @@ class PlayerRetryTest {
202
200
 
203
201
  @Test
204
202
  fun `removing a failed current track while paused is paused`() {
205
- failTerminally(Uri.fromFile(silentWav()).toString())
203
+ failTerminally(folder.silentWavUri(Duration.ofMinutes(1)))
206
204
  player.pause()
207
205
  shadowOf(Looper.getMainLooper()).idle()
208
206
 
@@ -217,7 +215,7 @@ class PlayerRetryTest {
217
215
  fun `adding a track after a cancelled retry stays idle and paused`() {
218
216
  pauseDuringRetry { player.pause() }
219
217
 
220
- player.add(TestFixtures.track(src = Uri.fromFile(silentWav()).toString()))
218
+ player.add(TestFixtures.track(src = folder.silentWavUri(Duration.ofMinutes(1))))
221
219
  shadowOf(Looper.getMainLooper()).idle()
222
220
 
223
221
  assertEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
@@ -228,7 +226,7 @@ class PlayerRetryTest {
228
226
  fun `adding to an empty queue loads`() {
229
227
  player.setup(PlayerSetupOptions())
230
228
 
231
- player.add(TestFixtures.track(src = Uri.fromFile(silentWav()).toString()))
229
+ player.add(TestFixtures.track(src = folder.silentWavUri(Duration.ofMinutes(1))))
232
230
  shadowOf(Looper.getMainLooper()).idle()
233
231
 
234
232
  assertNotEquals(MediaPlayer.STATE_IDLE, player.exoPlayer.playbackState)
@@ -276,7 +274,7 @@ class PlayerRetryTest {
276
274
  @Test
277
275
  fun `a load failing while paused stays paused and the next play reloads`() {
278
276
  val server = ServerSocket(0)
279
- val wav = silentWav().readBytes()
277
+ val wav = silentWavBytes(Duration.ofMinutes(1))
280
278
  val sent = CountDownLatch(1)
281
279
  val drop = CountDownLatch(1)
282
280
  // Serves five seconds of the minute, holds the connection until the drop, then refuses.
@@ -379,7 +377,7 @@ class PlayerRetryTest {
379
377
  @Test
380
378
  fun `a track change on a stopped engine starts no Load until it is prepared`() {
381
379
  goOnline(context)
382
- val wav = Uri.fromFile(silentWav()).toString()
380
+ val wav = folder.silentWavUri(Duration.ofMinutes(1))
383
381
  play(wav, wav, retryPolicy = RetryPolicy.Infinite())
384
382
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
385
383
  player.exoPlayer.stop()
@@ -491,7 +489,7 @@ class PlayerRetryTest {
491
489
  @Test
492
490
  fun `a failure report that arrives after a reconnect is not the new connection's`() {
493
491
  goOnline(context)
494
- play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
492
+ play(folder.silentWavUri(Duration.ofMinutes(1)), retryPolicy = RetryPolicy.Infinite())
495
493
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
496
494
 
497
495
  // Posted from the playback thread before the go-live reconnect, run after it.
@@ -506,7 +504,7 @@ class PlayerRetryTest {
506
504
  @Test
507
505
  fun `with retry, a stuck engine reconnects and never ends in an error`() {
508
506
  goOnline(context)
509
- play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
507
+ play(folder.silentWavUri(Duration.ofMinutes(1)), retryPolicy = RetryPolicy.Infinite())
510
508
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
511
509
 
512
510
  // As media3 leaves a stuck engine: idle, with the error.
@@ -522,7 +520,7 @@ class PlayerRetryTest {
522
520
  @Test
523
521
  fun `a title after a reconnect inside the retry loop shows`() {
524
522
  goOnline(context)
525
- play(Uri.fromFile(silentWav()).toString(), retryPolicy = RetryPolicy.Infinite())
523
+ play(folder.silentWavUri(Duration.ofMinutes(1)), retryPolicy = RetryPolicy.Infinite())
526
524
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
527
525
  player.exoPlayer.stop()
528
526
  player.playerListener.onPlayerError(stuck())
@@ -536,7 +534,7 @@ class PlayerRetryTest {
536
534
 
537
535
  @Test
538
536
  fun `with retry off, a stuck engine ends in the stalled error`() {
539
- play(Uri.fromFile(silentWav()).toString())
537
+ play(folder.silentWavUri(Duration.ofMinutes(1)))
540
538
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
541
539
 
542
540
  player.playerListener.onPlayerError(stuck())
@@ -606,27 +604,4 @@ class PlayerRetryTest {
606
604
  shadowOf(Looper.getMainLooper()).idle()
607
605
  }
608
606
  }
609
-
610
- /** A minute of 8 kHz mono 8-bit silence, long enough that no test reaches its end. */
611
- private fun silentWav(): File {
612
- val samples = 8_000 * 60
613
- val header =
614
- ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
615
- put("RIFF".toByteArray())
616
- putInt(36 + samples)
617
- put("WAVEfmt ".toByteArray())
618
- putInt(16)
619
- putShort(1)
620
- putShort(1)
621
- putInt(8_000)
622
- putInt(8_000)
623
- putShort(1)
624
- putShort(8)
625
- put("data".toByteArray())
626
- putInt(samples)
627
- }
628
- return folder.newFile("silence.wav").apply {
629
- writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
630
- }
631
- }
632
607
  }
@@ -1,6 +1,5 @@
1
1
  package com.audiobrowser.player
2
2
 
3
- import android.net.Uri
4
3
  import android.os.Looper
5
4
  import androidx.media3.common.C
6
5
  import androidx.media3.common.Player as MediaPlayer
@@ -8,12 +7,10 @@ import androidx.media3.common.util.Util
8
7
  import com.audiobrowser.Callbacks
9
8
  import com.audiobrowser.TestFixtures
10
9
  import com.audiobrowser.model.PlayerSetupOptions
10
+ import com.audiobrowser.silentWavUri
11
11
  import com.audiobrowser.util.TrackFactory
12
12
  import com.margelo.nitro.audiobrowser.Playback
13
13
  import com.margelo.nitro.audiobrowser.PlaybackState
14
- import java.io.File
15
- import java.nio.ByteBuffer
16
- import java.nio.ByteOrder
17
14
  import java.time.Duration
18
15
  import java.util.concurrent.TimeUnit
19
16
  import org.junit.After
@@ -152,7 +149,7 @@ class PlayerSetupEndedTest {
152
149
  player.setup(PlayerSetupOptions())
153
150
  player.setCallbacks(callbacks)
154
151
  disableAudio()
155
- val src = Uri.fromFile(silentWav()).toString()
152
+ val src = folder.silentWavUri(Duration.ofMillis(500))
156
153
  player.exoPlayer.setMediaItems(
157
154
  List(tracks) { TrackFactory.toMedia3(TestFixtures.track(title = "$it", src = src)) }
158
155
  )
@@ -195,27 +192,4 @@ class PlayerSetupEndedTest {
195
192
  shadowOf(Looper.getMainLooper()).idle()
196
193
  }
197
194
  }
198
-
199
- /** Half a second of 8 kHz mono 8-bit silence. */
200
- private fun silentWav(): File {
201
- val samples = 4_000
202
- val header =
203
- ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
204
- put("RIFF".toByteArray())
205
- putInt(36 + samples)
206
- put("WAVEfmt ".toByteArray())
207
- putInt(16)
208
- putShort(1)
209
- putShort(1)
210
- putInt(8_000)
211
- putInt(8_000)
212
- putShort(1)
213
- putShort(8)
214
- put("data".toByteArray())
215
- putInt(samples)
216
- }
217
- return folder.newFile("silence.wav").apply {
218
- writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
219
- }
220
- }
221
195
  }
@@ -2,7 +2,6 @@ package com.audiobrowser.player
2
2
 
3
3
  import android.content.Context
4
4
  import android.media.AudioManager
5
- import android.net.Uri
6
5
  import android.os.Looper
7
6
  import androidx.media3.common.C
8
7
  import androidx.media3.common.Player as MediaPlayer
@@ -10,14 +9,13 @@ import com.audiobrowser.Callbacks
10
9
  import com.audiobrowser.TestFixtures
11
10
  import com.audiobrowser.model.PlayerSetupOptions
12
11
  import com.audiobrowser.remote.FakeRemote
12
+ import com.audiobrowser.silentWavUri
13
13
  import com.audiobrowser.util.TrackFactory
14
14
  import com.margelo.nitro.audiobrowser.Playback
15
15
  import com.margelo.nitro.audiobrowser.PlaybackState
16
16
  import com.margelo.nitro.audiobrowser.PlayingState
17
17
  import com.margelo.nitro.audiobrowser.RemoteOutputsOptions
18
- import java.io.File
19
- import java.nio.ByteBuffer
20
- import java.nio.ByteOrder
18
+ import java.time.Duration
21
19
  import org.junit.After
22
20
  import org.junit.Assert.assertEquals
23
21
  import org.junit.Assert.assertFalse
@@ -65,7 +63,7 @@ class PlayerSuppressionTest {
65
63
  fun play() {
66
64
  player.setup(PlayerSetupOptions())
67
65
  player.setCallbacks(callbacks)
68
- val src = Uri.fromFile(silentWav()).toString()
66
+ val src = folder.silentWavUri(Duration.ofMinutes(1))
69
67
  player.exoPlayer.trackSelectionParameters =
70
68
  player.exoPlayer.trackSelectionParameters
71
69
  .buildUpon()
@@ -353,27 +351,4 @@ class PlayerSuppressionTest {
353
351
  shadowOf(Looper.getMainLooper()).idle()
354
352
  }
355
353
  }
356
-
357
- /** A minute of 8 kHz mono 8-bit silence, long enough that no test reaches its end. */
358
- private fun silentWav(): File {
359
- val samples = 8_000 * 60
360
- val header =
361
- ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
362
- put("RIFF".toByteArray())
363
- putInt(36 + samples)
364
- put("WAVEfmt ".toByteArray())
365
- putInt(16)
366
- putShort(1)
367
- putShort(1)
368
- putInt(8_000)
369
- putInt(8_000)
370
- putShort(1)
371
- putShort(8)
372
- put("data".toByteArray())
373
- putInt(samples)
374
- }
375
- return folder.newFile("silence.wav").apply {
376
- writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
377
- }
378
- }
379
354
  }
@@ -1,6 +1,5 @@
1
1
  package com.audiobrowser.player
2
2
 
3
- import android.net.Uri
4
3
  import android.os.Looper
5
4
  import androidx.media3.common.C
6
5
  import androidx.media3.common.Player as MediaPlayer
@@ -10,12 +9,10 @@ import com.audiobrowser.TestFixtures
10
9
  import com.audiobrowser.goOnline
11
10
  import com.audiobrowser.model.PlayerSetupOptions
12
11
  import com.audiobrowser.model.RetryPolicy
12
+ import com.audiobrowser.silentWavUri
13
13
  import com.audiobrowser.util.TrackFactory
14
14
  import com.margelo.nitro.audiobrowser.Playback
15
15
  import com.margelo.nitro.audiobrowser.PlaybackState
16
- import java.io.File
17
- import java.nio.ByteBuffer
18
- import java.nio.ByteOrder
19
16
  import java.time.Duration
20
17
  import org.junit.After
21
18
  import org.junit.Assert.assertEquals
@@ -55,7 +52,7 @@ class SessionPlayTest {
55
52
 
56
53
  @Test
57
54
  fun `an app's play handler takes a session play once and nothing plays`() {
58
- load(Uri.fromFile(silentWav()).toString())
55
+ load(folder.silentWavUri(Duration.ofMillis(500)))
59
56
  player.stop()
60
57
  `when`(callbacks.handleRemotePlay()).thenReturn(true)
61
58
 
@@ -70,7 +67,7 @@ class SessionPlayTest {
70
67
 
71
68
  @Test
72
69
  fun `a session play after a stop loads once and plays`() {
73
- load(Uri.fromFile(silentWav()).toString())
70
+ load(folder.silentWavUri(Duration.ofMillis(500)))
74
71
  player.stop()
75
72
  val before = states().size
76
73
 
@@ -82,7 +79,7 @@ class SessionPlayTest {
82
79
 
83
80
  @Test
84
81
  fun `setPlayWhenReady(true) after a stop loads`() {
85
- load(Uri.fromFile(silentWav()).toString())
82
+ load(folder.silentWavUri(Duration.ofMillis(500)))
86
83
  player.stop()
87
84
 
88
85
  // As JS setPlayWhenReady(true) does: the setter alone.
@@ -106,7 +103,7 @@ class SessionPlayTest {
106
103
 
107
104
  @Test
108
105
  fun `a setup's raised intent over a stop loads nothing`() {
109
- load(Uri.fromFile(silentWav()).toString())
106
+ load(folder.silentWavUri(Duration.ofMillis(500)))
110
107
  player.stop()
111
108
 
112
109
  val before = states().size
@@ -168,7 +165,7 @@ class SessionPlayTest {
168
165
  .build()
169
166
  player.applySetupPlayWhenReady(true)
170
167
 
171
- player.setQueue(arrayOf(TestFixtures.track(src = Uri.fromFile(silentWav()).toString())))
168
+ player.setQueue(arrayOf(TestFixtures.track(src = folder.silentWavUri(Duration.ofMillis(500)))))
172
169
 
173
170
  idleUntil("playing") { player.playbackState == PlaybackState.PLAYING }
174
171
  }
@@ -212,27 +209,4 @@ class SessionPlayTest {
212
209
  shadowOf(Looper.getMainLooper()).idle()
213
210
  }
214
211
  }
215
-
216
- /** Half a second of 8 kHz mono 8-bit silence. */
217
- private fun silentWav(): File {
218
- val samples = 4_000
219
- val header =
220
- ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
221
- put("RIFF".toByteArray())
222
- putInt(36 + samples)
223
- put("WAVEfmt ".toByteArray())
224
- putInt(16)
225
- putShort(1)
226
- putShort(1)
227
- putInt(8_000)
228
- putInt(8_000)
229
- putShort(1)
230
- putShort(8)
231
- put("data".toByteArray())
232
- putInt(samples)
233
- }
234
- return folder.newFile("silence.wav").apply {
235
- writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
236
- }
237
- }
238
212
  }
@@ -1,10 +1,51 @@
1
1
  package com.audiobrowser.player
2
2
 
3
+ import androidx.media3.common.C
4
+ import androidx.media3.common.MediaItem
3
5
  import androidx.media3.common.Player as MediaPlayer
6
+ import androidx.media3.common.Timeline
7
+ import androidx.media3.exoplayer.source.ForwardingTimeline
8
+ import androidx.media3.exoplayer.source.SinglePeriodTimeline
4
9
  import org.junit.Assert.assertEquals
5
10
  import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+ import org.mockito.Mockito.mock
13
+ import org.mockito.Mockito.`when`
14
+ import org.robolectric.RobolectricTestRunner
6
15
 
16
+ @RunWith(RobolectricTestRunner::class)
7
17
  class SessionViewListenerTest {
18
+
19
+ private val view = mock(MediaPlayer::class.java)
20
+ private val told = mutableListOf<String>()
21
+ private val target =
22
+ object : MediaPlayer.Listener {
23
+ override fun onPlaybackStateChanged(playbackState: Int) {
24
+ told += "state $playbackState"
25
+ }
26
+
27
+ override fun onIsPlayingChanged(isPlaying: Boolean) {
28
+ told += "playing $isPlaying"
29
+ }
30
+
31
+ override fun onTimelineChanged(timeline: Timeline, reason: Int) {
32
+ told += "timeline"
33
+ }
34
+
35
+ override fun onPositionDiscontinuity(
36
+ oldPosition: MediaPlayer.PositionInfo,
37
+ newPosition: MediaPlayer.PositionInfo,
38
+ reason: Int,
39
+ ) {
40
+ told += "position ${newPosition.positionMs}"
41
+ }
42
+ }
43
+
44
+ private fun listener(): SessionViewListener {
45
+ `when`(view.currentTimeline).thenReturn(Timeline.EMPTY)
46
+ return SessionViewListener(target, view)
47
+ }
48
+
8
49
  @Test
9
50
  fun `forwards every callback media3's listener declares`() {
10
51
  val declared =
@@ -18,4 +59,167 @@ class SessionViewListenerTest {
18
59
 
19
60
  assertEquals(emptyList<Any>(), declared.filterNot { it in overridden })
20
61
  }
62
+
63
+ @Test
64
+ fun `tells the view's state once each time it moves`() {
65
+ `when`(view.playbackState).thenReturn(MediaPlayer.STATE_BUFFERING)
66
+ val listener = listener()
67
+
68
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_IDLE)
69
+ `when`(view.playbackState).thenReturn(MediaPlayer.STATE_READY)
70
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_READY)
71
+ listener.onPlaybackStateChanged(MediaPlayer.STATE_READY)
72
+
73
+ assertEquals(listOf("state ${MediaPlayer.STATE_READY}"), told)
74
+ }
75
+
76
+ @Test
77
+ fun `tells the view's playing once each time it moves`() {
78
+ val listener = listener()
79
+
80
+ listener.onIsPlayingChanged(true)
81
+ `when`(view.isPlaying).thenReturn(true)
82
+ listener.onIsPlayingChanged(true)
83
+ listener.onIsPlayingChanged(true)
84
+
85
+ assertEquals(listOf("playing true"), told)
86
+ }
87
+
88
+ @Test
89
+ fun `tells a timeline that changes, and not its period turning placeholder and back`() {
90
+ val listener = listener()
91
+ val a = timeline("a", durationUs = 60_000_000)
92
+
93
+ listener.onTimelineChanged(a, MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED)
94
+ listener.onTimelineChanged(
95
+ PlaceholderPeriods(a),
96
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
97
+ )
98
+ listener.onTimelineChanged(
99
+ timeline("a", durationUs = 60_000_000),
100
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
101
+ )
102
+ assertEquals(listOf("timeline"), told)
103
+
104
+ listener.onTimelineChanged(
105
+ PeriodAt(a, positionInWindowUs = -1_000_000),
106
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
107
+ )
108
+ listener.onTimelineChanged(
109
+ timeline("a", durationUs = 30_000_000),
110
+ MediaPlayer.TIMELINE_CHANGE_REASON_SOURCE_UPDATE,
111
+ )
112
+ listener.onTimelineChanged(
113
+ timeline("b", durationUs = 30_000_000),
114
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
115
+ )
116
+ listener.onTimelineChanged(Timeline.EMPTY, MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED)
117
+ assertEquals(List(5) { "timeline" }, told)
118
+ }
119
+
120
+ @Test
121
+ fun `tells a queue whose shuffle order changes`() {
122
+ val listener = listener()
123
+
124
+ listener.onTimelineChanged(
125
+ TwoItems(shuffled = false),
126
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
127
+ )
128
+ listener.onTimelineChanged(
129
+ TwoItems(shuffled = true),
130
+ MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
131
+ )
132
+
133
+ assertEquals(listOf("timeline", "timeline"), told)
134
+ }
135
+
136
+ @Test
137
+ fun `tells a position that moves, and not a seek to where it already is`() {
138
+ val listener = listener()
139
+
140
+ listener.onPositionDiscontinuity(
141
+ position(120_000),
142
+ position(0),
143
+ MediaPlayer.DISCONTINUITY_REASON_SEEK,
144
+ )
145
+ listener.onPositionDiscontinuity(
146
+ position(0),
147
+ position(0),
148
+ MediaPlayer.DISCONTINUITY_REASON_SEEK,
149
+ )
150
+
151
+ assertEquals(listOf("position 0"), told)
152
+ }
153
+
154
+ private fun position(positionMs: Long) =
155
+ MediaPlayer.PositionInfo(UID, 0, UID, 0, positionMs, positionMs, C.INDEX_UNSET, C.INDEX_UNSET)
156
+
157
+ private fun timeline(mediaId: String, durationUs: Long): Timeline =
158
+ SinglePeriodTimeline(
159
+ durationUs,
160
+ /* isSeekable= */ true,
161
+ /* isDynamic= */ false,
162
+ /* useLiveConfiguration= */ false,
163
+ /* manifest= */ null,
164
+ MediaItem.Builder().setMediaId(mediaId).build(),
165
+ )
166
+
167
+ private companion object {
168
+ val UID = Any()
169
+ }
170
+
171
+ /** Two items, in reverse when shuffled. */
172
+ private class TwoItems(private val shuffled: Boolean) : Timeline() {
173
+ override fun getWindowCount() = 2
174
+
175
+ override fun getWindow(index: Int, window: Window, defaultPositionProjectionUs: Long) =
176
+ window.set(
177
+ index,
178
+ MediaItem.Builder().setMediaId("$index").build(),
179
+ null,
180
+ C.TIME_UNSET,
181
+ C.TIME_UNSET,
182
+ C.TIME_UNSET,
183
+ true,
184
+ false,
185
+ null,
186
+ 0,
187
+ C.TIME_UNSET,
188
+ index,
189
+ index,
190
+ 0,
191
+ )
192
+
193
+ override fun getPeriodCount() = 2
194
+
195
+ override fun getPeriod(index: Int, period: Period, setIds: Boolean): Period =
196
+ period.set(index, index, index, C.TIME_UNSET, 0)
197
+
198
+ override fun getIndexOfPeriod(uid: Any) = uid as? Int ?: C.INDEX_UNSET
199
+
200
+ override fun getUidOfPeriod(index: Int): Any = index
201
+
202
+ override fun getFirstWindowIndex(shuffleModeEnabled: Boolean) =
203
+ if (shuffleModeEnabled && shuffled) 1 else 0
204
+
205
+ override fun getLastWindowIndex(shuffleModeEnabled: Boolean) =
206
+ if (shuffleModeEnabled && shuffled) 0 else 1
207
+
208
+ override fun getNextWindowIndex(index: Int, repeatMode: Int, shuffleModeEnabled: Boolean) =
209
+ if (index == getLastWindowIndex(shuffleModeEnabled)) C.INDEX_UNSET
210
+ else if (shuffleModeEnabled && shuffled) index - 1 else index + 1
211
+ }
212
+
213
+ private class PeriodAt(timeline: Timeline, private val positionInWindowUs: Long) :
214
+ ForwardingTimeline(timeline) {
215
+ override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
216
+ super.getPeriod(periodIndex, period, setIds).apply {
217
+ positionInWindowUs = this@PeriodAt.positionInWindowUs
218
+ }
219
+ }
220
+
221
+ private class PlaceholderPeriods(timeline: Timeline) : ForwardingTimeline(timeline) {
222
+ override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
223
+ super.getPeriod(periodIndex, period, setIds).apply { isPlaceholder = true }
224
+ }
21
225
  }