public void WhenFirstSubscriberAdded_ThenAttachesToSource()
        {
            var added   = 0;
            var removed = 0;

            var observable = new WeakObservableEvent <EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => added++,
                handler => removed++);

            observable.Subscribe(new Mock <IObserver <IEvent <EventArgs> > >().Object);

            Assert.Equal(1, added);
            Assert.Equal(0, removed);

            observable.Subscribe(new Mock <IObserver <IEvent <EventArgs> > >().Object);

            Assert.Equal(1, added);
            Assert.Equal(0, removed);
        }
        public void WhenFirstSubscriberAdded_ThenAttachesToSource()
        {
            var added = 0;
            var removed = 0;

            var observable = new WeakObservableEvent<EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => added++,
                handler => removed++);

            observable.Subscribe(new Mock<IObserver<IEvent<EventArgs>>>().Object);

            Assert.Equal(1, added);
            Assert.Equal(0, removed);

            observable.Subscribe(new Mock<IObserver<IEvent<EventArgs>>>().Object);

            Assert.Equal(1, added);
            Assert.Equal(0, removed);
        }
        public void WhenSourceEventRaised_ThenInvokesOnNextOnSubscriber()
        {
            EventHandler raiseHandler = null;
            EventArgs    args         = new EventArgs();
            var          subscriber   = new Mock <IObserver <IEvent <EventArgs> > >();
            var          observable   = new WeakObservableEvent <EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => raiseHandler = handler,
                handler => { });

            var subscription = observable.Subscribe(subscriber.Object);

            raiseHandler(this, args);

            subscriber.Verify(x => x.OnNext(It.Is <IEvent <EventArgs> >(e => e.EventArgs == args)));
        }
        public void WhenSourceEventRaised_ThenInvokesOnNextOnSubscriber()
        {
            EventHandler raiseHandler = null;
            EventArgs args = new EventArgs();
            var subscriber = new Mock<IObserver<IEvent<EventArgs>>>();
            var observable = new WeakObservableEvent<EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => raiseHandler = handler,
                handler => { });

            var subscription = observable.Subscribe(subscriber.Object);

            raiseHandler(this, args);

            subscriber.Verify(x => x.OnNext(It.Is<IEvent<EventArgs>>(e => e.EventArgs == args)));
        }
        public void WhenLastSubscriberRemovedByDisposingSubscription_ThenDetachesFromSource()
        {
            var added      = 0;
            var removed    = 0;
            var subscriber = new Mock <IObserver <IEvent <EventArgs> > >();
            var observable = new WeakObservableEvent <EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => added++,
                handler => removed++);

            var subscription = observable.Subscribe(subscriber.Object);

            subscription.Dispose();

            Assert.Equal(1, added);
            Assert.Equal(1, removed);
        }
        public void WhenLastSubscriberRemovedByDisposingSubscription_ThenDetachesFromSource()
        {
            var added = 0;
            var removed = 0;
            var subscriber = new Mock<IObserver<IEvent<EventArgs>>>();
            var observable = new WeakObservableEvent<EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => added++,
                handler => removed++);

            var subscription = observable.Subscribe(subscriber.Object);

            subscription.Dispose();

            Assert.Equal(1, added);
            Assert.Equal(1, removed);
        }
        public void WhenSourceEventRaisedAndWeakSubscriberDisposed_ThenRemovesSubscriber()
        {
            EventHandler raiseHandler   = null;
            var          removed        = 0;
            var          observer       = new Mock <IObserver <IEvent <EventArgs> > >();
            var          weakSubscriber = new WeakObserverEvent <EventArgs>(observer.Object.OnNext, observer.Object.OnError, observer.Object.OnCompleted);
            var          observable     = new WeakObservableEvent <EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => raiseHandler = handler,
                handler => removed++);

            observable.Subscribe(weakSubscriber);

            Assert.NotNull(raiseHandler);
            Assert.Equal(0, removed);

            observer = null;
            GC.Collect();

            raiseHandler(this, EventArgs.Empty);

            Assert.Equal(1, removed);
        }
        public void WhenSourceEventRaisedAndWeakSubscriberDisposed_ThenRemovesSubscriber()
        {
            EventHandler raiseHandler = null;
            var removed = 0;
            var observer = new Mock<IObserver<IEvent<EventArgs>>>();
            var weakSubscriber = new WeakObserverEvent<EventArgs>(observer.Object.OnNext, observer.Object.OnError, observer.Object.OnCompleted);
            var observable = new WeakObservableEvent<EventHandler, EventArgs>(
                handler => new EventHandler(handler.Invoke),
                handler => raiseHandler = handler,
                handler => removed++);

            observable.Subscribe(weakSubscriber);

            Assert.NotNull(raiseHandler);
            Assert.Equal(0, removed);

            observer = null;
            GC.Collect();

            raiseHandler(this, EventArgs.Empty);

            Assert.Equal(1, removed);
        }