public void AddTest() { var events = new BindableEvents(new Bindable <int>()); BindableCallback listener1 = ((c, o) => { }); BindableCallback listener2 = ((c, o) => { }); // add two events.Add(listener1); events.Add(listener2); // get the listeners BindableCallback listeners = (BindableCallback)UnitTestUtil.GetProtected(events, "Listeners"); Assert.AreEqual(listeners.GetInvocationList().Length, 2, "There should be two listeners by now"); events.Remove(listener1); // remove the first one listeners = (BindableCallback)UnitTestUtil.GetProtected(events, "Listeners"); Assert.AreEqual(listeners.GetInvocationList().Count(), 1, "There should only be one listener left"); events.Remove(listener1); // try to remove it again listeners = (BindableCallback)UnitTestUtil.GetProtected(events, "Listeners"); Assert.AreEqual(listeners.GetInvocationList().Count(), 1, "There should still be one listener left"); events.Remove(listener2); // remove the other one listeners = (BindableCallback)UnitTestUtil.GetProtected(events, "Listeners"); Assert.IsNull(listeners, "There should be no listener left"); }
/// <summary> /// Add a listener. /// </summary> /// <param name="listener">The listener</param> public void Add(BindableCallback listener) { // prevent that the same listener is added multiple times if (Listeners == null || !Listeners.GetInvocationList().Contains(listener)) { Listeners += listener; } }
public void AddingListenerMultipleTimesTest() { var events = new BindableEvents(new Bindable <int>()); BindableCallback listener1 = ((c, o) => { }); // add two events.Add(listener1); events.Add(listener1); // get the listeners BindableCallback listeners = (BindableCallback)UnitTestUtil.GetProtected(events, "Listeners"); Assert.AreEqual(listeners.GetInvocationList().Length, 1, "There should only be listeners registered"); }