gobot.io/x/gobot@v1.16.0/drivers/i2c/jhd1313m1_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*JHD1313M1Driver)(nil)
    13  
    14  // --------- HELPERS
    15  func initTestJHD1313M1Driver() (driver *JHD1313M1Driver) {
    16  	driver, _ = initTestJHD1313M1DriverWithStubbedAdaptor()
    17  	return
    18  }
    19  
    20  func initTestJHD1313M1DriverWithStubbedAdaptor() (*JHD1313M1Driver, *i2cTestAdaptor) {
    21  	adaptor := newI2cTestAdaptor()
    22  	return NewJHD1313M1Driver(adaptor), adaptor
    23  }
    24  
    25  // --------- TESTS
    26  
    27  func TestNewJHD1313M1Driver(t *testing.T) {
    28  	// Does it return a pointer to an instance of JHD1313M1Driver?
    29  	var mpl interface{} = NewJHD1313M1Driver(newI2cTestAdaptor())
    30  	_, ok := mpl.(*JHD1313M1Driver)
    31  	if !ok {
    32  		t.Errorf("NewJHD1313M1Driver() should have returned a *JHD1313M1Driver")
    33  	}
    34  }
    35  
    36  // Methods
    37  func TestJHD1313M1Driver(t *testing.T) {
    38  	jhd := initTestJHD1313M1Driver()
    39  
    40  	gobottest.Refute(t, jhd.Connection(), nil)
    41  	gobottest.Assert(t, strings.HasPrefix(jhd.Name(), "JHD1313M1"), true)
    42  }
    43  
    44  func TestJHD1313MDriverSetName(t *testing.T) {
    45  	d := initTestJHD1313M1Driver()
    46  	d.SetName("TESTME")
    47  	gobottest.Assert(t, d.Name(), "TESTME")
    48  }
    49  
    50  func TestJHD1313MDriverOptions(t *testing.T) {
    51  	d := NewJHD1313M1Driver(newI2cTestAdaptor(), WithBus(2))
    52  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    53  }
    54  
    55  func TestJHD1313MDriverStart(t *testing.T) {
    56  	d := initTestJHD1313M1Driver()
    57  	gobottest.Assert(t, d.Start(), nil)
    58  }
    59  
    60  func TestJHD1313MStartConnectError(t *testing.T) {
    61  	d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
    62  	adaptor.Testi2cConnectErr(true)
    63  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    64  }
    65  
    66  func TestJHD1313MDriverStartWriteError(t *testing.T) {
    67  	d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
    68  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    69  		return 0, errors.New("write error")
    70  	}
    71  	gobottest.Assert(t, d.Start(), errors.New("write error"))
    72  }
    73  
    74  func TestJHD1313MDriverHalt(t *testing.T) {
    75  	d := initTestJHD1313M1Driver()
    76  	d.Start()
    77  	gobottest.Assert(t, d.Halt(), nil)
    78  }
    79  
    80  func TestJHD1313MDriverSetRgb(t *testing.T) {
    81  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
    82  	d.Start()
    83  	gobottest.Assert(t, d.SetRGB(0x00, 0x00, 0x00), nil)
    84  }
    85  
    86  func TestJHD1313MDriverSetRgbError(t *testing.T) {
    87  	d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
    88  	d.Start()
    89  
    90  	a.i2cWriteImpl = func([]byte) (int, error) {
    91  		return 0, errors.New("write error")
    92  	}
    93  	gobottest.Assert(t, d.SetRGB(0x00, 0x00, 0x00), errors.New("write error"))
    94  }
    95  
    96  func TestJHD1313MDriverClear(t *testing.T) {
    97  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
    98  	d.Start()
    99  	gobottest.Assert(t, d.Clear(), nil)
   100  }
   101  
   102  func TestJHD1313MDriverClearError(t *testing.T) {
   103  	d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
   104  	d.Start()
   105  
   106  	a.i2cWriteImpl = func([]byte) (int, error) {
   107  		return 0, errors.New("write error")
   108  	}
   109  	gobottest.Assert(t, d.Clear(), errors.New("write error"))
   110  }
   111  
   112  func TestJHD1313MDriverHome(t *testing.T) {
   113  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   114  	d.Start()
   115  	gobottest.Assert(t, d.Home(), nil)
   116  }
   117  
   118  func TestJHD1313MDriverWrite(t *testing.T) {
   119  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   120  	d.Start()
   121  	gobottest.Assert(t, d.Write("Hello"), nil)
   122  }
   123  
   124  func TestJHD1313MDriverWriteError(t *testing.T) {
   125  	d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
   126  	d.Start()
   127  	a.i2cWriteImpl = func([]byte) (int, error) {
   128  		return 0, errors.New("write error")
   129  	}
   130  
   131  	gobottest.Assert(t, d.Write("Hello"), errors.New("write error"))
   132  }
   133  
   134  func TestJHD1313MDriverWriteTwoLines(t *testing.T) {
   135  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   136  	d.Start()
   137  	gobottest.Assert(t, d.Write("Hello\nthere"), nil)
   138  }
   139  
   140  func TestJHD1313MDriverWriteTwoLinesError(t *testing.T) {
   141  	d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
   142  	d.Start()
   143  
   144  	a.i2cWriteImpl = func([]byte) (int, error) {
   145  		return 0, errors.New("write error")
   146  	}
   147  	gobottest.Assert(t, d.Write("Hello\nthere"), errors.New("write error"))
   148  }
   149  
   150  func TestJHD1313MDriverSetPosition(t *testing.T) {
   151  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   152  	d.Start()
   153  	gobottest.Assert(t, d.SetPosition(2), nil)
   154  }
   155  
   156  func TestJHD1313MDriverSetSecondLinePosition(t *testing.T) {
   157  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   158  	d.Start()
   159  	gobottest.Assert(t, d.SetPosition(18), nil)
   160  }
   161  
   162  func TestJHD1313MDriverSetPositionInvalid(t *testing.T) {
   163  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   164  	d.Start()
   165  	gobottest.Assert(t, d.SetPosition(-1), ErrInvalidPosition)
   166  	gobottest.Assert(t, d.SetPosition(32), ErrInvalidPosition)
   167  }
   168  
   169  func TestJHD1313MDriverScroll(t *testing.T) {
   170  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   171  	d.Start()
   172  	gobottest.Assert(t, d.Scroll(true), nil)
   173  }
   174  
   175  func TestJHD1313MDriverReverseScroll(t *testing.T) {
   176  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   177  	d.Start()
   178  	gobottest.Assert(t, d.Scroll(false), nil)
   179  }
   180  
   181  func TestJHD1313MDriverSetCustomChar(t *testing.T) {
   182  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   183  	data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
   184  	d.Start()
   185  	gobottest.Assert(t, d.SetCustomChar(0, data), nil)
   186  }
   187  
   188  func TestJHD1313MDriverSetCustomCharError(t *testing.T) {
   189  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   190  	data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
   191  	d.Start()
   192  	gobottest.Assert(t, d.SetCustomChar(10, data), errors.New("can't set a custom character at a position greater than 7"))
   193  }
   194  
   195  func TestJHD1313MDriverSetCustomCharWriteError(t *testing.T) {
   196  	d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
   197  	d.Start()
   198  
   199  	a.i2cWriteImpl = func([]byte) (int, error) {
   200  		return 0, errors.New("write error")
   201  	}
   202  	data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
   203  	gobottest.Assert(t, d.SetCustomChar(0, data), errors.New("write error"))
   204  }
   205  
   206  func TestJHD1313MDriverCommands(t *testing.T) {
   207  	d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
   208  	d.Start()
   209  
   210  	err := d.Command("SetRGB")(map[string]interface{}{"r": "1", "g": "1", "b": "1"})
   211  	gobottest.Assert(t, err, nil)
   212  
   213  	err = d.Command("Clear")(map[string]interface{}{})
   214  	gobottest.Assert(t, err, nil)
   215  
   216  	err = d.Command("Home")(map[string]interface{}{})
   217  	gobottest.Assert(t, err, nil)
   218  
   219  	err = d.Command("Write")(map[string]interface{}{"msg": "Hello"})
   220  	gobottest.Assert(t, err, nil)
   221  
   222  	err = d.Command("SetPosition")(map[string]interface{}{"pos": "1"})
   223  	gobottest.Assert(t, err, nil)
   224  
   225  	err = d.Command("Scroll")(map[string]interface{}{"lr": "true"})
   226  	gobottest.Assert(t, err, nil)
   227  }