2008-02-11 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingTest.cs
index 80b858d6890f8c6143d1f14628180626846ee9e8..09d95b31f157cffc4529786dd8c1768eb4e02ea8 100644 (file)
@@ -235,6 +235,199 @@ namespace MonoTests.System.Windows.Forms.DataBinding {
 
                        Assert.AreEqual (DBNull.Value, c.Tag, "1");
                }
+
+               // For this case to work, the data source property needs
+               // to have an associated 'PropertyChanged' event.
+               [Test]
+               public void DataSourcePropertyChanged ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       MockItem item = new MockItem ("A", 0);
+                       Binding binding = new Binding ("Text", item, "Text");
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual ("A", c.Text, "#A1");
+
+                       item.Text = "B";
+                       Assert.AreEqual ("B", c.Text, "#B1");
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void IsBindingTest ()
+               {
+                       MockItem [] items = new MockItem [] { new MockItem ("A", 0) };
+                       Binding binding = new Binding ("Text", items, "Text");
+                       Binding binding2 = new Binding ("Text", items [0], "Text");
+                       Assert.IsFalse (binding.IsBinding, "#A1");
+                       Assert.IsFalse (binding2.IsBinding, "#A2");
+
+                       Control c = new Control ();
+                       Control c2 = new Control ();
+                       c.DataBindings.Add (binding);
+                       c2.DataBindings.Add (binding2);
+                       Assert.IsFalse (binding.IsBinding, "#B1");
+                       Assert.IsFalse (binding2.IsBinding, "#B2");
+
+                       c.CreateControl ();
+                       c2.CreateControl ();
+                       Assert.IsFalse (binding.IsBinding, "#C1");
+                       Assert.IsFalse (binding2.IsBinding, "#C2");
+
+                       Form form = new Form ();
+                       form.ShowInTaskbar = false;
+                       form.Controls.Add (c);
+                       form.Controls.Add (c2);
+                       Assert.IsTrue (binding.IsBinding, "#D1");
+                       Assert.IsTrue (binding2.IsBinding, "#D2");
+
+                       form.Show ();
+
+                       // Important part -
+                       // IsBinding is true ALWAYS with PropertyManager, even when
+                       // ResumeBinding has been called
+                       //
+                       CurrencyManager curr_manager = (CurrencyManager)form.BindingContext [items];
+                       PropertyManager prop_manager = (PropertyManager)form.BindingContext [items [0]];
+                       curr_manager.SuspendBinding ();
+                       prop_manager.SuspendBinding ();
+                       //Assert.IsFalse (binding.IsBinding, "#E1"); // Comment by now
+                       Assert.IsTrue (binding2.IsBinding, "#E2");
+
+                       curr_manager.ResumeBinding ();
+                       prop_manager.ResumeBinding ();
+                       Assert.IsTrue (binding.IsBinding, "#F1");
+                       Assert.IsTrue (binding2.IsBinding, "#F2");
+
+                       form.Dispose ();
+               }
+
+#if NET_2_0
+               [Test]
+               public void ReadValueTest ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       ChildMockItem item = new ChildMockItem ();
+                       item.ObjectValue = "A";
+                       Binding binding = new Binding ("Tag", item, "ObjectValue");
+                       binding.ControlUpdateMode = ControlUpdateMode.Never;
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual (null, c.Tag, "#A1");
+
+                       item.ObjectValue = "B";
+                       Assert.AreEqual (null, c.Tag, "#B1");
+
+                       binding.ReadValue ();
+                       Assert.AreEqual ("B", c.Tag, "#C1");
+
+                       item.ObjectValue = "C";
+                       binding.ReadValue ();
+                       Assert.AreEqual ("C", c.Tag, "#D1");
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void WriteValueTest ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       MockItem item = new MockItem ();
+                       item.Text = "A";
+                       Binding binding = new Binding ("Text", item, "Text");
+                       binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual ("A", c.Text, "#A1");
+
+                       c.Text = "B";
+                       Assert.AreEqual ("A", item.Text, "#B1");
+
+                       binding.WriteValue ();
+                       Assert.AreEqual ("B", item.Text, "#C1");
+               }
+
+               [Test]
+               public void ControlUpdateModeTest ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       MockItem item = new MockItem ("A", 0);
+                       Binding binding = new Binding ("Text", item, "Text");
+                       binding.ControlUpdateMode = ControlUpdateMode.Never;
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual (String.Empty, c.Text, "#A1");
+
+                       item.Text = "B";
+                       Assert.AreEqual (String.Empty, c.Text, "#B1");
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void DataSourceUpdateModeTest ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       MockItem item = new MockItem ("A", 0);
+                       Binding binding = new Binding ("Text", item, "Text");
+                       binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual ("A", c.Text, "#A1");
+
+                       c.Text = "B";
+                       Assert.AreEqual ("A", item.Text, "#B1");
+
+                       binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
+                       Assert.AreEqual ("A", item.Text, "#C1");
+
+                       c.Text = "C";
+                       Assert.AreEqual ("C", item.Text, "#D1");
+
+                       // This requires a Validation even, which we can't test
+                       // by directly modifying the property
+                       binding.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
+
+                       c.Text = "D";
+                       Assert.AreEqual ("C", item.Text, "#E1");
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void DataSourceNullValueTest ()
+               {
+                       Control c = new Control ();
+                       c.BindingContext = new BindingContext ();
+                       c.CreateControl ();
+
+                       ChildMockItem item = new ChildMockItem ();
+                       item.ObjectValue = "A";
+                       Binding binding = new Binding ("Tag", item, "ObjectValue");
+                       binding.DataSourceNullValue = "NonNull";
+
+                       c.DataBindings.Add (binding);
+                       Assert.AreEqual (c.Tag, "A", "#A1");
+
+                       // Since Tag property doesn't have a 
+                       // TagChanged event, we need to force an update
+                       c.Tag = null;
+                       binding.WriteValue ();
+                       Assert.AreEqual (item.ObjectValue, "NonNull", "#B1");
+               }
+#endif
        }
 
        class ChildMockItem : MockItem