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 using NUnit.Framework;
13 using System;
14 using System.ComponentModel;
15 
16 namespace MonoTests.System.ComponentModel
17 {
18 	[TestFixture]
19 	public class EventHandlerListTests
20 	{
21 		[SetUp]
GetReady()22 		public void GetReady ()
23 		{
24 		}
25 
26 		int calls = 0;
27 
Deleg1(object o, EventArgs e)28 		void Deleg1 (object o, EventArgs e)
29 		{
30 			calls++;
31 		}
32 
Deleg2(object o, EventArgs e)33 		void Deleg2 (object o, EventArgs e)
34 		{
35 			calls <<= 1;
36 		}
37 
38 		[Test]
All()39 		public void All ()
40 		{
41 			EventHandlerList list = new EventHandlerList ();
42 			string i1 = "i1";
43 			string i2 = "i2";
44 			EventHandler one = new EventHandler (Deleg1);
45 			EventHandler two = new EventHandler (Deleg2);
46 			EventHandler d;
47 
48 			Assert.IsNull (list [i1], "All #01");
49 			Assert.IsNull (list [i2], "All #02");
50 
51 			list.AddHandler (i1, one);
52 			d = list [i1] as EventHandler;
53 			Assert.IsNotNull (d, "All #03");
54 
55 			d (this, EventArgs.Empty);
56 			Assert.AreEqual (1, calls, "All #04");
57 
58 			list.AddHandler (i2, two);
59 			d = list [i1] as EventHandler;
60 			Assert.IsNotNull (d, "All #05");
61 
62 			d (this, EventArgs.Empty);
63 			Assert.AreEqual (2, calls, "All #06");
64 
65 			d = list [i2] as EventHandler;
66 			Assert.IsNotNull (d, "All #07");
67 
68 			d (this, EventArgs.Empty);
69 			Assert.AreEqual (4, calls, "All #08");
70 
71 			list.AddHandler (i2, two);
72 			d = list [i2] as EventHandler;
73 			Assert.IsNotNull (d, "All #08");
74 
75 			d (this, EventArgs.Empty);
76 			Assert.AreEqual (16, calls, "All #09");
77 
78 			list.RemoveHandler (i1, one);
79 			d = list [i1] as EventHandler;
80 			Assert.IsNull (d, "All #10");
81 
82 			list.RemoveHandler (i2, two);
83 			d = list [i2] as EventHandler;
84 			Assert.IsNotNull (d, "All #11");
85 
86 			list.RemoveHandler (i2, two);
87 			d = list [i2] as EventHandler;
88 			Assert.IsNull (d, "All #12");
89 
90 			list.AddHandler (i1, one);
91 			d = list [i1] as EventHandler;
92 			Assert.IsNotNull (d, "All #13");
93 
94 			list.AddHandler (i2, two);
95 			d = list [i2] as EventHandler;
96 			Assert.IsNotNull (d, "All #14");
97 
98 			list.AddHandler (i1, null);
99 			Assert.IsNotNull (list [i1], "All #15");
100 
101 			list.AddHandler (i2, null);
102 			Assert.IsNotNull (list [i2], "All #16");
103 
104 			list.Dispose ();
105 		}
106 
107 		[Test]
NullKey()108 		public void NullKey ()
109 		{
110 			EventHandlerList list = new EventHandlerList ();
111 			EventHandler one = new EventHandler (Deleg1);
112 
113 			list.AddHandler (null, one);
114 			EventHandler d = list [null] as EventHandler;
115 			Assert.IsNotNull (d, "NullKey #01");
116 
117 			list.RemoveHandler (null, one);
118 			d = list [null] as EventHandler;
119 			Assert.IsNull (d, "NullKey #02");
120 		}
121 	}
122 }
123 
124