|
|
@@ -6,10 +6,13 @@ import (
|
|
|
)
|
|
|
|
|
|
type fakeTransport struct {
|
|
|
- reportIDs []byte
|
|
|
- reports [][]byte
|
|
|
- response []byte
|
|
|
- readCalls int
|
|
|
+ reportIDs []byte
|
|
|
+ reports [][]byte
|
|
|
+ response []byte
|
|
|
+ queue [][]byte
|
|
|
+ readCalls int
|
|
|
+ readErr error
|
|
|
+ readErrAfter int
|
|
|
}
|
|
|
|
|
|
func (f *fakeTransport) SendReport(reportID byte, report []byte) (int, error) {
|
|
|
@@ -20,6 +23,14 @@ func (f *fakeTransport) SendReport(reportID byte, report []byte) (int, error) {
|
|
|
|
|
|
func (f *fakeTransport) Read(buf []byte) (int, error) {
|
|
|
f.readCalls++
|
|
|
+ if f.readErr != nil && f.readCalls > f.readErrAfter {
|
|
|
+ return 0, f.readErr
|
|
|
+ }
|
|
|
+ if len(f.queue) > 0 {
|
|
|
+ next := f.queue[0]
|
|
|
+ f.queue = f.queue[1:]
|
|
|
+ return copy(buf, next), nil
|
|
|
+ }
|
|
|
if len(f.response) > 0 {
|
|
|
return copy(buf, f.response), nil
|
|
|
}
|
|
|
@@ -35,7 +46,7 @@ func TestSetValueUsesQMKRGBLightPayload(t *testing.T) {
|
|
|
transport := &fakeTransport{}
|
|
|
protocol := Protocol{handle: transport}
|
|
|
|
|
|
- if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
|
|
|
+ if err := protocol.SetValue(ChannelRgblight, 0x02, 0x00); err != nil {
|
|
|
t.Fatalf("SetValue() error = %v", err)
|
|
|
}
|
|
|
|
|
|
@@ -65,7 +76,7 @@ func TestSetValueConsumesQMKResponse(t *testing.T) {
|
|
|
transport := &fakeTransport{response: response}
|
|
|
protocol := Protocol{handle: transport}
|
|
|
|
|
|
- if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
|
|
|
+ if err := protocol.SetValue(ChannelRgblight, 0x02, 0x00); err != nil {
|
|
|
t.Fatalf("SetValue() error = %v", err)
|
|
|
}
|
|
|
if transport.readCalls != 1 {
|
|
|
@@ -73,31 +84,11 @@ func TestSetValueConsumesQMKResponse(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-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 {
|
|
|
+ if err := protocol.SetColor(ChannelRgblight, 0x2a, 0x80); err != nil {
|
|
|
t.Fatalf("SetColor() error = %v", err)
|
|
|
}
|
|
|
|
|
|
@@ -113,55 +104,6 @@ func TestSetColorUsesQMKColorValue(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-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
|
|
|
@@ -171,7 +113,7 @@ func TestGetValueReturnsQMKValueData(t *testing.T) {
|
|
|
transport := &fakeTransport{response: response}
|
|
|
protocol := Protocol{handle: transport}
|
|
|
|
|
|
- got, err := protocol.GetValue(RGBLight, 0x01)
|
|
|
+ got, err := protocol.GetValue(ChannelRgblight, 0x01)
|
|
|
if err != nil {
|
|
|
t.Fatalf("GetValue() error = %v", err)
|
|
|
}
|
|
|
@@ -193,7 +135,7 @@ func TestGetValueRejectsUnhandledResponse(t *testing.T) {
|
|
|
response[0] = 0xff
|
|
|
protocol := Protocol{handle: &fakeTransport{response: response}}
|
|
|
|
|
|
- if _, err := protocol.GetValue(RGBLight, 0x01); err == nil {
|
|
|
+ if _, err := protocol.GetValue(ChannelRgblight, 0x01); err == nil {
|
|
|
t.Fatal("GetValue() error = nil, want unhandled response error")
|
|
|
}
|
|
|
}
|
|
|
@@ -202,12 +144,12 @@ func TestSetValueWritesEachImpact80EffectChannel(t *testing.T) {
|
|
|
transport := &fakeTransport{}
|
|
|
protocol := Protocol{handle: transport}
|
|
|
cases := []struct {
|
|
|
- channel LEDType
|
|
|
+ channel Channel
|
|
|
effect byte
|
|
|
}{
|
|
|
- {RGBLight, 4},
|
|
|
- {RGBMatrix, 5},
|
|
|
- {SideLight, 4},
|
|
|
+ {ChannelRgblight, 4},
|
|
|
+ {ChannelRgbMatrix, 5},
|
|
|
+ {ChannelAudio, 4},
|
|
|
}
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
@@ -237,7 +179,7 @@ func TestGetValueReturnsTwoByteColor(t *testing.T) {
|
|
|
transport := &fakeTransport{response: response}
|
|
|
protocol := Protocol{handle: transport}
|
|
|
|
|
|
- got, err := protocol.GetValue(RGBMatrix, 0x04)
|
|
|
+ got, err := protocol.GetValue(ChannelRgbMatrix, 0x04)
|
|
|
if err != nil {
|
|
|
t.Fatalf("GetValue() error = %v", err)
|
|
|
}
|