Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Handle xesam:albumArtist being a string and not an array #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions modules/media/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ func (i *Info) updateMetadata(metadata map[string]dbus.Variant) {
}
i.AlbumArtist = ""
if aArtist, ok := metadata["xesam:albumArtist"]; ok {
artists, _ := aArtist.Value().([]string)
if len(artists) > 0 {
i.AlbumArtist = artists[0]
sArtists, ok := aArtist.Value().([]string)
if !ok {
artists, _ := aArtist.Value().(string)
i.AlbumArtist = artists
} else {
if len(sArtists) > 0 {
i.AlbumArtist = sArtists[0]
}
}
}
i.Album = ""
Expand Down
31 changes: 23 additions & 8 deletions modules/media/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ func TestMedia(t *testing.T) {
var lastInfo Info
pl.RepeatingOutput(func(i Info) bar.Output {
lastInfo = i
artist := i.Artist
if artist == "" {
artist = i.AlbumArtist
}
return outputs.Textf("[%s, %v] %s - %s",
i.PlaybackStatus, i.TruncatedPosition("k"), i.Title, i.Artist)
i.PlaybackStatus, i.TruncatedPosition("k"), i.Title, artist)
})
out = testBar.NextOutput("on output format change")
out.AssertText([]string{"[Playing, 0s] Title - Artist1"})
Expand All @@ -160,16 +164,27 @@ func TestMedia(t *testing.T) {
"xesam:albumArtist": dbus.MakeVariant([]string{"Person1", "Person2"}),
"mpris:length": dbus.MakeVariant(180 * 1000 * 1000),
}, dbusWatcher.SignalTypeInvalidated)
out = testBar.NextOutput("on metadata update")
out.AssertText([]string{"[Playing, 0s] Song - "})

out = testBar.NextOutput("on metadata update, albumArtist array")
out.AssertText([]string{"[Playing, 0s] Song - Person1"})

obj.SetPropertyForTest("Metadata", map[string]dbus.Variant{
"xesam:title": dbus.MakeVariant("Song"),
"mpris:trackid": dbus.MakeVariant("3"),
"xesam:albumArtist": dbus.MakeVariant("Person"),
"mpris:length": dbus.MakeVariant(180 * 1000 * 1000),
}, dbusWatcher.SignalTypeInvalidated)

out = testBar.NextOutput("on metadata update, albumArtist string")
out.AssertText([]string{"[Playing, 0s] Song - Person"})

timing.AdvanceBy(time.Second)
out = testBar.NextOutput("on time passing")
out.AssertText([]string{"[Playing, 1s] Song - "})
out.AssertText([]string{"[Playing, 1s] Song - Person"})

obj.SetPropertyForTest("PlaybackStatus", "Paused", dbusWatcher.SignalTypeChanged)
out = testBar.NextOutput("on pause")
out.AssertText([]string{"[Paused, 1s] Song - "})
out.AssertText([]string{"[Paused, 1s] Song - Person"})

timing.AdvanceBy(time.Second)
testBar.AssertNoOutput("on time passing, but paused")
Expand All @@ -180,13 +195,13 @@ func TestMedia(t *testing.T) {
<-calls, "On scroll with custom output")

obj.Emit("Seeked", 2*1000*1000)
testBar.NextOutput("on seek").AssertText([]string{"[Paused, 2s] Song - "})
testBar.NextOutput("on seek").AssertText([]string{"[Paused, 2s] Song - Person"})

obj.SetPropertyForTest("PlaybackStatus", "Stopped", dbusWatcher.SignalTypeChanged)
testBar.NextOutput("on stop").AssertText([]string{"[Stopped, 0s] Song - "})
testBar.NextOutput("on stop").AssertText([]string{"[Stopped, 0s] Song - Person"})

obj.SetPropertyForTest("PlaybackStatus", "Playing", dbusWatcher.SignalTypeChanged)
testBar.NextOutput("on play").AssertText([]string{"[Playing, 0s] Song - "})
testBar.NextOutput("on play").AssertText([]string{"[Playing, 0s] Song - Person"})

lastInfo.Stop()
require.Equal(t,
Expand Down