| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package rgb
- import (
- "path/filepath"
- "strings"
- "testing"
- "netdome.biz/paul/qmk-rgb/internal/via"
- )
- // impact80 is the catalog the vendored definition file produces, because that
- // file is where every effect name now comes from.
- func impact80(t *testing.T) *Catalog {
- t.Helper()
- def, err := LoadDefinition(filepath.Join("..", "..", "definitions", "impact80.json"))
- if err != nil {
- t.Fatalf("LoadDefinition(definitions/impact80.json) error = %v", err)
- }
- return def.Catalog
- }
- func TestCatalogHasTheVendorEffectFamilies(t *testing.T) {
- catalog := impact80(t)
- // The vendor's definition is the whole catalog, so its counts are the
- // catalog's counts and there is nothing beside it.
- if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 46 {
- t.Errorf("backlight effects = %d, want 46", got)
- }
- if got := len(catalog.Names(via.ChannelRgblight)); got != 7 {
- t.Errorf("logo effects = %d, want 7", got)
- }
- if got := len(catalog.Names(via.ChannelAudio)); got != 7 {
- t.Errorf("side effects = %d, want 7", got)
- }
- }
- // The board takes one effect ID more than its definition names, and the tool has
- // no source for that one's name. It says "unknown" rather than inventing a label,
- // which is the whole rule: a name the vendor does not publish is a name the tool
- // would be making up.
- func TestEffectTheDefinitionDoesNotNameIsUnknown(t *testing.T) {
- catalog := impact80(t)
- tests := []struct {
- id uint8
- want string
- }{
- {45, "riverflow"},
- {46, unknownEffectName},
- {47, unknownEffectName},
- }
- for _, tt := range tests {
- if got := catalog.EffectName(via.ChannelRgbMatrix, tt.id); got != tt.want {
- t.Errorf("EffectName(rgb_matrix, %d) = %q, want %q", tt.id, got, tt.want)
- }
- }
- if _, ok := catalog.EffectID(via.ChannelRgbMatrix, "freeze"); ok {
- t.Error("EffectID(rgb_matrix, \"freeze\") = found, want not found: no source names that ID")
- }
- }
- // Nothing is compiled in for any board, so a board the data directory does not
- // describe has no names at all and is driven through raw effect IDs.
- func TestThereIsNoCatalogWithoutADefinition(t *testing.T) {
- if catalog := NewCatalog("", nil); len(catalog.Effects(via.ChannelRgbMatrix)) != 0 {
- t.Error("a catalog with no entries has effects, want none")
- }
- if _, ok := impact80(t).EffectID(via.ChannelRgbMatrix, "riverflow"); !ok {
- t.Error("the vendored definition does not name riverflow, want it to")
- }
- }
- func TestCatalogIDResolvesNamesAndAliases(t *testing.T) {
- catalog := impact80(t)
- tests := []struct {
- channel via.Channel
- name string
- want uint8
- }{
- {via.ChannelRgblight, "light", 5},
- {via.ChannelRgblight, "solid", 5},
- {via.ChannelRgblight, "breathe", 4},
- {via.ChannelRgbMatrix, "solid_color", 1},
- {via.ChannelRgbMatrix, "rainbow_moving_chevron", 17},
- }
- for _, tt := range tests {
- got, ok := catalog.EffectID(tt.channel, tt.name)
- if !ok {
- t.Errorf("EffectID(%d, %q) not found", tt.channel, tt.name)
- continue
- }
- if got != tt.want {
- t.Errorf("EffectID(%d, %q) = %d, want %d", tt.channel, tt.name, got, tt.want)
- }
- }
- }
- func TestCatalogEffectNameReportsUnknownForAnUnknownID(t *testing.T) {
- catalog := impact80(t)
- if got := catalog.EffectName(via.ChannelRgbMatrix, 200); got != "unknown" {
- t.Errorf("EffectName(3, 200) = %q, want %q", got, "unknown")
- }
- }
- func TestCatalogDefaultEffectPerChannel(t *testing.T) {
- catalog := impact80(t)
- // The first effect the board's own list names that is not the off entry.
- if got, ok := catalog.DefaultEffect(via.ChannelRgblight); !ok || got != 1 {
- t.Errorf("DefaultEffect(2) = %d, %t, want 1, true", got, ok)
- }
- if got, ok := catalog.DefaultEffect(via.ChannelRgbMatrix); !ok || got != 1 {
- t.Errorf("DefaultEffect(3) = %d, %t, want 1, true", got, ok)
- }
- }
- func TestResolveEffectWithoutACatalogRefuses(t *testing.T) {
- _, _, err := ResolveEffect(nil, "wave", []via.Channel{via.ChannelRgblight}, false)
- if err == nil {
- t.Fatal("ResolveEffect(nil, ...) expected an error, got nil")
- }
- if !strings.Contains(err.Error(), "effect <index>") {
- t.Errorf("error = %q, want it to point at the ID form", err)
- }
- }
- // ID 5 is light on channel 2 and rainbow_beacon on channel 3, so the same name
- // cannot be applied to both.
- func TestResolveEffectSkipsAChannelThatDoesNotSupportTheName(t *testing.T) {
- catalog := impact80(t)
- targets, skipped, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}, false)
- if err != nil {
- t.Fatalf("ResolveEffect() error = %v", err)
- }
- if len(targets) != 1 || targets[0].Channel != via.ChannelRgblight || targets[0].ID != 5 {
- t.Errorf("targets = %+v, want one target on channel 2 with id 5", targets)
- }
- if len(skipped) != 1 || skipped[0] != "rgb_matrix" {
- t.Errorf("skipped = %v, want [rgb_matrix]", skipped)
- }
- }
- // Asking for the unsupported channel by name is an error, not a skip: a command
- // that quietly did nothing would look like a command that worked.
- func TestResolveEffectRejectsTheOnlyChannelWhenItDoesNotSupportTheName(t *testing.T) {
- catalog := impact80(t)
- _, _, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgbMatrix}, true)
- if err == nil {
- t.Fatal("ResolveEffect() expected an error, want light rejected on the backlight")
- }
- }
- func TestResolveEffectRejectsANameNoChannelKnows(t *testing.T) {
- catalog := impact80(t)
- if _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true); err == nil {
- t.Fatal("ResolveEffect() expected an error for an unknown name")
- }
- }
- // "static" is a documented alias. It must resolve through every caller, not
- // only the one that rewrites the string before calling: the backlight catalog
- // spells its solid effect solid_color, so the alias chain needs both steps.
- func TestResolveEffectAcceptsTheStaticAliasOnEveryPath(t *testing.T) {
- catalog := impact80(t)
- for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio} {
- targets, _, err := ResolveEffect(catalog, "static", []via.Channel{ch}, true)
- if err != nil {
- t.Errorf("ResolveEffect(static on channel %d) error = %v", ch, err)
- continue
- }
- if len(targets) != 1 {
- t.Errorf("ResolveEffect(static on channel %d) = %d targets, want 1", ch, len(targets))
- }
- }
- }
- // Whether a command may skip an unsupported effect is the caller's knowledge,
- // not something the channel count can tell it. A board with a single lighting
- // channel and no zone named must still behave like a default command.
- func TestResolveEffectDistinguishesExplicitFromDefault(t *testing.T) {
- catalog := impact80(t)
- both := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}
- // Default command, one channel cannot do it: skipped, not refused.
- targets, skipped, err := ResolveEffect(catalog, "rainbow_moving_chevron", both, false)
- if err != nil {
- t.Errorf("default command error = %v, want the unsupported channel skipped", err)
- }
- if len(skipped) != 1 || skipped[0] != "rgblight" {
- t.Errorf("skipped = %v, want [rgblight]", skipped)
- }
- if len(targets) != 1 || targets[0].Channel != via.ChannelRgbMatrix {
- t.Errorf("targets = %+v, want the backlight only", targets)
- }
- // Same name, named channel only: refused.
- if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true); err == nil {
- t.Error("explicit request error = nil, want it refused")
- }
- // One channel reported by a board that has the name elsewhere: skipped with a
- // warning rather than called unknown, and never silently applied.
- targets, skipped, err = ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false)
- if err != nil {
- t.Errorf("single-channel default command error = %v, want a skip", err)
- }
- if len(targets) != 0 || len(skipped) != 1 {
- t.Errorf("targets = %+v, skipped = %v, want nothing applied and one skip", targets, skipped)
- }
- }
- // A name the board has but the named channel does not is not an unknown effect:
- // the user named a real effect and put it on the wrong channel, and that is a
- // different mistake with a different fix.
- func TestResolveEffectSaysTheChannelDoesNotSupportTheName(t *testing.T) {
- catalog := impact80(t)
- _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true)
- if err == nil {
- t.Fatal("ResolveEffect() expected an error, got nil")
- }
- if !strings.Contains(err.Error(), "not supported on rgblight") {
- t.Errorf("error = %q, want it to name the channel that cannot do it", err)
- }
- }
- // And a name no channel of the board has stays an unknown effect.
- func TestResolveEffectKeepsUnknownEffectForANameTheBoardLacks(t *testing.T) {
- catalog := impact80(t)
- _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true)
- if err == nil {
- t.Fatal("ResolveEffect() expected an error, got nil")
- }
- if !strings.Contains(err.Error(), "unknown effect") {
- t.Errorf("error = %q, want it to stay an unknown effect", err)
- }
- }
|