コード例 #1
0
        public void ReferenceCountingTest(TestMethodRecord tmr)
        {
            //
            // In this test we are observing how the various keyboard hook
            // classes are interacting with their implementation classes. They
            // should add references in their constructor and release them when
            // disposed. The implementation should only be disposed when the ref
            // count is zero.
            //
            KeyboardHookTestImpl testImpl = new KeyboardHookTestImpl();

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == false, "Not initially disposed.");

            using (IKeyboardHook hook1 = HookFactory.CreateKeyboardHook(testImpl))
            {
                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");

                using (IKeyboardHookExt hook2 = HookFactory.CreateKeyboardHookExt(testImpl))
                {
                    tmr.RunTest(testImpl.ReferenceCount == 2, "Now references count is 2.");
                    tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
                }

                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
            }

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == true, "Disposed.");
        }
コード例 #2
0
        public void CreateKeyboardHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook hook = HookFactory.CreateKeyboardHook())
            {
                tmr.RunTest(hook != null, "IKeyboardHook created successfully with default options.");
            }

            using (IKeyboardHookExt hookExt = HookFactory.CreateKeyboardHookExt())
            {
                tmr.RunTest(hookExt != null, "IKeyboardHookExt created successfully with default options.");
            }
        }
コード例 #3
0
        private void TestInstallHook(TestMethodRecord tmr, ISystemHook hook, string name)
        {
            tmr.WriteLine("Testing keyboard hook of type: " + name);

            tmr.RunTest(hook.IsHooked == false, "Hook is not installed.");

            hook.InstallHook();
            tmr.RunTest(hook.IsHooked, "Hook installed successfully.");

            hook.UninstallHook();
            tmr.RunTest(hook.IsHooked == false, "Hook is unstalled successfully.");
        }
コード例 #4
0
 public void DefaultCreationTest(TestMethodRecord tmr)
 {
     using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt())
     {
         tmr.RunTest(keyboard != null, "Keyboard hook created.");
     }
 }
コード例 #5
0
 public void CreationTest(TestMethodRecord tmr)
 {
     using (IMouseHookExt mouseHook = HookFactory.CreateMouseHookExt())
     {
         tmr.RunTest(mouseHook != null, "Mouse hook created.");
     }
 }
コード例 #6
0
        public void CreateMouseHookTests(TestMethodRecord tmr)
        {
            using (IMouseHook mouseHook = HookFactory.CreateMouseHook())
            {
                tmr.RunTest(mouseHook != null, "IMouseHook created successfully with default options.");
            }

            using (IMouseHookExt mouseHookExt = HookFactory.CreateMouseHookExt())
            {
                tmr.RunTest(mouseHookExt != null, "IMouseHookExt created successfully with default options.");
            }

            using (IMouseHookLite mouseHookLite = HookFactory.CreateMouseHookLite())
            {
                tmr.RunTest(mouseHookLite != null, "IMouseHookLite created successfully with default options.");
            }
        }
コード例 #7
0
        public void UnsupportedFilterTypesTest2(TestMethodRecord tmr)
        {
            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                hook.FilterMessageWrapper(12345);

                tmr.RunTest(true, "Filter message succeeded on HookTypes.MouseLL.");
            }
        }
コード例 #8
0
        public void HookInstallTests(TestMethodRecord tmr)
        {
            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                //
                // Test that we can install the hook as a local hook.
                //
                hook.InstallHook();

                //
                // Failure is signalled with an exception, so there is nothing
                // to actually test. That's why we're passing true.
                //
                tmr.RunTest(true, "Installed the hook as a global mouse hook.");

                hook.UninstallHook();

                tmr.RunTest(true, "Uninstalled the hook as a global mouse hook.");
            }
        }
