Пример #1
0
        public void TestMouseScrollEventProducer_Callback()
        {
            /* PRECONDITIONS */
            Debug.Assert(mouseModule != null);
            Debug.Assert(mouseScrollEventProducer != null);
            Debug.Assert(hookNativeMethodsMock != null);
            Debug.Assert(hookNativeMethodsMock.Mock != null);

            /* GIVEN */
            //get the callback
            GlobalHook.CppGetMessageCallback callback = GetCallback();

            //setting up fake nativeMouseMock behaviors, messages and corresponding expected Events
            GlobalHook.HookMessage[] hookMessages =
            {
                new GlobalHook.HookMessage {
                    Type = (uint)GlobalHook.MessageType.WM_MOUSEWHEEL, Hwnd = (IntPtr)1, wParam = (IntPtr)0x00780000, Data = new int[]{                                                                                             10, 100 }
                },
                new GlobalHook.HookMessage {
                    Type = (uint)GlobalHook.MessageType.WM_MOUSEWHEEL, Hwnd = (IntPtr)2, wParam = (IntPtr)0x00080000, Data = new int[]{                                                                                             20, 200 }
                },
                new GlobalHook.HookMessage {
                    Type = (uint)GlobalHook.MessageType.WM_MOUSEWHEEL, Hwnd = (IntPtr)23, wParam = (IntPtr)0xfff80000, Data = new int[]{                                                                                             54,  23 }
                },
                new GlobalHook.HookMessage {
                    Type = (uint)GlobalHook.MessageType.WM_MOUSEWHEEL, Hwnd = (IntPtr)43, wParam = (IntPtr)0xff880000, Data = new int[]{                                                                                             33, 101 }
                }
            };
            MouseScrollEvent[] expectedEvents =
            {
                new MouseScrollEvent {
                    ScrollAmount = 120, HWnd = "1", MousePosition = new Point{
                        X = 10, Y = 100
                    }
                },
                new MouseScrollEvent {
                    ScrollAmount = 8, HWnd = "2", MousePosition = new Point{
                        X = 20, Y = 200
                    }
                },
                new MouseScrollEvent {
                    ScrollAmount = -8, HWnd = "23", MousePosition = new Point{
                        X = 54, Y = 23
                    }
                },
                new MouseScrollEvent {
                    ScrollAmount = -120, HWnd = "43", MousePosition = new Point{
                        X = 33, Y = 101
                    }
                }
            };

            Assert.IsTrue(hookMessages.Length == expectedEvents.Length);

            /* WHEN */
            // consumedEvent.Signal() will be called gdw one event has been found and meet expectation
            using var consumedEvent = new CountdownEvent(hookMessages.Length);
            Assert.IsTrue(consumedEvent.CurrentCount == hookMessages.Length);

            //didStartConsumingEvent.Set() will be called in Await method gdw the consumer is attached
            using var didStartConsumingEvent = new ManualResetEvent(false);

            //Running the task in another thread
            var thread = new Thread(async() =>
            {
                await foreach (var @event in Await <IAsyncEnumerable <MouseScrollEvent> >(mouseScrollEventProducer.GetEvents(), didStartConsumingEvent))
                {
                    if (!IsMouseScrollEventFound(@event, expectedEvents))
                    {
                        continue;
                    }
                    consumedEvent.Signal();
                }
            });

            thread.Start();
            // true if the consumer is attached!
            Assert.IsTrue(didStartConsumingEvent.WaitOne(maxWaitTime));

            // We must call the callback after the consumer is attached!
            // otherwise the message is automatically dismissed.
            foreach (GlobalHook.HookMessage message in hookMessages)
            {
                callback(message);
            }

            /* THEN */
            //true if all events generated by the fake messages have meet expectation.
            Assert.IsTrue(consumedEvent.Wait(maxWaitTime), "Did not find all matching mouse scroll events in time.");

            //total shut down and resources release
            mouseModule.IsActive = false;
            mouseModule.Initialize(false);
        }