summaryrefslogtreecommitdiff
path: root/internal/gui/audio_player_test.go
blob: 2dd3b0ca0020cdcbdd74f8efd98a073aef23f74f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gui

import (
	"errors"
	"path/filepath"
	"reflect"
	"testing"
)

func TestLinuxAudioCommandCandidates(t *testing.T) {
	t.Run("mp3 prefers mpg123", func(t *testing.T) {
		audioFile := "/tmp/audio.mp3"
		got := linuxAudioCommandCandidates(audioFile)
		want := []audioCommandCandidate{
			{name: "mpg123", args: []string{"-q", audioFile}},
			{name: "ffplay", args: []string{"-nodisp", "-autoexit", "-loglevel", "quiet", audioFile}},
			{name: "play", args: []string{"-q", audioFile}},
			{name: "paplay", args: []string{audioFile}},
			{name: "aplay", args: []string{"-q", audioFile}},
		}
		if !reflect.DeepEqual(got, want) {
			t.Fatalf("linuxAudioCommandCandidates(mp3) mismatch\nwant: %#v\ngot:  %#v", want, got)
		}
	})

	t.Run("wav avoids mpg123", func(t *testing.T) {
		audioFile := "/tmp/audio.wav"
		got := linuxAudioCommandCandidates(audioFile)
		if got[0].name != "ffplay" {
			t.Fatalf("first wav candidate = %q, want %q", got[0].name, "ffplay")
		}
		for _, candidate := range got {
			if candidate.name == "mpg123" {
				t.Fatalf("wav candidates unexpectedly include mpg123: %#v", got)
			}
		}
	})
}

func TestLinuxAudioPlaybackCommandUsesFormatCompatiblePlayer(t *testing.T) {
	audioFile := "/tmp/audio.wav"
	cmd, err := linuxAudioPlaybackCommand(audioFile, func(name string) (string, error) {
		switch name {
		case "ffplay":
			return filepath.Join("/usr/bin", name), nil
		default:
			return "", errors.New("not found")
		}
	})
	if err != nil {
		t.Fatalf("linuxAudioPlaybackCommand() unexpected error: %v", err)
	}

	if got, want := filepath.Base(cmd.Path), "ffplay"; got != want {
		t.Fatalf("command path base = %q, want %q", got, want)
	}
	if len(cmd.Args) < 2 || cmd.Args[len(cmd.Args)-1] != audioFile {
		t.Fatalf("command args = %#v, want final arg %q", cmd.Args, audioFile)
	}
}