Example #1
0
        public void ForwardsArgsWhenRaising()
        {
            WeakEvent<EventHandler<EventArgs1>> handler = new WeakEvent<EventHandler<EventArgs1>>();

            handler.Add(EventReceiver1.Callback);

            EventArgs1 args = new EventArgs1(2);

            handler.Raise(null, args);

            Assert.That(EventReceiver1.Args, Is.EqualTo(args));
        }
Example #2
0
        public void ForwardsSenderWhenRaising()
        {
            WeakEvent<EventHandler<EventArgs1>> handler = new WeakEvent<EventHandler<EventArgs1>>();

            handler.Add(EventReceiver1.Callback);

            EventArgs1 args = new EventArgs1(2);
            object sender = new object();

            handler.Raise(sender, args);

            Assert.That(EventReceiver1.Sender, Is.EqualTo(sender));
        }
Example #3
0
        public void IgnoresGarbageCollectedListeners()
        {
            WeakEvent<EventHandler<EventArgs1>> handler = new WeakEvent<EventHandler<EventArgs1>>();

            EventReceiver1 receiver = new EventReceiver1();
            WeakReference receiverReference = new WeakReference(receiver);

            EventArgs1 args = new EventArgs1(2);
            object sender = new object();

            handler.Add(receiver.Callback);

            handler.Raise(sender, args);

            // ReSharper disable RedundantAssignment
            receiver = null;
            // ReSharper restore RedundantAssignment

            GC.Collect();

            Assert.That(receiverReference.IsAlive, Is.False);

            handler.Raise(sender, args);

            Assert.That(handler.ListenersCount, Is.EqualTo(0));
        }
Example #4
0
 public void Callback(object sender, EventArgs1 args)
 {
     Sender = sender;
     Args = args;
     CallCounter++;
 }