public void FilteredHandlerLifeCycleTest()
        {
            var handler1Called = false;
            var handler2Called = false;
            var handler3Called = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            //------------------
            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handler1Called = true; });
            listener.RegisterHandler(NotifyCollectionChangedAction.Remove, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Remove); handler2Called = true; });
            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handler3Called = true; });

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, null);

            handler1Called.Is(true);
            handler2Called.Is(true);
            handler3Called.Is(true);

            //------------------
            handler1Called = false;
            handler2Called = false;
            handler3Called = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, null);

            handler1Called.Is(false);
            handler2Called.Is(false);
            handler3Called.Is(false);
        }
Exemplo n.º 2
0
        public void FilteredHandlerLifeCycleTest()
        {
            var handler1Called = false;
            var handler2Called = false;
            var handler3Called = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            //------------------
            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handler1Called = true; });
            listener.RegisterHandler(NotifyCollectionChangedAction.Remove, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Remove); handler2Called = true; });
            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handler3Called = true; });

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, null);

            handler1Called.Is(true);
            handler2Called.Is(true);
            handler3Called.Is(true);

            //------------------
            handler1Called = false;
            handler2Called = false;
            handler3Called = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, null);

            handler1Called.Is(false);
            handler2Called.Is(false);
            handler3Called.Is(false);
        }
Exemplo n.º 3
0
            public HandlerMemoryLeakTestClass(INotifyCollectionChanged publisher)
            {
                Listener = new CollectionChangedEventListener(publisher);

                // This handler refers "this".
                NotifyCollectionChangedEventHandler handler = (sender, e) => { ToString(); };

                Listener.RegisterHandler(handler);
                Listener.RegisterHandler(NotifyCollectionChangedAction.Reset, handler);
            }
Exemplo n.º 4
0
        public void SourceReferenceMemoryLeakTest()
        {
            var handler1Success = false;

            var publisherStrongReference = new TestEventPublisher();
            var publisherWeakReference   = new WeakReference <TestEventPublisher>(publisherStrongReference);

            var listener = new CollectionChangedEventListener(publisherStrongReference);

            listener.RegisterHandler((sender, e) => handler1Success = true);

            publisherStrongReference.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            listener.Dispose();
            publisherStrongReference = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            TestEventPublisher resultPublisher = null;

            publisherWeakReference.TryGetTarget(out resultPublisher).Is(false);
            resultPublisher.IsNull();
        }
Exemplo n.º 5
0
        public void BasicConstructorLifeCycleTest()
        {
            var listenerSuccess = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher, (sender, e) => listenerSuccess = true);

            //------------------
            listenerSuccess.Is(false);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            listenerSuccess.Is(true);

            //------------------
            listenerSuccess = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            listenerSuccess.Is(false);

            try
            {
                listener.RegisterHandler((sender, e) => listenerSuccess = true);
            }
            catch (Exception e)
            {
                e.GetType().Is(typeof(ObjectDisposedException));
            }
        }
        public void BasicConstructorLifeCycleTest()
        {
            var listenerSuccess = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher, (sender, e) => listenerSuccess = true);

            //------------------
            listenerSuccess.Is(false);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            listenerSuccess.Is(true);

            //------------------
            listenerSuccess = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            listenerSuccess.Is(false);

            try
            {
                listener.RegisterHandler((sender, e) => listenerSuccess = true);
            }
            catch (Exception e)
            {
                e.GetType().Is(typeof(ObjectDisposedException));
            }
        }
