Exemple #1
0
        public void WhenNotificationSuspendedEventsShouldNotBeRaised()
        {
            SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock);
            var collectionTracker = new NotifiableCollectionTracker <Item>(collection);

            using (collection.SuspendNotifications())
            {
                for (int i = 0; i < 10; i++)
                {
                    var item = new Item();
                    collection.Add(item);
                    collection.Remove(item);
                }
                using (collection.SuspendNotifications())
                {
                    collection.Add(new Item());
                }
                collectionTracker.ChangedItems.ShouldBeEmpty();
            }
            ThreadManagerMock.InvokeOnUiThreadAsync();
            collectionTracker.AssertEquals();
        }
Exemple #2
0
        public void CollectionShouldTrackChangesCorrectInSourceCollection()
        {
            const int count                = 10;
            var       collection           = new ObservableCollection <Item>();
            var       filterableCollection = new FilterableNotifiableCollection <Item>(collection, ThreadManagerMock)
            {
                Filter = _ => true
            };
            var collectionTracker = new NotifiableCollectionTracker <Item>(filterableCollection);

            var item   = new Item();
            var items  = new[] { new Item(), new Item(), new Item() };
            var items2 = new[] { new Item {
                                     Hidden = true
                                 }, new Item(), new Item {
                                     Hidden = true
                                 } };

            for (int i = 0; i < count; i++)
            {
                collection.AddRange(items);
                collection.SequenceEqual(filterableCollection).ShouldBeTrue();
                collection.AddRange(items2);
                collection.SequenceEqual(filterableCollection).ShouldBeTrue();
                collection.RemoveRange(items);
                collection.SequenceEqual(filterableCollection).ShouldBeTrue();
            }
            for (int i = 0; i < collection.Count; i++)
            {
                collection[i] = item;
                collection.SequenceEqual(filterableCollection).ShouldBeTrue();
            }

            ThreadManagerMock.InvokeOnUiThreadAsync();
            collectionTracker.AssertChangedEquals();
            collection.Count.ShouldEqual(count * items2.Length);

            collection.Clear();
            ThreadManagerMock.InvokeOnUiThreadAsync();
            collection.SequenceEqual(filterableCollection).ShouldBeTrue();
            collection.Count.ShouldEqual(0);
            collectionTracker.AssertChangedEquals();
        }
        public override void CollectionShouldTrackChangesCorrect()
        {
            const int count = 10;
            SynchronizedNotifiableCollection <Item> collection = CreateNotifiableCollection <Item>(ExecutionMode.None,
                                                                                                   ThreadManagerMock);
            var collectionTracker = new NotifiableCollectionTracker <Item>(collection);
            var items             = new[] { new Item(), new Item(), new Item() };
            var items2            = new[] { new Item(), new Item(), new Item() };

            using (collection.SuspendNotifications())
            {
                for (int i = 0; i < count; i++)
                {
                    collection.AddRange(items);
                    collection.AddRange(items2);
                    collection.RemoveRange(items);
                }
            }
            ThreadManagerMock.InvokeOnUiThreadAsync();
            collectionTracker.AssertEquals();
            collection.Count.ShouldEqual(count * 3);
        }
Exemple #4
0
        public void CollectionShouldTrackChangesCorrectWithFilter()
        {
            const int count      = 10;
            var       collection = (FilterableNotifiableCollection <Item>)CreateNotifiableCollection <Item>(ExecutionMode.None, ThreadManagerMock);

            collection.Filter = item => !item.Hidden;

            var collectionTracker = new NotifiableCollectionTracker <Item>(collection);

            using (collection.SuspendNotifications())
            {
                var item   = new Item();
                var items  = new[] { new Item(), new Item(), new Item() };
                var items2 = new[] { new Item {
                                         Hidden = true
                                     }, new Item(), new Item {
                                         Hidden = true
                                     } };
                for (int i = 0; i < count; i++)
                {
                    collection.AddRange(items);
                    collection.AddRange(items2);
                    collection.RemoveRange(items);
                }
                for (int i = 0; i < collection.Count; i++)
                {
                    collection[i] = item;
                }
            }
            ThreadManagerMock.InvokeOnUiThreadAsync();
            collectionTracker.ChangingItems.OrderBy(item => item.Id)
            .SequenceEqual(collection.SourceCollection.OrderBy(item => item.Id))
            .ShouldBeTrue();
            collectionTracker.AssertChangedEquals();
            collection.Count.ShouldEqual(count);
        }
        private void BasePropertyTestInternal(Func <string> getProperty, Action <string> updateProperty, string propertyName, Action <ExecutionMode> changeRaiseOnUi, bool suspend)
        {
            changeRaiseOnUi(ExecutionMode.None);
            int          count    = 0;
            string       lastProp = null;
            const string value    = "value";

            PropertyChanged += (sender, args) =>
            {
                lastProp = args.PropertyName;
                count++;
            };

            lastProp.ShouldBeNull();
            getProperty().ShouldBeNull();
            updateProperty(value);
            getProperty().ShouldEqual(value);
            if (!suspend)
            {
                lastProp.ShouldEqual(propertyName);
                count.ShouldEqual(1);
            }
            else
            {
                count.ShouldEqual(0);
            }

            //Invoke on ui thread
            updateProperty(null);
            changeRaiseOnUi(ExecutionMode.AsynchronousOnUiThread);
            _threadManagerMock.InvokeOnUiThreadAsync();
            count    = 0;
            lastProp = null;
            updateProperty(value);
            getProperty().ShouldEqual(value);
            lastProp.ShouldBeNull();
            if (!suspend)
            {
                _threadManagerMock.InvokeOnUiThreadAsync();
                lastProp.ShouldEqual(propertyName);
                count.ShouldEqual(1);
            }
            else
            {
                _threadManagerMock.InvokeOnUiThreadAsync();
                count.ShouldEqual(0);
            }

            //Invoke on ui thread synchronous
            updateProperty(null);
            changeRaiseOnUi(ExecutionMode.SynchronousOnUiThread);
            count    = 0;
            lastProp = null;
            _threadManagerMock.InvokeOnUiThread();
            updateProperty(value);
            getProperty().ShouldEqual(value);
            lastProp.ShouldBeNull();
            if (!suspend)
            {
                _threadManagerMock.InvokeOnUiThread();
                lastProp.ShouldEqual(propertyName);
                count.ShouldEqual(1);
            }
            else
            {
                _threadManagerMock.InvokeOnUiThread();
                count.ShouldEqual(0);
            }
        }