|
| 1 | +// Copyright 2025 Jacek Olszak |
| 2 | +// This code is licensed under MIT license (see LICENSE for details) |
| 3 | + |
| 4 | +package pikey_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/elgopher/pi/pikey" |
| 10 | + "github.com/elgopher/pi/piloop" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + aDownEvent = pikey.Event{Type: pikey.EventDown, Key: pikey.A} |
| 17 | + aUpEvent = pikey.Event{Type: pikey.EventUp, Key: pikey.A} |
| 18 | + ctrlDownEvent = pikey.Event{Type: pikey.EventDown, Key: pikey.Ctrl} |
| 19 | +) |
| 20 | + |
| 21 | +func TestRegisterShortcut(t *testing.T) { |
| 22 | + t.Run("single key", func(t *testing.T) { |
| 23 | + executionTimes := 0 |
| 24 | + shortcut := pikey.RegisterShortcut(func() { |
| 25 | + executionTimes++ |
| 26 | + }, pikey.A) |
| 27 | + require.NotNil(t, shortcut) |
| 28 | + |
| 29 | + pikey.Target().Publish(aDownEvent) |
| 30 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 31 | + |
| 32 | + t.Run("should execute callback when key is pressed", func(t *testing.T) { |
| 33 | + assert.Equal(t, 1, executionTimes) |
| 34 | + }) |
| 35 | + |
| 36 | + executionTimes = 0 |
| 37 | + |
| 38 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 39 | + |
| 40 | + t.Run("should not execute callback again on next frame when key is still pressed", func(t *testing.T) { |
| 41 | + assert.Equal(t, 0, executionTimes) |
| 42 | + }) |
| 43 | + |
| 44 | + shortcut.Unregister() |
| 45 | + |
| 46 | + pikey.Target().Publish(aDownEvent) |
| 47 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 48 | + |
| 49 | + t.Run("should not execute callback after shortcut is unregistered", func(t *testing.T) { |
| 50 | + assert.Equal(t, 0, executionTimes) |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("multiple keys", func(t *testing.T) { |
| 55 | + executionTimes := 0 |
| 56 | + shortcut := pikey.RegisterShortcut(func() { |
| 57 | + executionTimes++ |
| 58 | + }, pikey.A, pikey.Ctrl) |
| 59 | + require.NotNil(t, shortcut) |
| 60 | + // when |
| 61 | + pikey.Target().Publish(aDownEvent) |
| 62 | + pikey.Target().Publish(ctrlDownEvent) |
| 63 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 64 | + // then |
| 65 | + assert.Equal(t, 1, executionTimes) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("should not run callback when all keys are not pressed simultaneously, but was down before", func(t *testing.T) { |
| 69 | + executionTimes := 0 |
| 70 | + shortcut := pikey.RegisterShortcut(func() { |
| 71 | + executionTimes++ |
| 72 | + }, pikey.A, pikey.Ctrl) |
| 73 | + require.NotNil(t, shortcut) |
| 74 | + |
| 75 | + pikey.Target().Publish(aDownEvent) |
| 76 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 77 | + require.Equal(t, 0, executionTimes) |
| 78 | + // when |
| 79 | + pikey.Target().Publish(ctrlDownEvent) |
| 80 | + pikey.Target().Publish(aUpEvent) // "A" no longer down |
| 81 | + piloop.Target().Publish(piloop.EventLateUpdate) |
| 82 | + // then |
| 83 | + assert.Equal(t, 0, executionTimes) |
| 84 | + }) |
| 85 | +} |
0 commit comments