コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #3
0
        public void HasRegisteredHotKeysWillReturnFalseIfNoHotKeyWasRegistered()
        {
            using (var dummyHotKeyManager = new DummyHotKeyManager())
            {
                // Act
                var hasRegisteredHotKeys = dummyHotKeyManager.HasRegisteredHotKeys;

                // Assert
                hasRegisteredHotKeys.Should().BeFalse();
            }
        }
コード例 #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>();
        }
コード例 #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>();
        }
コード例 #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();
            }
        }
コード例 #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);
            }
        }
コード例 #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>();
                }
            }
        }
コード例 #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);
                }
            }
        }
コード例 #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);
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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>();
            }
        }
コード例 #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();
            }
        }