2016-05-13 16:09:36 -07:00
|
|
|
// Based on aplay audio adaptor written by @colemanserious (https://github.com/colemanserious)
|
|
|
|
package audio
|
|
|
|
|
|
|
|
import (
|
2023-10-20 10:27:09 +02:00
|
|
|
"os"
|
2016-05-24 20:04:51 -07:00
|
|
|
"os/exec"
|
2017-04-06 09:50:01 +02:00
|
|
|
"strings"
|
2016-05-13 16:09:36 -07:00
|
|
|
"testing"
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2016-05-13 16:09:36 -07:00
|
|
|
)
|
|
|
|
|
2016-09-25 12:17:01 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-08-26 14:23:03 +02:00
|
|
|
|
2016-05-13 16:09:36 -07:00
|
|
|
func TestAudioAdaptor(t *testing.T) {
|
2016-09-25 12:17:01 +02:00
|
|
|
a := NewAdaptor()
|
2016-05-13 16:09:36 -07:00
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, a.Connect())
|
|
|
|
assert.Nil(t, a.Finalize())
|
2016-05-13 16:09:36 -07:00
|
|
|
}
|
2016-05-24 17:57:59 -07:00
|
|
|
|
2017-04-06 09:50:01 +02:00
|
|
|
func TestAudioAdaptorName(t *testing.T) {
|
|
|
|
a := NewAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(a.Name(), "Audio"))
|
2017-04-06 09:50:01 +02:00
|
|
|
a.SetName("NewName")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "NewName", a.Name())
|
2017-04-06 09:50:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 17:57:59 -07:00
|
|
|
func TestAudioAdaptorCommandsWav(t *testing.T) {
|
|
|
|
cmd, _ := CommandName("whatever.wav")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "aplay", cmd)
|
2016-05-24 17:57:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAudioAdaptorCommandsMp3(t *testing.T) {
|
|
|
|
cmd, _ := CommandName("whatever.mp3")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "mpg123", cmd)
|
2016-05-24 17:57:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAudioAdaptorCommandsUnknown(t *testing.T) {
|
|
|
|
cmd, err := CommandName("whatever.unk")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotEqual(t, "mpg123", cmd)
|
|
|
|
assert.Errorf(t, err, "Unknown filetype for audio file.")
|
2016-05-24 17:57:59 -07:00
|
|
|
}
|
2016-05-24 18:49:58 -07:00
|
|
|
|
|
|
|
func TestAudioAdaptorSoundWithNoFilename(t *testing.T) {
|
2016-09-25 12:17:01 +02:00
|
|
|
a := NewAdaptor()
|
2016-05-24 18:49:58 -07:00
|
|
|
|
|
|
|
errors := a.Sound("")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "Requires filename for audio file.", errors[0].Error())
|
2016-05-24 18:49:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
|
2016-09-25 12:17:01 +02:00
|
|
|
a := NewAdaptor()
|
2016-05-24 18:49:58 -07:00
|
|
|
|
|
|
|
errors := a.Sound("doesnotexist.mp3")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "stat doesnotexist.mp3: no such file or directory", errors[0].Error())
|
2016-05-24 18:49:58 -07:00
|
|
|
}
|
2016-05-24 20:04:51 -07:00
|
|
|
|
|
|
|
func TestAudioAdaptorSoundWithValidMP3Filename(t *testing.T) {
|
2023-10-20 10:27:09 +02:00
|
|
|
execCommand = myExecCommand
|
2016-05-24 20:04:51 -07:00
|
|
|
|
2016-09-25 12:17:01 +02:00
|
|
|
a := NewAdaptor()
|
2016-05-24 20:04:51 -07:00
|
|
|
defer func() { execCommand = exec.Command }()
|
|
|
|
|
|
|
|
errors := a.Sound("../../examples/laser.mp3")
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, 0, len(errors))
|
|
|
|
}
|
|
|
|
|
|
|
|
func myExecCommand(command string, args ...string) *exec.Cmd {
|
|
|
|
cs := []string{"-test.run=TestHelperProcess", "--", command}
|
|
|
|
cs = append(cs, args...)
|
|
|
|
cmd := exec.Command(os.Args[0], cs...)
|
|
|
|
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
|
|
|
|
return cmd
|
2016-05-24 20:04:51 -07:00
|
|
|
}
|