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.Windows.Forms;
31 using NUnit.Framework;
33 namespace MonoTests.System.Windows.Forms.DataBinding {
36 public class BindingTest {
39 protected virtual void SetUp ()
44 protected virtual void TearDown ()
49 public void CtorTest ()
51 string prop = "PROPERTY NAME";
52 object data_source = new object ();
53 string data_member = "DATA MEMBER";
54 Binding b = new Binding (prop, data_source, data_member);
56 Assert.IsNull (b.BindingManagerBase, "ctor1");
57 Console.WriteLine ("MEMBER INFO: " + b.BindingMemberInfo);
58 Assert.IsNotNull (b.BindingMemberInfo, "ctor2");
59 Assert.IsNull (b.Control, "ctor3");
60 Assert.IsFalse (b.IsBinding, "ctor4");
62 Assert.AreSame (b.PropertyName, prop, "ctor5");
63 Assert.AreSame (b.DataSource, data_source, "ctor6");
67 public void CtorNullTest ()
69 Binding b = new Binding (null, null, null);
71 Assert.IsNull (b.PropertyName, "ctornull1");
72 Assert.IsNull (b.DataSource, "ctornull2");
76 public void BindingManagerBaseTest ()
78 if (TestHelper.RunningOnUnix) {
79 Assert.Ignore ("Fails at the moment");
82 Control c1 = new Control ();
83 Control c2 = new Control ();
86 c1.BindingContext = new BindingContext ();
87 c2.BindingContext = c1.BindingContext;
89 binding = c2.DataBindings.Add ("Text", c1, "Text");
91 Assert.IsNull (binding.BindingManagerBase, "1");
96 Assert.IsNull (binding.BindingManagerBase, "2");
98 c2.DataBindings.Remove (binding);
99 binding = c2.DataBindings.Add ("Text", c1, "Text");
101 Assert.IsTrue (binding.BindingManagerBase != null, "3");
105 /* create control and set binding context */
106 public void BindingContextChangedTest ()
108 Control c = new Control ();
109 // Test BindingContextChanged Event
110 c.BindingContextChanged += new EventHandler (Event_Handler1);
111 BindingContext bcG1 = new BindingContext ();
113 c.BindingContext = bcG1;
114 Assert.AreEqual (1, eventcount, "A1");
118 /* create control and show control */
119 public void BindingContextChangedTest2 ()
121 Form f = new Form ();
122 f.ShowInTaskbar = false;
123 Control c = new Control ();
126 c.BindingContextChanged += new EventHandler (Event_Handler1);
130 Assert.AreEqual (1, eventcount, "A1");
132 Assert.AreEqual (2, eventcount, "A1");
138 /* create control, set binding context, and show control */
139 public void BindingContextChangedTest3 ()
141 Form f = new Form ();
142 f.ShowInTaskbar = false;
144 Control c = new Control ();
147 c.BindingContextChanged += new EventHandler (Event_Handler1);
149 c.BindingContext = new BindingContext ();;
151 Assert.AreEqual (1, eventcount, "A1");
156 public void BindingContextChangedTest4 ()
158 Form f = new Form ();
159 f.ShowInTaskbar = false;
161 ContainerControl cc = new ContainerControl ();
163 Control c = new Control ();
167 c.BindingContextChanged += new EventHandler (Event_Handler1);
168 cc.BindingContextChanged += new EventHandler (Event_Handler1);
169 f.BindingContextChanged += new EventHandler (Event_Handler1);
174 Assert.AreEqual (5, eventcount, "A1");
176 Assert.AreEqual (8, eventcount, "A1");
182 public void Event_Handler1 (object sender, EventArgs e)
184 //Console.WriteLine (sender.GetType());
185 //Console.WriteLine (Environment.StackTrace);
190 public void DataBindingCountTest1 ()
192 Control c = new Control ();
193 Assert.AreEqual (0, c.DataBindings.Count, "1");
194 c.DataBindings.Add (new Binding ("Text", c, "Name"));
195 Assert.AreEqual (1, c.DataBindings.Count, "2");
197 Binding b = c.DataBindings[0];
198 Assert.AreEqual (c, b.Control, "3");
199 Assert.AreEqual (c, b.DataSource, "4");
200 Assert.AreEqual ("Text", b.PropertyName, "5");
201 Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "6");
205 public void DataBindingCountTest2 ()
207 Control c = new Control ();
208 Control c2 = new Control ();
209 Assert.AreEqual (0, c.DataBindings.Count, "1");
210 c.DataBindings.Add (new Binding ("Text", c2, "Name"));
211 Assert.AreEqual (1, c.DataBindings.Count, "2");
212 Assert.AreEqual (0, c2.DataBindings.Count, "3");
214 Binding b = c.DataBindings[0];
215 Assert.AreEqual (c, b.Control, "4");
216 Assert.AreEqual (c2, b.DataSource, "5");
217 Assert.AreEqual ("Text", b.PropertyName, "6");
218 Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "7");
222 public void DataSourceNullTest ()
224 ChildMockItem item = new ChildMockItem ();
225 Control c = new Control ();
227 item.ObjectValue = null;
229 c.DataBindings.Add ("Tag", item, "ObjectValue");
231 Form f = new Form ();
234 f.Show (); // Need this to init data binding
236 Assert.AreEqual (DBNull.Value, c.Tag, "1");
239 // For this case to work, the data source property needs
240 // to have an associated 'PropertyChanged' event.
242 public void DataSourcePropertyChanged ()
244 Control c = new Control ();
245 c.BindingContext = new BindingContext ();
248 MockItem item = new MockItem ("A", 0);
249 Binding binding = new Binding ("Text", item, "Text");
251 c.DataBindings.Add (binding);
252 Assert.AreEqual ("A", c.Text, "#A1");
255 Assert.AreEqual ("B", c.Text, "#B1");
259 [Category ("NotWorking")]
260 public void IsBindingTest ()
262 MockItem [] items = new MockItem [] { new MockItem ("A", 0) };
263 Binding binding = new Binding ("Text", items, "Text");
264 Binding binding2 = new Binding ("Text", items [0], "Text");
265 Assert.IsFalse (binding.IsBinding, "#A1");
266 Assert.IsFalse (binding2.IsBinding, "#A2");
268 Control c = new Control ();
269 Control c2 = new Control ();
270 c.DataBindings.Add (binding);
271 c2.DataBindings.Add (binding2);
272 Assert.IsFalse (binding.IsBinding, "#B1");
273 Assert.IsFalse (binding2.IsBinding, "#B2");
277 Assert.IsFalse (binding.IsBinding, "#C1");
278 Assert.IsFalse (binding2.IsBinding, "#C2");
280 Form form = new Form ();
281 form.ShowInTaskbar = false;
282 form.Controls.Add (c);
283 form.Controls.Add (c2);
284 Assert.IsTrue (binding.IsBinding, "#D1");
285 Assert.IsTrue (binding2.IsBinding, "#D2");
290 // IsBinding is true ALWAYS with PropertyManager, even when
291 // ResumeBinding has been called
293 CurrencyManager curr_manager = (CurrencyManager)form.BindingContext [items];
294 PropertyManager prop_manager = (PropertyManager)form.BindingContext [items [0]];
295 curr_manager.SuspendBinding ();
296 prop_manager.SuspendBinding ();
297 //Assert.IsFalse (binding.IsBinding, "#E1"); // Comment by now
298 Assert.IsTrue (binding2.IsBinding, "#E2");
300 curr_manager.ResumeBinding ();
301 prop_manager.ResumeBinding ();
302 Assert.IsTrue (binding.IsBinding, "#F1");
303 Assert.IsTrue (binding2.IsBinding, "#F2");
310 public void ReadValueTest ()
312 Control c = new Control ();
313 c.BindingContext = new BindingContext ();
316 ChildMockItem item = new ChildMockItem ();
317 item.ObjectValue = "A";
318 Binding binding = new Binding ("Tag", item, "ObjectValue");
319 binding.ControlUpdateMode = ControlUpdateMode.Never;
321 c.DataBindings.Add (binding);
322 Assert.AreEqual (null, c.Tag, "#A1");
324 item.ObjectValue = "B";
325 Assert.AreEqual (null, c.Tag, "#B1");
327 binding.ReadValue ();
328 Assert.AreEqual ("B", c.Tag, "#C1");
330 item.ObjectValue = "C";
331 binding.ReadValue ();
332 Assert.AreEqual ("C", c.Tag, "#D1");
336 [Category ("NotWorking")]
337 public void WriteValueTest ()
339 Control c = new Control ();
340 c.BindingContext = new BindingContext ();
343 MockItem item = new MockItem ();
345 Binding binding = new Binding ("Text", item, "Text");
346 binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
348 c.DataBindings.Add (binding);
349 Assert.AreEqual ("A", c.Text, "#A1");
352 Assert.AreEqual ("A", item.Text, "#B1");
354 binding.WriteValue ();
355 Assert.AreEqual ("B", item.Text, "#C1");
359 public void ControlUpdateModeTest ()
361 Control c = new Control ();
362 c.BindingContext = new BindingContext ();
365 MockItem item = new MockItem ("A", 0);
366 Binding binding = new Binding ("Text", item, "Text");
367 binding.ControlUpdateMode = ControlUpdateMode.Never;
369 c.DataBindings.Add (binding);
370 Assert.AreEqual (String.Empty, c.Text, "#A1");
373 Assert.AreEqual (String.Empty, c.Text, "#B1");
377 [Category ("NotWorking")]
378 public void DataSourceUpdateModeTest ()
380 Control c = new Control ();
381 c.BindingContext = new BindingContext ();
384 MockItem item = new MockItem ("A", 0);
385 Binding binding = new Binding ("Text", item, "Text");
386 binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
388 c.DataBindings.Add (binding);
389 Assert.AreEqual ("A", c.Text, "#A1");
392 Assert.AreEqual ("A", item.Text, "#B1");
394 binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
395 Assert.AreEqual ("A", item.Text, "#C1");
398 Assert.AreEqual ("C", item.Text, "#D1");
400 // This requires a Validation even, which we can't test
401 // by directly modifying the property
402 binding.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
405 Assert.AreEqual ("C", item.Text, "#E1");
409 [Category ("NotWorking")]
410 public void DataSourceNullValueTest ()
412 Control c = new Control ();
413 c.BindingContext = new BindingContext ();
416 ChildMockItem item = new ChildMockItem ();
417 item.ObjectValue = "A";
418 Binding binding = new Binding ("Tag", item, "ObjectValue");
419 binding.DataSourceNullValue = "NonNull";
421 c.DataBindings.Add (binding);
422 Assert.AreEqual (c.Tag, "A", "#A1");
424 // Since Tag property doesn't have a
425 // TagChanged event, we need to force an update
427 binding.WriteValue ();
428 Assert.AreEqual (item.ObjectValue, "NonNull", "#B1");
433 class ChildMockItem : MockItem
437 public ChildMockItem ()
442 public object ObjectValue