gobot.io/x/gobot@v1.16.0/drivers/gpio/pir_motion_driver_test.go (about)

     1  package gpio
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"gobot.io/x/gobot"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  var _ gobot.Driver = (*PIRMotionDriver)(nil)
    14  
    15  const motionTestDelay = 150
    16  
    17  func initTestPIRMotionDriver() *PIRMotionDriver {
    18  	return NewPIRMotionDriver(newGpioTestAdaptor(), "1")
    19  }
    20  
    21  func TestPIRMotionDriverHalt(t *testing.T) {
    22  	d := initTestPIRMotionDriver()
    23  	go func() {
    24  		<-d.halt
    25  	}()
    26  	gobottest.Assert(t, d.Halt(), nil)
    27  }
    28  
    29  func TestPIRMotionDriver(t *testing.T) {
    30  	d := NewPIRMotionDriver(newGpioTestAdaptor(), "1")
    31  	gobottest.Refute(t, d.Connection(), nil)
    32  
    33  	d = NewPIRMotionDriver(newGpioTestAdaptor(), "1", 30*time.Second)
    34  	gobottest.Assert(t, d.interval, 30*time.Second)
    35  }
    36  
    37  func TestPIRMotionDriverStart(t *testing.T) {
    38  	sem := make(chan bool, 0)
    39  	a := newGpioTestAdaptor()
    40  	d := NewPIRMotionDriver(a, "1")
    41  
    42  	gobottest.Assert(t, d.Start(), nil)
    43  
    44  	d.Once(MotionDetected, func(data interface{}) {
    45  		gobottest.Assert(t, d.Active, true)
    46  		sem <- true
    47  	})
    48  
    49  	a.TestAdaptorDigitalRead(func(string) (val int, err error) {
    50  		val = 1
    51  		return
    52  	})
    53  
    54  	select {
    55  	case <-sem:
    56  	case <-time.After(motionTestDelay * time.Millisecond):
    57  		t.Errorf("PIRMotionDriver Event \"MotionDetected\" was not published")
    58  	}
    59  
    60  	d.Once(MotionStopped, func(data interface{}) {
    61  		gobottest.Assert(t, d.Active, false)
    62  		sem <- true
    63  	})
    64  
    65  	a.TestAdaptorDigitalRead(func(string) (val int, err error) {
    66  		val = 0
    67  		return
    68  	})
    69  
    70  	select {
    71  	case <-sem:
    72  	case <-time.After(motionTestDelay * time.Millisecond):
    73  		t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
    74  	}
    75  
    76  	d.Once(Error, func(data interface{}) {
    77  		sem <- true
    78  	})
    79  
    80  	a.TestAdaptorDigitalRead(func(string) (val int, err error) {
    81  		err = errors.New("digital read error")
    82  		return
    83  	})
    84  
    85  	select {
    86  	case <-sem:
    87  	case <-time.After(motionTestDelay * time.Millisecond):
    88  		t.Errorf("PIRMotionDriver Event \"Error\" was not published")
    89  	}
    90  }
    91  
    92  func TestPIRDriverDefaultName(t *testing.T) {
    93  	d := initTestPIRMotionDriver()
    94  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "PIR"), true)
    95  }
    96  
    97  func TestPIRDriverSetName(t *testing.T) {
    98  	d := initTestPIRMotionDriver()
    99  	d.SetName("mybot")
   100  	gobottest.Assert(t, d.Name(), "mybot")
   101  }