channel_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package via
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. )
  7. // probeQueue scripts one answer per channel the probe visits, so a test can say
  8. // which channels the keyboard has.
  9. func probeQueue(present ...Channel) [][]byte {
  10. queue := make([][]byte, 0, probeChannelMax)
  11. for c := 1; c <= probeChannelMax; c++ {
  12. buf := make([]byte, 32)
  13. if containsChannel(present, Channel(c)) {
  14. buf[0] = byte(CustomGet)
  15. buf[3] = 160
  16. } else {
  17. buf[0] = byte(Unhandled)
  18. }
  19. buf[1] = byte(c)
  20. buf[2] = probeValueID
  21. queue = append(queue, buf)
  22. }
  23. return queue
  24. }
  25. func containsChannel(channels []Channel, want Channel) bool {
  26. for _, c := range channels {
  27. if c == want {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. func TestDetectChannelsFindsTheAnsweredChannels(t *testing.T) {
  34. transport := &fakeTransport{queue: probeQueue(ChannelRgblight, ChannelRgbMatrix, ChannelAudio)}
  35. protocol := Protocol{handle: transport}
  36. got, err := protocol.DetectChannels()
  37. if err != nil {
  38. t.Fatalf("DetectChannels() error = %v", err)
  39. }
  40. want := []Channel{ChannelRgblight, ChannelRgbMatrix, ChannelAudio}
  41. if len(got) != len(want) {
  42. t.Fatalf("DetectChannels() = %v, want %v", got, want)
  43. }
  44. for i := range want {
  45. if got[i] != want[i] {
  46. t.Errorf("DetectChannels()[%d] = %d, want %d", i, got[i], want[i])
  47. }
  48. }
  49. }
  50. func TestDetectChannelsAsksBrightnessOnEveryChannelFromOneToFifteen(t *testing.T) {
  51. transport := &fakeTransport{queue: probeQueue()}
  52. protocol := Protocol{handle: transport}
  53. if _, err := protocol.DetectChannels(); err != nil {
  54. t.Fatalf("DetectChannels() error = %v", err)
  55. }
  56. if len(transport.reports) != probeChannelMax {
  57. t.Fatalf("requests = %d, want %d", len(transport.reports), probeChannelMax)
  58. }
  59. for i, report := range transport.reports {
  60. wantChannel := byte(i + 1)
  61. if report[0] != byte(CustomGet) {
  62. t.Errorf("request %d command = 0x%02x, want 0x%02x", i, report[0], CustomGet)
  63. }
  64. if report[1] != wantChannel {
  65. t.Errorf("request %d channel = %d, want %d", i, report[1], wantChannel)
  66. }
  67. if report[2] != probeValueID {
  68. t.Errorf("request %d value = 0x%02x, want 0x%02x (brightness)", i, report[2], probeValueID)
  69. }
  70. }
  71. }
  72. // A channel list missing entries is indistinguishable from a complete one, so a
  73. // transport failure has to abort the probe instead of shrinking it.
  74. func TestDetectChannelsFailsOnATransportErrorMidProbe(t *testing.T) {
  75. transport := &fakeTransport{
  76. queue: probeQueue(ChannelRgblight, ChannelRgbMatrix, ChannelAudio),
  77. readErr: errors.New("read: interrupted system call"),
  78. readErrAfter: 3,
  79. }
  80. protocol := Protocol{handle: transport}
  81. _, err := protocol.DetectChannels()
  82. if err == nil {
  83. t.Fatal("DetectChannels() expected an error, got nil")
  84. }
  85. if !strings.Contains(err.Error(), "channel 4") {
  86. t.Errorf("error = %q, want it to name the channel that failed", err)
  87. }
  88. }
  89. func TestDetectChannelsReportsNoChannelsWhenNoneArePresent(t *testing.T) {
  90. transport := &fakeTransport{queue: probeQueue()}
  91. protocol := Protocol{handle: transport}
  92. got, err := protocol.DetectChannels()
  93. if err != nil {
  94. t.Fatalf("DetectChannels() error = %v", err)
  95. }
  96. if len(got) != 0 {
  97. t.Errorf("DetectChannels() = %v, want empty", got)
  98. }
  99. }
  100. func TestChannelSubsystemNames(t *testing.T) {
  101. tests := []struct {
  102. channel Channel
  103. want string
  104. }{
  105. {ChannelBacklight, "backlight"},
  106. {ChannelRgblight, "rgblight"},
  107. {ChannelRgbMatrix, "rgb_matrix"},
  108. {ChannelAudio, "audio"},
  109. {ChannelLedMatrix, "led_matrix"},
  110. {Channel(9), ""},
  111. }
  112. for _, tt := range tests {
  113. if got := tt.channel.Subsystem(); got != tt.want {
  114. t.Errorf("Channel(%d).Subsystem() = %q, want %q", tt.channel, got, tt.want)
  115. }
  116. }
  117. }