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) 2008 Novell, Inc.
23 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
28 using System.ComponentModel;
29 using System.Windows.Forms;
31 using NUnit.Framework;
33 namespace MonoTests.System.Windows.Forms.DataBinding {
36 public class BindingsCollectionTest : TestHelper
40 // CollectionChanging event test section
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;
48 void CollectionChangingHandler (object o, CollectionChangeEventArgs args)
50 BindingsCollection coll = (BindingsCollection)o;
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");
59 public void CollectionChangingTest ()
61 Control c = new Control ();
62 c.BindingContext = new BindingContext ();
65 ControlBindingsCollection binding_coll = c.DataBindings;
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);
71 binding_coll.CollectionChanging += CollectionChangingHandler;
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");
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");
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");