1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 // Brian O'Keefe (zer0keefie@gmail.com)
25 using System.Collections.Generic;
26 using System.Collections.ObjectModel;
27 using System.Collections.Specialized;
28 using System.ComponentModel;
29 using MonoTests.System.Collections.Specialized;
30 using NUnit.Framework;
32 namespace MonoTests.System.Collections.ObjectModel {
35 public class ReadOnlyObservableCollectionTest {
37 public void ClassTest()
39 // Because the collection cannot change, check to make sure exceptions are thrown for modifications.
41 List<char> initial = new List<char> ();
46 ObservableCollection<char> collection = new ObservableCollection<char> (initial);
47 ReadOnlyObservableCollection<char> readOnlyCollection = new ReadOnlyObservableCollection<char> (collection);
51 PropertyChangedEventHandler pceh = delegate (object sender, PropertyChangedEventArgs e) {
52 Assert.Fail ("No properties should change.");
55 NotifyCollectionChangedEventHandler ncceh = delegate (object sender, NotifyCollectionChangedEventArgs e) {
56 Assert.Fail ("The collection should not change.");
59 ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged += pceh;
61 ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged += ncceh;
63 // Done with the events.
64 ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged -= pceh;
66 ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged -= ncceh;
68 Assert.AreEqual (3, readOnlyCollection.Count, "RO_1");
69 CollectionChangedEventValidators.AssertEquivalentLists (initial, readOnlyCollection, "RO_2");
71 // Modifying the underlying collection
73 bool propChanged = false;
75 pceh = delegate (object sender, PropertyChangedEventArgs e) {
79 ncceh = delegate (object sender, NotifyCollectionChangedEventArgs e) {
80 CollectionChangedEventValidators.ValidateAddOperation (e, new char [] { 'I' }, 3, "RO_3");
83 ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged += pceh;
85 ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged += ncceh;
87 // In theory, this will cause the properties to change.
90 Assert.IsTrue (propChanged, "RO_4");