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