예제 #1
0
            public void SuspendsValidationWhileAddingAndRemovingItems()
            {
                int counter = 0;

                var fastCollection = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) => counter++;

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);
                    fastCollection.Add(2);
                    fastCollection.Add(3);
                    fastCollection.Add(4);
                    fastCollection.Add(5);

                    fastCollection.Remove(5);
                    fastCollection.Remove(4);
                    fastCollection.Remove(3);
                    fastCollection.Remove(2);
                    fastCollection.Remove(1);
                }

                Assert.AreEqual(0, counter);
            }
예제 #2
0
            public void RaisesTwoEvents()
            {
                var eventArgsList  = new List <NotifyRangedListChangedEventArgs>();
                var fastCollection = new FastBindingList <int>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                fastCollection.AddItems(new[] { 1, 2, 3, 4 });

                fastCollection.ListChanged += (sender, args) =>
                {
                    eventArgsList.Add(args as NotifyRangedListChangedEventArgs);
                };

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.RemoveItems(new[] { 4 });
                    fastCollection.AddItems(new[] { 5 });
                }

                Assert.AreEqual(2, eventArgsList.Count);

                Assert.Contains(5, eventArgsList.First(args => args.Action == NotifyRangedListChangedAction.Add).NewItems);
                Assert.Contains(4, eventArgsList.First(args => args.Action == NotifyRangedListChangedAction.Remove).OldItems);
            }
예제 #3
0
            public void ListIsSortedDescendingAfterSortDescendingByStringField()
            {
                var pdc  = TypeDescriptor.GetProperties(typeof(TestModel));
                var desc = pdc.Find("TestStringProperty", false);

                var fastCollection = new FastBindingList <TestModel>();
                var tm0            = new TestModel()
                {
                    TestIntProperty = 1, TestStringProperty = "Test1", TestTypeProperty = null
                };
                var tm1 = new TestModel()
                {
                    TestIntProperty = 2, TestStringProperty = "Test2", TestTypeProperty = null
                };
                var tm2 = new TestModel()
                {
                    TestIntProperty = 3, TestStringProperty = "Test3", TestTypeProperty = null
                };
                var tmn = new TestModel()
                {
                    TestIntProperty = 4, TestStringProperty = null, TestTypeProperty = null
                };

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.Add(tm0);
                fastCollection.Add(tm1);
                fastCollection.Add(tmn);
                fastCollection.Add(tm2);

                ((IBindingList)fastCollection).ApplySort(desc, ListSortDirection.Descending);

                CollectionAssert.AreEqual(new List <TestModel> {
                    tm2, tm1, tm0, tmn
                }, fastCollection);
            }
예제 #4
0
            public void ReturnsItemIndexWhenItemWasFound()
            {
                var pdc  = TypeDescriptor.GetProperties(typeof(TestModel));
                var desc = pdc.Find("TestProperty", false);

                var fastCollection = new FastBindingList <TestModel>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test1"
                });
                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test2"
                });
                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test3"
                });

                var idx0 = ((IBindingList)fastCollection).Find(desc, "Test1");
                var idx1 = ((IBindingList)fastCollection).Find(desc, "Test2");
                var idx2 = ((IBindingList)fastCollection).Find(desc, "Test3");

                Assert.AreEqual(0, idx0);
                Assert.AreEqual(1, idx1);
                Assert.AreEqual(2, idx2);
            }
예제 #5
0
            public void RemovingItemsInRemovingMode()
            {
                var counter   = 0;
                var eventArgs = (NotifyRangedListChangedEventArgs)null;

                var fastCollection = new FastBindingList <int> {
                    1, 2, 3, 4, 5
                };

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e as NotifyRangedListChangedEventArgs;
                };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Removing))
                {
                    fastCollection.Remove(1);
                    fastCollection.Remove(2);
                    fastCollection.Remove(3);
                    fastCollection.Remove(4);
                    fastCollection.Remove(5);
                }

                Assert.AreEqual(1, counter);
                Assert.AreEqual(ListChangedType.Reset, eventArgs.ListChangedType);
                Assert.AreEqual(NotifyRangedListChangedAction.Remove, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.OldItems, new[] { 1, 2, 3, 4, 5 });
            }
