Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / WindowsBase / Test / System.ComponentModel / SortDescriptionCollectionTest.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 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Brian O'Keefe (zer0keefie@gmail.com)
24 //
25
26 using System;
27 using System.Collections.Generic;
28 using System.Linq;
29 using System.Text;
30 using NUnit.Framework;
31 using System.ComponentModel;
32 using System.Collections.Specialized;
33
34 namespace MonoTests.System.ComponentModel {
35         [TestFixture]
36         public class SortDescriptionCollectionTest 
37         {
38                 public SortDescriptionCollectionTest()
39                 {
40                 }
41
42                 [Test]
43                 public void SortDescriptionCollectionAddTest()
44                 {
45                         SortDescriptionCollection sdc = new SortDescriptionCollection ();
46                         SortDescription addedItem = new SortDescription ("NONE", ListSortDirection.Ascending);
47
48                         ((INotifyCollectionChanged)sdc).CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
49                                 Assert.AreEqual (NotifyCollectionChangedAction.Add, e.Action, "ADD_#0");
50                                 addedItem = (SortDescription)e.NewItems [0];
51                                 Assert.AreEqual (true, addedItem.IsSealed, "ADD_#0b");
52                         };
53
54                         sdc.Add (new SortDescription ("A", ListSortDirection.Ascending));
55
56                         Assert.AreEqual ("A", addedItem.PropertyName, "ADD_#1");
57                         Assert.AreEqual (ListSortDirection.Ascending, addedItem.Direction, "ADD_#2");
58                         Assert.AreEqual (true, addedItem.IsSealed, "ADD_#3");
59                 }
60
61                 [Test]
62                 public void SortDescriptionCollectionAddNoHandlerTest()
63                 {
64                         SortDescriptionCollection sdc = new SortDescriptionCollection ();
65                         SortDescription addedItem = new SortDescription ("A", ListSortDirection.Ascending);
66
67                         sdc.Add (addedItem);
68
69                         addedItem = sdc[0];
70
71                         Assert.AreEqual ("A", addedItem.PropertyName, "ADDN_#1");
72                         Assert.AreEqual (ListSortDirection.Ascending, addedItem.Direction, "ADDN_#2");
73                         Assert.AreEqual (true, addedItem.IsSealed, "ADDN_#3");
74                 }
75
76                 [Test]
77                 public void SortDescriptionCollectionRemoveTest()
78                 {
79                         SortDescriptionCollection sdc = new SortDescriptionCollection ();
80                         SortDescription removedItem = new SortDescription ("NONE", ListSortDirection.Ascending);
81
82                         sdc.Add (new SortDescription ("A", ListSortDirection.Ascending));
83
84                         ((INotifyCollectionChanged)sdc).CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
85                                 Assert.AreEqual (NotifyCollectionChangedAction.Remove, e.Action, "REM_#0");
86                                 removedItem = (SortDescription)e.OldItems [0];
87                                 Assert.AreEqual (true, removedItem.IsSealed, "REM_#0b");
88                         };
89
90                         sdc.RemoveAt (0);
91
92                         Assert.AreEqual ("A", removedItem.PropertyName, "REM_#1");
93                         Assert.AreEqual (ListSortDirection.Ascending, removedItem.Direction, "REM_#2");
94                         Assert.AreEqual (true, removedItem.IsSealed, "REM_#3");
95                 }
96
97                 [Test]
98                 public void SortDescriptionCollectionClearTest()
99                 {
100                         SortDescriptionCollection sdc = new SortDescriptionCollection ();
101                         bool eventFired = false;
102
103                         sdc.Add (new SortDescription ("A", ListSortDirection.Ascending));
104
105                         ((INotifyCollectionChanged)sdc).CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
106                                 Assert.AreEqual (NotifyCollectionChangedAction.Reset, e.Action, "CLR_#0");
107                                 eventFired = true;
108                         };
109
110                         sdc.Clear ();
111
112                         Assert.IsTrue (eventFired, "CLR_#1");
113                 }
114
115                 [Test]
116                 public void SortDescriptionCollectionSetTest()
117                 {
118                         SortDescriptionCollection sdc = new SortDescriptionCollection ();
119                         int addEvent = 0, removeEvent = 0;
120
121                         SortDescription addedItem = new SortDescription ("NONE", ListSortDirection.Ascending);
122                         SortDescription removedItem = new SortDescription ("NONE", ListSortDirection.Ascending);
123
124                         sdc.Add (new SortDescription ("A", ListSortDirection.Ascending));
125
126                         ((INotifyCollectionChanged)sdc).CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
127                                 switch (e.Action) {
128                                         case NotifyCollectionChangedAction.Add:
129                                                 addEvent++;
130                                                 addedItem = (SortDescription)e.NewItems [0];
131                                                 break;
132                                         case NotifyCollectionChangedAction.Remove:
133                                                 removeEvent++;
134                                                 removedItem = (SortDescription)e.OldItems [0];
135                                                 break;
136                                         default:
137                                                 Assert.Fail ("SET_#0");
138                                                 break;
139                                 }
140                         };
141
142                         sdc [0] = new SortDescription ("B", ListSortDirection.Descending);
143
144                         Assert.AreEqual (1, addEvent, "SET_#1");
145                         Assert.AreEqual (1, removeEvent, "SET_#2");
146
147                         Assert.AreEqual ("A", removedItem.PropertyName, "REM_#1");
148                         Assert.AreEqual (ListSortDirection.Ascending, removedItem.Direction, "REM_#2");
149                         Assert.AreEqual (true, removedItem.IsSealed, "REM_#3");
150
151                         Assert.AreEqual ("B", addedItem.PropertyName, "ADD_#1");
152                         Assert.AreEqual (ListSortDirection.Descending, addedItem.Direction, "ADD_#2");
153                         Assert.AreEqual (true, addedItem.IsSealed, "ADD_#3");
154                 }
155
156                 [Test]
157                 public void GetEmptyCollection ()
158                 {
159                         var collection = SortDescriptionCollection.Empty;
160                         CollectionAssert.IsEmpty (collection, "A1");
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof(NotSupportedException))]
165                 public void AddToEmptyCollection ()
166                 {
167                         var collection = SortDescriptionCollection.Empty;
168                         collection.Add (new SortDescription ());
169                 }
170
171                 [Test]
172                 public void RemoveFromEmptyCollection ()
173                 {
174                         var collection = SortDescriptionCollection.Empty;
175                         Assert.IsFalse (collection.Remove (new SortDescription ()), "A1");
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof(NotSupportedException))]
180                 public void RemoveAtIndexFromEmptyCollection ()
181                 {
182                         var collection = SortDescriptionCollection.Empty;
183                         collection.RemoveAt (0);
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof(NotSupportedException))]
188                 public void ClearEmptyCollection ()
189                 {
190                         var collection = SortDescriptionCollection.Empty;
191                         collection.Clear ();
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof(NotSupportedException))]
196                 public void InsertIntoEmptyCollection ()
197                 {
198                         var collection = SortDescriptionCollection.Empty;
199                         collection.Insert (0, new SortDescription ());
200                 }
201         }
202 }