|
| 1 | +package pulseaudio |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/jfreymuth/pulse/proto" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParseBluetoothCodecs(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + out string |
| 14 | + expected []string |
| 15 | + wantErr bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "server reply", |
| 19 | + out: `[{"name":"sbc","description":"SBC"},{"name":"aptx_hd","description":"aptX HD"}]`, |
| 20 | + expected: []string{"sbc", "aptx_hd"}, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "empty list", |
| 24 | + out: "[]", |
| 25 | + expected: []string{}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "garbage", |
| 29 | + out: "not json", |
| 30 | + wantErr: true, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tt := range tests { |
| 35 | + t.Run(tt.name, func(t *testing.T) { |
| 36 | + got, err := parseBluetoothCodecs(tt.out) |
| 37 | + if (err != nil) != tt.wantErr { |
| 38 | + t.Fatalf("parseBluetoothCodecs() error = %v, wantErr %v", err, tt.wantErr) |
| 39 | + } |
| 40 | + if !tt.wantErr && !reflect.DeepEqual(got, tt.expected) { |
| 41 | + t.Errorf("parseBluetoothCodecs() = %v, want %v", got, tt.expected) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestPickBluetoothCodec(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + available []string |
| 51 | + current string |
| 52 | + expected string |
| 53 | + }{ |
| 54 | + {"best available", []string{"sbc", "aptx", "aptx_hd"}, "sbc", "aptx_hd"}, |
| 55 | + {"fallback to aptx", []string{"sbc", "aptx"}, "sbc", "aptx"}, |
| 56 | + {"nothing better than sbc", []string{"sbc"}, "sbc", ""}, |
| 57 | + {"already on aptx, hd missing", []string{"sbc", "aptx"}, "aptx", ""}, |
| 58 | + {"already on aptx, hd offered", []string{"sbc", "aptx", "aptx_hd"}, "aptx", "aptx_hd"}, |
| 59 | + {"already on best", []string{"aptx_hd", "aptx"}, "aptx_hd", ""}, |
| 60 | + } |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + if got := pickBluetoothCodec(tt.available, tt.current); got != tt.expected { |
| 64 | + t.Errorf("pickBluetoothCodec(%v, %q) = %q, want %q", tt.available, tt.current, got, tt.expected) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestEnsureBluetoothCodecSkipsBest(t *testing.T) { |
| 71 | + pa := &PulseAudioBackend{} |
| 72 | + src := &proto.GetSourceInfoReply{ |
| 73 | + SourceIndex: 7, |
| 74 | + Properties: proto.PropList{"bluetooth.codec": proto.PropListString(preferredBluetoothCodecs[0])}, |
| 75 | + } |
| 76 | + |
| 77 | + pa.ensureBluetoothCodec(src) |
| 78 | + |
| 79 | + if _, attempted := pa.btCodecAttempted.Load(src.SourceIndex); attempted { |
| 80 | + t.Errorf("source already on %s must not be attempted", preferredBluetoothCodecs[0]) |
| 81 | + } |
| 82 | +} |
0 commit comments