2006-12-19 Daniel Nauck <dna@mono-project.de>
[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 {
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                 /* create control and set binding context */
77                 public void BindingContextChangedTest ()
78                 {
79                         Control c = new Control ();
80                         // Test BindingContextChanged Event
81                         c.BindingContextChanged += new EventHandler (Event_Handler1);
82                         BindingContext bcG1 = new BindingContext ();
83                         eventcount = 0;
84                         c.BindingContext = bcG1;
85                         Assert.AreEqual (1, eventcount, "A1");
86                 }
87
88                 [Test]
89                 [Category ("NotWorking")]
90                 /* create control and show control */
91                 public void BindingContextChangedTest2 ()
92                 {
93                         Form f = new Form ();
94                         f.ShowInTaskbar = false;
95                         Control c = new Control ();
96                         f.Controls.Add (c);
97
98                         c.BindingContextChanged += new EventHandler (Event_Handler1);
99                         eventcount = 0;
100                         f.Show ();
101                         Assert.AreEqual (2, eventcount, "A1");
102                         f.Dispose();
103                 }
104
105                 [Test]
106                 /* create control, set binding context, and show control */
107                 public void BindingContextChangedTest3 ()
108                 {
109                         Form f = new Form ();
110                         f.ShowInTaskbar = false;
111
112                         Control c = new Control ();
113                         f.Controls.Add (c);
114
115                         c.BindingContextChanged += new EventHandler (Event_Handler1);
116                         eventcount = 0;
117                         c.BindingContext = new BindingContext ();;
118                         f.Show ();
119                         Assert.AreEqual (1, eventcount, "A1");
120                         f.Dispose ();
121                 }
122
123                 int eventcount;
124                 public void Event_Handler1 (object sender, EventArgs e)
125                 {
126                         Console.WriteLine (Environment.StackTrace);
127                         eventcount++;
128                 }
129
130                 [Test]
131                 public void DataBindingCountTest1 ()
132                 {
133                         Control c = new Control ();
134                         Assert.AreEqual (0, c.DataBindings.Count, "1");
135                         c.DataBindings.Add (new Binding ("Text", c, "Name"));
136                         Assert.AreEqual (1, c.DataBindings.Count, "2");
137
138                         Binding b = c.DataBindings[0];
139                         Assert.AreEqual (c, b.Control, "3");
140                         Assert.AreEqual (c, b.DataSource, "4");
141                         Assert.AreEqual ("Text", b.PropertyName, "5");
142                         Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "6");
143                 }
144
145                 [Test]
146                 public void DataBindingCountTest2 ()
147                 {
148                         Control c = new Control ();
149                         Control c2 = new Control ();
150                         Assert.AreEqual (0, c.DataBindings.Count, "1");
151                         c.DataBindings.Add (new Binding ("Text", c2, "Name"));
152                         Assert.AreEqual (1, c.DataBindings.Count, "2");
153                         Assert.AreEqual (0, c2.DataBindings.Count, "3");
154
155                         Binding b = c.DataBindings[0];
156                         Assert.AreEqual (c, b.Control, "4");
157                         Assert.AreEqual (c2, b.DataSource, "5");
158                         Assert.AreEqual ("Text", b.PropertyName, "6");
159                         Assert.AreEqual ("Name", b.BindingMemberInfo.BindingField, "7");
160                 }
161         }
162
163 }
164