Exemplo n.º 1
0
        public void WindowMovementEventProducerCallbackTest()
        {
            /* PRECONDITIONS */
            Debug.Assert(windowManagementModule != null);
            Debug.Assert(windowMovementEventProducer != null);
            Debug.Assert(hookNativeMethods != null);
            Debug.Assert(hookNativeMethods.Mock != null);
            Debug.Assert(nativeWindowManagement != null);

            /* GIVEN */
            var callback = GetCallback();

            nativeWindowManagement.GetProcessName();
            nativeWindowManagement.GetTitle();
            nativeWindowManagement.GetWindowRect();
            nativeWindowManagement.GetPoint();
            nativeWindowManagement.IsRectSizeEqual();

            using var consumedEvent = new CountdownEvent(1);

            using var didStartConsumingEvent = new ManualResetEvent(false);

            var thread = new Thread(async() =>
            {
                await foreach (var @event in Await(
                                   windowMovementEventProducer.GetEvents(), didStartConsumingEvent))
                {
                    if (!(@event.Title.Equals("Title") &&
                          @event.ProcessName.Equals("ProcessName") &&
                          @event.OldLocation.Equals(new Point(0, 0)) &&
                          @event.NewLocation.Equals(new Point(1, 1))))
                    {
                        continue;
                    }

                    consumedEvent.Signal();
                }
            });

            thread.Start();

            Assert.IsTrue(didStartConsumingEvent.WaitOne(MaxWaitTime));
            callback(new GlobalHook.HookMessage
            {
                Type = (uint)GlobalHook.MessageType.WM_ENTERSIZEMOVE, Hwnd = (IntPtr)1
            });
            callback(new GlobalHook.HookMessage
            {
                Type = (uint)GlobalHook.MessageType.WM_EXITSIZEMOVE, Hwnd = (IntPtr)1
            });

            /* THEN */
            Assert.IsTrue(consumedEvent.Wait(MaxWaitTime), "Did not find all matching window movement event in time.");

            windowManagementModule.IsActive = false;
            windowManagementModule.Initialize(false);
        }
        public void WindowFocusEventProducerCallbackTest()
        {
            /* PRECONDITIONS */
            Debug.Assert(windowManagementModule != null);
            Debug.Assert(windowFocusEventProducer != null);
            Debug.Assert(hookNativeMethods != null);
            Debug.Assert(hookNativeMethods.Mock != null);
            Debug.Assert(nativeWindowManagement != null);

            /* GIVEN */
            const int WA_ACTIVE = 1;

            var callback = GetCallback();

            nativeWindowManagement.GetForegroundWindow();
            nativeWindowManagement.GetProcessName();
            nativeWindowManagement.GetTitle();

            using var consumedEvent = new CountdownEvent(1);

            using var didStartConsumingEvent = new ManualResetEvent(false);

            var thread = new Thread(async() =>
            {
                await foreach (var @event in Await(
                                   windowFocusEventProducer.GetEvents(), didStartConsumingEvent))
                {
                    if (!(@event.Title.Equals("Title") &&
                          @event.ProcessName.Equals("ProcessName")))
                    {
                        continue;
                    }

                    consumedEvent.Signal();
                }
            });

            thread.Start();

            Assert.IsTrue(didStartConsumingEvent.WaitOne(MaxWaitTime));
            callback(new GlobalHook.HookMessage
            {
                Type = (uint)GlobalHook.MessageType.WM_ACTIVATE, wParam = (IntPtr)WA_ACTIVE,
                Hwnd = (IntPtr)1
            });

            /* THEN */
            Assert.IsTrue(consumedEvent.Wait(MaxWaitTime), "Did not find all matching window focus event in time.");

            windowFocusEventProducer.StopCapture();
            windowManagementModule.Initialize(false);
        }
Exemplo n.º 3
0
        public void WindowStateChangedEventProducerCallbackTest()
        {
            /* PRECONDITIONS */
            Debug.Assert(windowManagementModule != null);
            Debug.Assert(windowStateChangedEventProducer != null);
            Debug.Assert(hookNativeMethods != null);
            Debug.Assert(hookNativeMethods.Mock != null);
            Debug.Assert(nativeWindowManagement != null);

            /* GIVEN */
            var callback = GetCallback();

            nativeWindowManagement.GetProcessName();
            nativeWindowManagement.GetTitle();
            nativeWindowManagement.IsRectSizeNotEqual();
            nativeWindowManagement.GetWindowRect();

            GlobalHook.HookMessage[] hookMessages =
            {
                new GlobalHook.HookMessage
                {
                    Type   = (uint)GlobalHook.MessageType.WM_SIZE,
                    wParam = (IntPtr)WindowState.Maximized,
                    Hwnd   = (IntPtr)1
                },
                new GlobalHook.HookMessage
                {
                    Type   = (uint)GlobalHook.MessageType.WM_SIZE,
                    wParam = (IntPtr)WindowState.Minimized, Hwnd = (IntPtr)1
                },
                new GlobalHook.HookMessage
                {
                    Type = (uint)GlobalHook.MessageType.WM_ENTERSIZEMOVE, Hwnd = (IntPtr)1
                },
                new GlobalHook.HookMessage
                {
                    Type = (uint)GlobalHook.MessageType.WM_EXITSIZEMOVE, Hwnd = (IntPtr)1
                }
            };

            WindowStateChangedEvent[] expectedEvents =
            {
                new WindowStateChangedEvent
                {
                    ProcessName = "ProcessName",
                    Title       = "Title",
                    WindowState = WindowState.Maximized
                },

                new WindowStateChangedEvent
                {
                    ProcessName = "ProcessName",
                    Title       = "Title",
                    WindowState = WindowState.Minimized
                },
                new WindowStateChangedEvent
                {
                    ProcessName = "ProcessName",
                    Title       = "Title",
                    WindowState = WindowState.Normal
                }
            };
            Assert.IsTrue(hookMessages.Length == expectedEvents.Length + 1);

            /* WHEN */
            using var consumedEvent = new CountdownEvent(expectedEvents.Length);
            Assert.IsTrue(consumedEvent.CurrentCount == expectedEvents.Length);
            using var didStartConsumingEvent = new ManualResetEvent(false);

            var thread = new Thread(async() =>
            {
                await foreach (var @event in Await(
                                   windowStateChangedEventProducer.GetEvents(), didStartConsumingEvent))
                {
                    if (!IsWindowStateChangedEventFound(@event, expectedEvents))
                    {
                        continue;
                    }

                    consumedEvent.Signal();
                }
            });

            thread.Start();

            Assert.IsTrue(didStartConsumingEvent.WaitOne(MaxWaitTime));

            foreach (var msg in hookMessages)
            {
                callback(msg);
            }

            /* THEN */
            Assert.IsTrue(consumedEvent.Wait(MaxWaitTime),
                          "Did not find all matching window state changed event in time.");

            windowStateChangedEventProducer.StopCapture();
            windowManagementModule.Initialize(false);
        }