in class/WPF.Toolkit/:
[moon.git] / test / 2.0 / moon-unit / System.Windows.Threading / DispatcherTest.cs
bloba8b88065e171f0bb41e3a15c7da7e1e31a4e2893
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:
8 //
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.
22 //Authors:
23 // Andreia Gaita (shana@jitted.com)
26 using System;
27 using System.Windows;
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
41 [TestClass]
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 {
53 try {
54 Console.WriteLine (count++);
55 e.BeginInvoke (delegate {
56 Console.WriteLine ("invoked");
57 });
58 } finally {
59 wait.Set ();
61 });
63 for (int i = 0; i < 20; i++) {
64 if (i == 10)
65 e = null;
66 timer.Change (0, Timeout.Infinite);
67 wait.WaitOne ();
69 Console.WriteLine ("done");
73 [TestMethod]
74 [SilverlightBug]
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 {
81 try {
82 DispatcherSynchronizationContext.Current.Send (delegate {
83 Assert.AreEqual (mainui, Thread.CurrentThread.ManagedThreadId, "MainThreadCheck");
84 }, null);
85 } finally {
86 wait.Set ();
88 });
89 timer.Change (0, Timeout.Infinite);
90 wait.WaitOne ();
93 [TestMethod]
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 {
100 try {
101 b.Dispatcher.BeginInvoke ( delegate {
102 Assert.AreEqual (mainui, Thread.CurrentThread.ManagedThreadId, "MainThreadCheck");
104 } finally {
105 wait.Set ();
108 timer.Change (0, Timeout.Infinite);
109 wait.WaitOne ();
112 [TestMethod]
113 [Asynchronous]
114 public void RestartTimer ()
116 int count = 0;
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 ();
129 [TestMethod]
130 [Asynchronous]
131 public void RestartTimer2 ()
133 bool complete=false;
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;};
141 int count = 0;
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 ();