File: EventHandlerListTests.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (123 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// System.ComponentModel.EventHandlerList test cases
//
// Authors:
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//      Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (c) 2002 Ximian, Inc. (http://www.ximian.com)
// (c) 2003 Martin Willemoes Hansen
//

using NUnit.Framework;
using System;
using System.ComponentModel;

namespace MonoTests.System.ComponentModel
{
	[TestFixture]
	public class EventHandlerListTests
	{
		[SetUp]
		public void GetReady ()
		{
		}

		int calls = 0;

		void Deleg1 (object o, EventArgs e)
		{
			calls++;
		}

		void Deleg2 (object o, EventArgs e)
		{
			calls <<= 1;
		}

		[Test]
		public void All ()
		{
			EventHandlerList list = new EventHandlerList ();
			string i1 = "i1";
			string i2 = "i2";
			EventHandler one = new EventHandler (Deleg1);
			EventHandler two = new EventHandler (Deleg2);
			EventHandler d;

			Assert.IsNull (list [i1], "All #01");
			Assert.IsNull (list [i2], "All #02");

			list.AddHandler (i1, one);
			d = list [i1] as EventHandler;
			Assert.IsNotNull (d, "All #03");

			d (this, EventArgs.Empty);
			Assert.AreEqual (1, calls, "All #04");

			list.AddHandler (i2, two);
			d = list [i1] as EventHandler;
			Assert.IsNotNull (d, "All #05");

			d (this, EventArgs.Empty);
			Assert.AreEqual (2, calls, "All #06");

			d = list [i2] as EventHandler;
			Assert.IsNotNull (d, "All #07");

			d (this, EventArgs.Empty);
			Assert.AreEqual (4, calls, "All #08");

			list.AddHandler (i2, two);
			d = list [i2] as EventHandler;
			Assert.IsNotNull (d, "All #08");

			d (this, EventArgs.Empty);
			Assert.AreEqual (16, calls, "All #09");

			list.RemoveHandler (i1, one);
			d = list [i1] as EventHandler;
			Assert.IsNull (d, "All #10");

			list.RemoveHandler (i2, two);
			d = list [i2] as EventHandler;
			Assert.IsNotNull (d, "All #11");

			list.RemoveHandler (i2, two);
			d = list [i2] as EventHandler;
			Assert.IsNull (d, "All #12");

			list.AddHandler (i1, one);
			d = list [i1] as EventHandler;
			Assert.IsNotNull (d, "All #13");

			list.AddHandler (i2, two);
			d = list [i2] as EventHandler;
			Assert.IsNotNull (d, "All #14");

			list.AddHandler (i1, null);
			Assert.IsNotNull (list [i1], "All #15");

			list.AddHandler (i2, null);
			Assert.IsNotNull (list [i2], "All #16");

			list.Dispose ();
		}
		
		[Test]
		public void NullKey ()
		{
			EventHandlerList list = new EventHandlerList ();
			EventHandler one = new EventHandler (Deleg1);
			
			list.AddHandler (null, one);
			EventHandler d = list [null] as EventHandler;
			Assert.IsNotNull (d, "NullKey #01");
			
			list.RemoveHandler (null, one);
			d = list [null] as EventHandler;
			Assert.IsNull (d, "NullKey #02");
		}
	}
}