2008-02-11 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingTest.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper  jackson@ximian.com
24
25
26 using System;
27 using System.Data;
28 using System.Collections;
29 using System.Windows.Forms;
30
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Windows.Forms.DataBinding {
34
35         [TestFixture]
36         public class BindingTest {
37
38                 [SetUp]
39                 protected virtual void SetUp ()
40                 {
41                 }
42
43                 [TearDown]
44                 protected virtual void TearDown ()
45                 {
46                 }
47
48                 [Test]
49                 public void CtorTest ()
50                 {
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);
55
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");
61
62                         Assert.AreSame (b.PropertyName, prop, "ctor5");
63                         Assert.AreSame (b.DataSource, data_source, "ctor6");
64                 }
65
66                 [Test]
67                 public void CtorNullTest ()
68                 {
69                         Binding b = new Binding (null, null, null);
70
71                         Assert.IsNull (b.PropertyName, "ctornull1");
72                         Assert.IsNull (b.DataSource, "ctornull2");
73                 }
74
75                 [Test]
76                 public void BindingManagerBaseTest ()
77                 {
78                         if (TestHelper.RunningOnUnix) {
79                                 Assert.Ignore ("Fails at the moment");
80                         }
81
82                         Control c1 = new Control ();
83                         Control c2 = new Control ();
84                         Binding binding;
85
86                         c1.BindingContext = new BindingContext ();
87                         c2.BindingContext = c1.BindingContext;
88
89                         binding = c2.DataBindings.Add ("Text", c1, "Text");
90
91                         Assert.IsNull (binding.BindingManagerBase, "1");
92
93                         c1.CreateControl ();
94                         c2.CreateControl ();
95
96                         Assert.IsNull (binding.BindingManagerBase, "2");
97
98                         c2.DataBindings.Remove (binding);
99                         binding = c2.DataBindings.Add ("Text", c1, "Text");
100
101                         Assert.IsTrue (binding.BindingManagerBase != null, "3");
102                 }
103
104                 [Test]
105                 /* create control and set binding context */
106                 public void BindingContextChangedTest ()
107                 {
108                         Control c = new Control ();
109                         // Test BindingContextChanged Event
110                         c.BindingContextChanged += new EventHandler (Event_Handler1);
111                         BindingContext bcG1 = new BindingContext ();
112                         eventcount = 0;
113                         c.BindingContext = bcG1;
114                         Assert.AreEqual (1, eventcount, "A1");
115                 }
116
117                 [Test]
118                 /* create control and show control */
119                 public void BindingContextChangedTest2 ()
120                 {
121                         Form f = new Form ();
122                         f.ShowInTaskbar = false;
123                         Control c = new Control ();
124                         f.Controls.Add (c);
125
126                         c.BindingContextChanged += new EventHandler (Event_Handler1);
127                         eventcount = 0;
128                         f.Show ();
129 #if NET_2_0
130                         Assert.AreEqual (1, eventcount, "A1");
131 #else
132                         Assert.AreEqual (2, eventcount, "A1");
133 #endif
134                         f.Dispose();
135                 }
136
137                 [Test]
138                 /* create control, set binding context, and show control */
139                 public void BindingContextChangedTest3 ()
140                 {
141                         Form f = new Form ();
142                         f.ShowInTaskbar = false;
143
144                         Control c = new Control ();
145                         f.Controls.Add (c);
146
147                         c.BindingContextChanged += new EventHandler (Event_Handler1);
148                         eventcount = 0;
149                         c.BindingContext = new BindingContext ();;
150                         f.Show ();
151                         Assert.AreEqual (1, eventcount, "A1");
152                         f.Dispose ();
153                 }
154
155                 [Test]
156                 public void BindingContextChangedTest4 ()
157                 {
158                         Form f = new Form ();
159                         f.ShowInTaskbar = false;
160
161                         ContainerControl cc = new ContainerControl ();
162
163                         Control c = new Control ();
164                         f.Controls.Add (cc);
165                         cc.Controls.Add (c);
166
167                         c.BindingContextChanged += new EventHandler (Event_Handler1);
168                         cc.BindingContextChanged += new EventHandler (Event_Handler1);
169                         f.BindingContextChanged += new EventHandler (Event_Handler1);
170
171                         eventcount = 0;
172                         f.Show ();
173 #if NET_2_0
174                         Assert.AreEqual (5, eventcount, "A1");
175 #else
176                         Assert.AreEqual (8, eventcount, "A1");
177 #endif
178                         f.Dispose ();
179                 }
180
181                 int eventcount;
182                 public void Event_Handler1 (object sender, EventArgs e)
183                 {
184                         //Console.WriteLine (sender.GetType());
185                         //Console.WriteLine (Environment.StackTrace);
186                         eventcount++;
187                 }
188
189                 [Test]
190                 public void DataBindingCountTest1 ()
191                 {
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");
196
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");
202                 }
203
204                 [Test]
205                 public void DataBindingCountTest2 ()
206                 {
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");
213
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");
219                 }
220
221                 [Test]
222                 public void DataSourceNullTest ()
223                 {
224                         ChildMockItem item = new ChildMockItem ();
225                         Control c = new Control ();
226                         c.Tag = null;
227                         item.ObjectValue = null;
228
229                         c.DataBindings.Add ("Tag", item, "ObjectValue");
230
231                         Form f = new Form ();
232                         f.Controls.Add (c);
233
234                         f.Show (); // Need this to init data binding
235
236                         Assert.AreEqual (DBNull.Value, c.Tag, "1");
237                 }
238
239                 // For this case to work, the data source property needs
240                 // to have an associated 'PropertyChanged' event.
241                 [Test]
242                 public void DataSourcePropertyChanged ()
243                 {
244                         Control c = new Control ();
245                         c.BindingContext = new BindingContext ();
246                         c.CreateControl ();
247
248                         MockItem item = new MockItem ("A", 0);
249                         Binding binding = new Binding ("Text", item, "Text");
250
251                         c.DataBindings.Add (binding);
252                         Assert.AreEqual ("A", c.Text, "#A1");
253
254                         item.Text = "B";
255                         Assert.AreEqual ("B", c.Text, "#B1");
256                 }
257
258                 [Test]
259                 [Category ("NotWorking")]
260                 public void IsBindingTest ()
261                 {
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");
267
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");
274
275                         c.CreateControl ();
276                         c2.CreateControl ();
277                         Assert.IsFalse (binding.IsBinding, "#C1");
278                         Assert.IsFalse (binding2.IsBinding, "#C2");
279
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");
286
287                         form.Show ();
288
289                         // Important part -
290                         // IsBinding is true ALWAYS with PropertyManager, even when
291                         // ResumeBinding has been called
292                         //
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");
299
300                         curr_manager.ResumeBinding ();
301                         prop_manager.ResumeBinding ();
302                         Assert.IsTrue (binding.IsBinding, "#F1");
303                         Assert.IsTrue (binding2.IsBinding, "#F2");
304
305                         form.Dispose ();
306                 }
307
308 #if NET_2_0
309                 [Test]
310                 public void ReadValueTest ()
311                 {
312                         Control c = new Control ();
313                         c.BindingContext = new BindingContext ();
314                         c.CreateControl ();
315
316                         ChildMockItem item = new ChildMockItem ();
317                         item.ObjectValue = "A";
318                         Binding binding = new Binding ("Tag", item, "ObjectValue");
319                         binding.ControlUpdateMode = ControlUpdateMode.Never;
320
321                         c.DataBindings.Add (binding);
322                         Assert.AreEqual (null, c.Tag, "#A1");
323
324                         item.ObjectValue = "B";
325                         Assert.AreEqual (null, c.Tag, "#B1");
326
327                         binding.ReadValue ();
328                         Assert.AreEqual ("B", c.Tag, "#C1");
329
330                         item.ObjectValue = "C";
331                         binding.ReadValue ();
332                         Assert.AreEqual ("C", c.Tag, "#D1");
333                 }
334
335                 [Test]
336                 [Category ("NotWorking")]
337                 public void WriteValueTest ()
338                 {
339                         Control c = new Control ();
340                         c.BindingContext = new BindingContext ();
341                         c.CreateControl ();
342
343                         MockItem item = new MockItem ();
344                         item.Text = "A";
345                         Binding binding = new Binding ("Text", item, "Text");
346                         binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
347
348                         c.DataBindings.Add (binding);
349                         Assert.AreEqual ("A", c.Text, "#A1");
350
351                         c.Text = "B";
352                         Assert.AreEqual ("A", item.Text, "#B1");
353
354                         binding.WriteValue ();
355                         Assert.AreEqual ("B", item.Text, "#C1");
356                 }
357
358                 [Test]
359                 public void ControlUpdateModeTest ()
360                 {
361                         Control c = new Control ();
362                         c.BindingContext = new BindingContext ();
363                         c.CreateControl ();
364
365                         MockItem item = new MockItem ("A", 0);
366                         Binding binding = new Binding ("Text", item, "Text");
367                         binding.ControlUpdateMode = ControlUpdateMode.Never;
368
369                         c.DataBindings.Add (binding);
370                         Assert.AreEqual (String.Empty, c.Text, "#A1");
371
372                         item.Text = "B";
373                         Assert.AreEqual (String.Empty, c.Text, "#B1");
374                 }
375
376                 [Test]
377                 [Category ("NotWorking")]
378                 public void DataSourceUpdateModeTest ()
379                 {
380                         Control c = new Control ();
381                         c.BindingContext = new BindingContext ();
382                         c.CreateControl ();
383
384                         MockItem item = new MockItem ("A", 0);
385                         Binding binding = new Binding ("Text", item, "Text");
386                         binding.DataSourceUpdateMode = DataSourceUpdateMode.Never;
387
388                         c.DataBindings.Add (binding);
389                         Assert.AreEqual ("A", c.Text, "#A1");
390
391                         c.Text = "B";
392                         Assert.AreEqual ("A", item.Text, "#B1");
393
394                         binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
395                         Assert.AreEqual ("A", item.Text, "#C1");
396
397                         c.Text = "C";
398                         Assert.AreEqual ("C", item.Text, "#D1");
399
400                         // This requires a Validation even, which we can't test
401                         // by directly modifying the property
402                         binding.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
403
404                         c.Text = "D";
405                         Assert.AreEqual ("C", item.Text, "#E1");
406                 }
407
408                 [Test]
409                 [Category ("NotWorking")]
410                 public void DataSourceNullValueTest ()
411                 {
412                         Control c = new Control ();
413                         c.BindingContext = new BindingContext ();
414                         c.CreateControl ();
415
416                         ChildMockItem item = new ChildMockItem ();
417                         item.ObjectValue = "A";
418                         Binding binding = new Binding ("Tag", item, "ObjectValue");
419                         binding.DataSourceNullValue = "NonNull";
420
421                         c.DataBindings.Add (binding);
422                         Assert.AreEqual (c.Tag, "A", "#A1");
423
424                         // Since Tag property doesn't have a 
425                         // TagChanged event, we need to force an update
426                         c.Tag = null;
427                         binding.WriteValue ();
428                         Assert.AreEqual (item.ObjectValue, "NonNull", "#B1");
429                 }
430 #endif
431         }
432
433         class ChildMockItem : MockItem
434         {
435                 object value;
436
437                 public ChildMockItem ()
438                         : base (null, 0)
439                 {
440                 }
441
442                 public object ObjectValue
443                 {
444                         get
445                         {
446                                 return value;
447                         }
448                         set
449                         {
450                                 this.value = value;
451                         }
452                 }
453         }
454
455 }
456