Merge pull request #217 from QuickJack/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / PropertyManagerTest.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 //      Chris Toshok    toshok@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 PropertyManagerTest : TestHelper {
37
38                 class TestClass1 {
39                         int prop;
40                         public TestClass1 ()
41                         {
42                                 prop = 0;
43                         }
44
45                         public int Property {
46                                 get { return prop; }
47                                 set {
48                                         prop = value;
49                                         if (PropertyChanged != null)
50                                                 PropertyChanged (this, EventArgs.Empty);
51                                 }
52                         }
53
54                         public event EventHandler PropertyChanged;
55                 }
56
57                 class TestClass2 {
58                         int prop;
59                         public TestClass2 ()
60                         {
61                                 prop = 0;
62                         }
63
64                         public int Property {
65                                 get { return prop; }
66                                 set { prop = value; }
67                         }
68                 }
69
70                 class TestClass3 {
71                         int prop;
72                         public TestClass3 ()
73                         {
74                                 prop = 0;
75                         }
76
77                         public int Property {
78                                 get { return prop; }
79                                 set {
80                                         prop = value;
81                                         if (Changed != null)
82                                                 Changed (this, EventArgs.Empty);
83                                 }
84                         }
85
86                         public event EventHandler Changed;
87                 }
88
89                 bool currentChangedRaised;
90                 bool positionChangedRaised;
91
92                 void OnCurrentChanged (object sender, EventArgs args)
93                 {
94                         currentChangedRaised = true;
95                 }
96
97                 //void OnPositionChanged (object sender, EventArgs args)
98                 //{
99                 //        positionChangedRaised = true;
100                 //}
101
102                 [Test]
103                 public void TestEvent ()
104                 {
105                         TestClass1 test = new TestClass1();
106                         BindingContext bc = new BindingContext ();
107
108                         BindingManagerBase bm = bc[test, "Property"];
109                         Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
110
111                         bm.CurrentChanged += new EventHandler (OnCurrentChanged);
112
113                         currentChangedRaised = positionChangedRaised = false;
114                         test.Property = 5;
115                         Assert.IsTrue (currentChangedRaised, "A2");
116                         Assert.IsFalse (positionChangedRaised, "A3");
117                 }
118
119                 [Test]
120                 public void TestNoEvent ()
121                 {
122                         TestClass2 test = new TestClass2();
123                         BindingContext bc = new BindingContext ();
124
125                         BindingManagerBase bm = bc[test, "Property"];
126                         Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
127
128                         bm.CurrentChanged += new EventHandler (OnCurrentChanged);
129
130                         currentChangedRaised = positionChangedRaised = false;
131                         test.Property = 5;
132                         Assert.IsFalse (currentChangedRaised, "A2");
133                         Assert.IsFalse (positionChangedRaised, "A3");
134                 }
135
136                 [Test]
137                 public void TestMemberEvent ()
138                 {
139                         TestClass1 test = new TestClass1();
140                         BindingContext bc = new BindingContext ();
141
142                         BindingManagerBase bm = bc[test];
143                         Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
144
145                         bm.CurrentChanged += new EventHandler (OnCurrentChanged);
146
147                         currentChangedRaised = positionChangedRaised = false;
148                         test.Property = 5;
149                         Assert.IsFalse (currentChangedRaised, "A2");
150                         Assert.IsFalse (positionChangedRaised, "A3");
151                 }
152
153                 [Test]
154                 public void TestMemberEvent2 ()
155                 {
156                         TestClass3 test = new TestClass3();
157                         BindingContext bc = new BindingContext ();
158
159                         BindingManagerBase bm = bc[test];
160                         Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
161
162                         bm.CurrentChanged += new EventHandler (OnCurrentChanged);
163
164                         currentChangedRaised = positionChangedRaised = false;
165                         test.Property = 5;
166                         Assert.IsFalse (currentChangedRaised, "A2");
167                         Assert.IsFalse (positionChangedRaised, "A3");
168                 }
169
170                 [Test]
171                 public void TestMemberNoEvent ()
172                 {
173                         TestClass2 test = new TestClass2();
174                         BindingContext bc = new BindingContext ();
175
176                         BindingManagerBase bm = bc[test];
177                         Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
178
179                         bm.CurrentChanged += new EventHandler (OnCurrentChanged);
180
181                         currentChangedRaised = positionChangedRaised = false;
182                         test.Property = 5;
183                         Assert.IsFalse (currentChangedRaised, "A2");
184                         Assert.IsFalse (positionChangedRaised, "A3");
185                 }
186
187         }
188
189 }