Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / WindowsBase / Test / System.Windows.Threading / DispatcherTest.cs
1 //
2 // Tests for System.Windows.Threading.Dispatcher.cs
3 //
4 // Author:
5 //    Miguel de Icaza (miguel@novell.com)
6 //
7
8 //
9 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Windows.Threading;
36 using System.Threading;
37
38 namespace MonoTests.System.Windows.Threading
39 {
40
41         delegate void Action ();
42         
43         [TestFixture]
44         public class DispatcherTest {
45                 EventWaitHandle wait, wait2;
46                 DispatcherOperation op;
47                 
48                 [SetUp]
49                 public void DispatcherSetup ()
50                 {
51                         wait = new EventWaitHandle (false, EventResetMode.AutoReset);
52                         wait2 = new EventWaitHandle (false, EventResetMode.AutoReset);
53                 }
54
55                 //
56                 // Tests that fields in a DispatcherOperation can be issued from
57                 // a separate thread
58                 //
59                 [Test]
60                 public void TestDispatcherOpOnThread ()
61                 {
62                         Thread t = new Thread (new ThreadStart (thread));
63                         Dispatcher d = Dispatcher.CurrentDispatcher;
64                         
65                         t.Start ();
66                         op = Dispatcher.CurrentDispatcher.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
67                                 Console.WriteLine ("Some methods");
68                         });
69                         wait.Set ();
70                         wait2.WaitOne ();
71                 }
72
73                 void thread ()
74                 {
75                         wait.WaitOne ();
76                         op.Priority = DispatcherPriority.DataBind;
77                         wait2.Set ();
78                 }
79
80                 [Test]
81                 public void TestDispatcherOrder ()
82                 {
83                         Dispatcher d = Dispatcher.CurrentDispatcher;
84
85                         DispatcherFrame frame = new DispatcherFrame ();
86                         bool fail = true;
87                         int next = 1;
88                         
89                         d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
90                                 if (next != 3)
91                                         throw new Exception ("Expected state 3, got " + next.ToString ());
92
93                                 next = 4;
94                                 Console.WriteLine ("First");
95                         });
96                         d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
97                                 if (next != 4)
98                                         throw new Exception ("Expected state 4, got " + next.ToString ());
99
100                                 next = 5;
101                                 Console.WriteLine ("Second");
102                         });
103                         d.BeginInvoke (DispatcherPriority.Send, (Action) delegate {
104                                 if (next != 1)
105                                         throw new Exception ("Expected state 1, got " + next.ToString ());
106                                 next = 2;
107                                 Console.WriteLine ("High Priority");
108                                 d.BeginInvoke (DispatcherPriority.Send, (Action) delegate {
109                                         if (next != 2)
110                                                 throw new Exception ("Expected state 2, got " + next.ToString ());
111
112                                         next = 3;
113                                         Console.WriteLine ("INSERTED");
114                                 });
115                         });
116                         d.BeginInvoke (DispatcherPriority.SystemIdle, (Action) delegate {
117                                 if (next != 6)
118                                         throw new Exception ("Expected state 6, got " + next.ToString ());
119                                 
120                                 Console.WriteLine ("Idle");
121                                 frame.Continue = false;
122                                 fail = false;
123                         });
124                         
125                         d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
126                                 if (next != 5)
127                                         throw new Exception ("Expected state 5, got " + next.ToString ());
128                                 next = 6;
129                                 Console.WriteLine ("Last normal");
130                         });
131                         
132                         Dispatcher.PushFrame (frame);
133
134                         if (fail)
135                                 throw new Exception ("Expected all states to run");
136                 }
137
138                 [Test]
139                 public void TestTwoArguments()
140                 {
141                         Dispatcher d = Dispatcher.CurrentDispatcher;
142                         DispatcherFrame frame = new DispatcherFrame();
143
144                         d.BeginInvoke (DispatcherPriority.Normal, (Action<int, string>) delegate(int arg1, string arg2) {
145                                 Assert.AreEqual(10, arg1, "arg1");
146                                 Assert.AreEqual("OK", arg2, "arg2");
147                                 frame.Continue = false;
148                         }, 10, "OK");
149
150                         Dispatcher.PushFrame(frame);
151                 }
152
153                 [Test]
154                 public void TestRunTwice()
155                 {
156                         Dispatcher d = Dispatcher.CurrentDispatcher;
157                         Action exit = delegate { Dispatcher.ExitAllFrames(); };
158                         int counter = 0;
159                         Action increment = delegate { counter++; };
160
161                         d.BeginInvoke(DispatcherPriority.Normal, exit);
162                         Dispatcher.Run();
163                         d.BeginInvoke(DispatcherPriority.Normal, increment);
164                         d.BeginInvoke(DispatcherPriority.Normal, increment);
165                         d.BeginInvoke(DispatcherPriority.Normal, exit);
166                         Dispatcher.Run();
167
168                         Assert.AreEqual(2, counter, "Counter of delegate invocation");
169                 }
170
171                 [Test]
172                 public void TestStopIfContinueIsFalse()
173                 {
174                         Dispatcher d = Dispatcher.CurrentDispatcher;
175                         DispatcherFrame frame = new DispatcherFrame();
176                         int counter = 0;
177
178                         d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
179                                 counter++;
180                         });
181                         d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
182                                 Dispatcher.ExitAllFrames();
183                         });
184
185                         frame.Continue = false;
186                         Dispatcher.PushFrame(frame);
187                         Assert.AreEqual(0, counter, "Counter of delegate invocation");
188                         frame.Continue = true;
189                         Dispatcher.PushFrame(frame);
190                         Assert.AreEqual(1, counter, "Counter of delegate invocation");
191                 }
192
193                 //
194                 // When a Dispatcher exits due to 'frame.Continue' being false,
195                 // it should not try to deque the same operation second time.
196                 //
197                 [Test]
198                 public void TestOperationDequeue()
199                 {
200
201                         Dispatcher d = Dispatcher.CurrentDispatcher;
202                         DispatcherFrame frame = new DispatcherFrame();
203                         Action exit = delegate { frame.Continue = false; };
204
205                         d.BeginInvoke(DispatcherPriority.Normal, exit);
206                         Dispatcher.PushFrame(frame);
207
208                         frame = new DispatcherFrame();
209                         d.BeginInvoke(DispatcherPriority.Background, exit);
210                         Dispatcher.PushFrame(frame);
211                 }
212
213                 [Test]
214                 public void TestPreemptedByHigherPriorityTask()
215                 {
216                         Dispatcher d = Dispatcher.CurrentDispatcher;
217                         DispatcherFrame frame = new DispatcherFrame();
218                         int counter = 0;
219                         Action increment = delegate { counter++; };
220
221                         d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
222                                 d.BeginInvoke(DispatcherPriority.Send, increment);
223                         });
224                         d.BeginInvoke(DispatcherPriority.Background, (Action) delegate {
225                                 frame.Continue = false;
226                         });
227
228                         Dispatcher.PushFrame(frame);
229                         Assert.AreEqual(1, counter, "Counter of delegate invocation");
230                 }
231         }
232 }
233
234