using System.Threading; using System.Windows.Controls; using System.Windows.Data; using Caliburn.Actions; using Caliburn.Messaging; using Caliburn.Metadata; using MbUnit.Framework; namespace Tests.Caliburn.Actions_can { [TestFixture(ApartmentState = ApartmentState.STA)] public class by_using_an_AsynchronousActionExecutor : DispatcherTestBase { [Test] public void execute_a_method_asynchronously_with_callback() { ExecuteWithDispatcher( delegate { PresenterInfo presenterInfo = PresenterInfo.Create(typeof(TestPresenter)); ActionInfo actionInfo = presenterInfo.FindAction("TestAction"); TestPresenter target = new TestPresenter(); Button source = new Button(); ActionMessage message = new ActionMessage(); message.Action = "TestAction"; message.Trigger = "Click"; message.Parameters.Add(new Parameter(3.4)); AsynchronousActionExecutor executor = new AsynchronousActionExecutor(actionInfo); executor.InitializeCallback(typeof(TestPresenter), "ActionCallback"); executor.Execute( source, message, target ); target.WaitHandle.WaitOne(); Assert.IsTrue(target.TestActionCalled, "Action not called."); Assert.IsTrue(target.ActionCallbackCalled, "Callback not called."); Assert.IsFalse(target.ActionRescueCalled, "Rescue called."); }); } [Test] public void perform_rescues_on_methods_that_throw_exceptions() { ExecuteWithDispatcher( delegate { PresenterInfo presenterInfo = PresenterInfo.Create(typeof(TestPresenter)); ActionInfo actionInfo = presenterInfo.FindAction("TestAction"); TestPresenter target = new TestPresenter(); Button source = new Button(); ActionMessage message = new ActionMessage(); message.Action = "TestAction"; message.Trigger = "Click"; message.Parameters.Add(new Parameter(3.4)); target.ShouldThrowException = true; AsynchronousActionExecutor executor = new AsynchronousActionExecutor(actionInfo); executor.InitializeCallback(typeof(TestPresenter), "ActionCallback"); executor.Execute( source, message, target ); target.WaitHandle.WaitOne(); Assert.IsTrue(target.TestActionCalled); Assert.IsFalse(target.ActionCallbackCalled); Assert.IsTrue(target.ActionRescueCalled); }); } [Test] public void coerce_method_input_types() { ExecuteWithDispatcher( delegate { PresenterInfo presenterInfo = PresenterInfo.Create(typeof(TestPresenter)); ActionInfo actionInfo = presenterInfo.FindAction("TestAction"); TestPresenter target = new TestPresenter(); Button source = new Button(); ActionMessage message = new ActionMessage(); message.Action = "TestAction"; message.Trigger = "Click"; message.Parameters.Add(new Parameter("3.4")); //notice that this is a string AsynchronousActionExecutor executor = new AsynchronousActionExecutor(actionInfo); executor.InitializeCallback(typeof(TestPresenter), "ActionCallback"); executor.Execute( source, message, target ); target.WaitHandle.WaitOne(); Assert.IsTrue(target.TestActionCalled); Assert.IsTrue(target.ActionCallbackCalled); Assert.IsFalse(target.ActionRescueCalled); }); } [Test] public void insure_methods_are_called_with_the_correct_number_of_arguments() { ExecuteWithDispatcher( delegate { PresenterInfo presenterInfo = PresenterInfo.Create(typeof(TestPresenter)); ActionInfo actionInfo = presenterInfo.FindAction("TestAction"); TestPresenter target = new TestPresenter(); Button source = new Button(); ActionMessage message = new ActionMessage(); message.Action = "TestAction"; message.Trigger = "Click"; message.Parameters.Add(new Parameter("3.4")); message.Parameters.Add(new Parameter("extra parameter")); AsynchronousActionExecutor executor = new AsynchronousActionExecutor(actionInfo); executor.InitializeCallback(typeof(TestPresenter), "ActionCallback"); executor.Execute( source, message, target ); target.WaitHandle.WaitOne(); Assert.IsTrue(target.TestActionCalled); Assert.IsTrue(target.ActionCallbackCalled); Assert.IsFalse(target.ActionRescueCalled); }); } [Test] public void bind_return_values_from_methods() { ExecuteWithDispatcher( delegate { PresenterInfo presenterInfo = PresenterInfo.Create(typeof(TestPresenter)); ActionInfo actionInfo = presenterInfo.FindAction("TestAction"); TestPresenter target = new TestPresenter(); Button source = new Button(); Binding returnBinding = new Binding("Content"); returnBinding.Source = source; ActionMessage message = new ActionMessage(); message.Action = "TestAction"; message.Trigger = "Click"; message.Parameters.Add(new Parameter(3.4)); message.Return = returnBinding; AsynchronousActionExecutor executor = new AsynchronousActionExecutor(actionInfo); executor.InitializeCallback(typeof(TestPresenter), "ActionCallback"); executor.Execute( source, message, target ); target.WaitHandle.WaitOne(); Assert.IsTrue(target.TestActionCalled); Assert.IsTrue(target.ActionCallbackCalled); Assert.IsFalse(target.ActionRescueCalled); Assert.AreEqual(4, source.Content); }); } } }