react-native-audio-browser 0.2.0-next.75 → 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.
Files changed (24) hide show
  1. package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +19 -26
  2. package/android/src/main/java/com/audiobrowser/Service.kt +3 -0
  3. package/android/src/main/java/com/audiobrowser/browser/BrowserManager.kt +16 -8
  4. package/android/src/main/java/com/audiobrowser/browser/TrackLoadHandler.kt +5 -3
  5. package/android/src/main/java/com/audiobrowser/http/RequestConfigBuilder.kt +10 -2
  6. package/android/src/main/java/com/audiobrowser/player/NowPlayingUpdater.kt +3 -0
  7. package/android/src/main/java/com/audiobrowser/player/Player.kt +7 -1
  8. package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +32 -11
  9. package/android/src/main/java/com/audiobrowser/util/AwaitCancellable.kt +45 -0
  10. package/android/src/test/java/com/audiobrowser/GateDecisionTest.kt +128 -0
  11. package/android/src/test/java/com/audiobrowser/ResolveFormattedErrorTest.kt +93 -0
  12. package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +35 -0
  13. package/android/src/test/java/com/audiobrowser/browser/BrowserManagerLayerResolutionTest.kt +75 -5
  14. package/android/src/test/java/com/audiobrowser/browser/BrowserManagerTest.kt +42 -0
  15. package/android/src/test/java/com/audiobrowser/browser/TrackLoadHandlerTest.kt +43 -0
  16. package/android/src/test/java/com/audiobrowser/http/RequestConfigBuilderTest.kt +62 -0
  17. package/android/src/test/java/com/audiobrowser/player/BrowseTilesTest.kt +31 -32
  18. package/android/src/test/java/com/audiobrowser/player/NowPlayingFormatterTest.kt +75 -0
  19. package/android/src/test/java/com/audiobrowser/player/NowPlayingUpdaterTest.kt +25 -0
  20. package/android/src/test/java/com/audiobrowser/player/PlayFromSearchTest.kt +108 -0
  21. package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +105 -0
  22. package/android/src/test/java/com/audiobrowser/util/AwaitCancellableTest.kt +136 -0
  23. package/android/src/test/java/com/audiobrowser/util/JsPromise.kt +44 -0
  24. package/package.json +2 -1
@@ -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
+ }
@@ -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.coroutines.Continuation
21
- import kotlin.coroutines.resume
22
- import kotlin.coroutines.suspendCoroutine
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 hop keeps the app's answer`() = runTest {
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(backgroundScope, FormattedNavigationError("Nothing here", null)) { custom },
223
+ formattedOrDefault(FormattedNavigationError("Nothing here", null)) { custom },
223
224
  )
224
225
  }
225
226
 
226
227
  @Test
227
- fun `the formatter hop falls back to the default on null and on a throw`() = runTest {
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(backgroundScope, default) { null })
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
  }
@@ -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") }