-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_test.go
912 lines (873 loc) · 26.7 KB
/
player_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
package mpris
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/godbus/dbus/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPlayer_Methods(t *testing.T) {
tests := []struct {
name string
givenName string
action func(p *Player)
expectedDest string
expectedPath dbus.ObjectPath
expectedMethod string
expectedFlags dbus.Flags
expectedArgs []interface{}
}{
{
name: "Next",
givenName: "next",
action: func(p *Player) {
p.Next()
},
expectedDest: "next",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.Next",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "Previous",
givenName: "previous",
action: func(p *Player) {
p.Previous()
},
expectedDest: "previous",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.Previous",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "Pause",
givenName: "pause",
action: func(p *Player) {
p.Pause()
},
expectedDest: "pause",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.Pause",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "Play",
givenName: "play",
action: func(p *Player) {
p.Play()
},
expectedDest: "play",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.Play",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "PlayPause",
givenName: "play-pause",
action: func(p *Player) {
p.PlayPause()
},
expectedDest: "play-pause",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.PlayPause",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "SeekTo",
givenName: "seek-to",
action: func(p *Player) {
p.SeekTo(12356789)
},
expectedDest: "seek-to",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.SeekTo",
expectedFlags: 0,
expectedArgs: []interface{}{int64(12356789)},
},
{
name: "Stop",
givenName: "stop",
action: func(p *Player) {
p.Stop()
},
expectedDest: "stop",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.Stop",
expectedFlags: 0,
expectedArgs: nil,
},
{
name: "SetPosition",
givenName: "set-position",
action: func(p *Player) {
p.SetPosition("/my/path", 123456789)
},
expectedDest: "set-position",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.SetPosition",
expectedFlags: 0,
expectedArgs: []interface{}{dbus.ObjectPath("/my/path"), int64(123456789)},
},
{
name: "OpenUri",
givenName: "open-uri",
action: func(p *Player) {
p.OpenURI("file://my/uri")
},
expectedDest: "open-uri",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Player.OpenUri",
expectedFlags: 0,
expectedArgs: []interface{}{"file://my/uri"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var givenDest string
var givenPath dbus.ObjectPath
var givenMethod string
var givenFlags dbus.Flags
var givenArgs []interface{}
tt.action(&Player{
name: tt.givenName,
connection: &dbusConnMock{
ObjectFunc: func(dest string, path dbus.ObjectPath) dbusBusObject {
givenDest = dest
givenPath = path
return &dbusBusObjectMock{
CallFunc: func(method string, flags dbus.Flags, args ...interface{}) dbusCall {
givenMethod = method
givenFlags = flags
givenArgs = args
return nil
},
}
},
},
})
assert.Equal(t, tt.expectedDest, givenDest, "given dest is not as expected")
assert.Equal(t, tt.expectedPath, givenPath, "given path is not as expected")
assert.Equal(t, tt.expectedMethod, givenMethod, "given method is not as expected")
assert.Equal(t, tt.expectedFlags, givenFlags, "given flags is not as expected")
assert.EqualValues(t, tt.expectedArgs, givenArgs, "given args is not as expected")
})
}
}
func TestPlayer_GetProperties(t *testing.T) {
tests := []struct {
name string
givenName string
callVariant dbus.Variant
callError error
runAndValidate func(t *testing.T, p *Player)
expectedDest string
expectedPath dbus.ObjectPath
expectedKey string
}{
{
name: "PlaybackStatus",
callVariant: dbus.MakeVariant("Paused"),
givenName: "playback-status",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.PlaybackStatus()
assert.NoError(t, err)
assert.Equal(t, PlaybackStatusPaused, s, "status is not as expected")
},
expectedDest: "playback-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.PlaybackStatus",
},
{
name: "PlaybackStatus error",
callError: errors.New("nope"),
givenName: "playback-status",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.PlaybackStatus()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.PlaybackStatus\": nope", fmt.Sprint(err))
},
expectedDest: "playback-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.PlaybackStatus",
},
{
name: "LoopStatus",
callVariant: dbus.MakeVariant("Track"),
givenName: "loop-status",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.LoopStatus()
assert.NoError(t, err)
assert.Equal(t, LoopStatusTrack, s, "status is not as expected")
},
expectedDest: "loop-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.LoopStatus",
},
{
name: "LoopStatus error",
callError: errors.New("nope"),
givenName: "loop-status",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.LoopStatus()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.LoopStatus\": nope", fmt.Sprint(err))
},
expectedDest: "loop-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.LoopStatus",
},
{
name: "Rate",
callVariant: dbus.MakeVariant(float64(3)),
givenName: "rate",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.Rate()
assert.NoError(t, err)
assert.Equal(t, float64(3), s, "rate is not as expected")
},
expectedDest: "rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Rate",
},
{
name: "Rate error",
callError: errors.New("nope"),
givenName: "rate",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.Rate()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.Rate\": nope", fmt.Sprint(err))
},
expectedDest: "rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Rate",
},
{
name: "Shuffle",
callVariant: dbus.MakeVariant(false),
givenName: "shuffle",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.Shuffle()
assert.NoError(t, err)
assert.Equal(t, false, s, "shuffle is not as expected")
},
expectedDest: "shuffle",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Shuffle",
},
{
name: "Shuffle error",
callError: errors.New("nope"),
givenName: "shuffle",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.Shuffle()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.Shuffle\": nope", fmt.Sprint(err))
},
expectedDest: "shuffle",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Shuffle",
},
{
name: "Metadata",
callVariant: dbus.MakeVariant(map[string]dbus.Variant{
"myKey1": dbus.MakeVariant(true),
"myKey2": dbus.MakeVariant("key2"),
}),
givenName: "metadata",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.Metadata()
assert.NoError(t, err)
assert.Equal(t, Metadata{
"myKey1": dbus.MakeVariant(true),
"myKey2": dbus.MakeVariant("key2"),
}, s, "metadata is not as expected")
},
expectedDest: "metadata",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Metadata",
},
{
name: "Metadata error",
callError: errors.New("nope"),
givenName: "metadata",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.Metadata()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.Metadata\": nope", fmt.Sprint(err))
},
expectedDest: "metadata",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Metadata",
},
{
name: "Volume",
callVariant: dbus.MakeVariant(0.5),
givenName: "volume",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.Volume()
assert.NoError(t, err)
assert.Equal(t, 0.5, s, "volume is not as expected")
},
expectedDest: "volume",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Volume",
},
{
name: "Volume error",
callError: errors.New("nope"),
givenName: "volume",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.Volume()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.Volume\": nope", fmt.Sprint(err))
},
expectedDest: "volume",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Volume",
},
{
name: "Position",
callVariant: dbus.MakeVariant(int64(220342)),
givenName: "position",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.Position()
assert.NoError(t, err)
assert.Equal(t, int64(220342), s, "position is not as expected")
},
expectedDest: "position",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Position",
},
{
name: "Position error",
callError: errors.New("nope"),
givenName: "position",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.Position()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.Position\": nope", fmt.Sprint(err))
},
expectedDest: "position",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.Position",
},
{
name: "MinimumRate",
callVariant: dbus.MakeVariant(0.000001),
givenName: "minimum-rate",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.MinimumRate()
assert.NoError(t, err)
assert.Equal(t, 0.000001, s, "minimum-rate is not as expected")
},
expectedDest: "minimum-rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.MinimumRate",
},
{
name: "MinimumRate error",
callError: errors.New("nope"),
givenName: "minimum-rate",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.MinimumRate()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.MinimumRate\": nope", fmt.Sprint(err))
},
expectedDest: "minimum-rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.MinimumRate",
},
{
name: "MaximumRate",
callVariant: dbus.MakeVariant(0.000001),
givenName: "maximum-rate",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.MaximumRate()
assert.NoError(t, err)
assert.Equal(t, 0.000001, s, "maximum-rate is not as expected")
},
expectedDest: "maximum-rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.MaximumRate",
},
{
name: "MaximumRate error",
callError: errors.New("nope"),
givenName: "maximum-rate",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.MaximumRate()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.MaximumRate\": nope", fmt.Sprint(err))
},
expectedDest: "maximum-rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.MaximumRate",
},
{
name: "CanGoNext",
callVariant: dbus.MakeVariant(true),
givenName: "can-go-next",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanGoNext()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-go-next is not as expected")
},
expectedDest: "can-go-next",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanGoNext",
},
{
name: "CanGoNext error",
callError: errors.New("nope"),
givenName: "can-go-next",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanGoNext()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanGoNext\": nope", fmt.Sprint(err))
},
expectedDest: "can-go-next",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanGoNext",
},
{
name: "CanGoPrevious",
callVariant: dbus.MakeVariant(true),
givenName: "can-go-previous",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanGoPrevious()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-go-previous is not as expected")
},
expectedDest: "can-go-previous",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanGoPrevious",
},
{
name: "CanGoPrevious error",
callError: errors.New("nope"),
givenName: "can-go-previous",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanGoPrevious()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanGoPrevious\": nope", fmt.Sprint(err))
},
expectedDest: "can-go-previous",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanGoPrevious",
},
{
name: "CanPlay",
callVariant: dbus.MakeVariant(true),
givenName: "can-play",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanPlay()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-play is not as expected")
},
expectedDest: "can-play",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanPlay",
},
{
name: "CanPlay error",
callError: errors.New("nope"),
givenName: "can-play",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanPlay()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanPlay\": nope", fmt.Sprint(err))
},
expectedDest: "can-play",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanPlay",
},
{
name: "CanPause",
callVariant: dbus.MakeVariant(true),
givenName: "can-pause",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanPause()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-pause is not as expected")
},
expectedDest: "can-pause",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanPause",
},
{
name: "CanPause error",
callError: errors.New("nope"),
givenName: "can-pause",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanPause()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanPause\": nope", fmt.Sprint(err))
},
expectedDest: "can-pause",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanPause",
},
{
name: "CanSeek",
callVariant: dbus.MakeVariant(true),
givenName: "can-seek",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanSeek()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-seek is not as expected")
},
expectedDest: "can-seek",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanSeek",
},
{
name: "CanSeek error",
callError: errors.New("nope"),
givenName: "can-seek",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanSeek()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanSeek\": nope", fmt.Sprint(err))
},
expectedDest: "can-seek",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanSeek",
},
{
name: "CanControl",
callVariant: dbus.MakeVariant(true),
givenName: "can-control",
runAndValidate: func(t *testing.T, p *Player) {
s, err := p.CanControl()
assert.NoError(t, err)
assert.Equal(t, true, s, "can-control is not as expected")
},
expectedDest: "can-control",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanControl",
},
{
name: "CanControl error",
callError: errors.New("nope"),
givenName: "can-control",
runAndValidate: func(t *testing.T, p *Player) {
_, err := p.CanControl()
assert.Equal(t, "failed to get property \"org.mpris.MediaPlayer2.Player.CanControl\": nope", fmt.Sprint(err))
},
expectedDest: "can-control",
expectedPath: "/org/mpris/MediaPlayer2",
expectedKey: "org.mpris.MediaPlayer2.Player.CanControl",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var calledDest string
var calledPath dbus.ObjectPath
var calledKey string
m := &dbusConnMock{
ObjectFunc: func(dest string, path dbus.ObjectPath) dbusBusObject {
calledDest = dest
calledPath = path
return &dbusBusObjectMock{
GetPropertyFunc: func(key string) (dbus.Variant, error) {
calledKey = key
return tt.callVariant, tt.callError
},
}
},
}
tt.runAndValidate(t, &Player{name: tt.givenName, connection: m})
assert.Equal(t, tt.expectedDest, calledDest, "called dest is not as expected")
assert.Equal(t, tt.expectedPath, calledPath, "called path is not as expected")
assert.Equal(t, tt.expectedKey, calledKey, "called key is not as expected")
})
}
}
func TestPlayer_SetProperties(t *testing.T) {
tests := []struct {
name string
givenName string
callError error
runAndValidate func(t *testing.T, p *Player)
expectedDest string
expectedPath dbus.ObjectPath
expectedProperty string
expectedValue interface{}
}{
{
name: "LoopStatus",
givenName: "loop-status",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetLoopStatus(LoopStatusTrack)
assert.NoError(t, err)
},
expectedDest: "loop-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.LoopStatus",
expectedValue: dbus.MakeVariant("Track"),
},
{
name: "LoopStatus error",
callError: errors.New("nope"),
givenName: "loop-status",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetLoopStatus(LoopStatusPlaylist)
assert.Equal(t, "failed to set property \"org.mpris.MediaPlayer2.Player.LoopStatus\": nope", fmt.Sprint(err))
},
expectedDest: "loop-status",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.LoopStatus",
expectedValue: dbus.MakeVariant("Playlist"),
},
{
name: "Rate",
givenName: "rate",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetRate(0.5)
assert.NoError(t, err)
},
expectedDest: "rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Rate",
expectedValue: dbus.MakeVariant(0.5),
},
{
name: "Rate error",
callError: errors.New("nope"),
givenName: "rate",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetRate(2)
assert.Equal(t, "failed to set property \"org.mpris.MediaPlayer2.Player.Rate\": nope", fmt.Sprint(err))
},
expectedDest: "rate",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Rate",
expectedValue: dbus.MakeVariant(float64(2)),
},
{
name: "Shuffle",
givenName: "shuffle",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetShuffle(true)
assert.NoError(t, err)
},
expectedDest: "shuffle",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Shuffle",
expectedValue: dbus.MakeVariant(true),
},
{
name: "Shuffle error",
callError: errors.New("nope"),
givenName: "shuffle",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetShuffle(false)
assert.Equal(t, "failed to set property \"org.mpris.MediaPlayer2.Player.Shuffle\": nope", fmt.Sprint(err))
},
expectedDest: "shuffle",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Shuffle",
expectedValue: dbus.MakeVariant(false),
},
{
name: "Volume",
givenName: "volume",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetVolume(1)
assert.NoError(t, err)
},
expectedDest: "volume",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Volume",
expectedValue: dbus.MakeVariant(float64(1)),
},
{
name: "Volume error",
callError: errors.New("nope"),
givenName: "volume",
runAndValidate: func(t *testing.T, p *Player) {
err := p.SetVolume(0.5)
assert.Equal(t, "failed to set property \"org.mpris.MediaPlayer2.Player.Volume\": nope", fmt.Sprint(err))
},
expectedDest: "volume",
expectedPath: "/org/mpris/MediaPlayer2",
expectedProperty: "org.mpris.MediaPlayer2.Player.Volume",
expectedValue: dbus.MakeVariant(0.5),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var calledDest string
var calledPath dbus.ObjectPath
var calledProperty string
var calledValue interface{}
m := &dbusConnMock{
ObjectFunc: func(dest string, path dbus.ObjectPath) dbusBusObject {
calledDest = dest
calledPath = path
return &dbusBusObjectMock{
SetPropertyFunc: func(p string, v interface{}) error {
calledProperty = p
calledValue = v
return tt.callError
},
}
},
}
tt.runAndValidate(t, &Player{name: tt.givenName, connection: m})
assert.Equal(t, tt.expectedDest, calledDest, "called dest is not as expected")
assert.Equal(t, tt.expectedPath, calledPath, "called path is not as expected")
assert.Equal(t, tt.expectedProperty, calledProperty, "called property is not as expected")
assert.Equal(t, tt.expectedValue, calledValue, "called value is not as expected")
})
}
}
func TestPlayer_Close(t *testing.T) {
tests := []struct {
name string
closeErr error
expectedErr string
}{
{
name: "Happycase",
}, {
name: "Close error",
closeErr: errors.New("unexpected error"),
expectedErr: "failed to close dbus connection: unexpected error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := dbusConnMock{
CloseFunc: func() error {
return tt.closeErr
},
}
err := Player{
connection: &mock,
}.Close()
require.Equal(t, msgOrEmpty(err), tt.expectedErr)
assert.Equal(t, 1, len(mock.CloseCalls()))
})
}
}
func TestPlayer_Seeked(t *testing.T) {
tests := []struct {
name string
givenSignals []*dbus.Signal
addMatchSignalErr error
expectedAddMatchSignalCallCount int
expectedSignalCallCount int
expectedErr string
expectedPositions []int
}{
{
name: "happycase",
expectedAddMatchSignalCallCount: 1,
expectedSignalCallCount: 1,
givenSignals: []*dbus.Signal{
{
Name: "org.mpris.MediaPlayer2.Player.Seeked",
Body: []interface{}{int64(1111)},
}, {
Name: "unknown name",
Body: []interface{}{int64(22222)},
}, { // multiple body infos
Name: "org.mpris.MediaPlayer2.Player.Seeked",
Body: []interface{}{int64(333111), int64(333222)},
}, { // invalid body type
Name: "org.mpris.MediaPlayer2.Player.Seeked",
Body: []interface{}{"4444444"},
}, {
Name: "org.mpris.MediaPlayer2.Player.Seeked",
Body: []interface{}{int64(55555555)},
},
},
expectedPositions: []int{
1111,
55555555,
},
}, {
name: "add match signal error",
addMatchSignalErr: errors.New("unexpected error"),
expectedAddMatchSignalCallCount: 1,
expectedSignalCallCount: 0,
expectedErr: "failed to add signal match option: unexpected error",
givenSignals: []*dbus.Signal{
{
Name: "org.mpris.MediaPlayer2.Player.Seeked",
Body: []interface{}{int64(1111)},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
mock := &dbusConnMock{
AddMatchSignalFunc: func(_ ...dbus.MatchOption) error {
return tt.addMatchSignalErr
},
SignalFunc: func(ch chan<- *dbus.Signal) {
go func() {
for _, sig := range tt.givenSignals {
ch <- sig
}
}()
},
}
poss, err := Player{
connection: mock,
}.Seeked(testCtx)
require.Equal(t, tt.expectedErr, msgOrEmpty(err))
var collectedPoss []int
if poss != nil {
for {
p, ok := <-poss
if !ok {
break
}
collectedPoss = append(collectedPoss, p)
}
}
assert.EqualValues(t, tt.expectedPositions, collectedPoss)
assert.Equal(t, tt.expectedAddMatchSignalCallCount, len(mock.AddMatchSignalCalls()))
assert.Equal(t, tt.expectedSignalCallCount, len(mock.SignalCalls()))
})
}
}
func TestNewPlayer(t *testing.T) {
oldDbusSessionBus := dbusSessionBus
defer func() {
dbusSessionBus = oldDbusSessionBus
}()
dbusConn := &dbus.Conn{}
dbusSessionBus = func() (conn *dbus.Conn, err error) {
return dbusConn, nil
}
p, err := NewPlayer("test")
assert.NoError(t, err, "unexpected error")
assert.Equal(t, &dbusConnWrapper{dbusConn}, p.connection)
}
func TestNewPlayer_Error(t *testing.T) {
oldDbusSessionBus := dbusSessionBus
defer func() {
dbusSessionBus = oldDbusSessionBus
}()
dbusSessionBusErr := errors.New("nope")
dbusSessionBus = func() (conn *dbus.Conn, err error) {
return nil, dbusSessionBusErr
}
expectedError := errors.New("failed to connect to session-bus: nope")
_, err := NewPlayer("test")
if fmt.Sprint(err) == fmt.Sprint() {
t.Fatalf("unexpected error. Given: %q, Expected: %q", err, expectedError)
}
}
func TestNewPlayerWithConnection(t *testing.T) {
dbusConn := &dbus.Conn{}
p := NewPlayerWithConnection("test", dbusConn)
assert.Equal(t, &dbusConnWrapper{dbusConn}, p.connection)
}
func msgOrEmpty(err error) string {
if err == nil {
return ""
}
return err.Error()
}