gobot.io/x/gobot/v2@v2.1.0/drivers/spi/ssd1306_driver_test.go (about) 1 package spi 2 3 import ( 4 "errors" 5 "image" 6 "sync" 7 "testing" 8 9 "gobot.io/x/gobot/v2" 10 "gobot.io/x/gobot/v2/gobottest" 11 ) 12 13 // this ensures that the implementation is based on spi.Driver, which implements the gobot.Driver 14 // and tests all implementations, so no further tests needed here for gobot.Driver interface 15 var _ gobot.Driver = (*SSD1306Driver)(nil) 16 17 func initTestSSDDriver() *SSD1306Driver { 18 return NewSSD1306Driver(newGpioTestAdaptor()) 19 } 20 21 func TestDriverSSDStart(t *testing.T) { 22 d := initTestSSDDriver() 23 gobottest.Assert(t, d.Start(), nil) 24 } 25 26 func TestDriverSSDHalt(t *testing.T) { 27 d := initTestSSDDriver() 28 d.Start() 29 gobottest.Assert(t, d.Halt(), nil) 30 } 31 32 func TestDriverSSDDisplay(t *testing.T) { 33 d := initTestSSDDriver() 34 d.Start() 35 gobottest.Assert(t, d.Display(), nil) 36 } 37 38 func TestSSD1306DriverShowImage(t *testing.T) { 39 d := initTestSSDDriver() 40 d.Start() 41 img := image.NewRGBA(image.Rect(0, 0, 640, 480)) 42 gobottest.Assert(t, d.ShowImage(img), errors.New("Image must match the display width and height")) 43 44 img = image.NewRGBA(image.Rect(0, 0, 128, 64)) 45 gobottest.Assert(t, d.ShowImage(img), nil) 46 } 47 48 type gpioTestAdaptor struct { 49 name string 50 port string 51 mtx sync.Mutex 52 Connector 53 testAdaptorDigitalWrite func() (err error) 54 testAdaptorServoWrite func() (err error) 55 testAdaptorPwmWrite func() (err error) 56 testAdaptorAnalogRead func() (val int, err error) 57 testAdaptorDigitalRead func() (val int, err error) 58 } 59 60 func (t *gpioTestAdaptor) ServoWrite(string, byte) (err error) { 61 t.mtx.Lock() 62 defer t.mtx.Unlock() 63 return t.testAdaptorServoWrite() 64 } 65 func (t *gpioTestAdaptor) PwmWrite(string, byte) (err error) { 66 t.mtx.Lock() 67 defer t.mtx.Unlock() 68 return t.testAdaptorPwmWrite() 69 } 70 func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) { 71 t.mtx.Lock() 72 defer t.mtx.Unlock() 73 return t.testAdaptorAnalogRead() 74 } 75 func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) { 76 t.mtx.Lock() 77 defer t.mtx.Unlock() 78 return t.testAdaptorDigitalRead() 79 } 80 func (t *gpioTestAdaptor) DigitalWrite(string, byte) (err error) { 81 t.mtx.Lock() 82 defer t.mtx.Unlock() 83 return t.testAdaptorDigitalWrite() 84 } 85 func (t *gpioTestAdaptor) Connect() (err error) { return } 86 func (t *gpioTestAdaptor) Finalize() (err error) { return } 87 func (t *gpioTestAdaptor) Name() string { return t.name } 88 func (t *gpioTestAdaptor) SetName(n string) { t.name = n } 89 func (t *gpioTestAdaptor) Port() string { return t.port } 90 91 func newGpioTestAdaptor() *gpioTestAdaptor { 92 a := newSpiTestAdaptor() 93 return &gpioTestAdaptor{ 94 port: "/dev/null", 95 testAdaptorDigitalWrite: func() (err error) { 96 return nil 97 }, 98 testAdaptorServoWrite: func() (err error) { 99 return nil 100 }, 101 testAdaptorPwmWrite: func() (err error) { 102 return nil 103 }, 104 testAdaptorAnalogRead: func() (val int, err error) { 105 return 99, nil 106 }, 107 testAdaptorDigitalRead: func() (val int, err error) { 108 return 1, nil 109 }, 110 Connector: a, 111 } 112 }