Exemplo n.º 1
0
        public void test_event_handlers_are_independent()
        {
            var handler1 = new EventHandler();
            var handler2 = new EventHandler();
            var handler3 = new EventHandler();

            var count1 = 0;
            var count2 = 0;
            var count3 = 0;

            handler1.AddEventHandler <EventHandlerTests>((fp) => { count1 += 1; });
            handler2.AddEventHandler <EventHandlerTests>((fp) => { count2 += 1; });
            handler2.AddEventHandler <EventHandlerTests>((fp) => { count2 += 1; });
            handler3.AddEventHandler <EventHandlerTests>((fp) => { count3 += 1; });
            handler3.AddEventHandler <EventHandlerTests>((fp) => { count3 += 1; });
            handler3.AddEventHandler <EventHandlerTests>((fp) => { count3 += 1; });

            handler1.Trigger(this);
            handler2.Trigger(this);
            handler3.Trigger(this);

            handler1.Trigger(this);
            handler2.Trigger(this);
            handler3.Trigger(this);

            Assert(count1 == 2);
            Assert(count2 == 4);
            Assert(count3 == 6);
        }
        public void test_event_handlers_are_independent()
        {
            var handler1 = new EventHandler();
              var handler2 = new EventHandler();
              var handler3 = new EventHandler();

              var count1 = 0;
              var count2 = 0;
              var count3 = 0;

              handler1.AddEventHandler<EventHandlerTests>((fp) => { count1 += 1; });
              handler2.AddEventHandler<EventHandlerTests>((fp) => { count2 += 1; });
              handler2.AddEventHandler<EventHandlerTests>((fp) => { count2 += 1; });
              handler3.AddEventHandler<EventHandlerTests>((fp) => { count3 += 1; });
              handler3.AddEventHandler<EventHandlerTests>((fp) => { count3 += 1; });
              handler3.AddEventHandler<EventHandlerTests>((fp) => { count3 += 1; });

              handler1.Trigger(this);
              handler2.Trigger(this);
              handler3.Trigger(this);

              handler1.Trigger(this);
              handler2.Trigger(this);
              handler3.Trigger(this);

              Assert(count1 == 2);
              Assert(count2 == 4);
              Assert(count3 == 6);
        }
Exemplo n.º 3
0
        public void test_trigger_event()
        {
            var handler = new EventHandler();
            EventHandlerTests eventResult = null;

            handler.AddEventHandler <EventHandlerTests>((fp) => { eventResult = fp; });

            handler.Trigger(this);
            Assert(eventResult == this);
        }
Exemplo n.º 4
0
        public void test_clearing_event_handler_completes_pending_events()
        {
            var eventHandler = new EventHandler();
            var hits         = new Dictionary <Guid, int>();
            var keys         = new List <Guid> {
                Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
            };

            keys.ForEach(i => hits[i] = 0);

            Action <EventTypeA> typeAHandler = ep => { hits[ep.Value] += 1; };
            Action <EventTypeB> typeBHandler = ep => { hits[ep.Value] += 1; };

            EventBinding <EventTypeA> binding = null;
            Action <EventTypeA>       removeBindingHandler = ep =>
            {
                if (binding != null)
                {
                    binding.Dispose();
                }
            };

            eventHandler.AddEventHandler(typeAHandler);
            eventHandler.AddEventHandler(typeAHandler);

            binding = eventHandler.AddEventHandler(removeBindingHandler);

            eventHandler.AddEventHandler(typeAHandler);
            eventHandler.AddEventHandler(typeAHandler);

            eventHandler.AddEventHandler(typeBHandler);

            // Trigger normal events
            eventHandler.Trigger(new EventTypeB()
            {
                Value = keys[0]
            });
            Assert(hits[keys[0]] == 1);

            // Trigger handler that removes itself
            eventHandler.Trigger(new EventTypeA()
            {
                Value = keys[1]
            });
            Assert(hits[keys[1]] == 4);

            // Remaining event handlers still work
            eventHandler.Trigger(new EventTypeA()
            {
                Value = keys[2]
            });
            Assert(hits[keys[2]] == 4);
        }
Exemplo n.º 5
0
        public void test_infinite_recursion_event_dispatch()
        {
            var handler1 = new EventHandler();
            var handler2 = new EventHandler();

            var count1 = 0;
            var count2 = 0;

            handler1.AddEventHandler <EventHandlerTests>((fp) =>
            {
                count1 += 1;
                handler2.Trigger(this);
            });

            handler2.AddEventHandler <EventHandlerTests>((fp) =>
            {
                count2 += 1;
                handler1.Trigger(this);
            });

            try
            {
                handler1.Trigger(this);
                Unreachable();
            }
            catch (Exception)
            {
            }

            Assert(count1 == 50);
            Assert(count2 == 50);

            count1 = 0;
            count2 = 0;

            try
            {
                handler1.Trigger(this, 10);
                Unreachable();
            }
            catch (Exception)
            {
            }

            Assert(count1 == 5);
            Assert(count2 == 5);
        }
        public void test_infinite_recursion_event_dispatch()
        {
            var handler1 = new EventHandler();
              var handler2 = new EventHandler();

              var count1 = 0;
              var count2 = 0;

              handler1.AddEventHandler<EventHandlerTests>((fp) =>
              {
            count1 += 1;
            handler2.Trigger(this);
              });

              handler2.AddEventHandler<EventHandlerTests>((fp) =>
              {
            count2 += 1;
            handler1.Trigger(this);
              });

              try
              {
            handler1.Trigger(this);
            Unreachable();
              }
              catch(Exception)
              {
              }

              Assert(count1 == 50);
              Assert(count2 == 50);

              count1 = 0;
              count2 = 0;

              try
              {
            handler1.Trigger(this, 10);
            Unreachable();
              }
              catch(Exception)
              {
              }

              Assert(count1 == 5);
              Assert(count2 == 5);
        }
        public void test_async_event()
        {
            EventManager.Instance.Clear(true);

              var handler = new EventHandler();
              var gotEvent = false;
              handler.AddEventHandler<DeferredEventTests>((fp) =>
              {
            gotEvent = true;
              });

              handler.TriggerAsync(this);

              Assert(gotEvent == false);
              EventManager.Instance.Update();

              Assert(gotEvent == true);

              EventManager.Instance.Clear(true);
        }
