public void CompareToWithUnequalObjectTypes()
        {
            var first  = new NotificationId("a");
            var second = new object();

            Assert.Throws <ArgumentException>(() => first.CompareTo(second));
        }
示例#2
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);
        }
        public void CompareToOperatorWithEqualObjects()
        {
            var    first  = new NotificationId("a");
            object second = new NotificationId("a");

            Assert.AreEqual(0, first.CompareTo(second));
        }
        public void LargerThanOperatorWithSecondObjectNull()
        {
            var            first  = new NotificationId("a");
            NotificationId second = null;

            Assert.IsTrue(first > second);
        }
        public void CompareToWithSmallerFirstObject()
        {
            var    first  = new NotificationId("a");
            object second = new NotificationId("b");

            Assert.IsTrue(first.CompareTo(second) < 0);
        }
        public void Clone()
        {
            var first  = new NotificationId("a");
            var second = first.Clone();

            Assert.AreEqual(first, second);
        }
        public void CompareToWithNullObject()
        {
            var    first  = new NotificationId("a");
            object second = null;

            Assert.AreEqual(1, first.CompareTo(second));
        }
        public void SmallerThanOperatorWithEqualObjects()
        {
            var first  = new NotificationId("a");
            var second = new NotificationId("a");

            Assert.IsFalse(first < second);
        }
        public void SmallerThanOperatorWithFirstObjectSmaller()
        {
            var first  = new NotificationId("a");
            var second = new NotificationId("b");

            Assert.IsTrue(first < second);
        }
        public void SmallerThanOperatorWithBothObjectsNull()
        {
            NotificationId first  = null;
            NotificationId second = null;

            Assert.IsFalse(first < second);
        }
        public void SmallerThanOperatorWithSecondObjectNull()
        {
            var            first  = new NotificationId("a");
            NotificationId second = null;

            Assert.IsFalse(first < second);
        }
        public void SmallerThanOperatorWithFirstObjectNull()
        {
            NotificationId first  = null;
            var            second = new NotificationId("a");

            Assert.IsTrue(first < second);
        }
示例#13
0
        /// <summary>
        /// Creates a <see cref="EventMapper"/> for a notification interface event.
        /// </summary>
        /// <param name="eventRegistration">The expression registering for the mapped event.</param>
        /// <returns>The method mapper.</returns>
        /// <exception cref="InvalidCommandMethodExpressionException">
        ///     Thrown when the <paramref name="eventRegistration"/> expression does not contain a event registration call on the
        ///     notification interface.
        /// </exception>
        /// <exception cref="TypeIsNotAValidCommandSetException">
        ///     Thrown when the delegate type of the event is not a <see cref="EventHandler"/> or a <see cref="EventHandler{T}"/>.
        /// </exception>
        public EventMapper From(Action <TNotification> eventRegistration)
        {
            var proxy  = new NotificationProxy();
            var tester = (TNotification)proxy.GetTransparentProxy();

            eventRegistration(tester);

            var eventName = proxy.Invocations.FirstOrDefault();

            if (eventName == null)
            {
                throw new InvalidNotificationMethodExpressionException();
            }

            var eventInfo = typeof(TNotification).GetEvent(eventName);

            if (eventInfo == null)
            {
                throw new InvalidNotificationMethodExpressionException();
            }

            return(new EventMapper(
                       StoreDefinition,
                       NotificationId.Create(eventInfo)));
        }
        public void LargerThanOperatorWithFirstObjectNull()
        {
            NotificationId first  = null;
            var            second = new NotificationId("a");

            Assert.IsFalse(first > second);
        }
示例#15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NotificationDefinition"/> class.
        /// </summary>
        /// <param name="id">The notification ID for the notification on the <see cref="INotificationSet"/>.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="id"/> is <see langword="null" />.
        /// </exception>
        public NotificationDefinition(NotificationId id)
        {
            {
                Lokad.Enforce.Argument(() => id);
            }

            m_Id = id;
        }
