public void CanAddDescriptionWhileEventIsFiring() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); Action <string> emptyDelegate = delegate { }; compositeWpfEvent.Subscribe(delegate { compositeWpfEvent.Subscribe(emptyDelegate); }); Assert.IsFalse(compositeWpfEvent.Contains(emptyDelegate)); compositeWpfEvent.Publish(null); Assert.IsTrue((compositeWpfEvent.Contains(emptyDelegate))); }
public void FilterEnablesActionTarget() { TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>(); bool goodFilterPublished = false; bool badFilterPublished = false; compositeWpfEvent.Subscribe(delegate { goodFilterPublished = true; }, ThreadOption.PublisherThread, true, delegate { return(true); }); compositeWpfEvent.Subscribe(delegate { badFilterPublished = true; }, ThreadOption.PublisherThread, true, delegate { return(false); }); compositeWpfEvent.Publish("test"); Assert.IsTrue(goodFilterPublished); Assert.IsFalse(badFilterPublished); }
public void CanHaveMultipleSubscribersAndRaiseCustomEvent() { var customEvent = new TestableCompositeWpfEvent <Payload>(); Payload payload = new Payload(); Payload received1 = null; Payload received2 = null; customEvent.Subscribe(delegate(Payload args) { received1 = args; }); customEvent.Subscribe(delegate(Payload args) { received2 = args; }); customEvent.Publish(payload); Assert.AreSame(received1, payload); Assert.AreSame(received2, payload); }
public void SubscribeDefaultsToPublisherThread() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); Action <string> action = delegate { }; var token = compositeWpfEvent.Subscribe(action, true); Assert.AreEqual(1, compositeWpfEvent.BaseSubscriptions.Count); Assert.AreEqual(typeof(EventSubscription <string>), compositeWpfEvent.BaseSubscriptions.ElementAt(0).GetType()); }
public void CanSubscribeAndRaiseEvent() { TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>(); bool published = false; compositeWpfEvent.Subscribe(delegate { published = true; }, ThreadOption.PublisherThread, true, delegate { return(true); }); compositeWpfEvent.Publish(null); Assert.IsTrue(published); }
public void ShouldUnsubscribeASingleDelegate() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); int callCount = 0; Action <string> actionEvent = delegate { callCount++; }; compositeWpfEvent.Subscribe(actionEvent); compositeWpfEvent.Subscribe(actionEvent); compositeWpfEvent.Publish(null); Assert.AreEqual <int>(2, callCount); callCount = 0; compositeWpfEvent.Unsubscribe(actionEvent); compositeWpfEvent.Publish(null); Assert.AreEqual <int>(1, callCount); }
public void RegisterReturnsTokenThatCanBeUsedToUnsubscribe() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); Action <string> action = delegate { }; var token = compositeWpfEvent.Subscribe(action); compositeWpfEvent.Unsubscribe(token); Assert.IsFalse(compositeWpfEvent.Contains(action)); }
public void InlineDelegateDeclarationsDoesNotGetCollectedIncorrectlyWithWeakReferences() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); bool published = false; compositeWpfEvent.Subscribe(delegate { published = true; }, ThreadOption.PublisherThread, false, delegate { return(true); }); GC.Collect(); compositeWpfEvent.Publish(null); Assert.IsTrue(published); }
public void SubscribeDefaultsThreadOptionAndNoFilter() { TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>(); int calledThreadID = -1; compositeWpfEvent.Subscribe(delegate { calledThreadID = Thread.CurrentThread.ManagedThreadId; }); compositeWpfEvent.Publish("test"); Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, calledThreadID); }
public void SubscribeTakesExecuteDelegateThreadOptionAndFilter() { TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>(); string receivedValue = null; compositeWpfEvent.Subscribe(delegate(string value) { receivedValue = value; }); compositeWpfEvent.Publish("test"); Assert.AreEqual("test", receivedValue); }
public void ContainsShouldSearchByToken() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); Action <string> action = delegate { }; var token = compositeWpfEvent.Subscribe(action); Assert.IsTrue(compositeWpfEvent.Contains(token)); compositeWpfEvent.Unsubscribe(action); Assert.IsFalse(compositeWpfEvent.Contains(token)); }
public void ShouldUnsubscribeFromUIThread() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); Action <string> actionEvent = delegate(string args) { }; compositeWpfEvent.Subscribe( actionEvent, ThreadOption.UIThread); Assert.IsTrue(compositeWpfEvent.Contains(actionEvent)); compositeWpfEvent.Unsubscribe(actionEvent); Assert.IsFalse(compositeWpfEvent.Contains(actionEvent)); }
public void SubscriberReceivesNotificationOnDifferentThread() { TestableCompositeWpfEvent <string> compositeWpfEvent = new TestableCompositeWpfEvent <string>(); int calledThreadId = -1; ManualResetEvent completeEvent = new ManualResetEvent(false); compositeWpfEvent.Subscribe(delegate { calledThreadId = Thread.CurrentThread.ManagedThreadId; completeEvent.Set(); }, ThreadOption.BackgroundThread); compositeWpfEvent.Publish(null); completeEvent.WaitOne(5000, false); Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, calledThreadId); }
public void ShouldNotGarbageCollectDelegateReferenceWhenUsingKeepAlive() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); var externalAction = new ExternalAction(); compositeWpfEvent.Subscribe(externalAction.ExecuteAction, ThreadOption.PublisherThread, true); WeakReference actionEventReference = new WeakReference(externalAction); externalAction = null; GC.Collect(); GC.Collect(); Assert.IsTrue(actionEventReference.IsAlive); compositeWpfEvent.Publish("testPayload"); Assert.AreEqual("testPayload", ((ExternalAction)actionEventReference.Target).PassedValue); }
public void ShouldNotExecuteOnGarbageCollectedDelegateReferenceWhenNotKeepAlive() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); ExternalAction externalAction = new ExternalAction(); compositeWpfEvent.Subscribe(externalAction.ExecuteAction); compositeWpfEvent.Publish("testPayload"); Assert.AreEqual("testPayload", externalAction.PassedValue); WeakReference actionEventReference = new WeakReference(externalAction); externalAction = null; GC.Collect(); Assert.IsFalse(actionEventReference.IsAlive); compositeWpfEvent.Publish("testPayload"); }
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); }
public void PayloadGetPassedInBackgroundHandler() { var customEvent = new TestableCompositeWpfEvent <Payload>(); Payload payload = new Payload(); ManualResetEvent backgroundWait = new ManualResetEvent(false); Payload backGroundThreadReceived = null; customEvent.Subscribe(delegate(Payload passedPayload) { backGroundThreadReceived = passedPayload; backgroundWait.Set(); }, ThreadOption.BackgroundThread, true); customEvent.Publish(payload); bool eventSet = backgroundWait.WaitOne(5000, false); Assert.IsTrue(eventSet); Assert.AreSame(backGroundThreadReceived, payload); }
public void ShouldNotExecuteOnGarbageCollectedFilterReferenceWhenNotKeepAlive() { var compositeWpfEvent = new TestableCompositeWpfEvent <string>(); bool wasCalled = false; Action <string> actionEvent = delegate { wasCalled = true; }; ExternalFilter filter = new ExternalFilter(); compositeWpfEvent.Subscribe(actionEvent, ThreadOption.PublisherThread, false, filter.AlwaysTrueFilter); compositeWpfEvent.Publish("testPayload"); Assert.IsTrue(wasCalled); wasCalled = false; WeakReference filterReference = new WeakReference(filter); filter = null; GC.Collect(); Assert.IsFalse(filterReference.IsAlive); compositeWpfEvent.Publish("testPayload"); Assert.IsFalse(wasCalled); }