protocol_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package via
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. type fakeTransport struct {
  7. reportIDs []byte
  8. reports [][]byte
  9. response []byte
  10. readCalls int
  11. }
  12. func (f *fakeTransport) SendReport(reportID byte, report []byte) (int, error) {
  13. f.reportIDs = append(f.reportIDs, reportID)
  14. f.reports = append(f.reports, bytes.Clone(report))
  15. return len(report), nil
  16. }
  17. func (f *fakeTransport) Read(buf []byte) (int, error) {
  18. f.readCalls++
  19. if len(f.response) > 0 {
  20. return copy(buf, f.response), nil
  21. }
  22. if len(f.reports) > 0 {
  23. return copy(buf, f.reports[len(f.reports)-1]), nil
  24. }
  25. return copy(buf, make([]byte, 32)), nil
  26. }
  27. func (f *fakeTransport) Close() error { return nil }
  28. func TestSetValueUsesQMKRGBLightPayload(t *testing.T) {
  29. transport := &fakeTransport{}
  30. protocol := Protocol{handle: transport}
  31. if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
  32. t.Fatalf("SetValue() error = %v", err)
  33. }
  34. want := make([]byte, 32)
  35. want[0] = 0x07
  36. want[1] = 0x02
  37. want[2] = 0x02
  38. want[3] = 0x00
  39. if len(transport.reports) != 1 {
  40. t.Fatalf("SendReport() calls = %d, want 1", len(transport.reports))
  41. }
  42. if transport.reportIDs[0] != 0x00 {
  43. t.Errorf("report ID = 0x%02x, want 0x00", transport.reportIDs[0])
  44. }
  45. if !bytes.Equal(transport.reports[0], want) {
  46. t.Errorf("report = %v, want %v", transport.reports[0], want)
  47. }
  48. }
  49. func TestSetValueConsumesQMKResponse(t *testing.T) {
  50. response := make([]byte, 32)
  51. response[0] = 0x07
  52. response[1] = 0x02
  53. response[2] = 0x02
  54. response[3] = 0x00
  55. transport := &fakeTransport{response: response}
  56. protocol := Protocol{handle: transport}
  57. if err := protocol.SetValue(RGBLight, 0x02, 0x00); err != nil {
  58. t.Fatalf("SetValue() error = %v", err)
  59. }
  60. if transport.readCalls != 1 {
  61. t.Errorf("Read() calls = %d, want 1", transport.readCalls)
  62. }
  63. }
  64. func TestSetColorUsesQMKColorValue(t *testing.T) {
  65. transport := &fakeTransport{}
  66. protocol := Protocol{handle: transport}
  67. if err := protocol.SetColor(RGBLight, 0x2a, 0x80); err != nil {
  68. t.Fatalf("SetColor() error = %v", err)
  69. }
  70. want := make([]byte, 32)
  71. want[0] = 0x07
  72. want[1] = 0x02
  73. want[2] = 0x04
  74. want[3] = 0x2a
  75. want[4] = 0x80
  76. if !bytes.Equal(transport.reports[0], want) {
  77. t.Errorf("report = %v, want %v", transport.reports[0], want)
  78. }
  79. }
  80. func TestDisableLightingUsesAllImpact80Channels(t *testing.T) {
  81. transport := &fakeTransport{}
  82. protocol := Protocol{handle: transport}
  83. if err := protocol.DisableLighting(); err != nil {
  84. t.Fatalf("DisableLighting() error = %v", err)
  85. }
  86. if len(transport.reports) != 6 {
  87. t.Fatalf("SendReport() calls = %d, want 6", len(transport.reports))
  88. }
  89. wantChannels := []byte{0x02, 0x03, 0x04}
  90. for i, channel := range wantChannels {
  91. effect := transport.reports[i*2]
  92. brightness := transport.reports[i*2+1]
  93. if effect[1] != channel || effect[2] != 0x02 || effect[3] != 0x00 {
  94. t.Errorf("effect report %d = %v, want channel 0x%02x effect 0", i, effect, channel)
  95. }
  96. if brightness[1] != channel || brightness[2] != 0x01 || brightness[3] != 0x00 {
  97. t.Errorf("brightness report %d = %v, want channel 0x%02x brightness 0", i, brightness, channel)
  98. }
  99. }
  100. }
  101. func TestEnableLightingUsesAllImpact80Channels(t *testing.T) {
  102. transport := &fakeTransport{}
  103. protocol := Protocol{handle: transport}
  104. if err := protocol.EnableLighting(); err != nil {
  105. t.Fatalf("EnableLighting() error = %v", err)
  106. }
  107. if len(transport.reports) != 6 {
  108. t.Fatalf("SendReport() calls = %d, want 6", len(transport.reports))
  109. }
  110. wantChannels := []byte{0x02, 0x03, 0x04}
  111. for i, channel := range wantChannels {
  112. effect := transport.reports[i*2]
  113. brightness := transport.reports[i*2+1]
  114. if effect[1] != channel || effect[2] != 0x02 || effect[3] != 0x01 {
  115. t.Errorf("effect report %d = %v, want channel 0x%02x effect 1", i, effect, channel)
  116. }
  117. if brightness[1] != channel || brightness[2] != 0x01 || brightness[3] != 160 {
  118. t.Errorf("brightness report %d = %v, want channel 0x%02x brightness 160", i, brightness, channel)
  119. }
  120. }
  121. }
  122. func TestGetValueReturnsQMKValueData(t *testing.T) {
  123. response := make([]byte, 32)
  124. response[0] = 0x08
  125. response[1] = 0x02
  126. response[2] = 0x01
  127. response[3] = 0xc8
  128. transport := &fakeTransport{response: response}
  129. protocol := Protocol{handle: transport}
  130. got, err := protocol.GetValue(RGBLight, 0x01)
  131. if err != nil {
  132. t.Fatalf("GetValue() error = %v", err)
  133. }
  134. if !bytes.Equal(got, []byte{0xc8}) {
  135. t.Errorf("GetValue() = %v, want [192]", got)
  136. }
  137. want := make([]byte, 32)
  138. want[0] = 0x08
  139. want[1] = 0x02
  140. want[2] = 0x01
  141. if !bytes.Equal(transport.reports[0], want) {
  142. t.Errorf("request = %v, want %v", transport.reports[0], want)
  143. }
  144. }
  145. func TestGetValueRejectsUnhandledResponse(t *testing.T) {
  146. response := make([]byte, 32)
  147. response[0] = 0xff
  148. protocol := Protocol{handle: &fakeTransport{response: response}}
  149. if _, err := protocol.GetValue(RGBLight, 0x01); err == nil {
  150. t.Fatal("GetValue() error = nil, want unhandled response error")
  151. }
  152. }