2 // Tests for System.Windows.Threading.Dispatcher.cs
5 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
34 using System.Globalization;
35 using System.Windows.Threading;
36 using System.Threading;
38 namespace MonoTests.System.Windows.Threading
41 delegate void Action ();
44 public class DispatcherTest {
45 EventWaitHandle wait, wait2;
46 DispatcherOperation op;
49 public void DispatcherSetup ()
51 wait = new EventWaitHandle (false, EventResetMode.AutoReset);
52 wait2 = new EventWaitHandle (false, EventResetMode.AutoReset);
56 // Tests that fields in a DispatcherOperation can be issued from
60 public void TestDispatcherOpOnThread ()
62 Thread t = new Thread (new ThreadStart (thread));
63 Dispatcher d = Dispatcher.CurrentDispatcher;
66 op = Dispatcher.CurrentDispatcher.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
67 Console.WriteLine ("Some methods");
76 op.Priority = DispatcherPriority.DataBind;
81 public void TestDispatcherOrder ()
83 Dispatcher d = Dispatcher.CurrentDispatcher;
85 DispatcherFrame frame = new DispatcherFrame ();
89 d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
91 throw new Exception ("Expected state 3, got " + next.ToString ());
94 Console.WriteLine ("First");
96 d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
98 throw new Exception ("Expected state 4, got " + next.ToString ());
101 Console.WriteLine ("Second");
103 d.BeginInvoke (DispatcherPriority.Send, (Action) delegate {
105 throw new Exception ("Expected state 1, got " + next.ToString ());
107 Console.WriteLine ("High Priority");
108 d.BeginInvoke (DispatcherPriority.Send, (Action) delegate {
110 throw new Exception ("Expected state 2, got " + next.ToString ());
113 Console.WriteLine ("INSERTED");
116 d.BeginInvoke (DispatcherPriority.SystemIdle, (Action) delegate {
118 throw new Exception ("Expected state 6, got " + next.ToString ());
120 Console.WriteLine ("Idle");
121 frame.Continue = false;
125 d.BeginInvoke (DispatcherPriority.Normal, (Action) delegate {
127 throw new Exception ("Expected state 5, got " + next.ToString ());
129 Console.WriteLine ("Last normal");
132 Dispatcher.PushFrame (frame);
135 throw new Exception ("Expected all states to run");
139 public void TestTwoArguments()
141 Dispatcher d = Dispatcher.CurrentDispatcher;
142 DispatcherFrame frame = new DispatcherFrame();
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;
150 Dispatcher.PushFrame(frame);
154 public void TestRunTwice()
156 Dispatcher d = Dispatcher.CurrentDispatcher;
157 Action exit = delegate { Dispatcher.ExitAllFrames(); };
159 Action increment = delegate { counter++; };
161 d.BeginInvoke(DispatcherPriority.Normal, exit);
163 d.BeginInvoke(DispatcherPriority.Normal, increment);
164 d.BeginInvoke(DispatcherPriority.Normal, increment);
165 d.BeginInvoke(DispatcherPriority.Normal, exit);
168 Assert.AreEqual(2, counter, "Counter of delegate invocation");
172 public void TestStopIfContinueIsFalse()
174 Dispatcher d = Dispatcher.CurrentDispatcher;
175 DispatcherFrame frame = new DispatcherFrame();
178 d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
181 d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
182 Dispatcher.ExitAllFrames();
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");
194 // When a Dispatcher exits due to 'frame.Continue' being false,
195 // it should not try to deque the same operation second time.
198 public void TestOperationDequeue()
201 Dispatcher d = Dispatcher.CurrentDispatcher;
202 DispatcherFrame frame = new DispatcherFrame();
203 Action exit = delegate { frame.Continue = false; };
205 d.BeginInvoke(DispatcherPriority.Normal, exit);
206 Dispatcher.PushFrame(frame);
208 frame = new DispatcherFrame();
209 d.BeginInvoke(DispatcherPriority.Background, exit);
210 Dispatcher.PushFrame(frame);
214 public void TestPreemptedByHigherPriorityTask()
216 Dispatcher d = Dispatcher.CurrentDispatcher;
217 DispatcherFrame frame = new DispatcherFrame();
219 Action increment = delegate { counter++; };
221 d.BeginInvoke(DispatcherPriority.Normal, (Action) delegate {
222 d.BeginInvoke(DispatcherPriority.Send, increment);
224 d.BeginInvoke(DispatcherPriority.Background, (Action) delegate {
225 frame.Continue = false;
228 Dispatcher.PushFrame(frame);
229 Assert.AreEqual(1, counter, "Counter of delegate invocation");