2007-01-10 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingContextTest.cs
blobf4e140f479f40f72983ccd5e3ad78cf1d3b33dc4
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.
20 // Copyright (c) 2006 Novell, Inc.
22 // Authors:
23 // Jackson Harper jackson@ximian.com
26 using System;
27 using System.Data;
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 {
38 [TestFixture]
39 public class BindingContextTest {
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);
55 public void _Clear ()
57 Clear ();
60 public void _ClearCore ()
62 ClearCore ();
65 public void _Remove (object data_source)
67 Remove (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);
82 [SetUp]
83 protected virtual void SetUp ()
87 [TearDown]
88 protected virtual void TearDown ()
92 [Test]
93 public void CtorTest ()
95 BindingContext bc = new BindingContext ();
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");
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");
113 [Test]
114 [ExpectedException (typeof (ArgumentNullException))]
115 public void TestIndexerNull ()
117 BindingContext bc = new BindingContext ();
118 BindingManagerBase a, b;
120 a = bc [null];
123 [Test]
124 public void TestIndexerNoMember ()
126 BindingContext bc = new BindingContext ();
127 ArrayList data_source = new ArrayList ();
128 BindingManagerBase a, b;
130 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
132 a = bc [data_source];
133 b = bc [data_source];
134 Assert.AreSame (a, b, "INNM1");
136 b = bc [data_source, String.Empty];
137 Assert.AreSame (a, b, "INNM2");
139 // Only one is added to the list
140 Assert.AreEqual (((ICollection) bc).Count, 1);
143 [Test]
144 public void TestIndexerWithMember ()
146 BindingContext bc = new BindingContext ();
147 ArrayList data_source = new ArrayList ();
148 BindingManagerBase a, b, c;
150 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
152 a = bc [data_source, "Length"];
153 b = bc [data_source, "Length"];
155 Assert.AreSame (a, b, "INWM1");
157 b = bc [data_source];
158 Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
160 c = bc [data_source];
161 Assert.AreSame (b, c, "INWM3");
163 b = bc [data_source, "Length"];
164 Assert.AreSame (a, b, "INWM4");
167 [Test]
168 [ExpectedException (typeof (ArgumentException))]
169 public void CantCreateChildList ()
171 BindingContext bc = new BindingContext ();
172 ArrayList data_source = new ArrayList ();
174 BindingManagerBase a = bc [data_source, "Items"];
177 [Test]
178 [ExpectedException (typeof (ArgumentException))]
179 public void CantCreateChildList2 ()
181 BindingContext bc = new BindingContext ();
182 ArrayList data_source = new ArrayList ();
184 BindingManagerBase a = bc [data_source, "Count"];
187 [Test]
188 public void CreateCurrencyManager ()
190 BindingContext bc = new BindingContext ();
191 ArrayList data_source = new ArrayList ();
192 CurrencyManager a = bc [data_source] as CurrencyManager;
194 Assert.IsNotNull (a, "CCM1");
197 [Test]
198 public void CreatePropertyManager ()
200 BindingContext bc = new BindingContext ();
201 object data_source = new object ();
202 PropertyManager a = bc [data_source] as PropertyManager;
204 Assert.IsNotNull (a, "CPM1");
207 private DataSet CreateRelatedDataSet ()
209 DataSet dataset = new DataSet ("DataSet");
210 DataTable dt1 = new DataTable ("Table1");
211 DataTable dt2 = new DataTable ("Table2");
212 DataColumn column;
214 column = new DataColumn ("One");
215 column.DataType = typeof (int);
216 column.Unique = true;
217 dt1.Columns.Add (column);
219 for (int i = 0; i < 10; i++) {
220 DataRow row = dt1.NewRow ();
221 row ["One"] = i;
222 dt1.Rows.Add (row);
225 column = new DataColumn ("Two");
226 column.DataType = typeof (int);
227 column.Unique = true;
228 dt2.Columns.Add (column);
230 for (int i = 0; i < 10; i++) {
231 DataRow row = dt2.NewRow ();
232 row ["Two"] = i;
233 dt2.Rows.Add (row);
236 dataset.Tables.Add (dt1);
237 dataset.Tables.Add (dt2);
238 dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
240 return dataset;
243 [Test]
244 public void CreateComplexManager ()
246 BindingContext bc = new BindingContext ();
247 DataSet dataset = CreateRelatedDataSet ();
248 CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
250 Assert.IsNotNull (cm, "CCCM1");
253 [Test]
254 [ExpectedException (typeof (ArgumentException))]
255 public void FailToCreateComplexManagerRelationDoesNotExist ()
257 BindingContext bc = new BindingContext ();
258 DataSet dataset = CreateRelatedDataSet ();
259 CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
262 [Test]
263 [ExpectedException (typeof (ArgumentException))]
264 public void FailToCreateComplexManagerNoTableSpecified ()
266 BindingContext bc = new BindingContext ();
267 DataSet dataset = CreateRelatedDataSet ();
268 CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
271 [Test]
272 [ExpectedException (typeof (ArgumentException))]
273 public void FailToCreateComplexChildTableSpecified ()
275 BindingContext bc = new BindingContext ();
276 DataSet dataset = CreateRelatedDataSet ();
277 CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
280 [Test]
281 [ExpectedException (typeof (NotImplementedException))]
282 public void CantSubscribeToCollectionChanged ()
284 BindingContext bc = new BindingContext ();
286 bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
289 [Test]
290 public void CantSubscribeToCollectionChanged2 ()
292 BindingContext bc = new BindingContext ();
294 bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
297 private void Dummy (object sender, CollectionChangeEventArgs e)
302 [Test]
303 [ExpectedException (typeof (ArgumentNullException))]
304 public void AddNullDataSource ()
306 BindingContextPoker p = new BindingContextPoker ();
308 p._Add (null, new PropertyManager ());
311 [Test]
312 [ExpectedException (typeof (ArgumentNullException))]
313 public void AddNullListManager ()
315 BindingContextPoker p = new BindingContextPoker ();
317 p._Add (new object (), null);
320 [Test]
321 public void Add ()
323 BindingContextPoker p = new BindingContextPoker ();
324 object data_source = new object ();
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");
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");
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");
348 [Test]
349 public void AddCore ()
351 BindingContextPoker p = new BindingContextPoker ();
352 object data_source = new object ();
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");
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");
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");
376 [Test]
377 [ExpectedException (typeof (ArgumentNullException))]
378 public void RemoveNull ()
380 BindingContextPoker p = new BindingContextPoker ();
381 p._Remove (null);
384 [Test]
385 public void Remove ()
387 BindingContextPoker p = new BindingContextPoker ();
388 object data_source = new object ();
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");
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");
408 [Test]
409 public void RemoveCore ()
411 BindingContextPoker p = new BindingContextPoker ();
412 object data_source = new object ();
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");
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");
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");
434 [Test]
435 public void Clear ()
437 BindingContextPoker p = new BindingContextPoker ();
438 object data_source = new object ();
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");
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");
455 [Test]
456 public void ClearCore ()
458 BindingContextPoker p = new BindingContextPoker ();
459 object data_source = new object ();
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");
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");
476 [Test]
477 public void TestContains ()
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");
486 [Test]
487 [ExpectedException (typeof (ArgumentNullException))]
488 public void TestContainsNull ()
490 BindingContextPoker p = new BindingContextPoker ();
491 p.Contains (null);
494 [Test]
495 public void TestContainsNull2 ()
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");
505 [Test]
506 public void TestGetEnumerator ()
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");