Beispiel #1
0
 public void CreationTest(TestMethodRecord tmr)
 {
     using (IMouseHookExt mouseHook = HookFactory.CreateMouseHookExt())
     {
         tmr.RunTest(mouseHook != null, "Mouse hook created.");
     }
 }
 public void DefaultCreationTest(TestMethodRecord tmr)
 {
     using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt())
     {
         tmr.RunTest(keyboard != null, "Keyboard hook created.");
     }
 }
        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.");
        }
 public void SingletonErrorTest1(TestMethodRecord tmr)
 {
     using (SystemHookTestWrapper hook1 = new SystemHookTestWrapper(HookTypes.MouseLL))
     {
         tmr.RegisterException("Creating a second hook of the same type will fail.", typeof(ManagedHooksException));
         SystemHookTestWrapper hook2 = new SystemHookTestWrapper(HookTypes.MouseLL);
     }
 }
        public void UnsupportedFilterTypesTest2(TestMethodRecord tmr)
        {
            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                hook.FilterMessageWrapper(12345);

                tmr.RunTest(true, "Filter message succeeded on HookTypes.MouseLL.");
            }
        }
        public void InstallHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook kHook = HookFactory.CreateKeyboardHook())
            {
                TestInstallHook(tmr, kHook, "IKeyboardHook");
            }

            using (IKeyboardHookExt kHookExt = HookFactory.CreateKeyboardHookExt())
            {
                TestInstallHook(tmr, kHookExt, "IKeyboardHookExt");
            }
        }
Beispiel #7
0
        public void UtilityMethodTests2(TestMethodRecord tmr)
        {
            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                int x = 0, y = 0;

                tmr.RegisterException("Invalid LPARAM in GetMousePosition utility method is not allowed..",
                                      typeof(ManagedHooksException));

                hook.GetMousePositionWrapper(new UIntPtr(512), new IntPtr(0), ref x, ref y);
            }
        }
Beispiel #8
0
        public void UtilityMethodTests1(TestMethodRecord tmr)
        {
            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                int vkCode = 0;

                tmr.RegisterException("Invalid LPARAM in GetKeyboardReading utility method is not allowed..",
                                      typeof(ManagedHooksException));

                hook.GetKeyboardReadingWrapper(new UIntPtr(512), new IntPtr(0), ref vkCode);
            }
        }
Beispiel #9
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.");
            }
        }
        public void ConstructionUnsupportedTypeTests(TestMethodRecord tmr)
        {
            HookTypes type = HookTypes.Hardware;

            tmr.WriteLine("If you implement the hook type HookTypes." + type +
                          ", change the type parameter to continue testing this senario.");

            tmr.RegisterException("An unimplemented hook type will cause an exception.",
                                  typeof(HookTypeNotImplementedException));

            SystemHookTestWrapper hook = new SystemHookTestWrapper(type);
        }
        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.");
        }
        public void UnsupportedFilterTypesTest1(TestMethodRecord tmr)
        {
            HookTypes type = HookTypes.Hardware;

            tmr.WriteLine("If you implement the hook type HookTypes." + type +
                          ", change the type parameter to continue testing this senario.");

            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                tmr.RegisterException("An unimplemented hook type will cause an exception (filter message).",
                                      typeof(ManagedHooksException));

                hook.FilterMessageWrapper(type, 12345);
            }
        }
Beispiel #13
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.");
            }
        }
Beispiel #14
0
        public void InstallHookTests(TestMethodRecord tmr)
        {
            using (IMouseHook mHook = HookFactory.CreateMouseHook())
            {
                TestInstallHook(tmr, mHook, "IMouseHook");
            }

            using (IMouseHookExt mHookExt = HookFactory.CreateMouseHookExt())
            {
                TestInstallHook(tmr, mHookExt, "IMouseHookExt");
            }

            using (IMouseHookLite mHookLite = HookFactory.CreateMouseHookLite())
            {
                TestInstallHook(tmr, mHookLite, "IMouseHookLite");
            }
        }
Beispiel #15
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, "Install the hook as a global mouse hook.");

                hook.UninstallHook();
            }
        }
        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.");
        }
Beispiel #17
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.");
        }
        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.");
        }
 public void NullImplementationTest(TestMethodRecord tmr)
 {
     tmr.RegisterException("Null implementation causes an exception.", typeof(ArgumentNullException));
     IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(null);
 }