Exemplo n.º 8
0
        public void test_async_event()
        {
            EventManager.Instance.Clear(true);

            var handler  = new EventHandler();
            var gotEvent = false;

            handler.AddEventHandler <DeferredEventTests>((fp) =>
            {
                gotEvent = true;
            });

            handler.TriggerAsync(this);

            Assert(gotEvent == false);
            EventManager.Instance.Update();

            Assert(gotEvent == true);

            EventManager.Instance.Clear(true);
        }
Exemplo n.º 9
0
 public void test_disposing_of_an_event_handler_disposes_correctly()
 {
     EventStream.Default.Clear();
     using (var handler1 = new EventHandler())
     {
         using (var handler2 = new EventHandler())
         {
             using (var handler3 = new EventHandler())
             {
                 handler1.AddEventHandler <EventHandlerTests>((fp) => { });
                 handler2.AddEventHandler <EventHandlerTests>((fp) => { });
                 handler2.AddEventHandler <EventHandlerTests>((fp) => { });
                 handler3.AddEventHandler <EventHandlerTests>((fp) => { });
                 handler3.AddEventHandler <EventHandlerTests>((fp) => { });
                 handler3.AddEventHandler <EventHandlerTests>((fp) => { });
                 Assert(EventStream.Default.Count == 6);
             }
             Assert(EventStream.Default.Count == 3);
         }
         Assert(EventStream.Default.Count == 1);
     }
     Assert(EventStream.Default.Count == 0);
 }
Exemplo n.º 10
0
 public void test_disposing_of_an_event_handler_disposes_correctly()
 {
     EventStream.Default.Clear();
       using (var handler1 = new EventHandler())
       {
     using (var handler2 = new EventHandler())
     {
       using (var handler3 = new EventHandler())
       {
     handler1.AddEventHandler<EventHandlerTests>((fp) => { });
     handler2.AddEventHandler<EventHandlerTests>((fp) => { });
     handler2.AddEventHandler<EventHandlerTests>((fp) => { });
     handler3.AddEventHandler<EventHandlerTests>((fp) => { });
     handler3.AddEventHandler<EventHandlerTests>((fp) => { });
     handler3.AddEventHandler<EventHandlerTests>((fp) => { });
     Assert(EventStream.Default.Count == 6);
       }
       Assert(EventStream.Default.Count == 3);
     }
     Assert(EventStream.Default.Count == 1);
       }
       Assert(EventStream.Default.Count == 0);
 }
Exemplo n.º 11
0
        public void test_recursive_event_dispatch()
        {
            var handler1 = new EventHandler();
            var handler2 = new EventHandler();
            var handler3 = new EventHandler();

            var count1 = 0;
            var count2 = 0;
            var count3 = 0;

            handler1.AddEventHandler <EventHandlerTests>((fp) =>
            {
                count1 += 1;

                var context = new EventContext();
                handler3.AddEventHandler <EventHandlerTests>((ep) =>
                {
                    count3 += 1;
                    context.Dispose();
                }, context);

                handler2.Trigger(this);
            });

            handler2.AddEventHandler <EventHandlerTests>((fp) =>
            {
                count2 += 1;
                handler3.Trigger(this);
            });

            handler1.Trigger(this);

            Assert(count1 == 1);
            Assert(count2 == 1);
            Assert(count3 == 1);
        }
Exemplo n.º 12
0
 public static void Reset()
 {
     Events           = new EventHandler();
     EventsDispatched = 0;
 }
Exemplo n.º 13
0
 public static void Reset()
 {
     Events = new EventHandler();
       EventsDispatched = 0;
 }
Exemplo n.º 14
0
        public void test_trigger_event()
        {
            var handler = new EventHandler();
              EventHandlerTests eventResult = null;
              handler.AddEventHandler<EventHandlerTests>((fp) => { eventResult = fp; });

              handler.Trigger(this);
              Assert(eventResult == this);
        }
Exemplo n.º 15
0
        public void test_recursive_event_dispatch()
        {
            var handler1 = new EventHandler();
              var handler2 = new EventHandler();
              var handler3 = new EventHandler();

              var count1 = 0;
              var count2 = 0;
              var count3 = 0;

              handler1.AddEventHandler<EventHandlerTests>((fp) =>
              {
            count1 += 1;

            var context = new EventContext();
            handler3.AddEventHandler<EventHandlerTests>((ep) =>
            {
              count3 += 1;
              context.Dispose();
            }, context);

            handler2.Trigger(this);
              });

              handler2.AddEventHandler<EventHandlerTests>((fp) =>
              {
            count2 += 1;
            handler3.Trigger(this);
              });

              handler1.Trigger(this);

              Assert(count1 == 1);
              Assert(count2 == 1);
              Assert(count3 == 1);
        }