2 // System.ComponentModel.EventHandlerList test cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
9 // (c) 2003 Martin Willemoes Hansen
12 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
15 using NUnit.Framework;
17 using System.Reflection;
21 using System.ComponentModel;
23 namespace MonoTests.System.ComponentModel
27 public class EventHandlerListTests
30 public class EventHandlerListTests
35 public void GetReady ()
38 static EventHandlerListTests ()
45 void Deleg1 (object o, EventArgs e)
50 void Deleg2 (object o, EventArgs e)
58 EventHandlerList list = new EventHandlerList ();
61 EventHandler one = new EventHandler (Deleg1);
62 EventHandler two = new EventHandler (Deleg2);
65 Assertion.AssertEquals ("All #01", null, list [i1]);
66 Assertion.AssertEquals ("All #02", null, list [i2]);
68 list.AddHandler (i1, one);
69 d = list [i1] as EventHandler;
70 Assertion.Assert ("All #03", d != null);
72 d (this, EventArgs.Empty);
73 Assertion.AssertEquals ("All #04", 1, calls);
75 list.AddHandler (i2, two);
76 d = list [i1] as EventHandler;
77 Assertion.Assert ("All #05", d != null);
79 d (this, EventArgs.Empty);
80 Assertion.AssertEquals ("All #06", 2, calls);
82 d = list [i2] as EventHandler;
83 Assertion.Assert ("All #07", d != null);
85 d (this, EventArgs.Empty);
86 Assertion.AssertEquals ("All #08", 4, calls);
88 list.AddHandler (i2, two);
89 d = list [i2] as EventHandler;
90 Assertion.Assert ("All #08", d != null);
92 d (this, EventArgs.Empty);
93 Assertion.AssertEquals ("All #09", 16, calls);
95 list.RemoveHandler (i1, one);
96 d = list [i1] as EventHandler;
97 Assertion.Assert ("All #10", d == null);
99 list.RemoveHandler (i2, two);
100 d = list [i2] as EventHandler;
101 Assertion.Assert ("All #11", d != null);
103 list.RemoveHandler (i2, two);
104 d = list [i2] as EventHandler;
105 Assertion.Assert ("All #12", d == null);
107 list.AddHandler (i1, one);
108 d = list [i1] as EventHandler;
109 Assertion.Assert ("All #13", d != null);
111 list.AddHandler (i2, two);
112 d = list [i2] as EventHandler;
113 Assertion.Assert ("All #14", d != null);
115 list.AddHandler (i1, null);
116 Assertion.Assert ("All #15", list [i1] != null);
118 list.AddHandler (i2, null);
119 Assertion.Assert ("All #16", list [i2] != null);
125 public void NullKey ()
127 EventHandlerList list = new EventHandlerList ();
128 EventHandler one = new EventHandler (Deleg1);
130 list.AddHandler (null, one);
131 EventHandler d = list [null] as EventHandler;
132 Assertion.Assert ("NullKey #01", d != null);
134 list.RemoveHandler (null, one);
135 d = list [null] as EventHandler;
136 Assertion.Assert ("NullKey #02", d == null);
140 void Assert (string msg, bool result)
143 Console.WriteLine (msg);
146 void AssertEquals (string msg, object expected, object real)
148 if (expected == null && real == null)
151 if (expected != null && expected.Equals (real))
154 Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
157 void Fail (string msg)
159 Console.WriteLine ("Failed: {0}", msg);
164 EventHandlerListTests p = new EventHandlerListTests ();
165 Type t = p.GetType ();
166 MethodInfo [] methods = t.GetMethods ();
167 foreach (MethodInfo m in methods) {
168 if (m.Name.Substring (0, 4) == "Test") {