Esempio n. 1
0
        public void UnSubscribe_With_Generic_Event()
        {
            bool   isNotifed = false;
            string r         = null;
            var    sub       = new Action <string>(_ =>
            {
                isNotifed = true;
                r         = _;
            });

            var p = new MyGenericEvent();
            var o = p.Subscribe(sub);

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.Token);

            p.Publish("a");

            Assert.IsTrue(isNotifed);
            Assert.AreEqual("a", r);

            isNotifed = false;

            Assert.IsTrue(p.Unsubscribe(o.Token));

            p.Publish("b");

            Assert.IsFalse(isNotifed);
            Assert.AreEqual("a", r);
        }
Esempio n. 2
0
        public void Subscribers_are_notified_With_Generic_Event()
        {
            int    i = 0;
            string r = null, r2 = null, r3 = null;
            var    sub = new Action <string>((param) =>
            {
                i++;
                r = param;
            });

            var sub2 = new Action <string>((param) =>
            {
                i++;
                r2 = param;
            });

            var sub3 = new Action <string>((param) =>
            {
                i++;
                r3 = param;
            });

            var p = new MyGenericEvent();

            p.Subscribe(sub);
            p.Subscribe(sub2);
            p.Subscribe(sub3);

            p.Publish("a");

            Assert.AreEqual(3, i);
            Assert.AreEqual("a", r);
            Assert.AreEqual("a", r2);
            Assert.AreEqual("a", r3);
        }
Esempio n. 3
0
        public async Task Publish_On_BackgroundThread_With_Generic_Event()
        {
            bool   isNotifed = false;
            string r         = null;
            var    sub       = new Action <string>(_ =>
            {
                isNotifed = true;
                r         = _;
            });

            var p = new MyGenericEvent();

            p.Subscribe(sub).WithExecutionStrategy(ExecutionStrategyType.BackgroundThread);

            p.Publish("a");

            await Task.Delay(1);

            Assert.IsTrue(isNotifed);
            Assert.AreEqual("a", r);
        }
Esempio n. 4
0
        public void Subscriber_is_notified_With_Generic_Event()
        {
            bool   isNotifed = false;
            string r         = null;
            var    sub       = new Action <string>((param) =>
            {
                isNotifed = true;
                r         = param;
            });

            var p = new MyGenericEvent();
            var o = p.Subscribe(sub);

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.Token);

            p.Publish("a");

            Assert.IsTrue(isNotifed);
            Assert.AreEqual("a", r);
        }
Esempio n. 5
0
        public void Filter_With_Generic_Event()
        {
            bool   isNotifed = false;
            string r         = null;
            var    sub       = new Action <string>((param) =>
            {
                isNotifed = true;
                r         = param;
            });

            var p = new MyGenericEvent();
            var o = p.Subscribe(sub).WithFilter((a) => a != "a");

            p.Publish("a");

            Assert.IsFalse(isNotifed);
            Assert.AreEqual(null, r);

            p.Publish("b");

            Assert.IsTrue(isNotifed);
            Assert.AreEqual("b", r);
        }
Esempio n. 6
0
        public void Publish_On_UIThread_With_Generic_Event()
        {
            bool   isNotifed = false;
            string r         = null;
            var    sub       = new Action <string>(_ =>
            {
                isNotifed = true;
                r         = _;
            });

            var p = new MyGenericEvent();

            var s = new TestableSynchronizationContext();

            SynchronizationContext.SetSynchronizationContext(s);
            p.SynchronizationContext = s;

            p.Subscribe(sub).WithExecutionStrategy(ExecutionStrategyType.UIThread);

            p.Publish("a");

            Assert.IsTrue(isNotifed);
            Assert.AreEqual("a", r);
        }
Esempio n. 7
0
 // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
 public void RaiseGenericEvent(SampleEventArgs args) => MyGenericEvent.Raise(this, args);