Пример #1
0
        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();
        }
Пример #2
0
        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();
            }
        }
Пример #3
0
        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();
                });
            }
        }
Пример #4
0
    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();
    }
Пример #5
0
    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();
        });
      }
    }
Пример #6
0
    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();
      }
    }