public void PrioritizedEventListenerDictionaryTests_Add_InsertTwo_Asc() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.HIGH, null)); dictionary.Add(GetType(), mockListener); var mockListener1 = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.NORMAL, null)); dictionary.Add(GetType(), mockListener1); // count the keys int he dictionary Assert.AreEqual(1, dictionary.Count); var collection = dictionary.ValueCollections; var iterator = 0; foreach (var item in collection) { foreach (var registration in item) { if (iterator == 0) { Assert.AreEqual(SurvivalKit.Events.Priority.HIGH, registration.EventHook.HookPriority); } else { Assert.AreEqual(SurvivalKit.Events.Priority.NORMAL, registration.EventHook.HookPriority); } iterator++; } } }
public void PrioritizedEventListenerDictionaryTests_ValueCollections_Multiple_MultipleEntries() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.NORMAL, null)); dictionary.Add(GetType(), mockListener); for (var iterator = 5; iterator > 0; iterator--) { dictionary.Add(typeof(IEventHook), mockListener); } var collections = dictionary.ValueCollections; Assert.IsNotNull(collections); Assert.AreEqual(2, collections.Count); var indexer = 0; foreach (var item in collections) { if (indexer == 0) { Assert.AreEqual(1, item.Count); } else { Assert.AreEqual(5, item.Count); } indexer++; } }
public void PrioritizedEventListenerDictionaryTests_Add_BothNull() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); dictionary.Add(null, null); }
public void PrioritizedEventListenerDictionaryTests_Add_KeyNull() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), (SurvivalKit.Events.Priority.HIGH), null)); dictionary.Add(null, mockListener); }
public void PrioritizedEventListenerDictionaryTests_Add_InsertSingle() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.NORMAL, null)); dictionary.Add(GetType(), mockListener); Assert.AreEqual(1, dictionary.Count); }
public void PrioritizedEventListenerDictionaryTests_ContainsKey_Valid() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.LOW, null)); dictionary.Add(GetType(), mockListener); Assert.IsTrue(dictionary.ContainsKey(GetType())); }
public void PrioritizedEventListenerDictionaryTests_ValueCollections_Multiple() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.NORMAL, null)); dictionary.Add(GetType(), mockListener); dictionary.Add(typeof(IEventHook), mockListener); var collections = dictionary.ValueCollections; Assert.IsNotNull(collections); Assert.AreEqual(2, collections.Count); foreach (var collection in collections) { Assert.AreEqual(GetType(), collection[0].EventHook.GetEventType()); } }
public void PrioritizedEventListenerDictionaryTests_Add_Random() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); for (var iterator = 0; iterator < 25; iterator++) { var prio = numberGenerator.Next(1, 5); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), (SurvivalKit.Events.Priority)prio, null)); dictionary.Add(GetType(), mockListener); } for (var iterator = 0; iterator < 25; iterator++) { var prio = numberGenerator.Next(1, 5); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(typeof(EventListenerRegistrationComparer), (SurvivalKit.Events.Priority)prio, null)); dictionary.Add(typeof(EventListenerRegistrationComparer), mockListener); } // count the keys int he dictionary Assert.AreEqual(2, dictionary.Count); var collection = dictionary.ValueCollections; foreach (var item in collection) { var topPrio = 100; foreach (var evtListenerRegistration in item) { if ((int)evtListenerRegistration.EventHook.HookPriority > topPrio) { Assert.Fail("Found an entry that has a higher priority than the previous item in the list"); } else if ((int)evtListenerRegistration.EventHook.HookPriority < topPrio) { topPrio = (int)evtListenerRegistration.EventHook.HookPriority; } } } }
public void PrioritizedEventListenerDictionaryTests_TryGetValue_Valid() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.HIGH, null)); dictionary.Add(typeof(PrioritizedEventListenerDictionaryTests), mockListener); List <EventListenerRegistration> list = null; var value = dictionary.TryGetValue(typeof(PrioritizedEventListenerDictionaryTests), out list); Assert.IsNotNull(list); Assert.AreEqual(1, list.Count); Assert.AreEqual(mockListener, list[0]); }
public void PrioritizedEventListenerDictionaryTests_Keys_Filled() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), (SurvivalKit.Events.Priority.HIGH), null)); dictionary.Add(GetType(), mockListener); var keys = new List <Type>(dictionary.Keys); Assert.IsNotNull(keys); Assert.AreEqual(1, keys.Count); Assert.AreEqual(GetType(), keys[0]); }
/// <summary> /// Method to register an event listener. /// </summary> /// <typeparam name="TListener">The type of the event listener.</typeparam> /// <param name="eventListener">The listener instance.</param> /// <returns> /// Returns <c>true</c> if the <see cref="TListener"/> was added to the registry of listeners. /// Returns <c>false</c> if the <see cref="TListener"/> was already added to the registry of listeners. /// </returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="eventListener"/> is <c>null</c>.</exception> public bool RegisterEventListener <TListener>(TListener eventListener) where TListener : EventListener { if (eventListener == null) { throw new ArgumentNullException("eventListener"); } LogUtility.Out("[SK] Registering " + eventListener.GetType().FullName); // TODO check if this event listener isn't already registered. var hooks = eventListener.GetEventHooks(); foreach (var eventHook in hooks) { var registration = new EventListenerRegistration(eventListener, eventHook); var type = eventHook.GetEventType(); _hookRegistry.Add(type, registration); } return(true); }