1 //Permission is hereby granted, free of charge, to any person obtaining
2 //a copy of this software and associated documentation files (the
3 //"Software"), to deal in the Software without restriction, including
4 //without limitation the rights to use, copy, modify, merge, publish,
5 //distribute, sublicense, and/or sell copies of the Software, and to
6 //permit persons to whom the Software is furnished to do so, subject to
7 //the following conditions:
9 //The above copyright notice and this permission notice shall be
10 //included in all copies or substantial portions of the Software.
12 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 //EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 //MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 //NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 //LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 //OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //Copyright (c) 2008 Novell, Inc.
23 // Andreia Gaita (shana@jitted.com)
28 using System
.Windows
.Controls
;
29 using System
.Threading
;
30 using System
.Diagnostics
;
31 using System
.Windows
.Threading
;
32 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
33 using Mono
.Moonlight
.UnitTesting
;
34 using Microsoft
.Silverlight
.Testing
;
35 using System
.Windows
.Shapes
;
36 using System
.Windows
.Media
.Animation
;
39 namespace MoonTest
.System
.Windows
.Threading
42 public class DispatcherTest
: SilverlightTest
44 public static int count
;
46 public void InvokeAndDestroy () {
47 TextBlock t
= new TextBlock ();
48 Dispatcher e
= t
.Dispatcher
;
50 ManualResetEvent wait
= new ManualResetEvent (false);
52 Timer timer
= new Timer ( delegate {
54 Console
.WriteLine (count
++);
55 e
.BeginInvoke (delegate {
56 Console
.WriteLine ("invoked");
63 for (int i
= 0; i
< 20; i
++) {
66 timer
.Change (0, Timeout
.Infinite
);
69 Console
.WriteLine ("done");
75 [Ignore ("this can crash the process/IE with SL2")]
76 public void SyncContextCheck ()
78 int mainui
= Thread
.CurrentThread
.ManagedThreadId
;
79 ManualResetEvent wait
= new ManualResetEvent (false);
80 Timer timer
= new Timer ( delegate {
82 DispatcherSynchronizationContext
.Current
.Send (delegate {
83 Assert
.AreEqual (mainui
, Thread
.CurrentThread
.ManagedThreadId
, "MainThreadCheck");
89 timer
.Change (0, Timeout
.Infinite
);
94 public void DODispatcher () {
95 Button b
= new Button ();
97 int mainui
= Thread
.CurrentThread
.ManagedThreadId
;
98 ManualResetEvent wait
= new ManualResetEvent (false);
99 Timer timer
= new Timer (delegate {
101 b
.Dispatcher
.BeginInvoke ( delegate {
102 Assert
.AreEqual (mainui
, Thread
.CurrentThread
.ManagedThreadId
, "MainThreadCheck");
108 timer
.Change (0, Timeout
.Infinite
);
114 public void RestartTimer ()
117 DispatcherTimer timer
= new DispatcherTimer ();
118 timer
.Interval
= TimeSpan
.FromMilliseconds (5);
119 timer
.Tick
+= delegate { count++; if (count == 5) timer.Stop (); }
;
120 Enqueue (() => {count = 0; timer.Start ();}
);
121 EnqueueConditional (() => count
== 5);
122 Enqueue (() => { count = 0; timer.Start (); }
);
123 EnqueueConditional (() => count
== 5);
124 Enqueue (() => { count = 0; timer.Start (); }
);
125 EnqueueConditional (() => count
== 5);
126 EnqueueTestComplete ();
131 public void RestartTimer2 ()
134 Rectangle r
= new Rectangle();
135 Storyboard sb
= new Storyboard ();
136 DoubleAnimation animation
= new DoubleAnimation { Duration = new Duration(TimeSpan.FromMilliseconds(100)), From = 10, To = 100 }
;
137 Storyboard
.SetTarget (animation
, r
);
138 Storyboard
.SetTargetProperty (animation
, new PropertyPath ("Width"));
139 sb
.Children
.Add (animation
);
140 sb
.Completed
+= delegate {complete = true;}
;
142 DispatcherTimer timer
= new DispatcherTimer ();
143 timer
.Interval
= TimeSpan
.FromMilliseconds (5);
144 timer
.Tick
+= delegate { if (count < 5) count++; }
;
145 Enqueue (() => { timer.Start (); }
);
146 Enqueue (() => { sb.Begin (); }
);
147 Enqueue (() => complete
= true);
148 Enqueue (() => { timer.Stop (); complete = false; }
);
149 Enqueue (() => { sb.Begin (); }
);
150 Enqueue (() => complete
= true);
151 Enqueue (() => {count =0; timer.Start (); complete = false; }
);
152 EnqueueConditional (() => count
== 5, TimeSpan
.FromMilliseconds (1000));
153 EnqueueTestComplete ();