Ejemplo n.º 1
0
        public void ForwardOnChangeTest()
        {
            // two bindables of different type
            var child  = new Bindable <int>();
            var parent = new Bindable <string>();

            parent.ForwardOnChange(child);

            var counter = 0;
            BindableCallback listener = ((c, o) => { counter++; });

            // listener on parent without immediate dispatch
            parent.OnChange(listener, false);

            // dispatch on the child
            child.Dispatch();

            Assert.AreEqual(1, counter, "The OnChange should have been forwarded once to the parent");

            // remove the forwarding
            parent.RemoveForwardedOnChange(child);

            // dispatch again on the child
            child.Dispatch();

            Assert.AreEqual(1, counter, "The OnChange should not have been forwarded again.");
        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
 public void OnChange(BindableCallback listener, bool dispatchImmediate)
 {
     if (dispatchImmediate)
     {
         listener(this, this);
     }
     BindableEvents.Add(listener);
 }
Ejemplo n.º 4
0
 /// <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;
     }
 }
Ejemplo n.º 5
0
 protected virtual void BindData <T>(IBindable <T> bindable, BindableCallback listener)
 {
     // remember the listener so it can be removed on destruction of the mediator
     RegisteredDataBindings.Add(
         new KeyValuePair <IBindable, BindableCallback>(
             bindable,
             listener
             )
         );
     // apply the listener
     bindable.OnChange(listener);
 }
Ejemplo n.º 6
0
        public void OnChangeWithImmediateDispatch()
        {
            var bindable = new Bindable <int>();

            var counter = 0;

            BindableCallback listener = ((c, o) => { counter++; });

            // add without immediate dispatch
            bindable.OnChange(listener);

            Assert.AreEqual(1, counter, "The listener should have been invoked onct through immediate dispatch.");
        }
Ejemplo n.º 7
0
        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");
        }
Ejemplo n.º 8
0
        public void OnChangeRemoveOnChangeTest()
        {
            var bindable = new Bindable <int>();

            var counter = 0;

            BindableCallback listener = ((c, o) => { counter++; });

            // add without immediate dispatch
            bindable.OnChange(listener, false);

            bindable.Dispatch();
            Assert.AreEqual(1, counter, "The callback should have been called once.");

            bindable.RemoveOnChange(listener);

            bindable.Dispatch();
            Assert.AreEqual(1, counter, "The callback should have not been called again.");
        }
Ejemplo n.º 9
0
        public void AddInvokeTest()
        {
            var events = new BindableEvents(new Bindable <int>());

            var counter1 = 0;
            var counter2 = 0;

            BindableCallback listener1 = ((c, o) => { counter1++; });
            BindableCallback listener2 = ((c, o) => { counter2++; });

            events.Add(listener1);
            events.Add(listener2);

            events.Dispatch(null);
            events.Dispatch(null);

            Assert.AreEqual(2, counter1, "listener1 should have been invoked 2 times");
            Assert.AreEqual(2, counter2, "listener2 should have been invoked 2 times");
        }
Ejemplo n.º 10
0
 public void RemoveOnChange(BindableCallback listener)
 {
     BindableEvents.Remove(listener);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Remove a listener.
 /// </summary>
 /// <param name="listener"></param>
 public void Remove(BindableCallback listener)
 {
     // no, just remove the listener itself
     Console.WriteLine("removing listener");
     Listeners -= listener;
 }