Exemplo n.º 1
0
        public void SafeInvoke_NullAction_ThrowsArgumentNullException(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            _control.CreateControl();
            var ex = Assert.Throws <ArgumentNullException>(() => { _control.SafeInvoke(null, "NullActionTest", errorHandling); });

            Assert.AreEqual("action", ex.ParamName);
        }
Exemplo n.º 2
0
        public void SafeInvoke_DisposedAfterInvokingOnUiThread_InvokedActionDequeued(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            string confirmationMessage = null;

            _control.CreateControl();
            IAsyncResult resultOfSafeInvokeCall1 = null;
            IAsyncResult resultOfSafeInvokeCall2 = null;
            bool         action2WasExecuted      = false;
            var          worker = new BackgroundWorker();

            worker.DoWork += (sender, args) =>
            {
                var ctrl = (Control)args.Argument;
                Console.WriteLine("SafeInvoke 1");
                resultOfSafeInvokeCall1 = ctrl.SafeInvoke(() =>
                {
                    Console.WriteLine("invoking 1");
                    confirmationMessage = "First action got invoked";
                    Assert.IsFalse(ctrl.InvokeRequired);
                    ctrl.Dispose();
                    Console.WriteLine("ctrl has been disposed");
                }, "Getting initial value, resetting control text, and disposing control.");
                Console.WriteLine("SafeInvoke 2");
                resultOfSafeInvokeCall2 = ctrl.SafeInvoke(() =>
                {
                    action2WasExecuted = true;
                    Assert.Fail("This should have been de-queued when the control was disposed.");
                }, "DisposedAfterInvokingOnUiThread", errorHandling);
                Console.WriteLine("About to sleep on worker thread : 50 ms");
                Thread.Sleep(60);
            };
            worker.RunWorkerAsync(_control);
            Thread.Sleep(20);
            Console.WriteLine("Ui thread waking up to begin processing queued-up aynchronous actions.");
            while (worker.IsBusy)
            {
                Application.DoEvents();
            }
            Assert.AreEqual("First action got invoked", confirmationMessage);
            Assert.IsNotNull(resultOfSafeInvokeCall1, "First call to SafeInvoke should have returned a non-null IAsyncResult.");
            Assert.IsFalse(resultOfSafeInvokeCall1.CompletedSynchronously, "Expected asynchronous invocation of action 1.");
            Assert.IsNull(_threadException, "If this is not null, then the second call to SafeInvoke must have thrown an exception.");
            Assert.IsNotNull(resultOfSafeInvokeCall2, "Second call to SafeInvoke should have returned a non-null IAsyncResult (even though it is later dequeued).");
            Assert.IsNull(_control.EndInvoke(resultOfSafeInvokeCall1));             // this should not throw an exception
            Assert.IsFalse(action2WasExecuted, "Action 2 should not have been executed at all.");
            if (Platform.IsWindows)
            {
                VerifyExpectedExceptionInNest <ObjectDisposedException>(() => _control.EndInvoke(resultOfSafeInvokeCall2));
            }
        }
Exemplo n.º 3
0
        public void SafeInvoke_OnUiThread_Disposed_Ignore_ReturnsWithoutInvokingOrThrowing(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            var i = 0;

            _control.CreateControl();
            _control.Dispose();
            Assert.IsNull(_control.SafeInvoke(() => { i++; }, "DisposedTest", errorHandling));
            Assert.AreEqual(0, i);
        }
Exemplo n.º 4
0
        public void SafeInvoke_OnUiThread_HandleNotCreated_NotIgnoreAll_ThrowsInvalidOperationException(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            var ex = Assert.Throws <InvalidOperationException>(() => { _control.SafeInvoke(() => { }, "HandleNotCreatedTest", errorHandling); });

            Assert.AreEqual("SafeInvoke called before the control's handle was created. (HandleNotCreatedTest)", ex.Message);
        }
Exemplo n.º 5
0
        public void SafeInvoke_NullControl_ThrowsArgumentNullException(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            var ex = Assert.Throws <ArgumentNullException>(() => { ControlExtensions.SafeInvoke(null, () => { }, "NullControlTest", errorHandling); });

            Assert.AreEqual("control", ex.ParamName);
        }
Exemplo n.º 6
0
        public void SafeInvoke_OnNonUiThread_Disposed_Ignore_ReturnsWithoutInvokingOrThrowing(ControlExtensions.ErrorHandlingAction errorHandling)
        {
            IAsyncResult resultOfSafeInvokeCall = null;
            var          i = 0;

            _control.CreateControl();
            _control.Dispose();
            var worker = new BackgroundWorker();

            worker.DoWork += (sender, args) =>
            {
                resultOfSafeInvokeCall = _control.SafeInvoke(() => { i++; }, "DisposedTest", errorHandling);
            };
            worker.RunWorkerAsync(_control);
            while (worker.IsBusy)
            {
                Application.DoEvents();
            }
            Assert.AreEqual(0, i);
            Assert.IsNull(resultOfSafeInvokeCall);
        }