| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package via
- import (
- "bytes"
- "testing"
- )
- type fakeTransport struct {
- reportIDs []byte
- reports [][]byte
- response []byte
- readCalls int
- }
- func (f *fakeTransport) SendReport(reportID byte, report []byte) (int, error) {
- f.reportIDs = append(f.reportIDs, reportID)
- f.reports = append(f.reports, bytes.Clone(report))
- return len(report), nil
- }
- func (f *fakeTransport) Read(buf []byte) (int, error) {
- f.readCalls++
- if len(f.response) > 0 {
- return copy(buf, f.response), nil
- }
- if len(f.reports) > 0 {
- return copy(buf, f.reports[len(f.reports)-1]), nil
- }
- return copy(buf, make([]byte, 32)), nil
- }
- func (f *fakeTransport) Close() error { return nil }
- func TestSetValueUsesQMKRGBLightPayload(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
- t.Fatalf("SetValue() error = %v", err)
- }
- want := make([]byte, 32)
- want[0] = 0x07
- want[1] = 0x02
- want[2] = 0x02
- want[3] = 0x00
- if len(transport.reports) != 1 {
- t.Fatalf("SendReport() calls = %d, want 1", len(transport.reports))
- }
- if transport.reportIDs[0] != 0x00 {
- t.Errorf("report ID = 0x%02x, want 0x00", transport.reportIDs[0])
- }
- if !bytes.Equal(transport.reports[0], want) {
- t.Errorf("report = %v, want %v", transport.reports[0], want)
- }
- }
- func TestSetValueConsumesQMKResponse(t *testing.T) {
- response := make([]byte, 32)
- response[0] = 0x07
- response[1] = 0x02
- response[2] = 0x02
- response[3] = 0x00
- transport := &fakeTransport{response: response}
- protocol := Protocol{handle: transport}
- if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
- t.Fatalf("SetValue() error = %v", err)
- }
- if transport.readCalls != 1 {
- t.Errorf("Read() calls = %d, want 1", transport.readCalls)
- }
- }
- func TestSetLightingColorUsesAllImpact80Channels(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- if err := protocol.SetLightingColor(0x55, 0xff); err != nil {
- t.Fatalf("SetLightingColor() error = %v", err)
- }
- wantChannels := []byte{0x02, 0x03, 0x04}
- if len(transport.reports) != len(wantChannels) {
- t.Fatalf("SendReport() calls = %d, want %d", len(transport.reports), len(wantChannels))
- }
- for i, channel := range wantChannels {
- report := transport.reports[i]
- if report[0] != 0x07 || report[1] != channel || report[2] != 0x04 || report[3] != 0x55 || report[4] != 0xff {
- t.Errorf("color report %d = %v, want channel 0x%02x hue 0x55 saturation 0xff", i, report, channel)
- }
- }
- }
- func TestSetColorUsesQMKColorValue(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- if err := protocol.SetColor(RGBLight, 0x2a, 0x80); err != nil {
- t.Fatalf("SetColor() error = %v", err)
- }
- want := make([]byte, 32)
- want[0] = 0x07
- want[1] = 0x02
- want[2] = 0x04
- want[3] = 0x2a
- want[4] = 0x80
- if !bytes.Equal(transport.reports[0], want) {
- t.Errorf("report = %v, want %v", transport.reports[0], want)
- }
- }
- func TestDisableLightingUsesAllImpact80Channels(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- if err := protocol.DisableLighting(); err != nil {
- t.Fatalf("DisableLighting() error = %v", err)
- }
- if len(transport.reports) != 6 {
- t.Fatalf("SendReport() calls = %d, want 6", len(transport.reports))
- }
- wantChannels := []byte{0x02, 0x03, 0x04}
- for i, channel := range wantChannels {
- effect := transport.reports[i*2]
- brightness := transport.reports[i*2+1]
- if effect[1] != channel || effect[2] != 0x02 || effect[3] != 0x00 {
- t.Errorf("effect report %d = %v, want channel 0x%02x effect 0", i, effect, channel)
- }
- if brightness[1] != channel || brightness[2] != 0x01 || brightness[3] != 0x00 {
- t.Errorf("brightness report %d = %v, want channel 0x%02x brightness 0", i, brightness, channel)
- }
- }
- }
- func TestEnableLightingUsesAllImpact80Channels(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- if err := protocol.EnableLighting(); err != nil {
- t.Fatalf("EnableLighting() error = %v", err)
- }
- if len(transport.reports) != 6 {
- t.Fatalf("SendReport() calls = %d, want 6", len(transport.reports))
- }
- wantChannels := []byte{0x02, 0x03, 0x04}
- wantEffects := []byte{0x04, 0x05, 0x04}
- for i, channel := range wantChannels {
- effect := transport.reports[i*2]
- brightness := transport.reports[i*2+1]
- if effect[1] != channel || effect[2] != 0x02 || effect[3] != wantEffects[i] {
- t.Errorf("effect report %d = %v, want channel 0x%02x effect %d", i, effect, channel, wantEffects[i])
- }
- if brightness[1] != channel || brightness[2] != 0x01 || brightness[3] != 160 {
- t.Errorf("brightness report %d = %v, want channel 0x%02x brightness 160", i, brightness, channel)
- }
- }
- }
- func TestGetValueReturnsQMKValueData(t *testing.T) {
- response := make([]byte, 32)
- response[0] = 0x08
- response[1] = 0x02
- response[2] = 0x01
- response[3] = 0xc8
- transport := &fakeTransport{response: response}
- protocol := Protocol{handle: transport}
- got, err := protocol.GetValue(RGBLight, 0x01)
- if err != nil {
- t.Fatalf("GetValue() error = %v", err)
- }
- if !bytes.Equal(got, []byte{0xc8}) {
- t.Errorf("GetValue() = %v, want [192]", got)
- }
- want := make([]byte, 32)
- want[0] = 0x08
- want[1] = 0x02
- want[2] = 0x01
- if !bytes.Equal(transport.reports[0], want) {
- t.Errorf("request = %v, want %v", transport.reports[0], want)
- }
- }
- func TestGetValueRejectsUnhandledResponse(t *testing.T) {
- response := make([]byte, 32)
- response[0] = 0xff
- protocol := Protocol{handle: &fakeTransport{response: response}}
- if _, err := protocol.GetValue(RGBLight, 0x01); err == nil {
- t.Fatal("GetValue() error = nil, want unhandled response error")
- }
- }
- func TestSetValueWritesEachImpact80EffectChannel(t *testing.T) {
- transport := &fakeTransport{}
- protocol := Protocol{handle: transport}
- cases := []struct {
- channel LEDType
- effect byte
- }{
- {RGBLight, 4},
- {RGBMatrix, 5},
- {SideLight, 4},
- }
- for _, tc := range cases {
- if err := protocol.SetValue(tc.channel, 0x02, tc.effect); err != nil {
- t.Fatalf("SetValue(%d) error = %v", tc.channel, err)
- }
- }
- if len(transport.reports) != len(cases) {
- t.Fatalf("SendReport() calls = %d, want %d", len(transport.reports), len(cases))
- }
- for i, tc := range cases {
- report := transport.reports[i]
- if report[0] != 0x07 || report[1] != byte(tc.channel) || report[2] != 0x02 || report[3] != tc.effect {
- t.Errorf("effect report %d = %v, want channel 0x%02x effect %d", i, report, tc.channel, tc.effect)
- }
- }
- }
- func TestGetValueReturnsTwoByteColor(t *testing.T) {
- response := make([]byte, 32)
- response[0] = 0x08
- response[1] = 0x03
- response[2] = 0x04
- response[3] = 0x2a
- response[4] = 0x80
- transport := &fakeTransport{response: response}
- protocol := Protocol{handle: transport}
- got, err := protocol.GetValue(RGBMatrix, 0x04)
- if err != nil {
- t.Fatalf("GetValue() error = %v", err)
- }
- if !bytes.Equal(got, []byte{0x2a, 0x80}) {
- t.Errorf("GetValue() = %v, want [42 128]", got)
- }
- want := make([]byte, 32)
- want[0] = 0x08
- want[1] = 0x03
- want[2] = 0x04
- if !bytes.Equal(transport.reports[0], want) {
- t.Errorf("request = %v, want %v", transport.reports[0], want)
- }
- }
|