[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / System / Test / System.Collections.ObjectModel / ReadOnlyObservableCollectionTest.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Authors:
21 //      Brian O'Keefe (zer0keefie@gmail.com)
22 //
23
24
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;
31
32 namespace MonoTests.System.Collections.ObjectModel {
33
34         [TestFixture]
35         public class ReadOnlyObservableCollectionTest {
36                 [Test]
37                 public void ClassTest()
38                 {
39                         // Because the collection cannot change, check to make sure exceptions are thrown for modifications.
40
41                         List<char> initial = new List<char> ();
42                         initial.Add ('A');
43                         initial.Add ('B');
44                         initial.Add ('C');
45
46                         ObservableCollection<char> collection = new ObservableCollection<char> (initial);
47                         ReadOnlyObservableCollection<char> readOnlyCollection = new ReadOnlyObservableCollection<char> (collection);
48
49                         // Test the events
50
51                         PropertyChangedEventHandler pceh = delegate (object sender, PropertyChangedEventArgs e) {
52                                 Assert.Fail ("No properties should change.");
53                         };
54
55                         NotifyCollectionChangedEventHandler ncceh = delegate (object sender, NotifyCollectionChangedEventArgs e) {
56                                 Assert.Fail ("The collection should not change.");
57                         };
58
59                         ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged += pceh;
60
61                         ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged += ncceh;
62
63                         // Done with the events.
64                         ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged -= pceh;
65
66                         ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged -= ncceh;
67
68                         Assert.AreEqual (3, readOnlyCollection.Count, "RO_1");
69                         CollectionChangedEventValidators.AssertEquivalentLists (initial, readOnlyCollection, "RO_2");
70
71                         // Modifying the underlying collection
72
73                         bool propChanged = false;
74
75                         pceh = delegate (object sender, PropertyChangedEventArgs e) {
76                                 propChanged = true;
77                         };
78
79                         ncceh = delegate (object sender, NotifyCollectionChangedEventArgs e) {
80                                 CollectionChangedEventValidators.ValidateAddOperation (e, new char [] { 'I' }, 3, "RO_3");
81                         };
82
83                         ((INotifyPropertyChanged)readOnlyCollection).PropertyChanged += pceh;
84
85                         ((INotifyCollectionChanged)readOnlyCollection).CollectionChanged += ncceh;
86
87                         // In theory, this will cause the properties to change.
88                         collection.Add ('I');
89
90                         Assert.IsTrue (propChanged, "RO_4");
91                 }
92         }
93 }
94