public void ShouldReceiveDelegateOnDifferentThread()
        {
            int threadId                = -1;
            int calledThreadId          = -1;
            ManualResetEvent setupEvent = new ManualResetEvent(false);
            bool             completed  = false;
            DispatcherEventSubscription <object> eventSubscription = null;

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action <object>)(arg =>
                {
                    calledThreadId = Thread.CurrentThread.ManagedThreadId;
                    completed      = true;
                })
            };

            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate <object>)(arg => true)
            };

            Thread mockUIThread = new Thread(delegate()
            {
                threadId = Thread.CurrentThread.ManagedThreadId;
                //eventSubscription = new DispatcherEventSubscription<object>(actionDelegateReference, filterDelegateReference, Dispatcher.CurrentDispatcher);
                eventSubscription = new DispatcherEventSubscription <object>(actionDelegateReference, filterDelegateReference, Application.Current.RootVisual.Dispatcher);
                setupEvent.Set();

                while (!completed)
                {
                    WPFThreadHelper.DoEvents();
                }
            }
                                             );

            mockUIThread.Start();
            //setupEvent.WaitOne(5000, false);
            setupEvent.WaitOne(5000);

            var executionStrategy = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(executionStrategy);

            executionStrategy.Invoke(new object[0]);

            bool joined = mockUIThread.Join(5000);

            completed = true;
            Assert.IsTrue(joined);

            Assert.AreEqual(threadId, calledThreadId);
        }
Exemple #2
0
        public void SubscriberReceivesNotificationOnDispatcherThread()
        {
            TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>();
            int threadId                  = -1;
            int calledThreadId            = -1;
            ManualResetEvent setupEvent   = new ManualResetEvent(false);
            bool             completed    = false;
            Thread           mockUIThread = new Thread(delegate()
            {
                threadId = Thread.CurrentThread.ManagedThreadId;
                //compositeWpfEvent.SettableUIDispatcher = Dispatcher.CurrentDispatcher;
                compositeWpfEvent.SettableUIDispatcher = Application.Current.RootVisual.Dispatcher;
                setupEvent.Set();

                while (!completed)
                {
                    WPFThreadHelper.DoEvents();
                }
            }
                                                       );

            mockUIThread.Start();
            string receivedPayload = null;

            //setupEvent.WaitOne(5000, false);
            setupEvent.WaitOne(5000);
            compositeWpfEvent.Subscribe(delegate(string args)
            {
                calledThreadId  = Thread.CurrentThread.ManagedThreadId;
                receivedPayload = args;
                completed       = true;
            }, ThreadOption.UIThread, true);

            compositeWpfEvent.Publish("Test Payload");

            bool joined = mockUIThread.Join(5000);

            completed = true;
            Assert.IsTrue(joined);

            Assert.AreEqual(threadId, calledThreadId);
            Assert.AreSame("Test Payload", receivedPayload);
        }
Exemple #3
0
        public void ShouldPassParametersCorrectly()
        {
            int threadId                       = -1;
            int calledThreadId                 = -1;
            ManualResetEvent setupEvent        = new ManualResetEvent(false);
            bool             completed         = false;
            object           receivedArgument1 = null;

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target =
                    (Action <object>)(arg1 =>
                {
                    calledThreadId
                        =
                            Thread
                            .
                            CurrentThread
                            .
                            ManagedThreadId;
                    receivedArgument1
                        =
                            arg1;
                    completed
                        =
                            true;
                })
            };
            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate <object>) delegate
                {
                    return(true);
                }
            };

            DispatcherEventSubscription <object> eventSubscription = null;
            Thread mockUIThread = new Thread(delegate()
            {
                threadId          = Thread.CurrentThread.ManagedThreadId;
                eventSubscription =
                    new DispatcherEventSubscription <object>(actionDelegateReference, filterDelegateReference, Dispatcher.CurrentDispatcher);
                setupEvent.Set();

                while (!completed)
                {
                    WPFThreadHelper.DoEvents();
                }
            }
                                             );

            mockUIThread.Start();

            setupEvent.WaitOne(5000, false);


            var executionStrategy = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(executionStrategy);

            object argument1 = new object();

            executionStrategy.Invoke(new[] { argument1 });

            bool joined = mockUIThread.Join(5000);

            completed = true;
            Assert.IsTrue(joined);

            Assert.AreEqual(threadId, calledThreadId);
            Assert.AreSame(argument1, receivedArgument1);
        }