예제 #6
0
            public void RaisesSingleRemoveEventIfTheAddedItemsAreASubSetOfTheRemovedItems()
            {
                var count = 0;
                NotifyRangedListChangedEventArgs eventArgs = null;
                var fastCollection = new FastBindingList <int>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                fastCollection.AddItems(new[] { 1, 2, 3, 4 });

                fastCollection.ListChanged += (sender, args) =>
                {
                    count++;
                    eventArgs = args as NotifyRangedListChangedEventArgs;
                };

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.RemoveItems(new[] { 4, 2, 3 });
                    fastCollection.AddItems(new[] { 2 });
                }

                Assert.AreEqual(NotifyRangedListChangedAction.Remove, eventArgs.Action);
                Assert.AreEqual(1, count);
                Assert.AreEqual(new[] { 4, 3 }, eventArgs.OldItems.OfType <int>().ToArray());
            }
예제 #7
0
            public void CascadedAddingItemsInAddingMode()
            {
                var counter   = 0;
                var eventArgs = (NotifyRangedListChangedEventArgs)null;

                var fastCollection = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e as NotifyRangedListChangedEventArgs;
                };

                var firstToken  = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);
                var secondToken = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);

                fastCollection.Add(1);
                fastCollection.Add(2);
                fastCollection.Add(3);
                fastCollection.Add(4);
                fastCollection.Add(5);

                secondToken.Dispose();
                Assert.AreEqual(0, counter);
                Assert.IsNull(eventArgs);

                firstToken.Dispose();
                Assert.AreEqual(1, counter);
                // ReSharper disable PossibleNullReferenceException
                Assert.AreEqual(ListChangedType.Reset, eventArgs.ListChangedType);
                Assert.AreEqual(NotifyRangedListChangedAction.Add, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.NewItems, new[] { 1, 2, 3, 4, 5 });
                // ReSharper restore PossibleNullReferenceException
            }
예제 #8
0
            public void RaisesResetEventWhileSorting()
            {
                var pdc  = TypeDescriptor.GetProperties(typeof(TestModel));
                var desc = pdc.Find("TestStringProperty", false);

                var counter   = 0;
                var eventArgs = (NotifyListChangedEventArgs)null;

                var fastCollection = new FastBindingList <TestModel>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.Add(new TestModel()
                {
                    TestIntProperty = 1, TestStringProperty = "Test1"
                });
                fastCollection.Add(new TestModel()
                {
                    TestIntProperty = 2, TestStringProperty = "Test2"
                });
                fastCollection.Add(new TestModel()
                {
                    TestIntProperty = 3, TestStringProperty = "Test3"
                });
                fastCollection.ListChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e as NotifyListChangedEventArgs;
                };

                ((IBindingList)fastCollection).ApplySort(desc, ListSortDirection.Ascending);

                Assert.AreEqual(1, counter);
                Assert.AreEqual(ListChangedType.Reset, eventArgs.ListChangedType);
            }
예제 #9
0
            public void ReturnsMinusOneWhenItemWasNotFound()
            {
                var pdc  = TypeDescriptor.GetProperties(typeof(TestModel));
                var desc = pdc.Find("TestProperty", false);

                var fastCollection = new FastBindingList <TestModel>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test1"
                });
                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test2"
                });
                fastCollection.Add(new TestModel()
                {
                    TestProperty = "Test3"
                });

                var idxnf = ((IBindingList)fastCollection).Find(desc, "Test4");

                Assert.AreEqual(-1, idxnf);
            }
예제 #10
0
            public void ThrowsArgumentNullExceptionForNullCollection()
            {
                var fastCollection = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => fastCollection.AddItems(null));
            }
예제 #11
0
            public void CallingResetWhileAddingItemsInAddingMode()
            {
                var counter   = 0;
                var eventArgs = (NotifyRangedListChangedEventArgs)null;

                var fastCollection = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e as NotifyRangedListChangedEventArgs;
                };

                var token = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);

                fastCollection.Add(1);
                fastCollection.Add(2);

                fastCollection.Reset();
                Assert.AreEqual(0, counter);

                fastCollection.Add(3);
                fastCollection.Add(4);
                fastCollection.Add(5);
                token.Dispose();

                Assert.AreEqual(1, counter);
                Assert.AreEqual(ListChangedType.Reset, eventArgs.ListChangedType);
                Assert.AreEqual(NotifyRangedListChangedAction.Add, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.NewItems, new[] { 1, 2, 3, 4, 5 });
            }
