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 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();
            }
        }
Example #3
0
        public void ObserverShouldRaiseValueChangedEventWhenPropertyChangedDoubleIndexer()
        {
            bool isInvoked    = false;
            var  model        = new BindingSourceModel();
            var  propertyName = GetMemberPath(model, sourceModel => sourceModel["test", 0]);
            var  observer     = CreateObserver(model, propertyName, false);

            observer.Listener = new HandlerToDelegate((sender, args) => isInvoked = true);

            isInvoked        = false;
            model["test", 0] = "test";
            isInvoked.ShouldBeFalse();
            model.OnPropertyChanged("Item[]");
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            model["test", 0] = "test1";
            isInvoked.ShouldBeFalse();
            model.OnPropertyChanged("Item[test,0]");
            isInvoked.ShouldBeTrue();
        }
Example #4
0
        public void ObserverShouldRaiseValueChangedEventWhenPropertyChangedIndexer()
        {
            bool isInvoked    = false;
            var  model        = new BindingSourceModel();
            var  propertyName = GetMemberPath(model, sourceModel => sourceModel["test"]);
            var  observer     = CreateObserver(model, propertyName, false);

            observer.ValueChanged += (sender, args) => isInvoked = true;

            isInvoked     = false;
            model["test"] = "test";
            isInvoked.ShouldBeFalse();
            model.OnPropertyChanged(ReflectionExtensions.IndexerName);
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            model["test"] = "test1";
            isInvoked.ShouldBeFalse();
            model.OnPropertyChanged("Item[test]");
            isInvoked.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);
        }
Example #6
0
        public void RaiseCanExecuteChangedShouldBeInvokedOnPropertyChanged()
        {
            var  notifier     = new BindingSourceModel();
            bool isInvoked    = false;
            var  relayCommand = CreateCommand(NodoAction, o => true);

            relayCommand.CanExecuteMode     = ExecutionMode.None;
            relayCommand.CanExecuteChanged += (sender, args) =>
            {
                isInvoked = true;
            };
            relayCommand.AddNotifier(notifier);
            notifier.OnPropertyChanged("Test", ExecutionMode.None);
            isInvoked.ShouldBeTrue();
        }