Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[mono.git] / mcs / nunit24 / NUnitCore / core / EventQueue.cs
1 // ****************************************************************\r
2 // Copyright 2007, Charlie Poole\r
3 // This is free software licensed under the NUnit license. You may\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4\r
5 // ****************************************************************\r
6 using System;\r
7 using System.Collections;\r
8 using System.Threading;\r
9 \r
10 namespace NUnit.Core\r
11 {\r
12         #region Individual Event Classes\r
13 \r
14         /// <summary>\r
15         /// NUnit.Core.Event is the abstract base for all stored events.\r
16         /// An Event is the stored representation of a call to the \r
17         /// EventListener interface and is used to record such calls\r
18         /// or to queue them for forwarding on another thread or at\r
19         /// a later time.\r
20         /// </summary>\r
21         public abstract class Event\r
22         {\r
23                 abstract public void Send( EventListener listener );\r
24         }\r
25 \r
26         public class RunStartedEvent : Event\r
27         {\r
28                 string name;\r
29                 int testCount;\r
30 \r
31                 public RunStartedEvent( string name, int testCount )\r
32                 {\r
33                         this.name = name;\r
34                         this.testCount = testCount;\r
35                 }\r
36 \r
37                 public override void Send( EventListener listener )\r
38                 {\r
39                         listener.RunStarted(name, testCount);\r
40                 }\r
41         }\r
42 \r
43         public class RunFinishedEvent : Event\r
44         {\r
45                 TestResult result;\r
46                 Exception exception;\r
47 \r
48                 public RunFinishedEvent( TestResult result )\r
49                 {\r
50                         this.result = result;\r
51                 }\r
52 \r
53                 public RunFinishedEvent( Exception exception )\r
54                 {\r
55                         this.exception = exception;\r
56                 }\r
57 \r
58                 public override void Send( EventListener listener )\r
59                 {\r
60                         if ( this.exception != null )\r
61                                 listener.RunFinished( this.exception );\r
62                         else\r
63                                 listener.RunFinished( this.result );\r
64                 }\r
65         }\r
66 \r
67         public class TestStartedEvent : Event\r
68         {\r
69                 TestName testName;\r
70 \r
71                 public TestStartedEvent( TestName testName )\r
72                 {\r
73                         this.testName = testName;\r
74                 }\r
75 \r
76                 public override void Send( EventListener listener )\r
77                 {\r
78                         listener.TestStarted( this.testName );\r
79                 }\r
80         }\r
81                         \r
82         public class TestFinishedEvent : Event\r
83         {\r
84                 TestCaseResult result;\r
85 \r
86                 public TestFinishedEvent( TestCaseResult result )\r
87                 {\r
88                         this.result = result;\r
89                 }\r
90 \r
91                 public override void Send( EventListener listener )\r
92                 {\r
93                         listener.TestFinished( this.result );\r
94                 }\r
95         }\r
96 \r
97         public class SuiteStartedEvent : Event\r
98         {\r
99                 TestName suiteName;\r
100 \r
101                 public SuiteStartedEvent( TestName suiteName )\r
102                 {\r
103                         this.suiteName = suiteName;\r
104                 }\r
105 \r
106                 public override void Send( EventListener listener )\r
107                 {\r
108                         listener.SuiteStarted( this.suiteName );\r
109                 }\r
110         }\r
111 \r
112         public class SuiteFinishedEvent : Event\r
113         {\r
114                 TestSuiteResult result;\r
115 \r
116                 public SuiteFinishedEvent( TestSuiteResult result )\r
117                 {\r
118                         this.result = result;\r
119                 }\r
120 \r
121                 public override void Send( EventListener listener )\r
122                 {\r
123                         listener.SuiteFinished( this.result );\r
124                 }\r
125         }\r
126 \r
127         public class UnhandledExceptionEvent : Event\r
128         {\r
129                 Exception exception;\r
130 \r
131                 public UnhandledExceptionEvent( Exception exception )\r
132                 {\r
133                         this.exception = exception;\r
134                 }\r
135 \r
136                 public override void Send( EventListener listener )\r
137                 {\r
138                         listener.UnhandledException( this.exception );\r
139                 }\r
140         }\r
141 \r
142         public class OutputEvent : Event\r
143         {\r
144                 TestOutput output;\r
145 \r
146                 public OutputEvent( TestOutput output )\r
147                 {\r
148                         this.output = output;\r
149                 }\r
150 \r
151                 public override void Send( EventListener listener )\r
152                 {\r
153                         listener.TestOutput( this.output );\r
154                 }\r
155         }\r
156 \r
157         #endregion\r
158 \r
159         /// <summary>\r
160         /// Implements a queue of work items each of which\r
161         /// is queued as a WaitCallback.\r
162         /// </summary>\r
163         public class EventQueue\r
164         {\r
165                 private Queue queue = new Queue();\r
166 \r
167                 public int Count\r
168                 {\r
169                         get \r
170                         {\r
171                                 lock( this )\r
172                                 {\r
173                                         return this.queue.Count; \r
174                                 }\r
175                         }\r
176                 }\r
177 \r
178                 public void Enqueue( Event e )\r
179                 {\r
180                         lock( this )\r
181                         {\r
182                                 this.queue.Enqueue( e );\r
183                                 Monitor.Pulse( this );\r
184                         }\r
185                 }\r
186 \r
187                 public Event Dequeue()\r
188                 {\r
189                         lock( this )\r
190                         {\r
191                                 return (Event)this.queue.Dequeue();\r
192                         }\r
193                 }\r
194         }\r
195 }\r