|
@@ -1,9 +1,13 @@
|
|
|
package rgb
|
|
package rgb
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "reflect"
|
|
|
|
|
+ "strings"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra"
|
|
|
|
|
+ intrgb "github.com/wobkey/rgb/internal/rgb"
|
|
|
|
|
+ "github.com/wobkey/rgb/internal/via"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func TestLightingEnabledRequiresActiveEffectAndBrightness(t *testing.T) {
|
|
func TestLightingEnabledRequiresActiveEffectAndBrightness(t *testing.T) {
|
|
@@ -52,3 +56,112 @@ func TestDeviceFlagIsInheritedByRGBCommands(t *testing.T) {
|
|
|
t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
|
|
t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestSelectedZonesDefaultsToAll(t *testing.T) {
|
|
|
|
|
+ originalTargetZone := targetZone
|
|
|
|
|
+ t.Cleanup(func() { targetZone = originalTargetZone })
|
|
|
|
|
+ targetZone = ""
|
|
|
|
|
+
|
|
|
|
|
+ got, err := selectedZones()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("selectedZones() unexpected error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ want := []intrgb.Zone{intrgb.ZoneLogo, intrgb.ZoneBacklight, intrgb.ZoneSide}
|
|
|
|
|
+ if !reflect.DeepEqual(got, want) {
|
|
|
|
|
+ t.Errorf("selectedZones() = %v, want %v", got, want)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestSelectedZonesRejectsUnknownZone(t *testing.T) {
|
|
|
|
|
+ originalTargetZone := targetZone
|
|
|
|
|
+ t.Cleanup(func() { targetZone = originalTargetZone })
|
|
|
|
|
+ targetZone = "matrix"
|
|
|
|
|
+
|
|
|
|
|
+ got, err := selectedZones()
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Fatal("selectedZones() expected error, got nil")
|
|
|
|
|
+ }
|
|
|
|
|
+ if got != nil {
|
|
|
|
|
+ t.Errorf("selectedZones() = %v, want nil", got)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestSelectedZonesExplicitZones(t *testing.T) {
|
|
|
|
|
+ originalTargetZone := targetZone
|
|
|
|
|
+ t.Cleanup(func() { targetZone = originalTargetZone })
|
|
|
|
|
+
|
|
|
|
|
+ cases := []struct {
|
|
|
|
|
+ name string
|
|
|
|
|
+ want intrgb.Zone
|
|
|
|
|
+ }{
|
|
|
|
|
+ {"logo", intrgb.ZoneLogo},
|
|
|
|
|
+ {"backlight", intrgb.ZoneBacklight},
|
|
|
|
|
+ {"side", intrgb.ZoneSide},
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, tc := range cases {
|
|
|
|
|
+ t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
+ targetZone = tc.name
|
|
|
|
|
+ got, err := selectedZones()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("selectedZones() unexpected error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ want := []intrgb.Zone{tc.want}
|
|
|
|
|
+ if !reflect.DeepEqual(got, want) {
|
|
|
|
|
+ t.Errorf("selectedZones() = %v, want %v", got, want)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestZoneChannels(t *testing.T) {
|
|
|
|
|
+ got := zoneChannels(intrgb.AllZones())
|
|
|
|
|
+ want := []via.LEDType{2, 3, 4}
|
|
|
|
|
+ if !reflect.DeepEqual(got, want) {
|
|
|
|
|
+ t.Errorf("zoneChannels() = %v, want %v", got, want)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestZoneFlagIsInheritedByRGBCommands(t *testing.T) {
|
|
|
|
|
+ originalTargetDevice := targetDevice
|
|
|
|
|
+ originalTargetZone := targetZone
|
|
|
|
|
+ t.Cleanup(func() {
|
|
|
|
|
+ targetDevice = originalTargetDevice
|
|
|
|
|
+ targetZone = originalTargetZone
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const wantPath = "/dev/hidraw5"
|
|
|
|
|
+ ran := false
|
|
|
|
|
+ cmd := Init()
|
|
|
|
|
+ cmd.AddCommand(&cobra.Command{
|
|
|
|
|
+ Use: "probe",
|
|
|
|
|
+ Run: func(*cobra.Command, []string) { ran = true },
|
|
|
|
|
+ })
|
|
|
|
|
+ cmd.SetArgs([]string{"--device", wantPath, "--zone", "backlight", "probe"})
|
|
|
|
|
+
|
|
|
|
|
+ if err := cmd.Execute(); err != nil {
|
|
|
|
|
+ t.Fatalf("Execute() error = %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if !ran {
|
|
|
|
|
+ t.Fatal("child command did not run")
|
|
|
|
|
+ }
|
|
|
|
|
+ if targetDevice != wantPath {
|
|
|
|
|
+ t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
|
|
|
|
|
+ }
|
|
|
|
|
+ if targetZone != "backlight" {
|
|
|
|
|
+ t.Errorf("targetZone = %q, want %q", targetZone, "backlight")
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestZoneValidationPrecedesDeviceOpening(t *testing.T) {
|
|
|
|
|
+ originalTargetZone := targetZone
|
|
|
|
|
+ t.Cleanup(func() { targetZone = originalTargetZone })
|
|
|
|
|
+ targetZone = "matrix"
|
|
|
|
|
+
|
|
|
|
|
+ _, _, err := OpenDevice()
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ t.Fatal("OpenDevice() expected zone error, got nil")
|
|
|
|
|
+ }
|
|
|
|
|
+ if !strings.Contains(err.Error(), "unknown zone") {
|
|
|
|
|
+ t.Errorf("OpenDevice() error = %v, want unknown zone error", err)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|