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);
292 private void Dummy (object sender, CollectionChangeEventArgs e)
298 [ExpectedException (typeof (ArgumentNullException))]
299 public void AddNullDataSource ()
301 BindingContextPoker p = new BindingContextPoker ();
303 p._Add (null, new PropertyManager ());
307 [ExpectedException (typeof (ArgumentNullException))]
308 public void AddNullListManager ()
310 BindingContextPoker p = new BindingContextPoker ();
312 p._Add (new object (), null);
318 BindingContextPoker p = new BindingContextPoker ();
319 object data_source = new object ();
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");
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");
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");
344 public void AddCore ()
346 BindingContextPoker p = new BindingContextPoker ();
347 object data_source = new object ();
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");
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");
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");
372 [ExpectedException (typeof (ArgumentNullException))]
373 public void RemoveNull ()
375 BindingContextPoker p = new BindingContextPoker ();
380 public void Remove ()
382 BindingContextPoker p = new BindingContextPoker ();
383 object data_source = new object ();
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");
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");
404 public void RemoveCore ()
406 BindingContextPoker p = new BindingContextPoker ();
407 object data_source = new object ();
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");
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");
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");
432 BindingContextPoker p = new BindingContextPoker ();
433 object data_source = new object ();
435 p._Add (data_source, new PropertyManager ());
436 p.collection_changed = -1;
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");
443 p.collection_changed = -1;
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");
451 public void ClearCore ()
453 BindingContextPoker p = new BindingContextPoker ();
454 object data_source = new object ();
456 p._Add (data_source, new PropertyManager ());
457 p.collection_changed = -1;
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");
464 p.collection_changed = -1;
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");
472 public void TestContains ()
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");
482 [ExpectedException (typeof (ArgumentNullException))]
483 public void TestContainsNull ()
485 BindingContextPoker p = new BindingContextPoker ();
490 public void TestGetEnumerator ()
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");