BindingsCollectionTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <[email protected]>
  24. //
  25. using System;
  26. using System.ComponentModel;
  27. using System.Windows.Forms;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Windows.Forms.DataBinding {
  30. [TestFixture]
  31. public class BindingsCollectionTest : TestHelper
  32. {
  33. #if NET_2_0
  34. //
  35. // CollectionChanging event test section
  36. //
  37. bool collection_changing_called;
  38. int collection_expected_count;
  39. string collection_expected_assert;
  40. CollectionChangeAction collection_action_expected;
  41. object collection_element_expected;
  42. void CollectionChangingHandler (object o, CollectionChangeEventArgs args)
  43. {
  44. BindingsCollection coll = (BindingsCollection)o;
  45. collection_changing_called = true;
  46. Assert.AreEqual (collection_expected_count, coll.Count, collection_expected_assert + "-0");
  47. Assert.AreEqual (collection_action_expected, args.Action, collection_expected_assert + "-1");
  48. Assert.AreEqual (collection_element_expected, args.Element, collection_expected_assert + "-2");
  49. }
  50. [Test]
  51. public void CollectionChangingTest ()
  52. {
  53. Control c = new Control ();
  54. c.BindingContext = new BindingContext ();
  55. c.CreateControl ();
  56. ControlBindingsCollection binding_coll = c.DataBindings;
  57. Binding binding = new Binding ("Text", new MockItem ("A", 0), "Text");
  58. Binding binding2 = new Binding ("Name", new MockItem ("B", 0), "Text");
  59. binding_coll.Add (binding);
  60. binding_coll.CollectionChanging += CollectionChangingHandler;
  61. collection_expected_count = 1;
  62. collection_action_expected = CollectionChangeAction.Add;
  63. collection_element_expected = binding2;
  64. collection_expected_assert = "#A0";
  65. binding_coll.Add (binding2);
  66. Assert.IsTrue (collection_changing_called, "#A1");
  67. collection_changing_called = false;
  68. collection_expected_count = 2;
  69. collection_action_expected = CollectionChangeAction.Remove;
  70. collection_element_expected = binding;
  71. collection_expected_assert = "#B0";
  72. binding_coll.Remove (binding);
  73. Assert.IsTrue (collection_changing_called, "#B1");
  74. collection_changing_called = false;
  75. collection_expected_count = 1;
  76. collection_element_expected = null;
  77. collection_action_expected = CollectionChangeAction.Refresh;
  78. collection_expected_assert = "#C0";
  79. binding_coll.Clear ();
  80. Assert.IsTrue (collection_changing_called, "#C1");
  81. }
  82. #endif
  83. }
  84. }