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