gobot.io/x/gobot@v1.16.0/platforms/joystick/joystick_driver_test.go (about)

     1  package joystick
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/veandco/go-sdl2/sdl"
     9  	"gobot.io/x/gobot"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  var _ gobot.Driver = (*Driver)(nil)
    14  
    15  func initTestDriver(config string) *Driver {
    16  	a := NewAdaptor()
    17  	a.connect = func(j *Adaptor) (err error) {
    18  		j.joystick = &testJoystick{}
    19  		return nil
    20  	}
    21  	a.Connect()
    22  	d := NewDriver(a, config)
    23  	d.poll = func() sdl.Event {
    24  		return nil
    25  	}
    26  	return d
    27  }
    28  
    29  func TestJoystickDriverName(t *testing.T) {
    30  	d := initTestDriver("./configs/xbox360_power_a_mini_proex.json")
    31  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "Joystick"), true)
    32  	d.SetName("NewName")
    33  	gobottest.Assert(t, d.Name(), "NewName")
    34  }
    35  
    36  func TestDriverStart(t *testing.T) {
    37  	d := initTestDriver("./configs/xbox360_power_a_mini_proex.json")
    38  	d.interval = 1 * time.Millisecond
    39  	gobottest.Assert(t, d.Start(), nil)
    40  	time.Sleep(2 * time.Millisecond)
    41  }
    42  
    43  func TestDriverHalt(t *testing.T) {
    44  	d := initTestDriver("./configs/xbox360_power_a_mini_proex.json")
    45  	go func() {
    46  		<-d.halt
    47  	}()
    48  	gobottest.Assert(t, d.Halt(), nil)
    49  }
    50  
    51  func TestDriverHandleEvent(t *testing.T) {
    52  	sem := make(chan bool)
    53  	d := initTestDriver("./configs/xbox360_power_a_mini_proex.json")
    54  	d.Start()
    55  
    56  	// left x stick
    57  	d.On(d.Event("left_x"), func(data interface{}) {
    58  		gobottest.Assert(t, int16(100), data.(int16))
    59  		sem <- true
    60  	})
    61  	d.handleEvent(&sdl.JoyAxisEvent{
    62  		Which: 0,
    63  		Axis:  0,
    64  		Value: 100,
    65  	})
    66  	select {
    67  	case <-sem:
    68  	case <-time.After(10 * time.Second):
    69  		t.Errorf("Button Event \"left_x\" was not published")
    70  	}
    71  
    72  	// x button press
    73  	d.On(d.Event("x_press"), func(data interface{}) {
    74  		sem <- true
    75  	})
    76  	d.handleEvent(&sdl.JoyButtonEvent{
    77  		Which:  0,
    78  		Button: 2,
    79  		State:  1,
    80  	})
    81  	select {
    82  	case <-sem:
    83  	case <-time.After(10 * time.Second):
    84  		t.Errorf("Button Event \"x_press\" was not published")
    85  	}
    86  
    87  	// x button  release
    88  	d.On(d.Event("x_release"), func(data interface{}) {
    89  		sem <- true
    90  	})
    91  	d.handleEvent(&sdl.JoyButtonEvent{
    92  		Which:  0,
    93  		Button: 2,
    94  		State:  0,
    95  	})
    96  	select {
    97  	case <-sem:
    98  	case <-time.After(10 * time.Second):
    99  		t.Errorf("Button Event \"x_release\" was not published")
   100  	}
   101  
   102  	// down button press
   103  	d.On(d.Event("down_press"), func(data interface{}) {
   104  		sem <- true
   105  	})
   106  	d.handleEvent(&sdl.JoyHatEvent{
   107  		Which: 0,
   108  		Hat:   0,
   109  		Value: 4,
   110  	})
   111  	select {
   112  	case <-sem:
   113  	case <-time.After(10 * time.Second):
   114  		t.Errorf("Hat Event \"down\" was not published")
   115  	}
   116  
   117  	err := d.handleEvent(&sdl.JoyHatEvent{
   118  		Which: 0,
   119  		Hat:   99,
   120  		Value: 4,
   121  	})
   122  
   123  	gobottest.Assert(t, err.Error(), "Unknown Hat: 99 4")
   124  
   125  	err = d.handleEvent(&sdl.JoyAxisEvent{
   126  		Which: 0,
   127  		Axis:  99,
   128  		Value: 100,
   129  	})
   130  
   131  	gobottest.Assert(t, err.Error(), "Unknown Axis: 99")
   132  
   133  	err = d.handleEvent(&sdl.JoyButtonEvent{
   134  		Which:  0,
   135  		Button: 99,
   136  		State:  0,
   137  	})
   138  
   139  	gobottest.Assert(t, err.Error(), "Unknown Button: 99")
   140  }
   141  
   142  func TestDriverInvalidConfig(t *testing.T) {
   143  	d := initTestDriver("./configs/doesnotexist")
   144  	err := d.Start()
   145  	gobottest.Assert(t, strings.Contains(err.Error(), "open ./configs/doesnotexist: no such file or directory"), true)
   146  }