EventHandlerListTests.cs 3.5 KB

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