Exemplo n.º 7
0
        public void MultipleHandlerLifeCycleTest()
        {
            var handler1Success = false;
            var handler2Success = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            //------------------
            handler1Success.Is(false);
            handler2Success.Is(false);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(false);
            handler2Success.Is(false);

            //------------------
            listener.RegisterHandler((sender, e) => handler1Success = true);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            handler2Success.Is(false);

            //------------------
            handler1Success = false;
            handler2Success = false;

            listener.RegisterHandler((sender, e) => handler2Success = true);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            handler2Success.Is(true);

            //------------------
            handler1Success = false;
            handler2Success = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(false);
            handler2Success.Is(false);
        }
        public void MultipleHandlerLifeCycleTest()
        {
            var handler1Success = false;
            var handler2Success = false;

            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            //------------------
            handler1Success.Is(false);
            handler2Success.Is(false);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(false);
            handler2Success.Is(false);

            //------------------
            listener.RegisterHandler((sender, e) => handler1Success = true);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            handler2Success.Is(false);

            //------------------
            handler1Success = false;
            handler2Success = false;

            listener.RegisterHandler((sender, e) => handler2Success = true);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            handler2Success.Is(true);

            //------------------
            handler1Success = false;
            handler2Success = false;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(false);
            handler2Success.Is(false);
        }
Exemplo n.º 9
0
        public void MultiThreadHandlerTest()
        {
            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            var handlerCalledCount = 0;

            var parentTask = new Task(() =>
            {
                var tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.AttachedToParent);

                for (int i = 0; i < 50; i++)
                {
                    tf.StartNew(() =>
                    {
                        for (int f = 0; f < 500; f++)
                        {
                            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handlerCalledCount++; });
                        }
                    });
                }
            });

            parentTask.Start();
            parentTask.Wait();

            handlerCalledCount.Is(0);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handlerCalledCount.Is(25000);

            handlerCalledCount = 0;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handlerCalledCount.Is(0);
        }
Exemplo n.º 10
0
        public static ReadOnlyDispatcherCollection <TViewModel> CreateReadOnlyDispatcherCollection <TModel, TViewModel>(
            [NotNull] IList <TModel> source, [NotNull] Func <TModel, TViewModel> converter,
            [NotNull] Dispatcher dispatcher)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }
            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            if (!(source is INotifyCollectionChanged sourceAsNotifyCollection))
            {
                throw new ArgumentException("sourceがINotifyCollectionChangedを実装していません");
            }

            var initCollection = new ObservableCollection <TViewModel>();

            foreach (var model in source)
            {
                initCollection.Add(converter(model));
            }

            var target = new DispatcherCollection <TViewModel>(initCollection, dispatcher);
            var result = new ReadOnlyDispatcherCollection <TViewModel>(target);

            var collectionChangedListener = new CollectionChangedEventListener(sourceAsNotifyCollection);

            result.EventListeners.Add(collectionChangedListener);

            collectionChangedListener.RegisterHandler((sender, e) =>
            {
                if (e == null)
                {
                    throw new ArgumentNullException(nameof(e));
                }
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    var vm = converter((TModel)e.NewItems?[0]);
                    InvokeOnDispatcher(() => target.Insert(e.NewStartingIndex, vm), dispatcher);
                    break;

                case NotifyCollectionChangedAction.Move:
                    InvokeOnDispatcher(() => target.Move(e.OldStartingIndex, e.NewStartingIndex), dispatcher);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                    {
                        ((IDisposable)target[e.OldStartingIndex])?.Dispose();
                    }
                    InvokeOnDispatcher(() => target.RemoveAt(e.OldStartingIndex), dispatcher);
                    break;

                case NotifyCollectionChangedAction.Replace:
                    if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                    {
                        ((IDisposable)target[e.NewStartingIndex])?.Dispose();
                    }
                    var replaceVm = converter((TModel)e.NewItems?[0]);
                    InvokeOnDispatcher(() => target[e.NewStartingIndex] = replaceVm, dispatcher);
                    break;

                case NotifyCollectionChangedAction.Reset:
                    if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                    {
                        foreach (var item in target.OfType <IDisposable>())
                        {
                            item.Dispose();
                        }
                    }

                    InvokeOnDispatcher(target.Clear, dispatcher);
                    break;

                default:
                    throw new ArgumentException();
                }
            });

            return(result);
        }
        public void SourceReferenceMemoryLeakTest()
        {
            var handler1Success = false;

            var publisherStrongReference = new TestEventPublisher();
            var publisherWeakReference = new WeakReference<TestEventPublisher>(publisherStrongReference);

            var listener = new CollectionChangedEventListener(publisherStrongReference);
            listener.RegisterHandler((sender, e) => handler1Success = true);

            publisherStrongReference.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handler1Success.Is(true);
            listener.Dispose();
            publisherStrongReference = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            TestEventPublisher resultPublisher = null;
            publisherWeakReference.TryGetTarget(out resultPublisher).Is(false);
            resultPublisher.IsNull();
        }
        public void MultiThreadHandlerTest()
        {
            var publisher = new TestEventPublisher();

            var listener = new CollectionChangedEventListener(publisher);

            var handlerCalledCount = 0;

            var parentTask = new Task(() =>
            {
                var tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.AttachedToParent);

                for (int i = 0; i < 50; i++)
                {
                    tf.StartNew(() =>
                    {
                        for (int f = 0; f < 500; f++)
                        {
                            listener.RegisterHandler(NotifyCollectionChangedAction.Add, (sender, e) => { e.Action.Is(NotifyCollectionChangedAction.Add); handlerCalledCount++; });
                        }

                    });
                }
            });

            parentTask.Start();
            parentTask.Wait();

            handlerCalledCount.Is(0);

            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handlerCalledCount.Is(25000);

            handlerCalledCount = 0;

            listener.Dispose();
            publisher.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, null);

            handlerCalledCount.Is(0);
        }
            public HandlerMemoryLeakTestClass(INotifyCollectionChanged publisher)
            {
                Listener = new CollectionChangedEventListener(publisher);

                // This handler refers "this".
                NotifyCollectionChangedEventHandler handler = (sender, e) => { ToString(); };
                Listener.RegisterHandler(handler);
                Listener.RegisterHandler(NotifyCollectionChangedAction.Reset, handler);
            }
