示例#1
0
        private void FireEvent()
        {
            var h        = new EventTestHandler();
            int expected = rand.Next();

            // Add handler
            obj.OnEvent += h.Handler;

            // Fire event
            obj.FireEvent(expected);
            h.Validate(true, expected);

            // Remove handler
            obj.OnEvent -= h.Handler;
            h.Reset();

            // Fire event
            expected = rand.Next();
            obj.FireEvent(expected);
            h.Validate(false);

            // Re-add handler
            obj.OnEvent += h.Handler;
            h.Reset();

            // Fire event
            expected = rand.Next();
            obj.FireEvent(expected);
            h.Validate(true, expected);

            obj.OnEvent -= h.Handler;
        }
示例#2
0
        private void MultipleSources()
        {
            var    h               = new EventTestHandler();
            int    expected        = rand.Next();
            string expectedMessage = expected.ToString();

            // Add handler
            obj.OnEvent        += h.Handler;
            obj.OnEventMessage += h.MessageHandler;

            // Fire event
            obj.FireEvent(expected);
            obj.FireEventMessage(expectedMessage);
            h.Validate(true, expected);
            h.ValidateMessage(true, expectedMessage);

            // Remove handler for first event source
            obj.OnEvent -= h.Handler;
            h.Reset();

            // Fire event
            expected        = rand.Next();
            expectedMessage = expected.ToString();
            obj.FireEvent(expected);
            obj.FireEventMessage(expectedMessage);
            h.Validate(false);
            h.ValidateMessage(true, expectedMessage);

            // Remove handler for second event source
            obj.OnEventMessage -= h.MessageHandler;
            h.Reset();

            // Fire event
            expected        = rand.Next();
            expectedMessage = expected.ToString();
            obj.FireEvent(expected);
            obj.FireEventMessage(expectedMessage);
            h.Validate(false);
            h.ValidateMessage(false);

            // Re-add handler
            obj.OnEvent        += h.Handler;
            obj.OnEventMessage += h.MessageHandler;
            h.Reset();

            // Fire event
            expected        = rand.Next();
            expectedMessage = expected.ToString();
            obj.FireEvent(expected);
            obj.FireEventMessage(expectedMessage);
            h.Validate(true, expected);
            h.ValidateMessage(true, expectedMessage);

            obj.OnEvent        -= h.Handler;
            obj.OnEventMessage -= h.MessageHandler;
        }
示例#3
0
        private void MultipleHandlers()
        {
            var     h1       = new EventTestHandler();
            var     h2       = new EventTestHandler();
            dynamic dh1      = new DynamicEventTestHandler();
            dynamic dh2      = new DynamicEventTestHandler();
            int     expected = rand.Next();

            // Add handlers
            obj.OnEvent += h1.Handler;
            obj.OnEvent += h2.Handler;
            obj.OnEvent += dh1;
            obj.OnEvent += dh2;

            // Fire event
            obj.FireEvent(expected);
            h1.Validate(true, expected);
            h2.Validate(true, expected);
            dh1.Handler.Validate(true, expected);
            dh2.Handler.Validate(true, expected);

            // Remove first handler
            obj.OnEvent -= h1.Handler;
            obj.OnEvent -= dh1;
            h1.Reset();
            h2.Reset();
            dh1.Handler.Reset();
            dh2.Handler.Reset();

            // Fire event
            expected = rand.Next();
            obj.FireEvent(expected);
            h1.Validate(false);
            h2.Validate(true, expected);
            dh1.Handler.Validate(false);
            dh2.Handler.Validate(true, expected);

            // Remove second handler
            obj.OnEvent -= h2.Handler;
            obj.OnEvent -= dh2;
            h1.Reset();
            h2.Reset();
            dh1.Handler.Reset();
            dh2.Handler.Reset();

            // Fire event
            expected = rand.Next();
            obj.FireEvent(expected);
            h1.Validate(false);
            h2.Validate(false);
            dh1.Handler.Validate(false);
            dh2.Handler.Validate(false);
        }