2 using System
.ComponentModel
;
4 using System
.Windows
.Controls
;
5 using System
.Windows
.Threading
;
7 using Microsoft
.Silverlight
.Testing
;
8 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
10 namespace MoonTest
.System
.Windows
.Threading
{
13 public class CrossThreadAccessTest
: SilverlightTest
{
15 private BackgroundWorker bw
= new BackgroundWorker ();
16 private bool complete
;
17 // async tests are nice but we need to make sure the async code gets executed
18 private bool executed
;
20 public CrossThreadAccessTest ()
22 bw
.DoWork
+= delegate (object sender
, DoWorkEventArgs e
) {
23 Action action
= (e
.Argument
as Action
);
24 Assert
.Throws
<UnauthorizedAccessException
> (delegate {
25 executed
= true; // been there, done that :)
29 bw
.RunWorkerCompleted
+= delegate {
34 private void CrossThreadTest (Action action
)
36 executed
= complete
= false;
37 Enqueue (() => { bw.RunWorkerAsync (action); }
);
38 EnqueueConditional (() => { return executed && complete; }
);
39 EnqueueTestComplete ();
44 public void ApplicationTest ()
46 CrossThreadTest (() => {
53 public void LoadComponentTest ()
55 CrossThreadTest (() => {
56 Application
.LoadComponent (this, new Uri ("/threads3;component/App.xaml", UriKind
.Relative
));
62 public void GetRootVisualTest ()
64 CrossThreadTest (() => {
65 Assert
.IsNotNull (Application
.Current
.RootVisual
);
71 public void SetRootVisualTest ()
73 TextBlock b
= new TextBlock ();
74 CrossThreadTest (() => {
75 Application
.Current
.RootVisual
= b
;
81 public void EventTest ()
83 TextBlock e1
= new TextBlock ();
84 CrossThreadTest (() => {
85 e1
.GotFocus
+= delegate (object sender
, RoutedEventArgs ev
) {
92 public void CreateDependencyObjectTest ()
94 CrossThreadTest (() => {
101 public void DependencyObjectFindNameTest ()
103 TextBlock b
= new TextBlock ();
104 CrossThreadTest (() => {
111 public void CreateDependencyPropertyTest ()
113 TextBlock e1
= new TextBlock ();
114 CrossThreadTest (() => {
121 public void DispatcherTimerTest ()
123 CrossThreadTest (() => {
124 new DispatcherTimer ();