@@ -2,12 +2,21 @@ package com.adamratzman.spotify.priv
2
2
3
3
import com.adamratzman.spotify.SpotifyClientApi
4
4
import com.adamratzman.spotify.buildSpotifyApi
5
+ import com.adamratzman.spotify.endpoints.client.ClientPlayerApi
6
+ import com.adamratzman.spotify.models.CollectionUri
7
+ import com.adamratzman.spotify.models.PlayHistoryContext
8
+ import com.adamratzman.spotify.models.PlayableUri
5
9
import com.adamratzman.spotify.models.SpotifyTrackUri
6
- import com.adamratzman.spotify.models.Track
7
10
import com.adamratzman.spotify.runBlockingTest
11
+ import kotlinx.coroutines.delay
8
12
import kotlin.test.Test
13
+ import kotlin.test.assertEquals
14
+ import kotlin.test.assertNotNull
9
15
import kotlin.test.assertTrue
16
+ import kotlin.time.ExperimentalTime
17
+ import kotlin.time.measureTime
10
18
19
+ @ExperimentalTime
11
20
class ClientPlayerApiTest {
12
21
lateinit var api: SpotifyClientApi
13
22
@@ -37,32 +46,126 @@ class ClientPlayerApiTest {
37
46
playableUrisToPlay = listOf (SpotifyTrackUri (" spotify:track:6WcinC5nKan2DMFUfjVerX" )),
38
47
deviceId = device.id
39
48
)
49
+ delay(1000 )
40
50
val getCurrentContext = suspend { api.player.getCurrentContext() }
41
51
var context = getCurrentContext()
42
52
assertTrue(context != null && context.isPlaying && context.track?.id == " 6WcinC5nKan2DMFUfjVerX" )
43
53
api.player.pause()
44
54
context = getCurrentContext()!!
45
55
46
- assertTrue(! context.isPlaying && context.track?.id != null )
56
+ assertTrue(! context.isPlaying)
57
+ assertNotNull(context.track?.id)
47
58
48
59
val playlist = api.playlists.getPlaylist(" 37i9dQZF1DXcBWIGoYBM5M" )!!
49
60
api.player.startPlayback(
50
61
collectionUri = playlist.uri
51
62
)
63
+ delay(1000 )
52
64
context = getCurrentContext()
53
65
assertTrue(context != null && context.isPlaying && context.track?.id == playlist.tracks.items.first().track!! .id)
54
66
api.player.pause()
67
+ }
68
+ }
69
+
70
+ @Test
71
+ fun testGetRecentlyPlayed () {
72
+ runBlockingTest {
73
+ if (! testPrereq()) return @runBlockingTest
74
+ api.player.getRecentlyPlayed()
75
+ }
76
+ }
55
77
78
+ @Test
79
+ fun testGetCurrentlyPlaying () {
80
+ runBlockingTest {
81
+ if (! testPrereq()) return @runBlockingTest
82
+ val device = api.player.getDevices().first()
56
83
84
+ val trackId = " 7lPN2DXiMsVn7XUKtOW1CS"
85
+ api.player.startPlayback(
86
+ playableUrisToPlay = listOf (PlayableUri (" spotify:track:$trackId " )),
87
+ deviceId = device.id
88
+ )
89
+ delay(1000 )
90
+ val currentlyPlayingObjectTrack = api.player.getCurrentlyPlaying()
91
+ assertNotNull(currentlyPlayingObjectTrack)
92
+ assertTrue(currentlyPlayingObjectTrack.isPlaying && currentlyPlayingObjectTrack.context == null )
93
+
94
+ val playlistId = " 3DhwYIoAZ8mXlxiBkCuOx7"
95
+ api.player.startPlayback(collectionUri = CollectionUri (" spotify:playlist:3DhwYIoAZ8mXlxiBkCuOx7" ))
96
+ delay(1000 )
97
+ val currentlyPlayingObjectPlaylist = api.player.getCurrentlyPlaying()
98
+ assertNotNull(currentlyPlayingObjectPlaylist)
99
+ assertTrue(currentlyPlayingObjectPlaylist.isPlaying)
100
+ assertEquals(playlistId, currentlyPlayingObjectPlaylist.context?.uri?.id)
101
+ assertEquals(PlayHistoryContext .ContextType .PLAYLIST , currentlyPlayingObjectPlaylist.context?.type)
102
+
103
+ api.player.pause()
57
104
}
58
105
}
59
106
60
107
@Test
61
- fun testGetRecentlyPlayed () {
108
+ fun testSeek () {
62
109
runBlockingTest {
63
110
if (! testPrereq()) return @runBlockingTest
64
111
val device = api.player.getDevices().first()
65
- api.player.getRecentlyPlayed()
112
+
113
+ val trackId = " 7lPN2DXiMsVn7XUKtOW1CS"
114
+ val track = api.tracks.getTrack(trackId)!!
115
+ api.player.startPlayback(
116
+ playableUrisToPlay = listOf (PlayableUri (" spotify:track:$trackId " )),
117
+ deviceId = device.id
118
+ )
119
+ api.player.pause()
120
+
121
+ val skipTo = track.length / 2
122
+ val delay = measureTime {
123
+ api.player.seek(skipTo.toLong())
124
+ api.player.resume()
125
+ }.inMilliseconds
126
+
127
+ val waitTime = 3000
128
+ delay(waitTime.toLong())
129
+ assertTrue(api.player.getCurrentlyPlaying()!! .progressMs!! >= waitTime - delay)
130
+ api.player.skipForward()
131
+ }
132
+ }
133
+
134
+ @Test
135
+ fun testSetPlaybackOptions () {
136
+ runBlockingTest {
137
+ if (! testPrereq()) return @runBlockingTest
138
+ val device = api.player.getDevices().first()
139
+ api.player.setRepeatMode(ClientPlayerApi .PlayerRepeatState .OFF , device.id)
140
+ api.player.setVolume(50 , device.id)
141
+ val context = api.player.getCurrentContext()!!
142
+ assertEquals(ClientPlayerApi .PlayerRepeatState .OFF , context.repeatState)
143
+
144
+ }
145
+ }
146
+
147
+ @Test
148
+ fun testSkipForwardBackward () {
149
+ runBlockingTest {
150
+ if (! testPrereq()) return @runBlockingTest
151
+ val device = api.player.getDevices().first()
152
+
153
+ val playlist = api.playlists.getPlaylist(" 37i9dQZF1DXcBWIGoYBM5M" )!!
154
+ api.player.startPlayback(
155
+ collectionUri = playlist.uri,
156
+ deviceId = device.id
157
+ )
158
+ delay(1000 )
159
+
160
+ api.player.skipForward()
161
+ delay(500 )
162
+ assertEquals(playlist.tracks[1 ].track!! .id, api.player.getCurrentlyPlaying()!! .track.id)
163
+
164
+ api.player.skipBehind()
165
+ delay(500 )
166
+ assertEquals(playlist.tracks[0 ].track!! .id, api.player.getCurrentlyPlaying()!! .track.id)
167
+
168
+ api.player.pause()
66
169
}
67
170
}
68
171
0 commit comments