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 ExecuteShouldCallCmdExecuteMethodOneTimeModeAfterDispose()
        {
            var parameter = new object();
            bool isInvoked = false;
            var command = new RelayCommand(o =>
            {
                o.ShouldEqual(parameter);
                isInvoked = true;
            });
            var srcAccessor = new BindingSourceAccessorMock();
            var source = new BindingSourceModel();
            var ctx = new DataContext(BindingBuilderConstants.Behaviors.ToValue(new List<IBindingBehavior> { new OneTimeBindingMode() }));
            var accessor = GetAccessor(source, BindingSourceModel.EventName, ctx, false, d => parameter);
            srcAccessor.GetValue = (info, context, arg3) => command;
            accessor.SetValue(srcAccessor, EmptyContext, true);
            accessor.Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

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

            isInvoked = false;
            source.RaiseEvent();
            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 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);
        }
        public void ExecuteShouldCallCmdExecuteMethod()
        {
            var parameter = new object();
            bool isInvoked = false;
            var command = new RelayCommand(o =>
            {
                o.ShouldEqual(parameter);
                isInvoked = true;
            });
            var srcAccessor = new BindingSourceAccessorMock();
            var source = new BindingSourceModel();
            var accessor = GetAccessor(source, BindingSourceModel.EventName, EmptyContext, false, d => parameter);
            srcAccessor.GetValue = (info, context, arg3) => command;
            accessor.SetValue(srcAccessor, EmptyContext, true);
            source.RaiseEvent();
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            source.RaiseEvent();
            isInvoked.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();
        }