CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BackgroundWorkerTests.cs
Go to the documentation of this file.
1
8//using System;
9//using System.ComponentModel;
10//using System.Globalization;
11//using System.Security.Claims;
12//using System.Security.Principal;
13//using System.Threading;
14//#if MSTEST
15//using Microsoft.VisualStudio.TestTools.UnitTesting;
16//#endif
17//using UnitDriven;
18//using BackgroundWorker = Csla.Threading.BackgroundWorker;
19
20//namespace Csla.Test.Threading
21//{
22
23
24// /// <summary>
25// ///This is a test class for BackgroundWorkerTest and is intended
26// ///to contain all BackgroundWorkerTest Unit Tests
27// ///</summary>
28// [TestClass]
29// public class BackgroundWorkerTests : TestBase
30// {
31
32// #region Additional test attributes
33// //
34// //You can use the following additional attributes as you write your tests:
35// private IPrincipal _originalPrincipal;
36// private CultureInfo _originalCulture;
37// private CultureInfo _originalUICulture;
38
39// [ClassInitialize]
40// public static void ClassInit(TestContext context)
41// {
42// BackgroundWorkerSyncContextHelper.Init();
43// }
44
45// [ClassCleanup]
46// public static void ClassCleanup()
47// {
48// BackgroundWorkerSyncContextHelper.Cleanup();
49// }
50
51// [TestInitialize]
52// public void Setup()
53// {
54// _originalPrincipal = Csla.ApplicationContext.User;
55// _originalCulture = Thread.CurrentThread.CurrentCulture;
56// _originalUICulture = Thread.CurrentThread.CurrentUICulture;
57// }
58
59// [TestCleanup]
60// public void Cleanup()
61// {
62// Csla.ApplicationContext.User = _originalPrincipal;
63
64// Thread.CurrentThread.CurrentCulture = _originalCulture;
65// Thread.CurrentThread.CurrentUICulture = _originalUICulture;
66// }
67
68// #endregion
69
70// /// <summary>
71// ///A test for BackgroundWorker Constructor
72// ///</summary>
73// [TestMethod]
74// public void BackgroundWorker_Constructor_DefaultValues()
75// {
76// using (var context = GetContext())
77// {
78// BackgroundWorker target = new BackgroundWorker();
79
80// context.Assert.IsFalse(target.WorkerReportsProgress, "WorkerReportsProgress is false by default");
81// context.Assert.IsFalse(target.WorkerSupportsCancellation, "WorkerSupportsCancellation is false by default");
82// context.Assert.Success();
83// }
84
85// }
86
87// /// <summary>
88// ///A test for BackgroundWorker normal run
89// ///</summary>
90// [TestMethod]
91
92// public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted()
93// {
94
95// using (UnitTestContext context = GetContext())
96// {
97// BackgroundWorkerSyncContextHelper.DoTests(() =>
98// {
99
100// var UIThreadid = Thread.CurrentThread.ManagedThreadId;
101
102// Csla.ApplicationContext.User = new ClaimsPrincipal();
103// TestResults.Add("BWTEST", "TEST");
104
105
106// Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("FR");
107// Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("FR");
108
109// BackgroundWorker target = new BackgroundWorker();
110// bool doWorkCalled = false;
111
112// target.DoWork += (o, e) =>
113// {
114// doWorkCalled = true;
115// context.Assert.IsFalse(Thread.CurrentThread.ManagedThreadId == UIThreadid);
116
117// // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent
118// context.Assert.IsTrue(Csla.ApplicationContext.User is ClaimsPrincipal);
119// context.Assert.AreEqual("TEST", TestResults.GetResult("BWTEST"));
120// context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper());
121// context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper());
122// };
123// target.RunWorkerCompleted += (o, e) =>
124// {
125// // assert that this callback comes on the "UI" thread
126// context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
127// context.Assert.IsNull(e.Error);
128// context.Assert.IsTrue(doWorkCalled, "Do work has been called");
129// context.Assert.Success();
130// };
131// target.RunWorkerAsync(null);
132
133
134// while (target.IsBusy)
135// {
136// // this is the equvalent to Application.DoEvents in Windows Forms
137// BackgroundWorkerSyncContextHelper.PumpDispatcher();
138// }
139
140// context.Complete();
141// });
142// }
143// }
144
145
146// /// <summary>
147// ///A test for BackgroundWorker when reporting progress
148// ///</summary>
149// [TestMethod]
150// public void BackgroundWorker_RunWorkerAsync_ReportsProgress()
151// {
152// using (UnitTestContext context = GetContext())
153// {
154// BackgroundWorkerSyncContextHelper.DoTests(() =>
155// {
156
157// var UIThreadid = Thread.CurrentThread.ManagedThreadId;
158// int numTimesProgressCalled = 0;
159
160// BackgroundWorker target = new BackgroundWorker();
161// target.DoWork += (o, e) =>
162// {
163// // report progress changed 10 times
164// for (int i = 1; i < 11; i++)
165// {
166// target.ReportProgress(i*10);
167// }
168// e.Result = new object();
169// };
170// target.WorkerReportsProgress = true;
171// target.ProgressChanged += (o, e) =>
172// {
173// context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
174// numTimesProgressCalled++;
175// };
176// target.RunWorkerCompleted += (o, e) =>
177// {
178// context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
179// context.Assert.IsNull(e.Error);
180// context.Assert.IsTrue(numTimesProgressCalled == 10,"ReportProgress has been called 10 times");
181// context.Assert.Success();
182// };
183// target.RunWorkerAsync(null);
184
185// while (target.IsBusy)
186// {
187// // this is the equvalent to Application.DoEvents in Windows Forms
188// BackgroundWorkerSyncContextHelper.PumpDispatcher();
189// }
190
191// context.Complete();
192// });
193// }
194// }
195
196// /// <summary>
197// ///A test for BackgroundWorker when reporting progress
198// ///</summary>
199// [TestMethod]
200// public void BackgroundWorker_DoWork_ThrowsInvalidOperationExcpetionWhenWorkerReportsProgressIsFalse()
201// {
202// UnitTestContext context = GetContext();
203
204// int numTimesProgressCalled = 0;
205
206// BackgroundWorker target = new BackgroundWorker();
207// target.DoWork += (o, e) =>
208// {
209// // report progress changed 10 times
210// for (int i = 1; i < 11; i++)
211// {
212// target.ReportProgress(i * 10);
213// }
214// };
215// target.WorkerReportsProgress = false;
216// target.ProgressChanged += (o, e) =>
217// {
218// numTimesProgressCalled++;
219// };
220// target.RunWorkerCompleted += (o, e) =>
221// {
222// // target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
223// context.Assert.IsTrue(e.Error is System.InvalidOperationException);
224// context.Assert.Success();
225// };
226// target.RunWorkerAsync(null);
227// context.Complete();
228// }
229
230// /// <summary>
231// ///A test for BackgroundWorker when reporting progress
232// ///</summary>
233// [TestMethod]
234// [TestCategory("SkipWhenLiveUnitTesting")]
235// public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue()
236// {
237// UnitTestContext context = GetContext();
238
239// BackgroundWorker target = new BackgroundWorker();
240// target.DoWork += (o, e) =>
241// {
242// // report progress changed 10 times
243// for (int i = 1; i < 11; i++)
244// {
245// Thread.Sleep(100);
246// if (target.CancellationPending)
247// {
248// e.Cancel = true;
249// return;
250// }
251// }
252// };
253// target.WorkerSupportsCancellation = true;
254// target.RunWorkerCompleted += (o, e) =>
255// {
256// // target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
257// context.Assert.IsNull(e.Error);
258// context.Assert.IsTrue(e.Cancelled);
259// context.Assert.Success();
260// };
261// target.RunWorkerAsync(null);
262// target.CancelAsync();
263// context.Complete();
264// }
265
266// /// <summary>
267// ///A test for BackgroundWorker when reporting progress
268// ///</summary>
269// [TestMethod]
270// [ExpectedException(typeof(System.InvalidOperationException))]
271// public void BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse()
272// {
273// using (UnitTestContext context = GetContext())
274// {
275// BackgroundWorker target = new BackgroundWorker();
276// target.DoWork += (o, e) =>
277// {
278// for (int i = 1; i < 11; i++)
279// {
280// Thread.Sleep(10);
281// }
282// };
283// target.WorkerSupportsCancellation = false;
284// target.RunWorkerAsync(null);
285
286// try
287// {
288// target.CancelAsync(); // this call throws exception
289// }
290// catch (InvalidOperationException ex)
291// {
292// context.Assert.Fail(ex);
293// }
294// context.Complete();
295// }
296// }
297// }
298//}