/// <summary>
        /// Generates an event handler that can be attached to the notification instance event.
        /// </summary>
        /// <returns>The event handler that can be attached to the notification instance event.</returns>
        public EventHandler GenerateHandler()
        {
            var definition = new NotificationDefinition(m_NotificationId);

            m_StoreDefinition(definition);
            return(definition.ForwardToListeners);
        }
        /// <summary>
        /// Generates an event handler that can be attached to the notification instance event.
        /// </summary>
        /// <typeparam name="T">The type of the event handler.</typeparam>
        /// <returns>The event handler that can be attached to the notification instance event.</returns>
        public EventHandler <T> GenerateHandler <T>() where T : EventArgs
        {
            var definition = new NotificationDefinition(m_NotificationId);

            m_StoreDefinition(definition);
            return(definition.ForwardToListeners);
        }
예제 #3
0
        public void GenerateTypedHandler()
        {
            NotificationDefinition          storedDefinition = null;
            Action <NotificationDefinition> storeDefinition  = d => storedDefinition = d;
            var id     = new NotificationId("a");
            var mapper = new EventMapper(storeDefinition, id);

            var handler = mapper.GenerateHandler <EventArgs>();

            Assert.IsNotNull(storedDefinition);
            Assert.AreEqual(id, storedDefinition.Id);

            var args             = new EventArgs();
            var wasActionInvoked = false;
            Action <NotificationId, EventArgs> action =
                (n, a) =>
            {
                wasActionInvoked = true;
                Assert.AreEqual(id, n);
                Assert.AreSame(args, a);
            };

            storedDefinition.OnNotification(action);

            var obj = new object();

            handler(obj, args);

            Assert.IsTrue(wasActionInvoked);
        }
예제 #4
0
        private void StoreDefinition(NotificationDefinition definition)
        {
            if (m_Definitions.ContainsKey(definition.Id))
            {
                throw new NotificationAlreadyRegisteredException();
            }

            m_Definitions.Add(definition.Id, definition);
        }
예제 #5
0
        public void RegisterWithoutBeingSignedIn()
        {
            var         wasInvoked = false;
            SendMessage sender     =
                (endpoint, message, retries) =>
            {
                wasInvoked = true;
            };

            var collection = new LocalNotificationCollection(new EndpointId("a"), sender);

            var id  = NotificationId.Create(typeof(IMockNotificationSet).GetEvent("OnMyEvent"));
            var def = new NotificationDefinition(id);

            collection.Register(new[] { def });

            Assert.AreEqual(1, collection.Count(pair => pair.Equals(id)));
            Assert.IsFalse(wasInvoked);
        }
예제 #6
0
        public void RaiseNormalEvent()
        {
            var                   knownEndpoint = new EndpointId("other");
            EndpointId            other         = null;
            ICommunicationMessage msg           = null;
            var                   wasInvoked    = false;
            SendMessage           sender        =
                (endpoint, message, retries) =>
            {
                wasInvoked = true;
                other      = endpoint;
                msg        = message;
            };

            var collection = new LocalNotificationCollection(new EndpointId("a"), sender);

            var id  = NotificationId.Create(typeof(IMockNotificationSet).GetEvent("OnMyEvent"));
            var def = new NotificationDefinition(id);
            var obj = new MockNotificationSet();

            obj.OnMyEvent += def.ForwardToListeners;
            collection.Register(new[] { def });

            Assert.AreEqual(1, collection.Count(pair => pair.Equals(id)));
            Assert.IsFalse(wasInvoked);

            collection.RegisterForNotification(knownEndpoint, id);

            var args = new EventArgs();

            obj.RaiseOnMyEvent(args);

            Assert.IsTrue(wasInvoked);
            Assert.AreEqual(knownEndpoint, other);
            Assert.IsInstanceOf <NotificationRaisedMessage>(msg);

            var notificationMsg = msg as NotificationRaisedMessage;

            Assert.AreEqual(id, notificationMsg.Notification.Notification);
            Assert.AreSame(args, notificationMsg.Notification.EventArgs);
        }
        public void ForwardToListeners()
        {
            var id         = new NotificationId("a");
            var definition = new NotificationDefinition(id);

            var args = new EventArgs();
            var wasHandlerInvoked = false;
            Action <NotificationId, EventArgs> handler =
                (n, e) =>
            {
                wasHandlerInvoked = true;
                Assert.AreEqual(id, n);
                Assert.AreSame(args, e);
            };

            definition.OnNotification(handler);

            var obj = new object();

            definition.ForwardToListeners(obj, args);

            Assert.IsTrue(wasHandlerInvoked);
        }