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 {
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);
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 protected virtual void SetUp ()
85 Console.WriteLine ("SETTING UP");
89 protected virtual void TearDown ()
91 Console.WriteLine ("TEARING DOWN");
95 public void CtorTest ()
97 BindingContext bc = new BindingContext ();
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");
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" };
111 Assert.AreEqual (0, ic.Count, "CT8");
112 Assert.IsFalse (ic.GetEnumerator ().MoveNext (), "CT9");
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void TestIndexerNull ()
119 BindingContext bc = new BindingContext ();
120 BindingManagerBase a, b;
126 public void TestIndexerNoMember ()
128 BindingContext bc = new BindingContext ();
129 ArrayList data_source = new ArrayList ();
130 BindingManagerBase a, b;
132 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
134 a = bc [data_source];
135 b = bc [data_source];
136 Assert.AreSame (a, b, "INNM1");
138 b = bc [data_source, String.Empty];
139 Assert.AreSame (a, b, "INNM2");
141 // Only one is added to the list
142 Assert.AreEqual (((ICollection) bc).Count, 1);
146 public void TestIndexerWithMember ()
148 BindingContext bc = new BindingContext ();
149 ArrayList data_source = new ArrayList ();
150 BindingManagerBase a, b, c;
152 data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
154 a = bc [data_source, "Length"];
155 b = bc [data_source, "Length"];
157 Assert.AreSame (a, b, "INWM1");
159 b = bc [data_source];
160 Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
162 c = bc [data_source];
163 Assert.AreSame (b, c, "INWM3");
165 b = bc [data_source, "Length"];
166 Assert.AreSame (a, b, "INWM4");
170 [ExpectedException (typeof (ArgumentException))]
171 public void CantCreateChildList ()
173 BindingContext bc = new BindingContext ();
174 ArrayList data_source = new ArrayList ();
176 BindingManagerBase a = bc [data_source, "Items"];
179 [Category ("NotWorking")]
181 [ExpectedException (typeof (ArgumentException))]
182 public void CantCreateChildList2 ()
184 BindingContext bc = new BindingContext ();
185 ArrayList data_source = new ArrayList ();
187 BindingManagerBase a = bc [data_source, "Count"];
191 public void CreateCurrencyManager ()
193 BindingContext bc = new BindingContext ();
194 ArrayList data_source = new ArrayList ();
195 CurrencyManager a = bc [data_source] as CurrencyManager;
197 Assert.IsNotNull (a, "CCM1");
201 public void CreatePropertyManager ()
203 BindingContext bc = new BindingContext ();
204 object data_source = new object ();
205 PropertyManager a = bc [data_source] as PropertyManager;
207 Assert.IsNotNull (a, "CPM1");
210 private DataSet CreateRelatedDataSet ()
212 DataSet dataset = new DataSet ("DataSet");
213 DataTable dt1 = new DataTable ("Table1");
214 DataTable dt2 = new DataTable ("Table2");
217 column = new DataColumn ("One");
218 column.DataType = typeof (int);
219 column.Unique = true;
220 dt1.Columns.Add (column);
222 for (int i = 0; i < 10; i++) {
223 DataRow row = dt1.NewRow ();
228 column = new DataColumn ("Two");
229 column.DataType = typeof (int);
230 column.Unique = true;
231 dt2.Columns.Add (column);
233 for (int i = 0; i < 10; i++) {
234 DataRow row = dt2.NewRow ();
239 dataset.Tables.Add (dt1);
240 dataset.Tables.Add (dt2);
241 dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
247 public void CreateComplexManager ()
249 BindingContext bc = new BindingContext ();
250 DataSet dataset = CreateRelatedDataSet ();
251 CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
253 Assert.IsNotNull (cm, "CCCM1");
257 [ExpectedException (typeof (ArgumentException))]
258 public void FailToCreateComplexManagerRelationDoesNotExist ()
260 BindingContext bc = new BindingContext ();
261 DataSet dataset = CreateRelatedDataSet ();
262 CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
266 [ExpectedException (typeof (ArgumentException))]
267 public void FailToCreateComplexManagerNoTableSpecified ()
269 BindingContext bc = new BindingContext ();
270 DataSet dataset = CreateRelatedDataSet ();
271 CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
275 [ExpectedException (typeof (ArgumentException))]
276 public void FailToCreateComplexChildTableSpecified ()
278 BindingContext bc = new BindingContext ();
279 DataSet dataset = CreateRelatedDataSet ();
280 CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
284 [ExpectedException (typeof (NotImplementedException))]
285 public void CantSubscribeToCollectionChanged ()
287 BindingContext bc = new BindingContext ();
289 bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
293 public void CantSubscribeToCollectionChanged2 ()
295 BindingContext bc = new BindingContext ();
297 bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
300 private void Dummy (object sender, CollectionChangeEventArgs e)
306 [ExpectedException (typeof (ArgumentNullException))]
307 public void AddNullDataSource ()
309 BindingContextPoker p = new BindingContextPoker ();
311 p._Add (null, new PropertyManager ());
315 [ExpectedException (typeof (ArgumentNullException))]
316 public void AddNullListManager ()
318 BindingContextPoker p = new BindingContextPoker ();
320 p._Add (new object (), null);
326 BindingContextPoker p = new BindingContextPoker ();
327 object data_source = new object ();
329 p.collection_changed = -1;
330 Assert.IsFalse (p.Contains (data_source), "ADD1");
331 Assert.AreEqual (0, ((ICollection) p).Count, "ADD2");
332 p._Add (data_source, new PropertyManager ());
333 Assert.IsTrue (p.Contains (data_source), "ADD3");
334 Assert.AreEqual (1, ((ICollection) p).Count, "ADD4");
335 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD5");
337 p.collection_changed = -1;
338 p._Add (data_source, new PropertyManager ());
339 Assert.IsTrue (p.Contains (data_source), "ADD6");
340 Assert.AreEqual (1, ((ICollection) p).Count, "ADD7");
341 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD8");
343 p.collection_changed = -1;
344 data_source = new object ();
345 p._Add (data_source, new PropertyManager ());
346 Assert.IsTrue (p.Contains (data_source), "ADD9");
347 Assert.AreEqual (2, ((ICollection) p).Count, "ADD10");
348 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD9");
352 public void AddCore ()
354 BindingContextPoker p = new BindingContextPoker ();
355 object data_source = new object ();
357 p.collection_changed = -1;
358 Assert.IsFalse (p.Contains (data_source), "ADDCORE1");
359 Assert.AreEqual (0, ((ICollection) p).Count, "ADDCORE2");
360 p._AddCore (data_source, new PropertyManager ());
361 Assert.IsTrue (p.Contains (data_source), "ADDCORE3");
362 Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE4");
363 Assert.AreEqual (p.collection_changed, -1, "ADDCORE5");
365 p.collection_changed = -1;
366 p._AddCore (data_source, new PropertyManager ());
367 Assert.IsTrue (p.Contains (data_source), "ADDCORE6");
368 Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE7");
369 Assert.AreEqual (p.collection_changed, -1, "ADDCORE8");
371 p.collection_changed = -1;
372 data_source = new object ();
373 p._AddCore (data_source, new PropertyManager ());
374 Assert.IsTrue (p.Contains (data_source), "ADDCORE9");
375 Assert.AreEqual (2, ((ICollection) p).Count, "ADDCORE10");
376 Assert.AreEqual (p.collection_changed, -1, "ADDCORE11");
380 [ExpectedException (typeof (ArgumentNullException))]
381 public void RemoveNull ()
383 BindingContextPoker p = new BindingContextPoker ();
388 public void Remove ()
390 BindingContextPoker p = new BindingContextPoker ();
391 object data_source = new object ();
393 p.collection_changed = -1;
394 p._Add (data_source, new PropertyManager ());
395 Assert.IsTrue (p.Contains (data_source), "REMOVE1");
396 Assert.AreEqual (1, ((ICollection) p).Count, "REMOVE2");
397 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVE3");
398 p._Remove (data_source);
399 Assert.IsFalse (p.Contains (data_source), "REMOVE4");
400 Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE5");
401 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE6");
404 p.collection_changed = -1;
405 p._Remove (data_source);
406 Assert.IsFalse (p.Contains (data_source), "REMOVE7");
407 Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE8");
408 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE9");
412 public void RemoveCore ()
414 BindingContextPoker p = new BindingContextPoker ();
415 object data_source = new object ();
417 p.collection_changed = -1;
418 p._Add (data_source, new PropertyManager ());
419 Assert.IsTrue (p.Contains (data_source), "REMOVECORE1");
420 Assert.AreEqual (1, ((ICollection) p).Count, "REMOVECORE2");
421 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVECORE3");
423 p.collection_changed = -1;
424 p._RemoveCore (data_source);
425 Assert.IsFalse (p.Contains (data_source), "REMOVECORE4");
426 Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE5");
427 Assert.AreEqual (p.collection_changed, -1, "REMOVECORE6");
430 p.collection_changed = -1;
431 p._Remove (data_source);
432 Assert.IsFalse (p.Contains (data_source), "REMOVECORE7");
433 Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE8");
434 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVECORE9");
440 BindingContextPoker p = new BindingContextPoker ();
441 object data_source = new object ();
443 p._Add (data_source, new PropertyManager ());
444 p.collection_changed = -1;
446 Assert.IsFalse (p.Contains (data_source), "CLEAR1");
447 Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
448 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
451 p.collection_changed = -1;
453 Assert.IsFalse (p.Contains (data_source), "CLEAR1");
454 Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
455 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
459 public void ClearCore ()
461 BindingContextPoker p = new BindingContextPoker ();
462 object data_source = new object ();
464 p._Add (data_source, new PropertyManager ());
465 p.collection_changed = -1;
467 Assert.IsFalse (p.Contains (data_source), "CLEARCORE1");
468 Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE2");
469 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE3");
472 p.collection_changed = -1;
474 Assert.IsFalse (p.Contains (data_source), "CLEARCORE4");
475 Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE5");
476 Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE6");
480 public void TestContains ()
482 BindingContextPoker p = new BindingContextPoker ();
483 object data_source = new object ();
484 p._Add (data_source, new PropertyManager ());
485 Assert.IsTrue (p.Contains (data_source), "#1");
486 Assert.IsFalse (p.Contains ("nonexistent"), "#2");
490 [ExpectedException (typeof (ArgumentNullException))]
491 public void TestContainsNull ()
493 BindingContextPoker p = new BindingContextPoker ();
498 public void TestContainsNull2 ()
500 BindingContextPoker p = new BindingContextPoker ();
501 object data_source = new object ();
502 p._Add (data_source, new PropertyManager ());
503 Assert.IsTrue (p.Contains (data_source, null), "#1");
504 Assert.IsFalse (p.Contains ("nonexistent", null), "#2");
509 public void TestGetEnumerator ()
511 BindingContextPoker p = new BindingContextPoker ();
512 object data_source = new object ();
513 PropertyManager pm = new PropertyManager ();
514 p._Add (data_source, pm);
515 IEnumerator e = ((IEnumerable) p).GetEnumerator ();
516 Assert.IsNotNull (e, "#1");
517 IDictionaryEnumerator de = e as IDictionaryEnumerator;
518 Assert.IsNotNull (de, "#2");
519 Assert.IsTrue (de.MoveNext (), "#3");
520 // In .NET Key is its internal type.
521 //Assert.AreEqual (data_source, de.Key, "#4");
522 //Assert.AreEqual (pm, de.Value, "#5");
523 Assert.IsFalse (de.MoveNext (), "#6");