**** Merged from MCS ****
[mono-project.git] / mcs / class / System / Test / System.ComponentModel / EventHandlerListTests.cs
blobc6213af65680e4deadbedc99c732bbd4e9b399a9
1 //
2 // System.ComponentModel.EventHandlerList test cases
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
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
14 #if NUNIT
15 using NUnit.Framework;
16 #else
17 using System.Reflection;
18 #endif
20 using System;
21 using System.ComponentModel;
23 namespace MonoTests.System.ComponentModel
25 #if NUNIT
26 [TestFixture]
27 public class EventHandlerListTests
29 #else
30 public class EventHandlerListTests
32 #endif
33 #if NUNIT
34 [SetUp]
35 public void GetReady ()
37 #else
38 static EventHandlerListTests ()
40 #endif
43 int calls = 0;
45 void Deleg1 (object o, EventArgs e)
47 calls++;
50 void Deleg2 (object o, EventArgs e)
52 calls <<= 1;
55 [Test]
56 public void All ()
58 EventHandlerList list = new EventHandlerList ();
59 string i1 = "i1";
60 string i2 = "i2";
61 EventHandler one = new EventHandler (Deleg1);
62 EventHandler two = new EventHandler (Deleg2);
63 EventHandler d;
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);
121 list.Dispose ();
124 #if !NUNIT
125 void Assert (string msg, bool result)
127 if (!result)
128 Console.WriteLine (msg);
131 void AssertEquals (string msg, object expected, object real)
133 if (expected == null && real == null)
134 return;
136 if (expected != null && expected.Equals (real))
137 return;
139 Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
142 void Fail (string msg)
144 Console.WriteLine ("Failed: {0}", msg);
147 static void Main ()
149 EventHandlerListTests p = new EventHandlerListTests ();
150 Type t = p.GetType ();
151 MethodInfo [] methods = t.GetMethods ();
152 foreach (MethodInfo m in methods) {
153 if (m.Name.Substring (0, 4) == "Test") {
154 m.Invoke (p, null);
158 #endif