public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted() { using (UnitTestContext context = GetContext()) { BackgroundWorkerSyncContextHelper.DoTests(() => { var UIThreadid = Thread.CurrentThread.ManagedThreadId; _originalPrincipal = Csla.ApplicationContext.User; _originalCulture = Thread.CurrentThread.CurrentCulture; _originalUICulture = Thread.CurrentThread.CurrentUICulture; Csla.ApplicationContext.User = new MyPrincipal(); Csla.ApplicationContext.ClientContext["BWTEST"] = "TEST"; Csla.ApplicationContext.GlobalContext["BWTEST"] = "TEST"; Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("FR"); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("FR"); BackgroundWorker target = new BackgroundWorker(); bool doWorkCalled = false; target.DoWork += (o, e) => { doWorkCalled = true; context.Assert.IsFalse(Thread.CurrentThread.ManagedThreadId == UIThreadid); // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent context.Assert.IsTrue(Csla.ApplicationContext.User is MyPrincipal); context.Assert.AreEqual("TEST", Csla.ApplicationContext.GlobalContext["BWTEST"]); context.Assert.AreEqual("TEST", Csla.ApplicationContext.ClientContext["BWTEST"]); context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper()); context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper()); }; target.RunWorkerCompleted += (o, e) => { // assert that this callback comes on the "UI" thread context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid); context.Assert.IsNull(e.Error); context.Assert.IsTrue(doWorkCalled, "Do work has been called"); context.Assert.Success(); }; target.RunWorkerAsync(null); while (target.IsBusy) { // this is the equvalent to Application.DoEvents in Windows Forms BackgroundWorkerSyncContextHelper.PumpDispatcher(); } context.Complete(); }); } }
public void BackgroundWorker_RunWorkerAsync_ReportsProgress() { using (UnitTestContext context = GetContext()) { BackgroundWorkerSyncContextHelper.DoTests(() => { var UIThreadid = Thread.CurrentThread.ManagedThreadId; int numTimesProgressCalled = 0; BackgroundWorker target = new BackgroundWorker(); target.DoWork += (o, e) => { // report progress changed 10 times for (int i = 1; i < 11; i++) { target.ReportProgress(i * 10); } e.Result = new object(); }; target.WorkerReportsProgress = true; target.ProgressChanged += (o, e) => { context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid); numTimesProgressCalled++; }; target.RunWorkerCompleted += (o, e) => { context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid); context.Assert.IsNull(e.Error); context.Assert.IsTrue(numTimesProgressCalled == 10, "ReportProgress has been called 10 times"); context.Assert.Success(); }; target.RunWorkerAsync(null); while (target.IsBusy) { // this is the equvalent to Application.DoEvents in Windows Forms BackgroundWorkerSyncContextHelper.PumpDispatcher(); } context.Complete(); }); } }