public void WeakEventManagerShouldSubscribeAndUnsubscribePropertyChangedEventSeveralSources()
        {
            const int count = 100;
            const string propertyName = "test";

            var model = new BindingSourceModel();
            var listeners = new List<EventListenerMock>();
            var invokedItems = new List<EventListenerMock>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                var disposable = weakEventManager.Subscribe(model, propertyName, listenerMock);
                listeners.Add(listenerMock);
                listenerMock.Handle = (o, o1) =>
                {
                    invokedItems.Add(listenerMock);
                    disposable.Dispose();
                };
            }
            model.OnPropertyChanged(propertyName + "1");
            model.OnPropertyChanged(propertyName);
            model.OnPropertyChanged(propertyName);

            invokedItems.Count.ShouldEqual(count);
            foreach (var listener in listeners)
                invokedItems.Contains(listener).ShouldBeTrue();
        }
        public void WeakEventManagerShouldSubscribeAndUnsubscribePropertyChangedEvent()
        {
            const string propertyName = "test";
            int invokedCount = 0;
            var model = new BindingSourceModel();
            var listenerMock = new EventListenerMock();
            IWeakEventManager weakEventManager = CreateWeakEventManager();
            var disposable = weakEventManager.Subscribe(model, propertyName, listenerMock);

            listenerMock.Handle = (o, o1) => invokedCount++;
            invokedCount.ShouldEqual(0);

            model.OnPropertyChanged(propertyName + "1");
            invokedCount.ShouldEqual(0);
            model.OnPropertyChanged(propertyName);
            invokedCount.ShouldEqual(1);

            disposable.Dispose();

            model.OnPropertyChanged(propertyName);
            invokedCount.ShouldEqual(1);
        }
        public void CreateEventGenericMemberAttachTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            IEventListener listener = new EventListenerMock();
            const string path = "path";
            var property = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) => null,
                (model, args) =>
                {
                    model.ShouldEqual(source);
                    args.ShouldNotBeNull();
                    isInvoked = true;
                });

            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeFalse();

            source = new BindingSourceModel();
            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();
        }
        public void CreateEventGenericSetterTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            IEventListener listener = new EventListenerMock();
            const string path = "path";
            var property = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) =>
            {
                info.ShouldNotBeNull();
                o.ShouldEqual(source);
                arg3.ShouldEqual(listener);
                isInvoked = true;
                return null;
            });
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(typeof(Delegate));
            property.CanRead.ShouldBeTrue();
            property.CanWrite.ShouldBeTrue();
            property.MemberType.ShouldEqual(BindingMemberType.Event);

            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();
        }
        public void CreateEventGenericTest()
        {
            IEventListener listener = new EventListenerMock();
            var source = new BindingSourceModel();
            const string path = "path";
            var @event = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) => null);
            @event.Path.ShouldEqual(path);
            @event.Type.ShouldEqual(typeof(Delegate));
            @event.CanRead.ShouldBeTrue();
            @event.CanWrite.ShouldBeTrue();
            @event.Member.ShouldBeNull();
            @event.MemberType.ShouldEqual(BindingMemberType.Event);

            @event.GetValue(source, null).ShouldBeType<BindingActionValue>();
            @event.SetValue(source, new object[] { listener });
            var value = (BindingActionValue)@event.GetValue(source, null);
            value.Member.ShouldEqual(@event);
            value.MemberSource.Target.ShouldEqual(source);
        }
        public void CreateNotifiableMemberGenericObserveTest()
        {
            bool isInvoked = false;
            bool raiseEvent = true;
            var listenerMock = new EventListenerMock
            {
                Handle = (o, o1) => isInvoked = true
            };
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => raiseEvent);

            IDisposable subscriber = property.TryObserve(source, listenerMock);
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();
            subscriber.ShouldNotBeNull();

            raiseEvent = false;
            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();

            raiseEvent = true;
            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeTrue();

            subscriber.Dispose();
            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();
        }
        public void CreateMemberGenericObserveTest()
        {
            bool isInvoked = false;
            IDisposable result = null;
            var listenerMock = new EventListenerMock();
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => { },
                (info, o, arg3) =>
                {
                    isInvoked = true;
                    info.ShouldNotBeNull();
                    o.ShouldEqual(source);
                    arg3.ShouldEqual(listenerMock);
                    return result;
                });

            property.TryObserve(source, listenerMock).ShouldBeNull();
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            result = new ActionToken(() => { });
            property.TryObserve(source, listenerMock).ShouldEqual(result);
            isInvoked.ShouldBeTrue();
        }
        public void CreateMemberGenericObserveEventTest()
        {
            bool isInvoked = false;
            var listenerMock = new EventListenerMock
            {
                Handle = (o, o1) => isInvoked = true
            };
            var source = new BindingSourceModel();
            const string path = "path";
            source.RaiseEvent();
            var property = AttachedBindingMember.CreateMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => { }, BindingSourceModel.EventName);

            IDisposable subscriber = property.TryObserve(source, listenerMock);
            source.RaiseEvent();
            isInvoked.ShouldBeTrue();
            subscriber.ShouldNotBeNull();

            isInvoked = false;
            source.RaiseEvent();
            isInvoked.ShouldBeTrue();

            subscriber.Dispose();
            isInvoked = false;
            source.RaiseEvent();
            isInvoked.ShouldBeFalse();
        }
        public void AutoPropertyObserveTest()
        {
            bool isInvoked = false;
            var listenerMock = new EventListenerMock
            {
                Handle = (o, o1) => isInvoked = true
            };
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateAutoProperty(path, typeof(string));

            IDisposable subscriber = property.TryObserve(source, listenerMock);
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();
            subscriber.ShouldNotBeNull();

            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();

            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeTrue();

            subscriber.Dispose();
            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();
        }
        public void WeakEventManagerShouldRemoveWeakListenersPropertyChanged()
        {
            const int count = 100;
            const string propertyName = "test";

            var model = new BindingSourceModel();
            var listeners = new List<WeakReference>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                weakEventManager.Subscribe(model, propertyName, listenerMock);
                listeners.Add(new WeakReference(listenerMock));
                listenerMock.Handle = (o, o1) => { };
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            model.OnPropertyChanged(propertyName + "1");
            model.OnPropertyChanged(propertyName);
            model.OnPropertyChanged(propertyName);

            listeners.All(reference => reference.Target == null).ShouldBeTrue();
        }
        public void WeakEventManagerShouldRemoveWeakListenersEvent()
        {
            const int count = 100;

            var model = new BindingSourceModel();
            var listeners = new List<WeakReference>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                weakEventManager.TrySubscribe(model, BindingSourceModel.EventName, listenerMock);
                listeners.Add(new WeakReference(listenerMock));
                listenerMock.Handle = (o, o1) => { };
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            model.RaiseEvent();
            model.RaiseEvent();

            listeners.All(reference => reference.Target == null).ShouldBeTrue();
        }
        public void WeakEventManagerShouldSubscribeAndUnsubscribeEventSeveralSources()
        {
            const int count = 100;
            var model = new BindingSourceModel();
            var listeners = new List<EventListenerMock>();
            var invokedItems = new List<EventListenerMock>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                var disposable = weakEventManager.TrySubscribe(model, BindingSourceModel.EventName, listenerMock);
                listeners.Add(listenerMock);
                listenerMock.Handle = (o, o1) =>
                {
                    invokedItems.Add(listenerMock);
                    disposable.Dispose();
                };
            }
            model.RaiseEvent();
            model.RaiseEvent();

            invokedItems.Count.ShouldEqual(count);
            foreach (var listener in listeners)
                invokedItems.Contains(listener).ShouldBeTrue();
        }
        public void WeakEventManagerShouldSubscribeAndUnsubscribeEvent()
        {
            int invokedCount = 0;
            var model = new BindingSourceModel();
            var listenerMock = new EventListenerMock();
            IWeakEventManager weakEventManager = CreateWeakEventManager();
            var disposable = weakEventManager.TrySubscribe(model, BindingSourceModel.EventName, listenerMock);

            listenerMock.Handle = (o, o1) => invokedCount++;

            invokedCount.ShouldEqual(0);
            model.RaiseEvent();
            invokedCount.ShouldEqual(1);

            disposable.Dispose();
            model.RaiseEvent();
            invokedCount.ShouldEqual(1);
        }