Exemplo n.º 1
0
        public void WindowReceivedWmHotkey()
        {
            const Int32 WM_HotKey        = 786;
            var         autoResetEvent   = new AutoResetEvent(false);
            var         wmHotKeyReceived = false;

            using (var dummyWindowWithMessageLoop = new DummyWindowWithMessageLoop((windowHandle, message, wParam, lParam) =>
            {
                if (message == WM_HotKey)
                {
                    wmHotKeyReceived = autoResetEvent.Set();
                }
            }))
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = () => {};

                    dummyHotKeyManager.Register(dummyWindowWithMessageLoop, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Act
                    dummyWindowWithMessageLoop.SendMessage(WM_HotKey, IntPtr.Zero, IntPtr.Zero);

                    // Wait one second to receive the sent message
                    autoResetEvent.WaitOne(TimeSpan.FromSeconds(1));

                    // Assert
                    wmHotKeyReceived.Should().BeTrue();
                }
            }
        }
Exemplo n.º 2
0
        public void HandleHotKeyWillReturnTrueAndCallActionIfHotKeyWasFound()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier  = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode  = (UInt32)65; // A
                    var    hotKeyActionSet = false;
                    Action hotKeyAction    = () =>
                    {
                        hotKeyActionSet = true;
                    };
                    dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Act
                    var handleHotKeyResult = dummyHotKeyManager.HandleHotKey(hotKeyModifier, virtualKeyCode);

                    // Assert
                    handleHotKeyResult.Should().BeTrue();
                    hotKeyActionSet.Should().BeTrue();
                }
            }
        }
Exemplo n.º 3
0
        public void HasRegisteredHotKeysWillReturnFalseIfNoHotKeyWasRegistered()
        {
            using (var dummyHotKeyManager = new DummyHotKeyManager())
            {
                // Act
                var hasRegisteredHotKeys = dummyHotKeyManager.HasRegisteredHotKeys;

                // Assert
                hasRegisteredHotKeys.Should().BeFalse();
            }
        }
Exemplo n.º 4
0
        public void UnregisterByIdWillFailIfDisposed()
        {
            // Arrange
            var hotKeyId = 0;
            DummyHotKeyManager dummyHotKeyManager = null;

            using (dummyHotKeyManager = new DummyHotKeyManager())
            {
            }

            // Act, Assert
            dummyHotKeyManager.Invoking(hkm => hkm.Unregister(hotKeyId)).Should().Throw <ObjectDisposedException>();
        }
Exemplo n.º 5
0
        public void HandleHotKeyWillFailIfDisposed()
        {
            // Arrange
            var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
            var virtualKeyCode = (UInt32)65; // A
            DummyHotKeyManager dummyHotKeyManager = null;

            using (dummyHotKeyManager = new DummyHotKeyManager())
            {
            }

            // Act, Assert
            dummyHotKeyManager.Invoking(hkm => hkm.HandleHotKey(hotKeyModifier, virtualKeyCode)).Should().Throw <ObjectDisposedException>();
        }
Exemplo n.º 6
0
        public void HandleHotKeyWillReturnFalseIfHotKeyWasNotFound()
        {
            using (var dummyHotKeyManager = new DummyHotKeyManager())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                var virtualKeyCode = (UInt32)65; // A

                // Act
                var handleHotKeyResult = dummyHotKeyManager.HandleHotKey(hotKeyModifier, virtualKeyCode);

                // Assert
                handleHotKeyResult.Should().BeFalse();
            }
        }
Exemplo n.º 7
0
        public void FindHotKeyIdWillReturnNullIfNotRegistered()
        {
            using (var dummyHotKeyManager = new DummyHotKeyManager())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                var virtualKeyCode = (UInt32)65; // A

                // Act
                var registeredHotKeyId = dummyHotKeyManager.FindHotKeyId(hotKeyModifier, virtualKeyCode);

                // Assert
                registeredHotKeyId.Should().Be(null);
            }
        }
Exemplo n.º 8
0
        public void RegisterWillFailIfActionIsNull()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = null;

                    // Act, Assert
                    dummyHotKeyManager.Invoking(hkm => hkm.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction)).Should().Throw <ArgumentNullException>();
                }
            }
        }
Exemplo n.º 9
0
        public void RegisterWillRegisterTheHotKey()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = () => { };

                    // Act
                    var hotKeyId = dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Assert
                    hotKeyId.Should().NotBe(0);
                }
            }
        }
Exemplo n.º 10
0
        public void FindHotKeyIdWillReturnIdIfRegistered()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = () => { };
                    var    hotKeyId       = dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Act
                    var registeredHotKeyId = dummyHotKeyManager.FindHotKeyId(hotKeyModifier, virtualKeyCode);

                    // Assert
                    registeredHotKeyId.Should().Be(hotKeyId);
                }
            }
        }
Exemplo n.º 11
0
        public void UnregisterByModifierAndKeyCodeWillUnregisterTheHotKey()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = () => { };
                    dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Act
                    dummyHotKeyManager.Unregister(hotKeyModifier, virtualKeyCode);

                    // Assert
                    dummyHotKeyManager.HasRegisteredHotKeys.Should().BeFalse();
                }
            }
        }
Exemplo n.º 12
0
        public void HasRegisteredHotKeysWillReturnTrueIfRegistrationWasSuccessfull()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                using (var dummyHotKeyManager = new DummyHotKeyManager())
                {
                    // Arrange
                    var    hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                    var    virtualKeyCode = (UInt32)65; // A
                    Action hotKeyAction   = () => { };
                    dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);

                    // Act
                    var hasRegisteredHotKeys = dummyHotKeyManager.HasRegisteredHotKeys;

                    // Assert
                    hasRegisteredHotKeys.Should().BeTrue();
                }
            }
        }
Exemplo n.º 13
0
        public void DisposeWillNotFailIfHotKeyManagerWasAlreadyDisposed()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var    hotKeyModifier  = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                var    virtualKeyCode  = (UInt32)65; // A
                var    hotKeyActionSet = false;
                Action hotKeyAction    = () =>
                {
                    hotKeyActionSet = true;
                };

                DummyHotKeyManager dummyHotKeyManager;
                using (dummyHotKeyManager = new DummyHotKeyManager())
                {
                }

                // Act, Assert
                dummyHotKeyManager.Invoking(hkm => hkm.Dispose()).Should().NotThrow <ObjectDisposedException>();
            }
        }
Exemplo n.º 14
0
        public void DisposeWillDisposeTheHotKeyManager()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var    hotKeyModifier  = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                var    virtualKeyCode  = (UInt32)65; // A
                var    hotKeyActionSet = false;
                Action hotKeyAction    = () =>
                {
                    hotKeyActionSet = true;
                };

                // Act
                DummyHotKeyManager dummyHotKeyManager;
                using (dummyHotKeyManager = new DummyHotKeyManager())
                {
                    dummyHotKeyManager.Register(dummyWindow, hotKeyModifier, virtualKeyCode, hotKeyAction);
                }

                // Assert
                dummyHotKeyManager.HasRegisteredHotKeys.Should().BeFalse();
            }
        }