Add [Category ("NotWorking")] to failing test.
[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 }
139
140