Merge pull request #325 from adbre/iss5464
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / BindingsCollectionTest.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) 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
24 //
25
26
27 using System;
28 using System.ComponentModel;
29 using System.Windows.Forms;
30
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Windows.Forms.DataBinding {
34
35         [TestFixture]
36         public class BindingsCollectionTest : TestHelper 
37         {
38 #if NET_2_0
39                 // 
40                 // CollectionChanging event test section
41                 //
42                 bool collection_changing_called;
43                 int collection_expected_count;
44                 string collection_expected_assert;
45                 CollectionChangeAction collection_action_expected;
46                 object collection_element_expected;
47
48                 void CollectionChangingHandler (object o, CollectionChangeEventArgs args)
49                 {
50                         BindingsCollection coll = (BindingsCollection)o;
51
52                         collection_changing_called = true;
53                         Assert.AreEqual (collection_expected_count, coll.Count, collection_expected_assert + "-0");
54                         Assert.AreEqual (collection_action_expected, args.Action, collection_expected_assert + "-1");
55                         Assert.AreEqual (collection_element_expected, args.Element, collection_expected_assert + "-2");
56                 }
57
58                 [Test]
59                 public void CollectionChangingTest ()
60                 {
61                         Control c = new Control ();
62                         c.BindingContext = new BindingContext ();
63                         c.CreateControl ();
64
65                         ControlBindingsCollection binding_coll = c.DataBindings;
66
67                         Binding binding = new Binding ("Text", new MockItem ("A", 0), "Text");
68                         Binding binding2 = new Binding ("Name", new MockItem ("B", 0), "Text");
69                         binding_coll.Add (binding);
70
71                         binding_coll.CollectionChanging += CollectionChangingHandler;
72
73                         collection_expected_count = 1;
74                         collection_action_expected = CollectionChangeAction.Add;
75                         collection_element_expected = binding2;
76                         collection_expected_assert = "#A0";
77                         binding_coll.Add (binding2);
78                         Assert.IsTrue (collection_changing_called, "#A1");
79
80                         collection_changing_called = false;
81                         collection_expected_count = 2;
82                         collection_action_expected = CollectionChangeAction.Remove;
83                         collection_element_expected = binding;
84                         collection_expected_assert = "#B0";
85                         binding_coll.Remove (binding);
86                         Assert.IsTrue (collection_changing_called, "#B1");
87
88                         collection_changing_called = false;
89                         collection_expected_count = 1;
90                         collection_element_expected = null;
91                         collection_action_expected = CollectionChangeAction.Refresh;
92                         collection_expected_assert = "#C0";
93                         binding_coll.Clear ();
94                         Assert.IsTrue (collection_changing_called, "#C1");
95                 }
96 #endif
97         }
98 }
99