示例#16
0
        public void Create()
        {
            var reg  = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithTypedEventHandler).GetEvent("OnMyEvent"));
            var args = new EventArgs();

            var eventRaisedData = new NotificationRaisedData(reg, args);

            Assert.AreSame(reg, eventRaisedData.Notification);
            Assert.AreSame(args, eventRaisedData.EventArgs);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="NotificationRaisedData"/> class.
        /// </summary>
        /// <param name="notification">The notification that was raised.</param>
        /// <param name="eventArgs">The event arguments for the notification.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="notification"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="eventArgs"/> is <see langword="null" />.
        /// </exception>
        public NotificationRaisedData(NotificationId notification, EventArgs eventArgs)
        {
            {
                Lokad.Enforce.Argument(() => notification);
                Lokad.Enforce.Argument(() => eventArgs);
            }

            m_Notification = notification;
            m_EventArgs    = eventArgs;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EventMapper"/> class.
        /// </summary>
        /// <param name="storeDefinition">The action that is used to store the notification definition.</param>
        /// <param name="notificationId">The ID for the notification interface event.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="storeDefinition"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="notificationId"/> is <see langword="null" />.
        /// </exception>
        internal EventMapper(Action <NotificationDefinition> storeDefinition, NotificationId notificationId)
        {
            {
                Lokad.Enforce.Argument(() => storeDefinition);
                Lokad.Enforce.Argument(() => notificationId);
            }

            m_StoreDefinition = storeDefinition;
            m_NotificationId  = notificationId;
        }
示例#19
0
        /// <summary>
        /// Creates the notification map for the given notification interface.
        /// </summary>
        /// <returns>The notification map.</returns>
        /// <exception cref="NotificationEventNotMappedException">
        ///     Thrown when one or more of the events on the notification interface are not mapped to instance events.
        /// </exception>
        public NotificationMap ToMap()
        {
            var type   = typeof(TNotification);
            var events = type.GetEvents();

            foreach (var eventInfo in events)
            {
                var id = NotificationId.Create(eventInfo);
                if (!m_Definitions.ContainsKey(id))
                {
                    throw new NotificationEventNotMappedException();
                }
            }

            return(new NotificationMap(type, m_Definitions.Values.ToArray()));
        }
示例#20
0
        public void FromWithEventUnregistration()
        {
            var mapper = NotificationMapper <IMockNotificationSet> .Create();

            mapper.From(s => s.OnMyEvent -= null)
            .GenerateHandler();

            var map = mapper.ToMap();

            Assert.AreEqual(1, map.Definitions.Length);

            var def = map.Definitions[0];

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

            Assert.AreEqual(id, def.Id);
        }
示例#21
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);
        }
示例#22
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);
        }
示例#23
0
        /// <summary>
        /// Registers a specific endpoint so that it may be notified when the specified event
        /// is raised.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="notification">The object that describes to which event the endpoint wants to be subscribed.</param>
        public void RegisterForNotification(EndpointId endpoint, NotificationId notification)
        {
            {
                Lokad.Enforce.Argument(() => endpoint);
                Lokad.Enforce.Argument(() => notification);
            }

            lock (m_Lock)
            {
                if (!m_RegisteredListeners.ContainsKey(notification))
                {
                    m_RegisteredListeners.Add(notification, new List <EndpointId>());
                }

                var list = m_RegisteredListeners[notification];
                if (!list.Contains(endpoint))
                {
                    list.Add(endpoint);
                }
            }
        }
示例#24
0
        /// <summary>
        /// Deregisters a specific endpoint so that it will no longer be notified when the specified event
        /// is raised.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="notification">The object that describes from which event the endpoint wants to be unsubscribed.</param>
        public void UnregisterFromNotification(EndpointId endpoint, NotificationId notification)
        {
            {
                Lokad.Enforce.Argument(() => endpoint);
                Lokad.Enforce.Argument(() => notification);
            }

            lock (m_Lock)
            {
                if (m_RegisteredListeners.ContainsKey(notification))
                {
                    var list = m_RegisteredListeners[notification];
                    if (list.Remove(endpoint))
                    {
                        if (list.Count == 0)
                        {
                            m_RegisteredListeners.Remove(notification);
                        }
                    }
                }
            }
        }
示例#25
0
        private void HandleEventAndForwardToListeners(NotificationId originatingEvent, EventArgs args)
        {
            List <EndpointId> endpoints = null;

            lock (m_Lock)
            {
                if (m_RegisteredListeners.ContainsKey(originatingEvent))
                {
                    endpoints = new List <EndpointId>(m_RegisteredListeners[originatingEvent]);
                }
            }

            if (endpoints != null)
            {
                foreach (var endpoint in endpoints)
                {
                    m_SendMessage(
                        endpoint,
                        new NotificationRaisedMessage(m_Current, new NotificationRaisedData(originatingEvent, args)),
                        CommunicationConstants.DefaultMaximuNumberOfRetriesForMessageSending);
                }
            }
        }
        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);
        }
 /// <summary>
 /// Serializes an <see cref="NotificationId"/> to a string in a reversible manner.
 /// </summary>
 /// <param name="id">The ID that should be serialized.</param>
 /// <returns>The serialized ID.</returns>
 public static string Serialize(NotificationId id)
 {
     return(id.ToString());
 }