コード例 #1
0
        public void WhenInstanceIsNull_ThenHandlerIsDetached()
        {
            WeakEventListenerFixture instance = null;
            var sender      = new EventSource();
            var invoked     = false;
            var detached    = false;
            var weakHandler =
                new WeakEventHandler <WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
            {
                invoked = true;
            },
                    handler =>
            {
                sender.Event -= handler.OnEvent;
                detached      = true;
            });

            sender.Event += weakHandler.OnEvent;

            sender.FireEvent(new EventArgs());

            Assert.IsFalse(invoked);
            Assert.IsTrue(detached);
        }
コード例 #2
0
        public void WhenDetached_ThenDoesNotInvokeCallbackOnEvent()
        {
            var instance    = this;
            var sender      = new EventSource();
            var invoked     = false;
            var weakHandler =
                new WeakEventHandler <WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
            {
                invoked = true;
            },
                    handler => { sender.Event -= handler.OnEvent; });

            sender.Event += weakHandler.OnEvent;
            weakHandler.Detach();

            sender.FireEvent(new EventArgs());

            Assert.IsFalse(invoked);
        }
コード例 #3
0
        public void WhenDetachedWithNullDetachAction_ThenInvokesCallbackOnEvent()
        {
            var instance    = this;
            var sender      = new EventSource();
            var invoked     = false;
            var weakHandler =
                new WeakEventHandler <WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
            {
                invoked = true;
            },
                    null);

            sender.Event += weakHandler.OnEvent;
            weakHandler.Detach();

            sender.FireEvent(new EventArgs());

            Assert.IsTrue(invoked);
        }
コード例 #4
0
        public void WhenDetached_ThenDoesNotInvokeCallbackOnEvent()
        {
            var instance = this;
            var sender = new EventSource();
            var invoked = false;
            var weakHandler =
                new WeakEventHandler<WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
                    {
                        invoked = true;
                    },
                    handler => { sender.Event -= handler.OnEvent; });

            sender.Event += weakHandler.OnEvent;
            weakHandler.Detach();

            sender.FireEvent(new EventArgs());

            Assert.IsFalse(invoked);
        }
コード例 #5
0
        public void WhenAttachedToAnEvent_ThenInvokesCallbackOnEvent()
        {
            var instance    = this;
            var sender      = new EventSource();
            var args        = new EventArgs();
            var invoked     = false;
            var weakHandler =
                new WeakEventHandler <WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
            {
                Assert.AreSame(this, i);
                Assert.AreSame(sender, s);
                Assert.AreSame(args, e);
                invoked = true;
            },
                    handler => { sender.Event -= handler.OnEvent; });

            sender.Event += weakHandler.OnEvent;

            sender.FireEvent(args);

            Assert.IsTrue(invoked);
        }
コード例 #6
0
        public void WhenAttachedToAnEvent_ThenInvokesCallbackOnEvent()
        {
            var instance = this;
            var sender = new EventSource();
            var args = new EventArgs();
            var invoked = false;
            var weakHandler =
                new WeakEventHandler<WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
                    {
                        Assert.AreSame(this, i);
                        Assert.AreSame(sender, s);
                        Assert.AreSame(args, e);
                        invoked = true;
                    },
                    handler => { sender.Event -= handler.OnEvent; });

            sender.Event += weakHandler.OnEvent;

            sender.FireEvent(args);

            Assert.IsTrue(invoked);
        }
コード例 #7
0
        public void WhenInstanceIsNull_ThenHandlerIsDetached()
        {
            WeakEventListenerFixture instance = null;
            var sender = new EventSource();
            var invoked = false;
            var detached = false;
            var weakHandler =
                new WeakEventHandler<WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
                    {
                        invoked = true;
                    },
                    handler =>
                    {
                        sender.Event -= handler.OnEvent;
                        detached = true;
                    });

            sender.Event += weakHandler.OnEvent;

            sender.FireEvent(new EventArgs());

            Assert.IsFalse(invoked);
            Assert.IsTrue(detached);
        }
コード例 #8
0
        public void WhenDetachedWithNullDetachAction_ThenInvokesCallbackOnEvent()
        {
            var instance = this;
            var sender = new EventSource();
            var invoked = false;
            var weakHandler =
                new WeakEventHandler<WeakEventListenerFixture, object, EventArgs>(
                    instance,
                    (i, s, e) =>
                    {
                        invoked = true;
                    },
                    null);

            sender.Event += weakHandler.OnEvent;
            weakHandler.Detach();

            sender.FireEvent(new EventArgs());

            Assert.IsTrue(invoked);
        }