コード例 #9
0
        public void EventTest(TestMethodRecord tmr)
        {
            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();

            using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(hookImpl))
            {
                keyboard.InstallHook();

                keyboard.KeyDown += new KeyboardEventHandlerExt(EventTest_KeyDown);

                //
                // Trigger a bunch of keyboard events. The event handler will
                // capture the output and we will then compare it here.
                //
                foreach (KeyboardHookEventArgs keyArg in keyArgs)
                {
                    hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, keyArg);
                }
            }

            tmr.RunTest(keyArgs.Length == eventTest_ArgsList.Count, "The proper number of events were caught.");

            bool same = true;

            for (int i = 0; i < keyArgs.Length; i++)
            {
                KeyboardHookEventArgs kea1 = keyArgs[i];
                KeyboardHookEventArgs kea2 = (KeyboardHookEventArgs)eventTest_ArgsList[i];

                if (!CompareKeyArgs(kea1, kea2))
                {
                    tmr.WriteLine("Failed event results comparison at index " + i);
                    same = false;
                    break;
                }
            }

            tmr.RunTest(same, "The event callback captured all the events successfully.");
        }
コード例 #10
0
        public void EventSinkTest(TestMethodRecord tmr)
        {
            MouseHookEventArgs[] mouseArgs = GenerateRandomClickArgs();

            MouseHookTestImpl mouseImpl = new MouseHookTestImpl();

            using (IMouseHookExt mouseHook = HookFactory.CreateMouseHookExt(mouseImpl))
            {
                mouseHook.LeftButtonDown += new MouseEventHandlerExt(EventSinkTest_LeftButtonDown);
                mouseHook.InstallHook();

                //
                // Trigger a bunch of mouse down events. The event handler will
                // capture the output and we will then compare it here.
                //
                foreach (MouseHookEventArgs mea in mouseArgs)
                {
                    mouseImpl.TriggerMouseAction(MouseEvents.LeftButtonDown, mea);
                }
            }

            tmr.RunTest(mouseArgs.Length == eventTest_ArgsList.Count, "The proper number of events were caught.");

            bool same = true;

            for (int i = 0; i < mouseArgs.Length; i++)
            {
                MouseHookEventArgs mea1 = mouseArgs[i];
                MouseHookEventArgs mea2 = (MouseHookEventArgs)eventTest_ArgsList[i];

                if (!CompareMouseArgs(mea1, mea2))
                {
                    tmr.WriteLine("Failed event results comparison at index " + i);
                    same = false;
                    break;
                }
            }

            tmr.RunTest(same, "The event callback captured all the events successfully.");
        }
コード例 #11
0
        public void SortedEventTest(TestMethodRecord tmr)
        {
            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();
            IKeyboardHookExt     keyboard = HookFactory.CreateKeyboardHookExt(hookImpl);

            keyboard.InstallHook();

            keyboard.KeyDown       += new KeyboardEventHandlerExt(SortedEventTest_KeyDown);
            keyboard.KeyUp         += new KeyboardEventHandlerExt(SortedEventTest_KeyUp);
            keyboard.SystemKeyUp   += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyUp);
            keyboard.SystemKeyDown += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyDownKeyUp);

            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookEventArgs kea1 = keyArgs[0];
            KeyboardHookEventArgs kea2 = keyArgs[1];
            KeyboardHookEventArgs kea3 = keyArgs[2];
            KeyboardHookEventArgs kea4 = keyArgs[3];

            hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, kea1);
            hookImpl.TriggerKeyAction(KeyboardEvents.KeyUp, kea2);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyDown, kea3);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyUp, kea4);

            tmr.RunTest(sortedEventTest_ArgsListDown.Count == 1, "One KeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListUp.Count == 1, "One KeyUp event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysDown.Count == 1, "One SystemKeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysUp.Count == 1, "One SystemKeyUp event trigged.");

            KeyboardHookEventArgs keaB1 = (KeyboardHookEventArgs)sortedEventTest_ArgsListDown[0];
            KeyboardHookEventArgs keaB2 = (KeyboardHookEventArgs)sortedEventTest_ArgsListUp[0];
            KeyboardHookEventArgs keaB3 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysDown[0];
            KeyboardHookEventArgs keaB4 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysUp[0];

            tmr.RunTest(CompareKeyArgs(kea1, keaB1), "Correct KeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea2, keaB2), "Correct KeyUp event caught.");
            tmr.RunTest(CompareKeyArgs(kea3, keaB3), "Correct SysKeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea4, keaB4), "Correct SysKeyUp event caught.");
        }