Exemplo n.º 14
0
        public static ReadOnlyDispatcherCollection <TViewModel> CreateReadOnlyDispatcherCollection <TModel, TViewModel>(IList <TModel> source, Func <TModel, TViewModel> converter, Dispatcher dispatcher)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var sourceAsNotifyCollection = source as INotifyCollectionChanged;

            if (sourceAsNotifyCollection == null)
            {
                throw new ArgumentException("sourceがINotifyCollectionChangedを実装していません");
            }

            var initCollection = new ObservableCollection <TViewModel>();
            var internalLock   = new object();

            lock (internalLock)
            {
                var target = new DispatcherCollection <TViewModel>(initCollection, dispatcher);
                var result = new ReadOnlyDispatcherCollection <TViewModel>(target);

                var collectionChangedListener = new CollectionChangedEventListener(sourceAsNotifyCollection);

                result.EventListeners.Add(collectionChangedListener);

                collectionChangedListener.RegisterHandler((sender, e) =>
                {
                    lock (internalLock)
                    {
                        switch (e.Action)
                        {
                        case NotifyCollectionChangedAction.Add:
                            target.Insert(e.NewStartingIndex, converter((TModel)e.NewItems[0]));
                            break;

                        case NotifyCollectionChangedAction.Move:
                            target.Move(e.OldStartingIndex, e.NewStartingIndex);
                            break;

                        case NotifyCollectionChangedAction.Remove:
                            if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                            {
                                ((IDisposable)target[e.OldStartingIndex]).Dispose();
                            }
                            target.RemoveAt(e.OldStartingIndex);
                            break;

                        case NotifyCollectionChangedAction.Replace:
                            if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                            {
                                ((IDisposable)target[e.NewStartingIndex]).Dispose();
                            }
                            target[e.NewStartingIndex] = converter((TModel)e.NewItems[0]);
                            break;

                        case NotifyCollectionChangedAction.Reset:
                            if (typeof(IDisposable).IsAssignableFrom(typeof(TViewModel)))
                            {
                                foreach (IDisposable item in target)
                                {
                                    item.Dispose();
                                }
                            }
                            target.Clear();
                            break;

                        default:
                            throw new ArgumentException();
                        }
                    }
                });

                foreach (var model in source)
                {
                    initCollection.Add(converter(model));
                }
                return(result);
            }
        }