EventHandlerListTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // System.ComponentModel.EventHandlerList test cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  9. // (c) 2003 Martin Willemoes Hansen
  10. //
  11. #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
  12. #if NUNIT
  13. using NUnit.Framework;
  14. #else
  15. using System.Reflection;
  16. #endif
  17. using System;
  18. using System.ComponentModel;
  19. namespace MonoTests.System.ComponentModel
  20. {
  21. #if NUNIT
  22. [TestFixture]
  23. public class EventHandlerListTests
  24. {
  25. #else
  26. public class EventHandlerListTests
  27. {
  28. #endif
  29. #if NUNIT
  30. [SetUp]
  31. public void GetReady ()
  32. {
  33. #else
  34. static EventHandlerListTests ()
  35. {
  36. #endif
  37. }
  38. int calls = 0;
  39. void Deleg1 (object o, EventArgs e)
  40. {
  41. calls++;
  42. }
  43. void Deleg2 (object o, EventArgs e)
  44. {
  45. calls <<= 1;
  46. }
  47. [Test]
  48. public void All ()
  49. {
  50. EventHandlerList list = new EventHandlerList ();
  51. string i1 = "i1";
  52. string i2 = "i2";
  53. EventHandler one = new EventHandler (Deleg1);
  54. EventHandler two = new EventHandler (Deleg2);
  55. EventHandler d;
  56. Assertion.AssertEquals ("All #01", null, list [i1]);
  57. Assertion.AssertEquals ("All #02", null, list [i2]);
  58. list.AddHandler (i1, one);
  59. d = list [i1] as EventHandler;
  60. Assertion.Assert ("All #03", d != null);
  61. d (this, EventArgs.Empty);
  62. Assertion.AssertEquals ("All #04", 1, calls);
  63. list.AddHandler (i2, two);
  64. d = list [i1] as EventHandler;
  65. Assertion.Assert ("All #05", d != null);
  66. d (this, EventArgs.Empty);
  67. Assertion.AssertEquals ("All #06", 2, calls);
  68. d = list [i2] as EventHandler;
  69. Assertion.Assert ("All #07", d != null);
  70. d (this, EventArgs.Empty);
  71. Assertion.AssertEquals ("All #08", 4, calls);
  72. list.AddHandler (i2, two);
  73. d = list [i2] as EventHandler;
  74. Assertion.Assert ("All #08", d != null);
  75. d (this, EventArgs.Empty);
  76. Assertion.AssertEquals ("All #09", 16, calls);
  77. list.RemoveHandler (i1, one);
  78. d = list [i1] as EventHandler;
  79. Assertion.Assert ("All #10", d == null);
  80. list.RemoveHandler (i2, two);
  81. d = list [i2] as EventHandler;
  82. Assertion.Assert ("All #11", d != null);
  83. list.RemoveHandler (i2, two);
  84. d = list [i2] as EventHandler;
  85. Assertion.Assert ("All #12", d == null);
  86. list.AddHandler (i1, one);
  87. d = list [i1] as EventHandler;
  88. Assertion.Assert ("All #13", d != null);
  89. list.AddHandler (i2, two);
  90. d = list [i2] as EventHandler;
  91. Assertion.Assert ("All #14", d != null);
  92. list.AddHandler (i1, null);
  93. Assertion.Assert ("All #15", list [i1] != null);
  94. list.AddHandler (i2, null);
  95. Assertion.Assert ("All #16", list [i2] != null);
  96. list.Dispose ();
  97. }
  98. [Test]
  99. public void NullKey ()
  100. {
  101. EventHandlerList list = new EventHandlerList ();
  102. EventHandler one = new EventHandler (Deleg1);
  103. list.AddHandler (null, one);
  104. EventHandler d = list [null] as EventHandler;
  105. Assertion.Assert ("NullKey #01", d != null);
  106. list.RemoveHandler (null, one);
  107. d = list [null] as EventHandler;
  108. Assertion.Assert ("NullKey #02", d == null);
  109. }
  110. #if !NUNIT
  111. void Assert (string msg, bool result)
  112. {
  113. if (!result)
  114. Console.WriteLine (msg);
  115. }
  116. void AssertEquals (string msg, object expected, object real)
  117. {
  118. if (expected == null && real == null)
  119. return;
  120. if (expected != null && expected.Equals (real))
  121. return;
  122. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  123. }
  124. void Fail (string msg)
  125. {
  126. Console.WriteLine ("Failed: {0}", msg);
  127. }
  128. static void Main ()
  129. {
  130. EventHandlerListTests p = new EventHandlerListTests ();
  131. Type t = p.GetType ();
  132. MethodInfo [] methods = t.GetMethods ();
  133. foreach (MethodInfo m in methods) {
  134. if (m.Name.Substring (0, 4) == "Test") {
  135. m.Invoke (p, null);
  136. }
  137. }
  138. }
  139. #endif
  140. }
  141. }