Source/Csla.test/Threading/BackgroundWorkerTests.cs
/// <summary>
///A test for BackgroundWorker when reporting progress
///</summary>
A test for BackgroundWorker when reporting progress
A test for BackgroundWorker when reporting progress
[TestMethod]
    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) =>
                              {
                                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)
          {
            BackgroundWorkerSyncContextHelper.PumpDispatcher();
          }
          context.Complete();
        });
      }
    }
[ TestMethod ] 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 ) => { 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 , " " ) ; context . Assert . Success ( ) ; } ; target . RunWorkerAsync ( null ) ; while ( target . IsBusy ) { BackgroundWorkerSyncContextHelper . PumpDispatcher ( ) ; } context . Complete ( ) ; } ) ; } }
A test for BackgroundWorker when reporting progress
A test for BackgroundWorker when reporting progress
// report progress changed 10 times // this is the equvalent to Application.DoEvents in Windows Forms 