react-native-audio-browser 0.2.0-next.46 → 0.2.0-next.47

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.
@@ -213,6 +213,22 @@ class MediaSessionCallback(private val player: Player) :
213
213
  return super.onSetRating(session, controller, rating)
214
214
  }
215
215
 
216
+ /**
217
+ * Where a legacy controller's `setRating` arrives, naming the current item, and a Media3
218
+ * controller's `setRating(mediaId, rating)`, naming any item: only the current one can be rated.
219
+ */
220
+ override fun onSetRating(
221
+ session: MediaSession,
222
+ controller: MediaSession.ControllerInfo,
223
+ mediaId: String,
224
+ rating: Rating,
225
+ ): ListenableFuture<SessionResult> =
226
+ if (mediaId == session.player.currentMediaItem?.mediaId) {
227
+ onSetRating(session, controller, rating)
228
+ } else {
229
+ super.onSetRating(session, controller, mediaId, rating)
230
+ }
231
+
216
232
  @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
217
233
  override fun onGetLibraryRoot(
218
234
  session: MediaLibraryService.MediaLibrarySession,
@@ -0,0 +1,97 @@
1
+ package com.audiobrowser
2
+
3
+ import android.content.ComponentName
4
+ import android.os.Binder
5
+ import android.os.Bundle
6
+ import android.os.Looper
7
+ import androidx.media3.common.HeartRating
8
+ import androidx.media3.session.MediaSession
9
+ import androidx.media3.session.SessionResult
10
+ import org.junit.After
11
+ import org.junit.Assert.assertEquals
12
+ import org.junit.Test
13
+ import org.junit.runner.RunWith
14
+ import org.robolectric.Robolectric
15
+ import org.robolectric.RobolectricTestRunner
16
+ import org.robolectric.RuntimeEnvironment
17
+ import org.robolectric.Shadows.shadowOf
18
+
19
+ /**
20
+ * Media3 hands a legacy controller's `setRating` to the callback that names a media id, with the
21
+ * current item's id. Robolectric cannot route a MediaControllerCompat to the session, so these call
22
+ * that callback as Media3 does.
23
+ */
24
+ @RunWith(RobolectricTestRunner::class)
25
+ class LegacyRatingTest {
26
+
27
+ private val controller = run {
28
+ val application = RuntimeEnvironment.getApplication()
29
+ shadowOf(application)
30
+ .setComponentNameAndServiceForBindService(
31
+ ComponentName(application, HeadlessTaskService::class.java),
32
+ Binder(),
33
+ )
34
+ Robolectric.buildService(Service::class.java).create()
35
+ }
36
+ private val service = controller.get()
37
+ private val player = service.player
38
+ private val legacyController =
39
+ MediaSession.ControllerInfo.createTestOnlyControllerInfo(
40
+ "legacy",
41
+ 0,
42
+ 0,
43
+ MediaSession.ControllerInfo.LEGACY_CONTROLLER_VERSION,
44
+ MediaSession.ControllerInfo.LEGACY_CONTROLLER_INTERFACE_VERSION,
45
+ false,
46
+ Bundle.EMPTY,
47
+ false,
48
+ )
49
+ private val session = service.onGetSession(legacyController)
50
+
51
+ @After
52
+ fun destroy() {
53
+ controller.destroy()
54
+ }
55
+
56
+ private fun idle() = shadowOf(Looper.getMainLooper()).idle()
57
+
58
+ private fun rate(mediaId: String, heart: Boolean): Int {
59
+ val result =
60
+ player
61
+ .getMediaSessionCallback()
62
+ .onSetRating(session, legacyController, mediaId, HeartRating(heart))
63
+ .get()
64
+ idle()
65
+ return result.resultCode
66
+ }
67
+
68
+ private fun currentMediaId() = session.player.currentMediaItem!!.mediaId
69
+
70
+ private fun loadTrack() {
71
+ controller.bind()
72
+ player.load(TestFixtures.track(id = "a", favorited = false))
73
+ idle()
74
+ }
75
+
76
+ @Test
77
+ fun `a rating of the current item sets its favorite and USER_RATING`() {
78
+ loadTrack()
79
+
80
+ assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), true))
81
+ assertEquals(true, player.currentTrack?.favorited)
82
+ assertEquals(HeartRating(true), session.player.mediaMetadata.userRating)
83
+
84
+ assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), false))
85
+ assertEquals(false, player.currentTrack?.favorited)
86
+ assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
87
+ }
88
+
89
+ @Test
90
+ fun `a rating of another item leaves the favorite`() {
91
+ loadTrack()
92
+
93
+ assertEquals(SessionResult.RESULT_ERROR_NOT_SUPPORTED, rate("b", true))
94
+ assertEquals(false, player.currentTrack?.favorited)
95
+ assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
96
+ }
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-audio-browser",
3
- "version": "0.2.0-next.46",
3
+ "version": "0.2.0-next.47",
4
4
  "description": "react-native-audio-browser is a react native package built with Nitro",
5
5
  "keywords": [
6
6
  "react-native",