public void Set() { var collection = new ObservableCollection <char>(); var propertyChanged = false; var changedProps = new List <string>(); NotifyCollectionChangedEventArgs args = null; collection.Add('A'); collection.Add('B'); collection.Add('C'); ((INotifyPropertyChanged)collection).PropertyChanged += (sender, e) => { No.Op(sender); propertyChanged = true; changedProps.Add(e.PropertyName); }; collection.CollectionChanged += (sender, e) => { No.Op(sender); args = e; }; collection[2] = 'I'; Assert.IsTrue(propertyChanged, "SET_1"); Assert.IsTrue(changedProps.Contains("Item[]"), "SET_2"); CollectionChangedEventValidators.ValidateReplaceOperation(args, new[] { 'C' }, new[] { 'I' }, 2, "SET_3"); }
public void Set() { ObservableCollection <char> collection = new ObservableCollection <char> (); bool propertyChanged = false; List <string> changedProps = new List <string> (); NotifyCollectionChangedEventArgs args = null; collection.Add('A'); collection.Add('B'); collection.Add('C'); ((INotifyPropertyChanged)collection).PropertyChanged += delegate(object sender, PropertyChangedEventArgs e) { propertyChanged = true; changedProps.Add(e.PropertyName); }; collection.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) { args = e; }; collection [2] = 'I'; Assert.IsTrue(propertyChanged, "SET_1"); Assert.IsTrue(changedProps.Contains("Item[]"), "SET_2"); CollectionChangedEventValidators.ValidateReplaceOperation(args, new char [] { 'C' }, new char [] { 'I' }, 2, "SET_3"); }
public void NotifyCollectionChangedEventArgsConstructor11Test() { var newItem = new object(); var oldItem = new object(); const int startIndex = 5; // Trying with Replace var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem, startIndex); CollectionChangedEventValidators.ValidateReplaceOperation(args, new[] { oldItem }, new[] { newItem }, startIndex, "#K01"); // Trying with Reset try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, newItem, oldItem, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Reset."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Move try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, newItem, oldItem, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Move."); } catch (ArgumentException ex) { GC.KeepAlive(ex); } // Trying with Add try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItem, oldItem, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Add."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Remove try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, newItem, oldItem)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Remove."); } catch (ArgumentException ex) { No.Op(ex); } }
public void ObservableCollection_Reentrant() { var collection = new ObservableCollection <char>(); var propertyChanged = false; var changedProps = new List <string>(); NotifyCollectionChangedEventArgs args = null; collection.Add('A'); collection.Add('B'); collection.Add('C'); PropertyChangedEventHandler pceh = (object sender, PropertyChangedEventArgs e) => { GC.KeepAlive(sender); propertyChanged = true; changedProps.Add(e.PropertyName); }; // Adding a PropertyChanged event handler ((INotifyPropertyChanged)collection).PropertyChanged += pceh; collection.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs e) => { GC.KeepAlive(sender); args = e; }; collection.CollectionChanged += (object sender, NotifyCollectionChangedEventArgs e) => { GC.KeepAlive(sender); GC.KeepAlive(e); // This one will attempt to break reentrancy try { collection.Add('X'); Assert.Fail("Reentrancy should not be allowed."); } catch (InvalidOperationException ex) { GC.KeepAlive(ex); } }; collection[2] = 'I'; Assert.IsTrue(propertyChanged, "REENT_1"); Assert.IsTrue(changedProps.Contains("Item[]"), "REENT_2"); CollectionChangedEventValidators.ValidateReplaceOperation(args, new char[] { 'C' }, new char[] { 'I' }, 2, "REENT_3"); // Removing the PropertyChanged event handler should work as well: ((INotifyPropertyChanged)collection).PropertyChanged -= pceh; }
public void NotifyCollectionChangedEventArgsConstructor8Test() { IList newItems = new List <object>(); IList oldItems = new List <object>(); const int startIndex = 5; // Trying with Replace var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems, startIndex); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, startIndex, "#H01"); // Add some items to test this one. newItems.Add(new object()); newItems.Add(new object()); newItems.Add(new object()); // Trying with Replace again args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems, startIndex); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, startIndex, "#H02"); // Add some more items to test this one. oldItems.Add(new object()); oldItems.Add(new object()); oldItems.Add(new object()); // Trying with Replace again args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems, startIndex); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, startIndex, "#H03"); // Trying with null arguments. try { // ReSharper disable once AssignNullToNotNullAttribute GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, null, oldItems, startIndex)); Assert.Fail("The newItems argument cannot be null."); } catch (ArgumentNullException ex) { No.Op(ex); } try { // ReSharper disable once AssignNullToNotNullAttribute GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, null, startIndex)); Assert.Fail("The oldItems argument cannot be null."); } catch (ArgumentNullException ex) { No.Op(ex); } // Trying with Reset try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, newItems, oldItems, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Reset."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Move try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, newItems, oldItems, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Move."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Add try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItems, oldItems, startIndex)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Add."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Remove try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, newItems, oldItems)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Remove."); } catch (ArgumentException ex) { No.Op(ex); } }
public void NotifyCollectionChangedEventArgsConstructor7Test() { var oldItem = new object(); var newItem = new object(); // Doesn't matter what the value of this is. // Trying with Add var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem); CollectionChangedEventValidators.ValidateReplaceOperation(args, new[] { oldItem }, new[] { newItem }, "#G01"); // Trying null items args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, null, oldItem); CollectionChangedEventValidators.ValidateReplaceOperation(args, new[] { oldItem }, new object[] { null }, "#G02"); args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, null); CollectionChangedEventValidators.ValidateReplaceOperation(args, new object[] { null }, new[] { newItem }, "#G03"); // Trying with Reset try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, newItem, oldItem)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Reset."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Move try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, newItem, oldItem)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Move."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Add try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItem, oldItem)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Add."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Remove try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, newItem, oldItem)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Remove."); } catch (ArgumentException ex) { No.Op(ex); } }
public void NotifyCollectionChangedEventArgsConstructor4Test() { /* Expected Behavior: * * If action is Replace: * If newItems is null, throw an ArgumentNullException. * If oldItems is null, throw an ArgumentNullException * Otherwise, success. * If action is not Replace, throw an ArgumentException */ IList newItems = new List <object>(); IList oldItems = new List <object>(); // Trying with Replace var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, "#D01"); // Add some items to test this one. newItems.Add(new object()); newItems.Add(new object()); newItems.Add(new object()); // Trying with Replace again args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, "#D02"); // Add some more items to test this one. oldItems.Add(new object()); oldItems.Add(new object()); oldItems.Add(new object()); // Trying with Replace again args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, oldItems); CollectionChangedEventValidators.ValidateReplaceOperation(args, oldItems, newItems, "#D03"); // Trying with null arguments. try { // ReSharper disable once AssignNullToNotNullAttribute GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, null, oldItems)); Assert.Fail("The newItems argument cannot be null."); } catch (ArgumentNullException ex) { No.Op(ex); } try { // ReSharper disable once AssignNullToNotNullAttribute GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, null)); Assert.Fail("The oldItems argument cannot be null."); } catch (ArgumentNullException ex) { No.Op(ex); } // Trying with Reset try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, newItems, oldItems)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Reset."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Move try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, newItems, oldItems)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Move."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Add try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItems, oldItems)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Add."); } catch (ArgumentException ex) { No.Op(ex); } // Trying with Remove try { GC.KeepAlive(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, newItems, oldItems)); Assert.Fail("Should not be able to call .ctor with NotifyCollectionChangedAction.Remove."); } catch (ArgumentException ex) { No.Op(ex); } }