Unit test for bug #821.
[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.DataBinding {
37
38         [TestFixture]
39         public class BindingContextTest : TestHelper {
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                 [Test]
83                 public void CtorTest ()
84                 {
85                         BindingContext bc = new BindingContext ();
86
87                         Assert.IsFalse (bc.IsReadOnly, "CT1");
88                         Assert.IsFalse (bc.Contains (this), "CT2");
89                         Assert.IsFalse (bc.Contains (this, String.Empty), "CT3");
90                         Assert.IsFalse (bc.Contains (this, "Me is String"), "CT4");
91
92                         // Test the ICollection interface
93                         ICollection ic = (ICollection) bc;
94                         Assert.AreEqual (ic.Count, 0, "CT5");
95                         Assert.IsFalse (ic.IsSynchronized, "CT6");
96                         Assert.IsNull (ic.SyncRoot, "CT7");
97                         object [] arr = new object [] { "A", "B", "C" };
98                         ic.CopyTo (arr, 0);
99                         Assert.AreEqual (0, ic.Count, "CT8");
100                         Assert.IsFalse (ic.GetEnumerator ().MoveNext (), "CT9");
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentNullException))]
105                 public void TestIndexerNull ()
106                 {
107                         BindingContext bc = new BindingContext ();
108                         BindingManagerBase a;
109
110                         a = bc [null];
111                         
112                         TestHelper.RemoveWarning (a, bc);
113                 }
114
115                 [Test]
116                 public void TestIndexerNoMember ()
117                 {
118                         BindingContext bc = new BindingContext ();
119                         ArrayList data_source = new ArrayList ();
120                         BindingManagerBase a, b;
121
122                         data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
123
124                         a = bc [data_source];
125                         b = bc [data_source];
126                         Assert.AreSame (a, b, "INNM1");
127
128                         b = bc [data_source, String.Empty];
129                         Assert.AreSame (a, b, "INNM2");
130
131                         // Only one is added to the list
132                         Assert.AreEqual (((ICollection) bc).Count, 1);
133                 }
134
135                 [Test]
136                 public void TestIndexerWithMember ()
137                 {
138                         BindingContext bc = new BindingContext ();
139                         ArrayList data_source = new ArrayList ();
140                         BindingManagerBase a, b, c, d;
141                         
142                         data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
143
144                         a = bc [data_source, "Length"];
145                         b = bc [data_source, "Length"];
146
147                         Assert.AreSame (a, b, "INWM1");
148
149                         b = bc [data_source];
150                         Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
151
152                         c = bc [data_source];
153                         Assert.AreSame (b, c, "INWM3");
154                         
155                         b = bc [data_source, "Length"];
156                         Assert.AreSame (a, b, "INWM4");
157
158                         // Non List type
159                         MockItem item = new MockItem ("Mono", -1);
160                         MockContainer container = new MockContainer ();
161                         container.Item = item;
162
163                         d = bc [container, "Item.Text"];
164                         Assert.AreEqual ("Mono", d.Current, "INWM5");
165                         Assert.AreEqual (1, d.Count, "INWM6");
166                         Assert.AreEqual (0, d.Position, "INWM7");
167
168                         d = bc [container, "Item.Text.Length"];
169                         Assert.AreEqual (4, d.Current, "INWM8");
170                         Assert.AreEqual (1, d.Count, "INWM9");
171                         Assert.AreEqual (0, d.Position, "INWM10");
172                 }
173
174 #if NET_2_0
175                 [Test]
176                 public void TestIndexerICurrencyManagerProvider ()
177                 {
178                         BindingContext bc = new BindingContext ();
179                         BindingSource source = new BindingSource ();
180
181                         // This way the actual CurrencyManager instance is NOT added to
182                         // BindingContext
183                         CurrencyManager cm = (CurrencyManager) bc [source];
184                         Assert.AreSame (cm, source.CurrencyManager, "A1");
185                         Assert.AreEqual (false, bc.Contains (source), "A2");
186                         Assert.AreEqual (0, ((ICollection)bc).Count, "A3");
187                 }
188 #endif
189
190                 [Test]
191                 [ExpectedException (typeof (ArgumentException))]
192                 public void CantCreateChildList ()
193                 {
194                         BindingContext bc = new BindingContext ();
195                         ArrayList data_source = new ArrayList ();
196
197                         BindingManagerBase a = bc [data_source, "Items"];
198                         
199                         TestHelper.RemoveWarning (a);
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ArgumentException))]
204                 public void CantCreateChildList2 ()
205                 {
206                         BindingContext bc = new BindingContext ();
207                         ArrayList data_source = new ArrayList ();
208
209                         BindingManagerBase a = bc [data_source, "Count"];
210                         
211                         TestHelper.RemoveWarning (a);
212                 }
213
214                 [Test]
215                 public void CreateCurrencyManager ()
216                 {
217                         BindingContext bc = new BindingContext ();
218                         ArrayList data_source = new ArrayList ();
219                         CurrencyManager a = bc [data_source] as CurrencyManager;
220
221                         Assert.IsNotNull (a, "CCM1");
222                 }
223
224                 [Test]
225                 public void CreatePropertyManager ()
226                 {
227                         BindingContext bc = new BindingContext ();
228                         object data_source = new object ();
229                         PropertyManager a = bc [data_source] as PropertyManager;
230
231                         Assert.IsNotNull (a, "CPM1");
232                 }
233
234                 private DataSet CreateRelatedDataSet ()
235                 {
236                         DataSet dataset = new DataSet ("DataSet");
237                         DataTable dt1 = new DataTable ("Table1");
238                         DataTable dt2 = new DataTable ("Table2");
239                         DataColumn column;
240
241                         column = new DataColumn ("One");
242                         column.DataType = typeof (int);
243                         column.Unique = true;
244                         dt1.Columns.Add (column);
245
246                         for (int i = 0; i < 10; i++) {
247                                 DataRow row = dt1.NewRow ();
248                                 row ["One"] = i;
249                                 dt1.Rows.Add (row);
250                         }
251                         
252                         column = new DataColumn ("Two");
253                         column.DataType = typeof (int);
254                         column.Unique = true;
255                         dt2.Columns.Add (column);
256
257                         for (int i = 0; i < 10; i++) {
258                                 DataRow row = dt2.NewRow ();
259                                 row ["Two"] = i;
260                                 dt2.Rows.Add (row);
261                         }
262
263                         dataset.Tables.Add (dt1);
264                         dataset.Tables.Add (dt2);
265                         dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
266
267                         return dataset;
268                 }
269
270                 [Test]
271                 public void CreateComplexManager ()
272                 {
273                         BindingContext bc = new BindingContext ();
274                         DataSet dataset = CreateRelatedDataSet ();
275                         CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
276
277                         Assert.IsNotNull (cm, "CCCM1");
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (ArgumentException))]
282                 public void FailToCreateComplexManagerRelationDoesNotExist ()
283                 {
284                         BindingContext bc = new BindingContext ();
285                         DataSet dataset = CreateRelatedDataSet ();
286                         CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
287
288                         TestHelper.RemoveWarning (cm);
289                 }
290
291                 [Test]
292                 [ExpectedException (typeof (ArgumentException))]
293                 public void FailToCreateComplexManagerNoTableSpecified ()
294                 {
295                         BindingContext bc = new BindingContext ();
296                         DataSet dataset = CreateRelatedDataSet ();
297                         CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
298
299                         TestHelper.RemoveWarning (cm);
300                 }
301
302                 [Test]
303                 [ExpectedException (typeof (ArgumentException))]
304                 public void FailToCreateComplexChildTableSpecified ()
305                 {
306                         BindingContext bc = new BindingContext ();
307                         DataSet dataset = CreateRelatedDataSet ();
308                         CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
309
310                         TestHelper.RemoveWarning (cm);
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (NotImplementedException))]
315                 public void CantSubscribeToCollectionChanged ()
316                 {
317                         BindingContext bc = new BindingContext ();
318
319                         bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
320                 }
321
322                 [Test]
323                 public void CantSubscribeToCollectionChanged2 ()
324                 {
325                         BindingContext bc = new BindingContext ();
326
327                         bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
328                 }
329
330                 private void Dummy (object sender, CollectionChangeEventArgs e)
331                 {
332
333                 }
334
335                 [Test]
336                 [ExpectedException (typeof (ArgumentNullException))]
337                 public void AddNullDataSource ()
338                 {
339                         BindingContextPoker p = new BindingContextPoker ();
340
341                         p._Add (null, new PropertyManager ());
342                 }
343
344                 [Test]
345                 [ExpectedException (typeof (ArgumentNullException))]
346                 public void AddNullListManager ()
347                 {
348                         BindingContextPoker p = new BindingContextPoker ();
349
350                         p._Add (new object (), null);
351                 }
352
353                 [Test]
354                 public void Add ()
355                 {
356                         BindingContextPoker p = new BindingContextPoker ();
357                         object data_source = new object ();
358
359                         p.collection_changed = -1;
360                         Assert.IsFalse (p.Contains (data_source), "ADD1");
361                         Assert.AreEqual (0, ((ICollection) p).Count, "ADD2");
362                         p._Add (data_source, new PropertyManager ());
363                         Assert.IsTrue (p.Contains (data_source), "ADD3");
364                         Assert.AreEqual (1, ((ICollection) p).Count, "ADD4");
365                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD5");
366
367                         p.collection_changed = -1;
368                         p._Add (data_source, new PropertyManager ());
369                         Assert.IsTrue (p.Contains (data_source), "ADD6");
370                         Assert.AreEqual (1, ((ICollection) p).Count, "ADD7");
371                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD8");
372
373                         p.collection_changed = -1;
374                         data_source = new object ();
375                         p._Add (data_source, new PropertyManager ());
376                         Assert.IsTrue (p.Contains (data_source), "ADD9");
377                         Assert.AreEqual (2, ((ICollection) p).Count, "ADD10");
378                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD9");
379                 }
380
381                 [Test]
382                 public void AddCore ()
383                 {
384                         BindingContextPoker p = new BindingContextPoker ();
385                         object data_source = new object ();
386
387                         p.collection_changed = -1;
388                         Assert.IsFalse (p.Contains (data_source), "ADDCORE1");
389                         Assert.AreEqual (0, ((ICollection) p).Count, "ADDCORE2");
390                         p._AddCore (data_source, new PropertyManager ());
391                         Assert.IsTrue (p.Contains (data_source), "ADDCORE3");
392                         Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE4");
393                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE5");
394
395                         p.collection_changed = -1;
396                         p._AddCore (data_source, new PropertyManager ());
397                         Assert.IsTrue (p.Contains (data_source), "ADDCORE6");
398                         Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE7");
399                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE8");
400
401                         p.collection_changed = -1;
402                         data_source = new object ();
403                         p._AddCore (data_source, new PropertyManager ());
404                         Assert.IsTrue (p.Contains (data_source), "ADDCORE9");
405                         Assert.AreEqual (2, ((ICollection) p).Count, "ADDCORE10");
406                         Assert.AreEqual (p.collection_changed, -1, "ADDCORE11");
407                 }
408
409                 [Test]
410                 [ExpectedException (typeof (ArgumentNullException))]
411                 public void RemoveNull ()
412                 {
413                         BindingContextPoker p = new BindingContextPoker ();
414                         p._Remove (null);
415                 }
416
417                 [Test]
418                 public void Remove ()
419                 {
420                         BindingContextPoker p = new BindingContextPoker ();
421                         object data_source = new object ();
422
423                         p.collection_changed = -1;
424                         p._Add (data_source, new PropertyManager ());
425                         Assert.IsTrue (p.Contains (data_source), "REMOVE1");
426                         Assert.AreEqual (1, ((ICollection) p).Count, "REMOVE2");
427                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVE3");
428                         p._Remove (data_source);
429                         Assert.IsFalse (p.Contains (data_source), "REMOVE4");
430                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE5");
431                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE6");
432
433                         // Double remove
434                         p.collection_changed = -1;
435                         p._Remove (data_source);
436                         Assert.IsFalse (p.Contains (data_source), "REMOVE7");
437                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE8");
438                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE9");
439                 }
440
441                 [Test]
442                 public void RemoveCore ()
443                 {
444                         BindingContextPoker p = new BindingContextPoker ();
445                         object data_source = new object ();
446
447                         p.collection_changed = -1;
448                         p._Add (data_source, new PropertyManager ());
449                         Assert.IsTrue (p.Contains (data_source), "REMOVECORE1");
450                         Assert.AreEqual (1, ((ICollection) p).Count, "REMOVECORE2");
451                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVECORE3");
452
453                         p.collection_changed = -1;
454                         p._RemoveCore (data_source);
455                         Assert.IsFalse (p.Contains (data_source), "REMOVECORE4");
456                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE5");
457                         Assert.AreEqual (p.collection_changed, -1, "REMOVECORE6");
458
459                         // Double remove
460                         p.collection_changed = -1;
461                         p._Remove (data_source);
462                         Assert.IsFalse (p.Contains (data_source), "REMOVECORE7");
463                         Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE8");
464                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVECORE9");
465                 }
466
467                 [Test]
468                 public void Clear ()
469                 {
470                         BindingContextPoker p = new BindingContextPoker ();
471                         object data_source = new object ();
472
473                         p._Add (data_source, new PropertyManager ());
474                         p.collection_changed = -1;
475                         p._Clear ();
476                         Assert.IsFalse (p.Contains (data_source), "CLEAR1");
477                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
478                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
479
480                         // Double clear
481                         p.collection_changed = -1;
482                         p._Clear ();
483                         Assert.IsFalse (p.Contains (data_source), "CLEAR1");
484                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
485                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
486                 }
487
488                 [Test]
489                 public void ClearCore ()
490                 {
491                         BindingContextPoker p = new BindingContextPoker ();
492                         object data_source = new object ();
493
494                         p._Add (data_source, new PropertyManager ());
495                         p.collection_changed = -1;
496                         p._Clear ();
497                         Assert.IsFalse (p.Contains (data_source), "CLEARCORE1");
498                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE2");
499                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE3");
500
501                         // Double clear
502                         p.collection_changed = -1;
503                         p._Clear ();
504                         Assert.IsFalse (p.Contains (data_source), "CLEARCORE4");
505                         Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE5");
506                         Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE6");
507                 }
508
509                 [Test]
510                 public void TestContains ()
511                 {
512                         BindingContextPoker p = new BindingContextPoker ();
513                         object data_source = new object ();
514                         p._Add (data_source, new PropertyManager ());
515                         Assert.IsTrue (p.Contains (data_source), "#1");
516                         Assert.IsFalse (p.Contains ("nonexistent"), "#2");
517                 }
518
519                 [Test]
520                 [ExpectedException (typeof (ArgumentNullException))]
521                 public void TestContainsNull ()
522                 {
523                         BindingContextPoker p = new BindingContextPoker ();
524                         p.Contains (null);
525                 }
526
527                 [Test]
528                 public void TestContainsNull2 ()
529                 {
530                         BindingContextPoker p = new BindingContextPoker ();
531                         object data_source = new object ();
532                         p._Add (data_source, new PropertyManager ());
533                         Assert.IsTrue (p.Contains (data_source, null), "#1");
534                         Assert.IsFalse (p.Contains ("nonexistent", null), "#2");
535                 }
536
537
538                 [Test]
539                 public void TestGetEnumerator ()
540                 {
541                         BindingContextPoker p = new BindingContextPoker ();
542                         object data_source = new object ();
543                         PropertyManager pm = new PropertyManager ();
544                         p._Add (data_source, pm);
545                         IEnumerator e = ((IEnumerable) p).GetEnumerator ();
546                         Assert.IsNotNull (e, "#1");
547                         IDictionaryEnumerator de = e as IDictionaryEnumerator;
548                         Assert.IsNotNull (de, "#2");
549                         Assert.IsTrue (de.MoveNext (), "#3");
550                         // In .NET Key is its internal type.
551                         //Assert.AreEqual (data_source, de.Key, "#4");
552                         //Assert.AreEqual (pm, de.Value, "#5");
553                         Assert.IsFalse (de.MoveNext (), "#6");
554                 }
555         }
556 }
557