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 ()
88 protected virtual void TearDown ()
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" }
;
109 Assert
.AreEqual (0, ic
.Count
, "CT8");
110 Assert
.IsFalse (ic
.GetEnumerator ().MoveNext (), "CT9");
114 [ExpectedException (typeof (ArgumentNullException
))]
115 public void TestIndexerNull ()
117 BindingContext bc
= new BindingContext ();
118 BindingManagerBase a
, b
;
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);
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");
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"];
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"];
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");
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");
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 ();
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 ();
236 dataset
.Tables
.Add (dt1
);
237 dataset
.Tables
.Add (dt2
);
238 dataset
.Relations
.Add ("Relation", dt1
.Columns
["One"], dt2
.Columns
["Two"]);
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");
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
;
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
;
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
;
281 [ExpectedException (typeof (NotImplementedException
))]
282 public void CantSubscribeToCollectionChanged ()
284 BindingContext bc
= new BindingContext ();
286 bc
.CollectionChanged
+= new CollectionChangeEventHandler (Dummy
);
290 public void CantSubscribeToCollectionChanged2 ()
292 BindingContext bc
= new BindingContext ();
294 bc
.CollectionChanged
-= new CollectionChangeEventHandler (Dummy
);
297 private void Dummy (object sender
, CollectionChangeEventArgs e
)
303 [ExpectedException (typeof (ArgumentNullException
))]
304 public void AddNullDataSource ()
306 BindingContextPoker p
= new BindingContextPoker ();
308 p
._Add (null, new PropertyManager ());
312 [ExpectedException (typeof (ArgumentNullException
))]
313 public void AddNullListManager ()
315 BindingContextPoker p
= new BindingContextPoker ();
317 p
._Add (new object (), null);
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");
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");
377 [ExpectedException (typeof (ArgumentNullException
))]
378 public void RemoveNull ()
380 BindingContextPoker p
= new BindingContextPoker ();
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");
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");
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");
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");
437 BindingContextPoker p
= new BindingContextPoker ();
438 object data_source
= new object ();
440 p
._Add (data_source
, new PropertyManager ());
441 p
.collection_changed
= -1;
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");
448 p
.collection_changed
= -1;
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");
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;
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");
469 p
.collection_changed
= -1;
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");
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");
487 [ExpectedException (typeof (ArgumentNullException
))]
488 public void TestContainsNull ()
490 BindingContextPoker p
= new BindingContextPoker ();
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");
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");