2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingContextTest.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) 2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper  jackson@ximian.com
24
25
26 using System;
27 using System.Data;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Windows.Forms;
31
32 using NUnit.Framework;
33
34 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
35
36 namespace MonoTests.System.Windows.Forms {
37
38         [TestFixture]
39         public class BindingContextTest {
40
41                 private class BindingContextPoker : BindingContext {
42
43                         public int collection_changed;
44
45                         public void _Add (object data_source, BindingManagerBase list_manager)
46                         {
47                                 Add (data_source, list_manager);
48                         }
49
50                         public void _AddCore (object data_source, BindingManagerBase list_manager)
51                         {
52                                 AddCore (data_source, list_manager);
53                         }
54
55                         public void _Clear ()
56                         {
57                                 Clear ();
58                         }
59
60                         public void _ClearCore ()
61                         {
62                                 ClearCore ();
63                         }
64
65                         public void _Remove (object data_source)
66                         {
67                                 Remove (data_source);
68                         }
69
70                         public void _RemoveCore (object data_source)
71                         {
72                                 RemoveCore (data_source);
73                         }
74
75                         protected override void OnCollectionChanged (CollectionChangeEventArgs ce)
76                         {
77                                 collection_changed = (int) ce.Action;
78                                 base.OnCollectionChanged (ce);
79                         }
80                 }
81
82                 [SetUp]
83                 protected virtual void SetUp ()
84                 {
85                 }
86
87                 [TearDown]
88                 protected virtual void TearDown ()
89                 {
90                 }
91
92                 [Test]
93                 public void CtorTest ()
94                 {
95                         BindingContext bc = new BindingContext ();
96
97                         Assert.IsFalse (bc.IsReadOnly, "CT1");
98                         Assert.IsFalse (bc.Contains (this), "CT2");
99                         Assert.IsFalse (bc.Contains (this, String.Empty), "CT3");
100                         Assert.IsFalse (bc.Contains (this, "Me is String"), "CT4");
101
102                         // Test the ICollection interface
103                         ICollection ic = (ICollection) bc;
104                         Assert.AreEqual (ic.Count, 0, "CT5");
105                         Assert.IsFalse (ic.IsSynchronized, "CT6");
106                         Assert.IsNull (ic.SyncRoot, "CT7");
107                         object [] arr = new object [] { "A", "B", "C" };
108                         ic.CopyTo (arr, 0);
109                         Assert.AreEqual (0, ic.Count, "CT8");
110                         Assert.IsFalse (ic.GetEnumerator ().MoveNext (), "CT9");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentNullException))]
115                 public void TestIndexerNull ()
116                 {
117                         BindingContext bc = new BindingContext ();
118                         BindingManagerBase a, b;
119
120                         a = bc [null];
121                 }
122
123                 [Test]
124                 public void TestIndexerNoMember ()
125                 {
126                         BindingContext bc = new BindingContext ();
127                         ArrayList data_source = new ArrayList ();
128                         BindingManagerBase a, b;
129
130                         data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
131
132                         a = bc [data_source];
133                         b = bc [data_source];
134                         Assert.AreSame (a, b, "INNM1");
135
136                         b = bc [data_source, String.Empty];
137                         Assert.AreSame (a, b, "INNM2");
138
139                         // Only one is added to the list
140                         Assert.AreEqual (((ICollection) bc).Count, 1);
141                 }
142
143                 [Test]
144                 public void TestIndexerWithMember ()
145                 {
146                         BindingContext bc = new BindingContext ();
147                         ArrayList data_source = new ArrayList ();
148                         BindingManagerBase a, b, c;
149                         
150                         data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
151
152                         a = bc [data_source, "Length"];
153                         b = bc [data_source, "Length"];
154
155                         Assert.AreSame (a, b, "INWM1");
156
157                         b = bc [data_source];
158                         Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
159
160                         c = bc [data_source];
161                         Assert.AreSame (b, c, "INWM3");
162                         
163                         b = bc [data_source, "Length"];
164                         Assert.AreSame (a, b, "INWM4");
165                 }
166
167                 [Test]
168                 [ExpectedException (typeof (ArgumentException))]
169                 public void CantCreateChildList ()
170                 {
171                         BindingContext bc = new BindingContext ();
172                         ArrayList data_source = new ArrayList ();
173
174                         BindingManagerBase a = bc [data_source, "Items"];
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (ArgumentException))]
179                 public void CantCreateChildList2 ()
180                 {
181                         BindingContext bc = new BindingContext ();
182                         ArrayList data_source = new ArrayList ();
183
184                         BindingManagerBase a = bc [data_source, "Count"];
185                 }
186
187                 [Test]
188                 public void CreateCurrencyManager ()
189                 {
190                         BindingContext bc = new BindingContext ();
191                         ArrayList data_source = new ArrayList ();
192                         CurrencyManager a = bc [data_source] as CurrencyManager;
193
194                         Assert.IsNotNull (a, "CCM1");
195                 }
196
197                 [Test]
198                 public void CreatePropertyManager ()
199                 {
200                         BindingContext bc = new BindingContext ();
201                         object data_source = new object ();
202                         PropertyManager a = bc [data_source] as PropertyManager;
203
204                         Assert.IsNotNull (a, "CPM1");
205                 }
206
207                 private DataSet CreateRelatedDataSet ()
208                 {
209                         DataSet dataset = new DataSet ("DataSet");
210                         DataTable dt1 = new DataTable ("Table1");
211                         DataTable dt2 = new DataTable ("Table2");
212                         DataColumn column;
213
214                         column = new DataColumn ("One");
215                         column.DataType = typeof (int);
216                         column.Unique = true;
217                         dt1.Columns.Add (column);
218
219                         for (int i = 0; i < 10; i++) {
220                                 DataRow row = dt1.NewRow ();
221                                 row ["One"] = i;
222                                 dt1.Rows.Add (row);
223                         }
224                         
225                         column = new DataColumn ("Two");
226                         column.DataType = typeof (int);
227                         column.Unique = true;
228                         dt2.Columns.Add (column);
229
230                         for (int i = 0; i < 10; i++) {
231                                 DataRow row = dt2.NewRow ();
232                                 row ["Two"] = i;
233                                 dt2.Rows.Add (row);
234                         }
235
236                         dataset.Tables.Add (dt1);
237                         dataset.Tables.Add (dt2);
238                         dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
239
240                         return dataset;
241                 }
242
243                 [Test]
244                 public void CreateComplexManager ()
245                 {
246                         BindingContext bc = new BindingContext ();
247                         DataSet dataset = CreateRelatedDataSet ();
248                         CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
249
250                         Assert.IsNotNull (cm, "CCCM1");
251                 }
252
253                 [Test]
254                 [ExpectedException (typeof (ArgumentException))]
255                 public void FailToCreateComplexManagerRelationDoesNotExist ()
256                 {
257                         BindingContext bc = new BindingContext ();
258                         DataSet dataset = CreateRelatedDataSet ();
259                         CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
260                 }
261
262                 [Test]
263                 [ExpectedException (typeof (ArgumentException))]
264                 public void FailToCreateComplexManagerNoTableSpecified ()
265                 {
266                         BindingContext bc = new BindingContext ();
267                         DataSet dataset = CreateRelatedDataSet ();
268                         CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
269                 }
270
271                 [Test]
272                 [ExpectedException (typeof (ArgumentException))]
273                 public void FailToCreateComplexChildTableSpecified ()
274                 {
275                         BindingContext bc = new BindingContext ();
276                         DataSet dataset = CreateRelatedDataSet ();
277                         CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (NotImplementedException))]
282                 public void CantSubscribeToCollectionChanged ()
283                 {
284                         BindingContext bc = new BindingContext ();
285
286                         bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
287                 }
288
289                 [Test]
290                 public void CantSubscribeToCollectionChanged2 ()
291                 {
292                         BindingContext bc = new BindingContext ();
293
294                         bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
295                 }
296
297                 private void Dummy (object sender, CollectionChangeEventArgs e)
298                 {
299
300                 }
301
302                 [Test]
303                 [ExpectedException (typeof (ArgumentNullException))]
304                 public void AddNullDataSource ()
305                 {
306                         BindingContextPoker p = new BindingContextPoker ();
307
308                         p._Add (null, new PropertyManager ());
309                 }
310
311                 [Test]
312                 [ExpectedException (typeof (ArgumentNullException))]
313                 public void AddNullListManager ()
314                 {
315                         BindingContextPoker p = new BindingContextPoker ();
316
317                         p._Add (new object (), null);
318                 }
319
320                 [Test]
321                 public void Add ()
322                 {
323                         BindingContextPoker p = new BindingContextPoker ();
324                         object data_source = new object ();
325
326                         p.collection_changed = -1;
327                         Assert.IsFalse (p.Contains (data_source), "ADD1");
328                         Assert.AreEqual (0, ((ICollection) p).Count, "ADD2");
329                         p._Add (data_source, new PropertyManager ());
330                         Assert.IsTrue (p.Contains (data_source), "ADD3");
331                         Assert.AreEqual (1, ((ICollection) p).Count, "ADD4");
332                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD5");
333
334                         p.collection_changed = -1;
335                         p._Add (data_source, new PropertyManager ());
336                         Assert.IsTrue (p.Contains (data_source), "ADD6");
337                         Assert.AreEqual (1, ((ICollection) p).Count, "ADD7");
338                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD8");
339
340                         p.collection_changed = -1;
341                         data_source = new object ();
342                         p._Add (data_source, new PropertyManager ());
343                         Assert.IsTrue (p.Contains (data_source), "ADD9");
344                         Assert.AreEqual (2, ((ICollection) p).Count, "ADD10");
345                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD9");
346                 }
347
348                 [Test]
349                 public void AddCore ()
350                 {
351                         BindingContextPoker p = new BindingContextPoker ();
352                         object data_source = new object ();
353
354                         p.collection_changed = -1;
355                         Assert.IsFalse (p.Contains (data_source), "ADDCORE1");
356                         Assert.AreEqual (0, ((ICollection) p).Count, "ADDCORE2");
357                         p._AddCore (data_source, new PropertyManager ());
358                         Assert.IsTrue (p.Contains (data_source), "ADDCORE3");
359                         Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE4");
360                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE5");
361
362                         p.collection_changed = -1;
363                         p._AddCore (data_source, new PropertyManager ());
364                         Assert.IsTrue (p.Contains (data_source), "ADDCORE6");
365                         Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE7");
366                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE8");
367
368                         p.collection_changed = -1;
369                         data_source = new object ();
370                         p._AddCore (data_source, new PropertyManager ());
371                         Assert.IsTrue (p.Contains (data_source), "ADDCORE9");
372                         Assert.AreEqual (2, ((ICollection) p).Count, "ADDCORE10");
373                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE11");
374                 }
375
376                 [Test]
377                 [ExpectedException (typeof (ArgumentNullException))]
378                 public void RemoveNull ()
379                 {
380                         BindingContextPoker p = new BindingContextPoker ();
381                         p._Remove (null);
382                 }
383
384                 [Test]
385                 public void Remove ()
386                 {
387                         BindingContextPoker p = new BindingContextPoker ();
388                         object data_source = new object ();
389
390                         p.collection_changed = -1;
391                         p._Add (data_source, new PropertyManager ());
392                         Assert.IsTrue (p.Contains (data_source), "REMOVE1");
393                         Assert.AreEqual (1, ((ICollection) p).Count, "REMOVE2");
394                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVE3");
395                         p._Remove (data_source);
396                         Assert.IsFalse (p.Contains (data_source), "REMOVE4");
397                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE5");
398                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE6");
399
400                         // Double remove
401                         p.collection_changed = -1;
402                         p._Remove (data_source);
403                         Assert.IsFalse (p.Contains (data_source), "REMOVE7");
404                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE8");
405                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE9");
406                 }
407
408                 [Test]
409                 public void RemoveCore ()
410                 {
411                         BindingContextPoker p = new BindingContextPoker ();
412                         object data_source = new object ();
413
414                         p.collection_changed = -1;
415                         p._Add (data_source, new PropertyManager ());
416                         Assert.IsTrue (p.Contains (data_source), "REMOVECORE1");
417                         Assert.AreEqual (1, ((ICollection) p).Count, "REMOVECORE2");
418                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVECORE3");
419
420                         p.collection_changed = -1;
421                         p._RemoveCore (data_source);
422                         Assert.IsFalse (p.Contains (data_source), "REMOVECORE4");
423                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE5");
424                         Assert.AreEqual (p.collection_changed, -1, "REMOVECORE6");
425
426                         // Double remove
427                         p.collection_changed = -1;
428                         p._Remove (data_source);
429                         Assert.IsFalse (p.Contains (data_source), "REMOVECORE7");
430                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE8");
431                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVECORE9");
432                 }
433
434                 [Test]
435                 public void Clear ()
436                 {
437                         BindingContextPoker p = new BindingContextPoker ();
438                         object data_source = new object ();
439
440                         p._Add (data_source, new PropertyManager ());
441                         p.collection_changed = -1;
442                         p._Clear ();
443                         Assert.IsFalse (p.Contains (data_source), "CLEAR1");
444                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
445                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
446
447                         // Double clear
448                         p.collection_changed = -1;
449                         p._Clear ();
450                         Assert.IsFalse (p.Contains (data_source), "CLEAR1");
451                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
452                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
453                 }
454
455                 [Test]
456                 public void ClearCore ()
457                 {
458                         BindingContextPoker p = new BindingContextPoker ();
459                         object data_source = new object ();
460
461                         p._Add (data_source, new PropertyManager ());
462                         p.collection_changed = -1;
463                         p._Clear ();
464                         Assert.IsFalse (p.Contains (data_source), "CLEARCORE1");
465                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE2");
466                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE3");
467
468                         // Double clear
469                         p.collection_changed = -1;
470                         p._Clear ();
471                         Assert.IsFalse (p.Contains (data_source), "CLEARCORE4");
472                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE5");
473                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE6");
474                 }
475
476                 [Test]
477                 public void TestContains ()
478                 {
479                         BindingContextPoker p = new BindingContextPoker ();
480                         object data_source = new object ();
481                         p._Add (data_source, new PropertyManager ());
482                         Assert.IsTrue (p.Contains (data_source), "#1");
483                         Assert.IsFalse (p.Contains ("nonexistent"), "#2");
484                 }
485
486                 [Test]
487                 [ExpectedException (typeof (ArgumentNullException))]
488                 public void TestContainsNull ()
489                 {
490                         BindingContextPoker p = new BindingContextPoker ();
491                         p.Contains (null);
492                 }
493
494                 [Test]
495                 public void TestContainsNull2 ()
496                 {
497                         BindingContextPoker p = new BindingContextPoker ();
498                         object data_source = new object ();
499                         p._Add (data_source, new PropertyManager ());
500                         Assert.IsTrue (p.Contains (data_source, null), "#1");
501                         Assert.IsFalse (p.Contains ("nonexistent", null), "#2");
502                 }
503
504
505                 [Test]
506                 public void TestGetEnumerator ()
507                 {
508                         BindingContextPoker p = new BindingContextPoker ();
509                         object data_source = new object ();
510                         PropertyManager pm = new PropertyManager ();
511                         p._Add (data_source, pm);
512                         IEnumerator e = ((IEnumerable) p).GetEnumerator ();
513                         Assert.IsNotNull (e, "#1");
514                         IDictionaryEnumerator de = e as IDictionaryEnumerator;
515                         Assert.IsNotNull (de, "#2");
516                         Assert.IsTrue (de.MoveNext (), "#3");
517                         // In .NET Key is its internal type.
518                         //Assert.AreEqual (data_source, de.Key, "#4");
519                         //Assert.AreEqual (pm, de.Value, "#5");
520                         Assert.IsFalse (de.MoveNext (), "#6");
521                 }
522         }
523 }
524