public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue() { UnitTestContext context = GetContext(); //int numTimesProgressCalled = 0; BackgroundWorker target = new BackgroundWorker(); target.DoWork += (o, e) => { // report progress changed 10 times for (int i = 1; i < 11; i++) { Thread.Sleep(100); if (target.CancellationPending) { e.Cancel = true; return; } } }; target.WorkerSupportsCancellation = true; target.RunWorkerCompleted += (o, e) => { // target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork context.Assert.IsNull(e.Error); context.Assert.IsTrue(e.Cancelled); context.Assert.Success(); }; target.RunWorkerAsync(null); target.CancelAsync(); context.Complete(); }
public void BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse() { using (UnitTestContext context = GetContext()) { BackgroundWorker target = new BackgroundWorker(); target.DoWork += (o, e) => { for (int i = 1; i < 11; i++) { Thread.Sleep(10); } }; target.WorkerSupportsCancellation = false; target.RunWorkerAsync(null); try { target.CancelAsync(); // this call throws exception } catch (InvalidOperationException ex) { context.Assert.Fail(ex); } } }
public void BackgroundWorker_RunWorkerAsync_ReportsProgress() { var threadid = Thread.CurrentThread.ManagedThreadId; using (UnitTestContext context = GetContext()) { 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) => { numTimesProgressCalled++; context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId); }; target.RunWorkerCompleted += (o, e) => { context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId); context.Assert.IsNull(e.Error); context.Assert.IsTrue(numTimesProgressCalled == 10, "ReportProgress has been called 10 times"); context.Assert.Success(); }; target.RunWorkerAsync(null); context.Complete(); } }
public void BackgroundWorker_DoWork_ThrowsInvalidOperationExcpetionWhenWorkerReportsProgressIsFalse() { UnitTestContext context = GetContext(); 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); } }; target.WorkerReportsProgress = false; target.ProgressChanged += (o, e) => { numTimesProgressCalled++; }; target.RunWorkerCompleted += (o, e) => { // target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork context.Assert.IsTrue(e.Error is System.InvalidOperationException); context.Assert.Success(); }; target.RunWorkerAsync(null); context.Complete(); }
public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted() { var threadid = Thread.CurrentThread.ManagedThreadId; using (UnitTestContext context = GetContext()) { bool doWorkCalled = false; BackgroundWorker target = new BackgroundWorker(); target.DoWork += (o, e) => { doWorkCalled = true; // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent context.Assert.IsFalse(threadid == Thread.CurrentThread.ManagedThreadId); 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) => { context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId); context.Assert.IsNull(e.Error); context.Assert.IsTrue(doWorkCalled, "Do work has been called"); context.Assert.Success(); }; target.RunWorkerAsync(null); context.Complete(); } }