public void SingleEventHandler() { var eventSource = new MockEventSource(); var eventHandler = new MockEventHandler(); var eventArgs = new EventArgs(); bool eventRaised = false; eventHandler.Action = (s, e) => { Assert.AreSame(eventSource, s); Assert.AreSame(eventArgs, e); eventRaised = true; }; eventSource.MyEvent += eventHandler.OnEvent; eventSource.RaiseMyEvent(eventArgs); Assert.IsTrue(eventRaised); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); eventRaised = false; eventSource.RaiseMyEvent(eventArgs); Assert.IsTrue(eventRaised); eventRaised = false; eventSource.MyEvent -= eventHandler.OnEvent; eventSource.RaiseMyEvent(eventArgs); Assert.IsFalse(eventRaised); }
public void EventWithInternalEventHandler() { var eventSource = new MockEventSource(); var eventHandler = new MockInternalEventHandler(); var eventArgs = new EventArgs(); bool eventRaised = false; eventHandler.Action = (s, e) => { Assert.AreSame(eventSource, s); Assert.AreSame(eventArgs, e); eventRaised = true; }; eventSource.MyEvent += eventHandler.OnEvent; eventSource.RaiseMyEvent(eventArgs); Assert.IsTrue(eventRaised); }
public void RegisterAndUnregisterEventHandler() { var eventSource = new MockEventSource(); var eventHandler = new MockEventHandler(); var eventArgs = new EventArgs(); bool eventRaised = false; eventHandler.Action = (s, e) => { Assert.AreSame(eventSource, s); Assert.AreSame(eventArgs, e); eventRaised = true; }; eventSource.MyEvent += eventHandler.OnEvent; eventSource.MyEvent -= eventHandler.OnEvent; eventSource.RaiseMyEvent(eventArgs); Assert.IsFalse(eventRaised); }