PropertyManagerTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 [email protected]
  24. using System;
  25. using System.Data;
  26. using System.Collections;
  27. using System.Windows.Forms;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Windows.Forms.DataBinding
  30. {
  31. [TestFixture]
  32. public class PropertyManagerTest : TestHelper {
  33. class TestClass1 {
  34. int prop;
  35. public TestClass1 ()
  36. {
  37. prop = 0;
  38. }
  39. public int Property {
  40. get { return prop; }
  41. set {
  42. prop = value;
  43. if (PropertyChanged != null)
  44. PropertyChanged (this, EventArgs.Empty);
  45. }
  46. }
  47. public event EventHandler PropertyChanged;
  48. }
  49. class TestClass2 {
  50. int prop;
  51. public TestClass2 ()
  52. {
  53. prop = 0;
  54. }
  55. public int Property {
  56. get { return prop; }
  57. set { prop = value; }
  58. }
  59. }
  60. class TestClass3 {
  61. int prop;
  62. public TestClass3 ()
  63. {
  64. prop = 0;
  65. }
  66. public int Property {
  67. get { return prop; }
  68. set {
  69. prop = value;
  70. if (Changed != null)
  71. Changed (this, EventArgs.Empty);
  72. }
  73. }
  74. public event EventHandler Changed;
  75. }
  76. bool currentChangedRaised;
  77. bool positionChangedRaised;
  78. void OnCurrentChanged (object sender, EventArgs args)
  79. {
  80. currentChangedRaised = true;
  81. }
  82. //void OnPositionChanged (object sender, EventArgs args)
  83. //{
  84. // positionChangedRaised = true;
  85. //}
  86. [Test]
  87. public void TestEvent ()
  88. {
  89. TestClass1 test = new TestClass1();
  90. BindingContext bc = new BindingContext ();
  91. BindingManagerBase bm = bc[test, "Property"];
  92. Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
  93. bm.CurrentChanged += new EventHandler (OnCurrentChanged);
  94. currentChangedRaised = positionChangedRaised = false;
  95. test.Property = 5;
  96. Assert.IsTrue (currentChangedRaised, "A2");
  97. Assert.IsFalse (positionChangedRaised, "A3");
  98. }
  99. [Test]
  100. public void TestNoEvent ()
  101. {
  102. TestClass2 test = new TestClass2();
  103. BindingContext bc = new BindingContext ();
  104. BindingManagerBase bm = bc[test, "Property"];
  105. Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
  106. bm.CurrentChanged += new EventHandler (OnCurrentChanged);
  107. currentChangedRaised = positionChangedRaised = false;
  108. test.Property = 5;
  109. Assert.IsFalse (currentChangedRaised, "A2");
  110. Assert.IsFalse (positionChangedRaised, "A3");
  111. }
  112. [Test]
  113. public void TestMemberEvent ()
  114. {
  115. TestClass1 test = new TestClass1();
  116. BindingContext bc = new BindingContext ();
  117. BindingManagerBase bm = bc[test];
  118. Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
  119. bm.CurrentChanged += new EventHandler (OnCurrentChanged);
  120. currentChangedRaised = positionChangedRaised = false;
  121. test.Property = 5;
  122. Assert.IsFalse (currentChangedRaised, "A2");
  123. Assert.IsFalse (positionChangedRaised, "A3");
  124. }
  125. [Test]
  126. public void TestMemberEvent2 ()
  127. {
  128. TestClass3 test = new TestClass3();
  129. BindingContext bc = new BindingContext ();
  130. BindingManagerBase bm = bc[test];
  131. Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
  132. bm.CurrentChanged += new EventHandler (OnCurrentChanged);
  133. currentChangedRaised = positionChangedRaised = false;
  134. test.Property = 5;
  135. Assert.IsFalse (currentChangedRaised, "A2");
  136. Assert.IsFalse (positionChangedRaised, "A3");
  137. }
  138. [Test]
  139. public void TestMemberNoEvent ()
  140. {
  141. TestClass2 test = new TestClass2();
  142. BindingContext bc = new BindingContext ();
  143. BindingManagerBase bm = bc[test];
  144. Assert.IsTrue (typeof (PropertyManager).IsAssignableFrom (bm.GetType()), "A1");
  145. bm.CurrentChanged += new EventHandler (OnCurrentChanged);
  146. currentChangedRaised = positionChangedRaised = false;
  147. test.Property = 5;
  148. Assert.IsFalse (currentChangedRaised, "A2");
  149. Assert.IsFalse (positionChangedRaised, "A3");
  150. }
  151. }
  152. }