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.Globalization;
30 using System.Windows.Forms;
32 using NUnit.Framework;
34 namespace MonoTests.System.Windows.Forms.DataBinding {
37 public class BindingTest : TestHelper {
40 public void CtorTest ()
42 string prop = "PROPERTY NAME";
43 object data_source = new object ();
44 string data_member = "DATA MEMBER";
45 Binding b = new Binding (prop, data_source, data_member);
47 Assert.IsNull (b.BindingManagerBase, "ctor1");
48 Console.WriteLine ("MEMBER INFO: " + b.BindingMemberInfo);
49 Assert.IsNotNull (b.BindingMemberInfo, "ctor2");
50 Assert.IsNull (b.Control, "ctor3");
51 Assert.IsFalse (b.IsBinding, "ctor4");
53 Assert.AreSame (b.PropertyName, prop, "ctor5");
54 Assert.AreSame (b.DataSource, data_source, "ctor6");
57 Assert.AreEqual (false, b.FormattingEnabled, "ctor7");
58 Assert.AreEqual (String.Empty, b.FormatString, "ctor8");
59 Assert.IsNull (b.FormatInfo, "ctor9");
60 Assert.IsNull (b.NullValue, "ctor10");
61 Assert.AreEqual (Convert.DBNull, b.DataSourceNullValue, "ctor11");
66 public void CtorNullTest ()
68 Binding b = new Binding (null, null, null);
70 Assert.IsNull (b.PropertyName, "ctornull1");
71 Assert.IsNull (b.DataSource, "ctornull2");
75 public void CtorEmptyProperty ()
77 Binding b = new Binding ("Text", 6, String.Empty);
78 Control c = new Control ();
79 c.BindingContext = new BindingContext ();
82 c.DataBindings.Add (b);
83 Assert.AreEqual ("6", c.Text, "A1");
87 public void BindingManagerBaseTest ()
89 if (TestHelper.RunningOnUnix) {
90 Assert.Ignore ("Fails at the moment");
93 Control c1 = new Control ();
94 Control c2 = new Control ();
97 c1.BindingContext = new BindingContext ();
98 c2.BindingContext = c1.BindingContext;
100 binding = c2.DataBindings.Add ("Text", c1, "Text");
102 Assert.IsNull (binding.BindingManagerBase, "1");
107 Assert.IsNull (binding.BindingManagerBase, "2");
109 c2.DataBindings.Remove (binding);
110 binding = c2.DataBindings.Add ("Text", c1, "Text");
112 Assert.IsTrue (binding.BindingManagerBase != null, "3");
116 /* create control and set binding context */
117 public void BindingContextChangedTest ()
119 Control c = new Control ();
120 // Test BindingContextChanged Event
121 c.BindingContextChanged += new EventHandler (Event_Handler1);
122 BindingContext bcG1 = new BindingContext ();
124 c.BindingContext = bcG1;
125 Assert.AreEqual (1, eventcount, "A1");
129 /* create control and show control */
130 public void BindingContextChangedTest2 ()
132 Form f = new Form ();
133 f.ShowInTaskbar = false;
134 Control c = new Control ();
137 c.BindingContextChanged += new EventHandler (Event_Handler1);
141 Assert.AreEqual (1, eventcount, "A1");
143 Assert.AreEqual (2, eventcount, "A1");
149 /* create control, set binding context, and show control */
150 public void BindingContextChangedTest3 ()
152 Form f = new Form ();
153 f.ShowInTaskbar = false;
155 Control c = new Control ();
158 c.BindingContextChanged += new EventHandler (Event_Handler1);
160 c.BindingContext = new BindingContext ();;
162 Assert.AreEqual (1, eventcount, "A1");
167 public void BindingContextChangedTest4 ()
169 Form f = new Form ();
170 f.ShowInTaskbar = false;
172 ContainerControl cc = new ContainerControl ();
174 Control c = new Control ();
178 c.BindingContextChanged += new EventHandler (Event_Handler1);
179 cc.BindingContextChanged += new EventHandler (Event_Handler1);
180 f.BindingContextChanged += new EventHandler (Event_Handler1);
185 Assert.AreEqual (5, eventcount, "A1");
187 Assert.AreEqual (8, eventcount, "A1");
193 public void Event_Handler1 (object sender, EventArgs e)
195 //Console.WriteLine (sender.GetType());
196 //Console.WriteLine (Environment.StackTrace);
201 public void DataBindingCountTest1 ()
203 Control c = new Control ();
204 Assert.AreEqual (0, c.DataBindings.Count, "1");
205 c.DataBindings.Add (new Binding ("Text", c, "Name"));
206 Assert.AreEqual (1, c.DataBindings.Count, "2");
208 Binding b = c.DataBindings[0];
209 Assert.AreEqual (c, b.Control, "3");
210 Assert.AreEqual (c, b.DataSource, "4");
211 Assert.AreEqual ("Text", b.PropertyName, "5");
212 Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "6");
216 public void DataBindingCountTest2 ()
218 Control c = new Control ();
219 Control c2 = new Control ();
220 Assert.AreEqual (0, c.DataBindings.Count, "1");
221 c.DataBindings.Add (new Binding ("Text", c2, "Name"));
222 Assert.AreEqual (1, c.DataBindings.Count, "2");
223 Assert.AreEqual (0, c2.DataBindings.Count, "3");
225 Binding b = c.DataBindings[0];
226 Assert.AreEqual (c, b.Control, "4");
227 Assert.AreEqual (c2, b.DataSource, "5");
228 Assert.AreEqual ("Text", b.PropertyName, "6");
229 Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "7");
233 public void DataSourceNullTest ()
235 ChildMockItem item = new ChildMockItem ();
236 Control c = new Control ();
238 item.ObjectValue = null;
240 c.DataBindings.Add ("Tag", item, "ObjectValue");
242 Form f = new Form ();
245 f.Show (); // Need this to init data binding
247 Assert.AreEqual (DBNull.Value, c.Tag, "1");
253 // For this case to work, the data source property needs
254 // to have an associated 'PropertyChanged' event.
256 public void DataSourcePropertyChanged ()
258 Control c = new Control ();
259 c.BindingContext = new BindingContext ();
262 MockItem item = new MockItem ("A", 0);
263 Binding binding = new Binding ("Text", item, "Text");
265 c.DataBindings.Add (binding);
266 Assert.AreEqual ("A", c.Text, "#A1");
269 Assert.AreEqual ("B", c.Text, "#B1");
273 public void DataSourcePropertyChanged_Original ()
275 Control c = new Control ();
276 c.BindingContext = new BindingContext ();
279 MockItem item = new MockItem ("A", 0);
280 Binding binding = new Binding ("Text", item, "Text");
282 c.DataBindings.Add (binding);
283 Assert.AreEqual ("A", c.Text, "#A1");
286 Assert.AreEqual ("B", c.Text, "#B1");
290 public void DataSourcePropertyChanged_Original_BadName ()
292 Control c = new Control ();
293 c.BindingContext = new BindingContext ();
296 MockItem item = new MockItem ("A", 0);
297 Binding binding = new Binding ("Text", item, "xxxxxxTextXXXXX");
300 c.DataBindings.Add (binding);
301 Assert.Fail ("exc1");
302 } catch (ArgumentException ex) {
303 Assert.AreEqual ("dataMember", ex.ParamName, "ex.ParamName"); // (test is not locale dependent)
308 public void DataSourcePropertyChanged_OneDeep ()
310 Control c = new Control ();
311 c.BindingContext = new BindingContext ();
314 MockItem item = new MockItem ("A", 0);
315 One parent = new One ();
316 parent.MockItem = item;
317 Binding binding = new Binding ("Text", parent, "MockItem.Text");
319 c.DataBindings.Add (binding);
320 Assert.AreEqual ("A", c.Text, "#A1");
323 Assert.AreEqual ("B", c.Text, "#B1");
327 public void DataSourcePropertyChanged_ThreeDeep ()
329 Control c = new Control ();
330 c.BindingContext = new BindingContext ();
333 MockItem item = new MockItem ("A", 0);
334 One parent = new One ();
335 parent.Two = new Two ();
336 parent.Two.Three = new Three ();
337 parent.Two.Three.MockItem = item;
338 Binding binding = new Binding ("Text", parent, "Two.Three.MockItem.Text");
340 c.DataBindings.Add (binding);
341 Console.WriteLine ("c.Text: " + c.Text);
342 Assert.AreEqual ("A", c.Text, "#A1");
345 Assert.AreEqual ("B", c.Text, "#B1");
347 Assert.AreEqual (1, c.DataBindings.Count, "c.DataBindings.Count");
348 BindingMemberInfo bmi = c.DataBindings[0].BindingMemberInfo;
349 Assert.AreEqual ("Two.Three.MockItem", bmi.BindingPath, "bmi.BindingPath");
350 Assert.AreEqual ("Two.Three.MockItem.Text", bmi.BindingMember, "bmi.BindingMember");
351 Assert.AreEqual ("Text", bmi.BindingField, "bmi.BindingField");
355 public void DataSourcePropertyChanged_DataSet ()
357 DataSet ds = new DataSet ();
359 DataTable table1 = new DataTable ("Customers");
360 table1.Columns.Add ("Id", typeof (int));
361 table1.Columns.Add ("Name", typeof (string));
362 table1.Rows.Add (new object[] {3, "customer1"});
363 table1.Rows.Add (new object[] {7, "customer2"});
364 ds.Tables.Add (table1);
366 DataTable table2 = new DataTable ("Orders");
367 table2.Columns.Add ("OrderId", typeof (int));
368 table2.Columns.Add ("CustomerId", typeof (int));
369 table2.Rows.Add (new object[] {56, 7});
370 table2.Rows.Add (new object[] {57, 3});
371 ds.Tables.Add (table2);
373 DataRelation relation = new DataRelation ("CustomerOrders", table1.Columns ["Id"],
374 table2.Columns ["CustomerId"]);
375 ds.Relations.Add (relation);
377 Control ctrl = new Control ();
378 ctrl.BindingContext = new BindingContext ();
379 ctrl.CreateControl ();
381 ctrl.DataBindings.Add ("Text", ds, "Customers.CustomerOrders.OrderId");
382 Assert.AreEqual ("57", ctrl.Text, "A1");
386 public void DataSourcePropertyDifferentType ()
388 Exception exc = new Exception (String.Empty, new ArgumentNullException ("PARAM"));
390 // The type of the property is Exception, but we know that the value
391 // is actually an ArgumentException, thus specify the ParamName property
392 Control ctrl = new Control ();
393 ctrl.BindingContext = new BindingContext ();
394 ctrl.CreateControl ();
396 ctrl.DataBindings.Add ("Text", exc, "InnerException.ParamName");
397 Assert.AreEqual ("PARAM", ctrl.Text, "A1");
401 public void IsBindingTest ()
403 MockItem [] items = new MockItem [] { new MockItem ("A", 0) };
404 Binding binding = new Binding ("Text", items, "Text");
405 Binding binding2 = new Binding ("Text", items [0], "Text");
406 Assert.IsFalse (binding.IsBinding, "#A1");
407 Assert.IsFalse (binding2.IsBinding, "#A2");
409 Control c = new Control ();
410 Control c2 = new Control ();
411 c.DataBindings.Add (binding);
412 c2.DataBindings.Add (binding2);
413 Assert.IsFalse (binding.IsBinding, "#B1");
414 Assert.IsFalse (binding2.IsBinding, "#B2");
418 Assert.IsFalse (binding.IsBinding, "#C1");
419 Assert.IsFalse (binding2.IsBinding, "#C2");
421 Form form = new Form ();
422 form.ShowInTaskbar = false;
423 form.Controls.Add (c);
424 form.Controls.Add (c2);
425 Assert.IsTrue (binding.IsBinding, "#D1");
426 Assert.IsTrue (binding2.IsBinding, "#D2");
431 // IsBinding is true ALWAYS with PropertyManager, even when
432 // ResumeBinding has been called
434 CurrencyManager curr_manager = (CurrencyManager)form.BindingContext [items];
435 PropertyManager prop_manager = (PropertyManager)form.BindingContext [items [0]];
436 curr_manager.SuspendBinding ();
437 prop_manager.SuspendBinding ();
438 Assert.IsFalse (binding.IsBinding, "#E1");
439 Assert.IsTrue (binding2.IsBinding, "#E2");
441 curr_manager.ResumeBinding ();
442 prop_manager.ResumeBinding ();
443 Assert.IsTrue (binding.IsBinding, "#F1");
444 Assert.IsTrue (binding2.IsBinding, "#F2");
451 public void ReadValueTest ()
453 Control c = new Control ();
454 c.BindingContext = new BindingContext ();
457 ChildMockItem item = new ChildMockItem ();
458 item.ObjectValue = "A";
459 Binding binding = new Binding ("Tag", item, "ObjectValue");
460 binding.ControlUpdateMode = ControlUpdateMode.Never;
462 c.DataBindings.Add (binding);
463 Assert.AreEqual (null, c.Tag, "#A1");
465 item.ObjectValue = "B";
466 Assert.AreEqual (null, c.Tag, "#B1");
468 binding.ReadValue ();
469 Assert.AreEqual ("B", c.Tag, "#C1");
471 item.ObjectValue = "C";
472 binding.ReadValue ();
473 Assert.AreEqual ("C", c.Tag, "#D1");
479 public void WriteValueTest ()
481 Control c = new Control ();
482 c.BindingContext = new BindingContext ();
485 MockItem item = new MockItem ();
487 Binding binding = new Binding ("Text", item, "Text");
488 binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
490 c.DataBindings.Add (binding);
491 Assert.AreEqual ("A", c.Text, "#A1");
494 Assert.AreEqual ("A", item.Text, "#B1");
496 binding.WriteValue ();
497 Assert.AreEqual ("B", item.Text, "#C1");
501 public void BindableComponentTest ()
503 Control c = new Control ();
505 MockItem item = new MockItem (String.Empty, 0);
506 Binding binding = new Binding ("Text", item, "Text");
508 c.DataBindings.Add (binding);
509 Assert.AreEqual (c, binding.Control, "#A1");
510 Assert.AreEqual (c, binding.BindableComponent, "#A2");
513 // Now use IBindableComponent - update binding when property changes
514 // since ToolStripItem doesn't have validation at all
516 BindableToolStripItem toolstrip_item = new BindableToolStripItem ();
517 toolstrip_item.BindingContext = new BindingContext ();
518 Binding binding2 = new Binding ("Text", item, "Text");
519 binding2.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
522 toolstrip_item.DataBindings.Add (binding2);
523 Assert.AreEqual (null, binding2.Control, "#B1");
524 Assert.AreEqual (toolstrip_item, binding2.BindableComponent, "#B2");
525 Assert.AreEqual (item.Text, toolstrip_item.Text, "#B3");
527 toolstrip_item.Text = "B";
528 Assert.AreEqual (toolstrip_item.Text, item.Text, "#C1");
532 public void ControlUpdateModeTest ()
534 Control c = new Control ();
535 c.BindingContext = new BindingContext ();
538 MockItem item = new MockItem ("A", 0);
539 Binding binding = new Binding ("Text", item, "Text");
540 binding.ControlUpdateMode = ControlUpdateMode.Never;
542 c.DataBindings.Add (binding);
543 Assert.AreEqual (String.Empty, c.Text, "#A1");
546 Assert.AreEqual (String.Empty, c.Text, "#B1");
550 public void DataSourceUpdateModeTest ()
552 Control c = new Control ();
553 c.BindingContext = new BindingContext ();
556 MockItem item = new MockItem ("A", 0);
557 Binding binding = new Binding ("Text", item, "Text");
558 binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
560 c.DataBindings.Add (binding);
561 Assert.AreEqual ("A", c.Text, "#A1");
564 Assert.AreEqual ("A", item.Text, "#B1");
566 binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
567 Assert.AreEqual ("A", item.Text, "#C1");
570 Assert.AreEqual ("C", item.Text, "#D1");
572 // This requires a Validation even, which we can't test
573 // by directly modifying the property
574 binding.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
577 Assert.AreEqual ("C", item.Text, "#E1");
581 public void DataSourceNullValueTest ()
583 Control c = new Control ();
584 c.BindingContext = new BindingContext ();
587 ChildMockItem item = new ChildMockItem ();
588 item.ObjectValue = "A";
589 Binding binding = new Binding ("Tag", item, "ObjectValue");
590 binding.DataSourceNullValue = "NonNull";
592 c.DataBindings.Add (binding);
593 Assert.AreEqual (c.Tag, "A", "#A1");
595 // Since Tag property doesn't have a
596 // TagChanged event, we need to force an update
598 binding.WriteValue ();
599 Assert.AreEqual (item.ObjectValue, "NonNull", "#B1");
603 public void NullValueTest ()
605 Control c = new Control ();
606 c.BindingContext = new BindingContext ();
609 DataTable table = new DataTable ();
610 table.Columns.Add ("Name", typeof (string));
611 table.Columns.Add ("Id", typeof (int));
612 table.Rows.Add (null, DBNull.Value);
614 Binding binding = new Binding ("Tag", table, "Name");
615 Binding binding2 = new Binding ("Width", table, "Id");
616 binding.FormattingEnabled = true;
617 binding.NullValue = "non-null";
618 binding2.FormattingEnabled = true;
619 binding2.NullValue = 101;
622 c.DataBindings.Add (binding);
623 c.DataBindings.Add (binding2);
625 Assert.AreEqual ("non-null", c.Tag, "#A1");
626 Assert.AreEqual (101, c.Width, "#A2");
630 public void FormattingEnabledTest ()
632 Control c = new Control ();
633 c.BindingContext = new BindingContext ();
636 MockItem item = new MockItem ();
638 Binding binding = new Binding ("Text", item, "Value");
639 binding.FormattingEnabled = true;
640 binding.FormatString = "p";
642 c.DataBindings.Add (binding);
643 Assert.AreEqual ((666).ToString ("p"), c.Text, "#A1");
645 binding.FormatString = "c";
646 Assert.AreEqual ((666).ToString ("c"), c.Text, "#B1");
647 Console.WriteLine (c.Text);
649 binding.FormattingEnabled = false;
650 Assert.AreEqual ((666).ToString (), c.Text, "#C1");
654 public void FormatStringTest ()
656 Binding binding = new Binding ("Text", null, "Text");
657 binding.FormatString = null;
659 Assert.AreEqual (String.Empty, binding.FormatString, "#A1");
665 class ChildMockItem : MockItem
669 public ChildMockItem ()
674 public object ObjectValue
688 class BindableToolStripItem : ToolStripItem, IBindableComponent
690 ControlBindingsCollection data_bindings;
691 BindingContext binding_context;
693 public ControlBindingsCollection DataBindings {
695 if (data_bindings == null)
696 data_bindings = new ControlBindingsCollection (this);
698 return data_bindings;
702 public BindingContext BindingContext
705 return binding_context;
708 binding_context = value;
717 //private global::System.Collections.Generic.IList<Two> m_twoList
718 // = new global::System.Collections.Generic.List<Two> ();
720 //public global::System.Collections.Generic.IList<Two> TwoList
722 // get { return m_twoList; }
730 get { return m_two; }
731 set { m_two = value; }
735 private MockItem m_MockItem;
737 public MockItem MockItem
739 get { return m_MockItem; }
740 set { m_MockItem = value; }
744 public override string ToString ()
746 return "!!! ToString on One !!!";
752 //private global::System.Collections.Generic.IList<MockItem> m_MockItemList
753 // = new global::System.Collections.Generic.List<MockItem> ();
755 //public global::System.Collections.Generic.IList<MockItem> MockItemList
757 // get { return m_MockItemList; }
761 private MockItem m_MockItem;
763 public MockItem MockItem
765 get { return m_MockItem; }
766 set { m_MockItem = value; }
769 private Three m_Three;
773 get { return m_Three; }
774 set { m_Three = value; }
777 public override string ToString ()
779 return "!!! ToString on Two !!!";
785 private MockItem m_MockItem;
787 public MockItem MockItem
789 get { return m_MockItem; }
790 set { m_MockItem = value; }
793 public override string ToString ()
795 return "!!! ToString on Three !!!";