Exemplo n.º 1
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.º 2
0
        public void UnregisterWillFailIfHotKeyAlreadyUnregistered()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
                var virtualKeyCode = (UInt32)65; // A
                var globalHotKey   = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode);
                globalHotKey.Unregister();

                // Act, Assert
                globalHotKey.Invoking(ghk => ghk.Unregister()).Should().Throw <ObjectDisposedException>();
            }
        }
Exemplo n.º 3
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.º 4
0
        public void ProbeWillBeFalseIfHotKeyIsRegistered()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
                var virtualKeyCode = (UInt32)65; // A
                using (JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode))
                {
                    // Act
                    var probeResult = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Probe(hotKeyModifier, virtualKeyCode);

                    // Assert
                    probeResult.Should().BeFalse();
                }
            }
        }
Exemplo n.º 5
0
        public void DisposeWillNotFailIfHotKeyWasAlreadyDisposed()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
                var virtualKeyCode = (UInt32)65; // A
                JanHafner.Toolkit.Windows.HotKey.GlobalHotKey globalHotKey = null;

                using (globalHotKey = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle,
                                                                                             hotKeyModifier, virtualKeyCode))
                {
                }

                // Act, Assert
                globalHotKey.Invoking(ghk => ghk.Dispose()).Should().NotThrow <ObjectDisposedException>();
            }
        }
Exemplo n.º 6
0
        public void UnregisterWillUnregisterTheHotKey()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
                var virtualKeyCode = (UInt32)65; // A
                var globalHotKey   = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode);

                // Acti
                globalHotKey.Unregister();

                // Assert
                globalHotKey.HotKeyModifier.Should().Be(HotKeyModifier.None);
                globalHotKey.WindowHandle.Should().Be(IntPtr.Zero);
                globalHotKey.VirtualKeyCode.Should().Be(0);
            }
        }
Exemplo n.º 7
0
        public void UnregisterWillFailIfWindowHandleIsInvalid()
        {
            // Arrange
            var windowHandle   = IntPtr.Zero;
            var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
            var virtualKeyCode = (UInt32)65; // A

            JanHafner.Toolkit.Windows.HotKey.GlobalHotKey globalHotKey = null;

            using (var dummyWindow = DummyWindow.Create())
            {
                windowHandle = dummyWindow.WindowHandle;

                globalHotKey = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(windowHandle, hotKeyModifier, virtualKeyCode);
            }

            globalHotKey.Invoking(ghk => ghk.Unregister()).Should().Throw <Win32Exception>();
        }
Exemplo n.º 8
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.º 9
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.º 10
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.º 11
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.º 12
0
        public void RegisterWillCreateGlobalHotKey()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
                var virtualKeyCode = (UInt32)65; // A

                // Act
                using (
                    var globalHotKey = JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle,
                                                                                              hotKeyModifier, virtualKeyCode))
                {
                    globalHotKey.Should().NotBeNull();
                    globalHotKey.Id.Should().NotBe(0);
                    globalHotKey.HotKeyModifier.Should().Be(hotKeyModifier);
                    globalHotKey.VirtualKeyCode.Should().Be(virtualKeyCode);
                    globalHotKey.WindowHandle.Should().Be(dummyWindow.WindowHandle);
                }
            }
        }
Exemplo n.º 13
0
        public void RegisterWillFailIfWindowHandleIsInvalid()
        {
            // Arrange
            var windowHandle   = IntPtr.Zero;
            var hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Shift | HotKeyModifier.Control;
            var virtualKeyCode = (UInt32)65; // A

            using (var dummyWindow = DummyWindow.Create())
            {
                // Invalidate window handle
                windowHandle = dummyWindow.WindowHandle;
            }

            Action registerFunctionActor = () =>
            {
                JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(windowHandle, hotKeyModifier, virtualKeyCode);
            };

            // Act
            registerFunctionActor.Should().Throw <Win32Exception>();
        }
Exemplo n.º 14
0
        public void RegisterWillFailIfHotKeyIsAlreadyRegistered()
        {
            using (var dummyWindow = DummyWindow.Create())
            {
                // Arrange
                const Int32 hotKeyAlreadyRegisteredErrorCode = 1409;
                var         hotKeyModifier = HotKeyModifier.Alt | HotKeyModifier.Control | HotKeyModifier.Shift;
                var         virtualKeyCode = (UInt32)65; // A

                using (JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode))
                {
                    Action registerFunctionActor = () =>
                    {
                        JanHafner.Toolkit.Windows.HotKey.GlobalHotKey.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode);
                    };

                    // Act
                    registerFunctionActor.Should().Throw <Win32Exception>().Where(ex => ex.NativeErrorCode == hotKeyAlreadyRegisteredErrorCode);
                }
            }
        }
Exemplo n.º 15
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.º 16
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();
            }
        }
Exemplo n.º 17
0
 public Int32 Register(DummyWindow dummyWindow, HotKeyModifier hotKeyModifier, UInt32 virtualKeyCode, Action action)
 {
     return(this.Register(dummyWindow.WindowHandle, hotKeyModifier, virtualKeyCode, action));
 }