ac22315f33a68dfa55ac3dab50c26a5f0e038b9b
[mono.git] / mcs / class / System / Test / System.ComponentModel / EventHandlerListTests.cs
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
10 //
11
12 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
13
14 #if NUNIT
15 using NUnit.Framework;
16 #else
17 using System.Reflection;
18 #endif
19
20 using System;
21 using System.ComponentModel;
22
23 namespace MonoTests.System.ComponentModel
24 {
25 #if NUNIT
26         [TestFixture]
27         public class EventHandlerListTests
28         {
29 #else
30         public class EventHandlerListTests
31         {
32 #endif
33 #if NUNIT
34                 [SetUp]
35                 public void GetReady ()
36                 {
37 #else
38                 static EventHandlerListTests ()
39                 {
40 #endif
41                 }
42
43                 int calls = 0;
44
45                 void Deleg1 (object o, EventArgs e)
46                 {
47                         calls++;
48                 }
49
50                 void Deleg2 (object o, EventArgs e)
51                 {
52                         calls <<= 1;
53                 }
54
55                 [Test]
56                 public void All ()
57                 {
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;
64
65                         Assertion.AssertEquals ("All #01", null, list [i1]);
66                         Assertion.AssertEquals ("All #02", null, list [i2]);
67
68                         list.AddHandler (i1, one);
69                         d = list [i1] as EventHandler;
70                         Assertion.Assert ("All #03", d != null);
71
72                         d (this, EventArgs.Empty);
73                         Assertion.AssertEquals ("All #04", 1, calls);
74
75                         list.AddHandler (i2, two);
76                         d = list [i1] as EventHandler;
77                         Assertion.Assert ("All #05", d != null);
78
79                         d (this, EventArgs.Empty);
80                         Assertion.AssertEquals ("All #06", 2, calls);
81
82                         d = list [i2] as EventHandler;
83                         Assertion.Assert ("All #07", d != null);
84
85                         d (this, EventArgs.Empty);
86                         Assertion.AssertEquals ("All #08", 4, calls);
87
88                         list.AddHandler (i2, two);
89                         d = list [i2] as EventHandler;
90                         Assertion.Assert ("All #08", d != null);
91
92                         d (this, EventArgs.Empty);
93                         Assertion.AssertEquals ("All #09", 16, calls);
94
95                         list.RemoveHandler (i1, one);
96                         d = list [i1] as EventHandler;
97                         Assertion.Assert ("All #10", d == null);
98
99                         list.RemoveHandler (i2, two);
100                         d = list [i2] as EventHandler;
101                         Assertion.Assert ("All #11", d != null);
102
103                         list.RemoveHandler (i2, two);
104                         d = list [i2] as EventHandler;
105                         Assertion.Assert ("All #12", d == null);
106
107                         list.AddHandler (i1, one);
108                         d = list [i1] as EventHandler;
109                         Assertion.Assert ("All #13", d != null);
110
111                         list.AddHandler (i2, two);
112                         d = list [i2] as EventHandler;
113                         Assertion.Assert ("All #14", d != null);
114
115                         list.AddHandler (i1, null);
116                         Assertion.Assert ("All #15", list [i1] != null);
117
118                         list.AddHandler (i2, null);
119                         Assertion.Assert ("All #16", list [i2] != null);
120
121                         list.Dispose ();
122                 }
123                 
124                 [Test]
125                 public void NullKey ()
126                 {
127                         EventHandlerList list = new EventHandlerList ();
128                         EventHandler one = new EventHandler (Deleg1);
129                         
130                         list.AddHandler (null, one);
131                         EventHandler d = list [null] as EventHandler;
132                         Assertion.Assert ("NullKey #01", d != null);
133                         
134                         list.RemoveHandler (null, one);
135                         d = list [null] as EventHandler;
136                         Assertion.Assert ("NullKey #02", d == null);
137                 }
138                 
139 #if !NUNIT
140                 void Assert (string msg, bool result)
141                 {
142                         if (!result)
143                                 Console.WriteLine (msg);
144                 }
145
146                 void AssertEquals (string msg, object expected, object real)
147                 {
148                         if (expected == null && real == null)
149                                 return;
150
151                         if (expected != null && expected.Equals (real))
152                                 return;
153
154                         Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
155                 }
156
157                 void Fail (string msg)
158                 {
159                         Console.WriteLine ("Failed: {0}", msg);
160                 }
161
162                 static void Main ()
163                 {
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") {
169                                         m.Invoke (p, null);
170                                 }
171                         }
172                 }
173 #endif
174         }
175 }
176