예제 #12
0
            private SuspensionContext <T> GetSuspensionContext <T>(FastBindingList <T> collection)
            {
                var t = typeof(FastBindingList <T>);
                var f = t.GetFieldEx("_suspensionContext", BindingFlags.Instance | BindingFlags.NonPublic);
                var v = f.GetValue(collection) as SuspensionContext <T>;

                return(v);
            }
예제 #13
0
            public void ThrowsInvalidOperationExceptionForClearingInAddingMode()
            {
                var fastCollection = new FastBindingList <int>();

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                    Assert.Throws <InvalidOperationException>(() => fastCollection.Clear());
                }
            }
예제 #14
0
        public void Find()
        {
            var coll = new FastBindingList <KeyValuePair <int, string> > {
                new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2")
            };

            Throws <ArgumentException>(() => coll.Find("X", null), "No property descriptor found for property name 'X' in type 'System.Collections.Generic.KeyValuePair`2[System.Int32,System.String]'.");
            Assert.AreEqual(-1, coll.Find(nameof(KeyValuePair <_, _> .Key), 0));
            Assert.AreEqual(0, coll.Find(nameof(KeyValuePair <_, _> .Key), 1));
        }
예제 #15
0
            public void ReturnsTrueWhenChangesAreSuspended()
            {
                var fastCollection = new FastBindingList <int>();

                using (fastCollection.SuspendChangeNotifications())
                {
                    Assert.IsTrue(fastCollection.IsDirty);
                }

                Assert.IsFalse(fastCollection.IsDirty);
            }
예제 #16
0
            public void ThrowsInvalidOperationExceptionForChangingMode()
            {
                var fastCollection = new FastBindingList <int> {
                    0
                };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                    Assert.Throws <InvalidOperationException>(() => { using (fastCollection.SuspendChangeNotifications(SuspensionMode.Removing)) { } });
                }
            }
예제 #17
0
            public void CleanedUpSuspensionContextAfterDoingNothing()
            {
                var fastCollection = new FastBindingList <int>();

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                }

                var context = GetSuspensionContext(fastCollection);

                Assert.IsNull(context);
            }
예제 #18
0
            public void ReturnsFalseAfterChangedDisposing()
            {
                var fastCollection = new FastBindingList <int>();

                var firstToken  = fastCollection.SuspendChangeNotifications();
                var secondToken = fastCollection.SuspendChangeNotifications();

                firstToken.Dispose();
                secondToken.Dispose();

                Assert.IsFalse(fastCollection.NotificationsSuspended);
            }
예제 #19
0
            public void ReturnsFalseAfterDisposing()
            {
                var fastCollection = new FastBindingList <int>();

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);

                    Assert.IsTrue(fastCollection.NotificationsSuspended);
                }

                Assert.IsFalse(fastCollection.NotificationsSuspended);
            }
예제 #20
0
            public void ReturnsSingleElementUsingLinq()
            {
                var fastCollection = new FastBindingList <int>();

                for (int i = 0; i < 43; i++)
                {
                    fastCollection.Add(i);
                }

                var allInts = (from x in fastCollection
                               where x == 42
                               select x).FirstOrDefault();

                Assert.AreEqual(42, allInts);
            }
예제 #21
0
            public void ResetWithoutSuspendChangeNotifications()
            {
                var collectionChanged = false;
                var fastCollection    = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) =>
                {
                    collectionChanged = true;
                };

                fastCollection.Reset();

                Assert.AreEqual(true, collectionChanged);
            }
예제 #22
0
            public void RaisesSingleEventWhileAddingRange()
            {
                int counter = 0;

                var fastCollection = new FastBindingList <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.ListChanged += (sender, e) => counter++;

                fastCollection.AddItems(new[] { 1, 2, 3, 4, 5 });

                Assert.AreEqual(1, counter);

                fastCollection.AddItems(new ArrayList(new[] { 1, 2, 3, 4, 5 }));

                Assert.AreEqual(2, counter);
            }
예제 #23
0
            public void ReturnsFalseWhenChangesAreNotSuspended()
            {
                var fastCollection = new FastBindingList <int>();

                Assert.IsFalse(fastCollection.IsDirty);
            }