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