| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package device
- import (
- "encoding/json"
- "strings"
- "testing"
- )
- func TestIndexDevices(t *testing.T) {
- tests := []struct {
- name string
- devices []Device
- want []string
- }{
- {
- name: "empty",
- devices: nil,
- want: nil,
- },
- {
- name: "single",
- devices: []Device{{Path: "/dev/hidraw7", VendorID: 0x36b0, ProductID: 0x309f}},
- want: []string{"/dev/hidraw7"},
- },
- {
- name: "sorted by vendor then product then path",
- devices: []Device{
- {Path: "/dev/hidraw9", VendorID: 0x9999, ProductID: 0x0001},
- {Path: "/dev/hidraw5", VendorID: 0x1111, ProductID: 0x0002},
- {Path: "/dev/hidraw2", VendorID: 0x1111, ProductID: 0x0001},
- },
- want: []string{"/dev/hidraw2", "/dev/hidraw5", "/dev/hidraw9"},
- },
- {
- name: "identical vendor and product sorted by path",
- devices: []Device{
- {Path: "/dev/hidrawB", VendorID: 0x36b0, ProductID: 0x309f},
- {Path: "/dev/hidrawA", VendorID: 0x36b0, ProductID: 0x309f},
- },
- want: []string{"/dev/hidrawA", "/dev/hidrawB"},
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := indexDevices(tt.devices)
- if len(got) != len(tt.want) {
- t.Fatalf("indexDevices() returned %d devices, want %d", len(got), len(tt.want))
- }
- for i, wantPath := range tt.want {
- if got[i].Path != wantPath {
- t.Errorf("indexDevices()[%d].Path = %q, want %q", i, got[i].Path, wantPath)
- }
- if got[i].Index != i+1 {
- t.Errorf("indexDevices()[%d].Index = %d, want %d", i, got[i].Index, i+1)
- }
- }
- })
- }
- }
- func TestIndexDevicesDoesNotMutateInput(t *testing.T) {
- input := []Device{
- {Path: "/dev/hidraw9", VendorID: 0x9999, ProductID: 0x0001},
- {Path: "/dev/hidraw2", VendorID: 0x1111, ProductID: 0x0001},
- }
- indexDevices(input)
- if input[0].Path != "/dev/hidraw9" {
- t.Errorf("indexDevices() mutated input: input[0].Path = %q, want %q", input[0].Path, "/dev/hidraw9")
- }
- if input[0].Index != 0 {
- t.Errorf("indexDevices() mutated input: input[0].Index = %d, want 0", input[0].Index)
- }
- }
- func TestDeviceJSONFieldNames(t *testing.T) {
- dev := Device{
- Index: 1,
- Path: "/dev/hidraw7",
- VendorID: 0x36b0,
- ProductID: 0x309f,
- Name: "Impact 80",
- }
- data, err := json.Marshal(dev)
- if err != nil {
- t.Fatalf("Marshal() returned error: %v", err)
- }
- var got map[string]any
- if err := json.Unmarshal(data, &got); err != nil {
- t.Fatalf("Unmarshal() returned error: %v", err)
- }
- for _, field := range []string{"index", "path", "vendorId", "productId", "name"} {
- if _, ok := got[field]; !ok {
- t.Errorf("Device JSON = %s, missing field %q", data, field)
- }
- }
- if _, ok := got["supported"]; ok {
- t.Errorf("Device JSON = %s, must not contain the misleading field \"supported\"", data)
- }
- }
- // A keyboard that reports no USB product string has no name, and the field is
- // omitted rather than sent empty: a consumer can tell "no name" from a name.
- func TestDeviceJSONOmitsUnnamedName(t *testing.T) {
- data, err := json.Marshal(Device{Index: 2, Path: "/dev/hidraw9"})
- if err != nil {
- t.Fatalf("Marshal() returned error: %v", err)
- }
- if strings.Contains(string(data), `"name"`) {
- t.Errorf("Device JSON = %s, want the name omitted for a keyboard that reports none", data)
- }
- }
- // The name is the keyboard's own, from the USB product string, so a data file
- // cannot go stale and disagree with the hardware.
- func TestDeviceTakesTheNameFromTheProductString(t *testing.T) {
- d := Device{Index: 1, Path: "p", VendorID: 0x36B0, ProductID: 0x309F, Name: "Impact 80"}
- if d.Name != "Impact 80" {
- t.Errorf("Name = %q, want the product string the keyboard reports", d.Name)
- }
- }
|