예제 #1
1
        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();
              }
        }
예제 #2
1
        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();
        }