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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006 Novell, Inc.
23 // Jackson Harper jackson@ximian.com
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Windows.Forms;
32 using NUnit.Framework;
34 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
36 namespace MonoTests.System.Windows.Forms.DataBinding {
39 public class BindingContextTest : TestHelper {
41 private class BindingContextPoker : BindingContext {
43 public int collection_changed;
45 public void _Add (object data_source, BindingManagerBase list_manager)
47 Add (data_source, list_manager);
50 public void _AddCore (object data_source, BindingManagerBase list_manager)
52 AddCore (data_source, list_manager);
60 public void _ClearCore ()
65 public void _Remove (object data_source)
70 public void _RemoveCore (object data_source)
72 RemoveCore (data_source);
75 protected override void OnCollectionChanged (CollectionChangeEventArgs ce)
77 collection_changed = (int) ce.Action;
78 base.OnCollectionChanged (ce);
83 public void CtorTest ()
85 BindingContext bc = new BindingContext ();
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");
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" };
99 Assert.AreEqual (0, ic.Count, "CT8");
100 Assert.IsFalse (ic.GetEnumerator ().MoveNext (), "CT9");
104 [ExpectedException (typeof (ArgumentNullException))]
105 public void TestIndexerNull ()
107 BindingContext bc = new BindingContext ();
108 BindingManagerBase a;
112 TestHelper.RemoveWarning (a, bc);
116 public void TestIndexerNoMember ()
118 BindingContext bc = new BindingContext ();
119 ArrayList data_source = new ArrayList ();
120 BindingManagerBase a, b;
122 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
124 a = bc [data_source];
125 b = bc [data_source];
126 Assert.AreSame (a, b, "INNM1");
128 b = bc [data_source, String.Empty];
129 Assert.AreSame (a, b, "INNM2");
131 // Only one is added to the list
132 Assert.AreEqual (((ICollection) bc).Count, 1);
136 public void TestIndexerWithMember ()
138 BindingContext bc = new BindingContext ();
139 ArrayList data_source = new ArrayList ();
140 BindingManagerBase a, b, c, d;
142 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
144 a = bc [data_source, "Length"];
145 b = bc [data_source, "Length"];
147 Assert.AreSame (a, b, "INWM1");
149 b = bc [data_source];
150 Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
152 c = bc [data_source];
153 Assert.AreSame (b, c, "INWM3");
155 b = bc [data_source, "Length"];
156 Assert.AreSame (a, b, "INWM4");
159 MockItem item = new MockItem ("Mono", -1);
160 MockContainer container = new MockContainer ();
161 container.Item = item;
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");
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");
176 public void TestIndexerICurrencyManagerProvider ()
178 BindingContext bc = new BindingContext ();
179 BindingSource source = new BindingSource ();
181 // This way the actual CurrencyManager instance is NOT added to
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");
191 [ExpectedException (typeof (ArgumentException))]
192 public void CantCreateChildList ()
194 BindingContext bc = new BindingContext ();
195 ArrayList data_source = new ArrayList ();
197 BindingManagerBase a = bc [data_source, "Items"];
199 TestHelper.RemoveWarning (a);
203 [ExpectedException (typeof (ArgumentException))]
204 public void CantCreateChildList2 ()
206 BindingContext bc = new BindingContext ();
207 ArrayList data_source = new ArrayList ();
209 BindingManagerBase a = bc [data_source, "Count"];
211 TestHelper.RemoveWarning (a);
215 public void CreateCurrencyManager ()
217 BindingContext bc = new BindingContext ();
218 ArrayList data_source = new ArrayList ();
219 CurrencyManager a = bc [data_source] as CurrencyManager;
221 Assert.IsNotNull (a, "CCM1");
225 public void CreatePropertyManager ()
227 BindingContext bc = new BindingContext ();
228 object data_source = new object ();
229 PropertyManager a = bc [data_source] as PropertyManager;
231 Assert.IsNotNull (a, "CPM1");
234 private DataSet CreateRelatedDataSet ()
236 DataSet dataset = new DataSet ("DataSet");
237 DataTable dt1 = new DataTable ("Table1");
238 DataTable dt2 = new DataTable ("Table2");
241 column = new DataColumn ("One");
242 column.DataType = typeof (int);
243 column.Unique = true;
244 dt1.Columns.Add (column);
246 for (int i = 0; i < 10; i++) {
247 DataRow row = dt1.NewRow ();
252 column = new DataColumn ("Two");
253 column.DataType = typeof (int);
254 column.Unique = true;
255 dt2.Columns.Add (column);
257 for (int i = 0; i < 10; i++) {
258 DataRow row = dt2.NewRow ();
263 dataset.Tables.Add (dt1);
264 dataset.Tables.Add (dt2);
265 dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
271 public void CreateComplexManager ()
273 BindingContext bc = new BindingContext ();
274 DataSet dataset = CreateRelatedDataSet ();
275 CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
277 Assert.IsNotNull (cm, "CCCM1");
281 [ExpectedException (typeof (ArgumentException))]
282 public void FailToCreateComplexManagerRelationDoesNotExist ()
284 BindingContext bc = new BindingContext ();
285 DataSet dataset = CreateRelatedDataSet ();
286 CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
288 TestHelper.RemoveWarning (cm);
292 [ExpectedException (typeof (ArgumentException))]
293 public void FailToCreateComplexManagerNoTableSpecified ()
295 BindingContext bc = new BindingContext ();
296 DataSet dataset = CreateRelatedDataSet ();
297 CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
299 TestHelper.RemoveWarning (cm);
303 [ExpectedException (typeof (ArgumentException))]
304 public void FailToCreateComplexChildTableSpecified ()
306 BindingContext bc = new BindingContext ();
307 DataSet dataset = CreateRelatedDataSet ();
308 CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
310 TestHelper.RemoveWarning (cm);
314 [ExpectedException (typeof (NotImplementedException))]
315 public void CantSubscribeToCollectionChanged ()
317 BindingContext bc = new BindingContext ();
319 bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
323 public void CantSubscribeToCollectionChanged2 ()
325 BindingContext bc = new BindingContext ();
327 bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
330 private void Dummy (object sender, CollectionChangeEventArgs e)
336 [ExpectedException (typeof (ArgumentNullException))]
337 public void AddNullDataSource ()
339 BindingContextPoker p = new BindingContextPoker ();
341 p._Add (null, new PropertyManager ());
345 [ExpectedException (typeof (ArgumentNullException))]
346 public void AddNullListManager ()
348 BindingContextPoker p = new BindingContextPoker ();
350 p._Add (new object (), null);
356 BindingContextPoker p = new BindingContextPoker ();
357 object data_source = new object ();
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");
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");
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");
382 public void AddCore ()
384 BindingContextPoker p = new BindingContextPoker ();
385 object data_source = new object ();
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");
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");
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");
410 [ExpectedException (typeof (ArgumentNullException))]
411 public void RemoveNull ()
413 BindingContextPoker p = new BindingContextPoker ();
418 public void Remove ()
420 BindingContextPoker p = new BindingContextPoker ();
421 object data_source = new object ();
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");
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");
442 public void RemoveCore ()
444 BindingContextPoker p = new BindingContextPoker ();
445 object data_source = new object ();
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");
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");
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");
470 BindingContextPoker p = new BindingContextPoker ();
471 object data_source = new object ();
473 p._Add (data_source, new PropertyManager ());
474 p.collection_changed = -1;
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");
481 p.collection_changed = -1;
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");
489 public void ClearCore ()
491 BindingContextPoker p = new BindingContextPoker ();
492 object data_source = new object ();
494 p._Add (data_source, new PropertyManager ());
495 p.collection_changed = -1;
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");
502 p.collection_changed = -1;
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");
510 public void TestContains ()
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");
520 [ExpectedException (typeof (ArgumentNullException))]
521 public void TestContainsNull ()
523 BindingContextPoker p = new BindingContextPoker ();
528 public void TestContainsNull2 ()
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");
539 public void TestGetEnumerator ()
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");