Remove Execute.TestExecuteSynchronously, in favour of a synchronous IDispatcher implementation

This commit is contained in:
Antony Male 2015-01-04 13:10:37 +00:00
parent 9765f6c3fe
commit 9ee23f1469
17 changed files with 91 additions and 96 deletions

View File

@ -36,7 +36,6 @@ namespace Stylet
this.dispatcher = dispatcher;
}
public void Post(Action action)
{
this.dispatcher.BeginInvoke(action);
@ -61,12 +60,7 @@ namespace Stylet
/// <summary>
/// Should be set to the UI thread's Dispatcher. This is normally done by the Bootstrapper.
/// </summary>
public static IDispatcher Dispatcher;
/// <summary>
/// FOR TESTING ONLY. Causes everything to execute synchronously
/// </summary>
public static bool TestExecuteSynchronously = false;
public static IDispatcher Dispatcher { get; set; }
private static bool? inDesignMode;
@ -82,7 +76,7 @@ namespace Stylet
private static void EnsureDispatcher()
{
if (Dispatcher == null && !TestExecuteSynchronously)
if (Dispatcher == null)
throw new InvalidOperationException("Execute.Dispatcher must be set before this method can be called. This should normally have been done by the Bootstrapper");
}
@ -93,10 +87,7 @@ namespace Stylet
public static void PostToUIThread(Action action)
{
EnsureDispatcher();
if (!TestExecuteSynchronously)
Dispatcher.Post(action);
else
action();
Dispatcher.Post(action);
}
/// <summary>
@ -108,15 +99,7 @@ namespace Stylet
public static Task PostToUIThreadAsync(Action action)
{
EnsureDispatcher();
if (!TestExecuteSynchronously)
{
return PostOnUIThreadInternalAsync(action);
}
else
{
action();
return Task.FromResult(false);
}
return PostOnUIThreadInternalAsync(action);
}
/// <summary>
@ -126,10 +109,10 @@ namespace Stylet
public static void OnUIThread(Action action)
{
EnsureDispatcher();
if (!TestExecuteSynchronously && !Dispatcher.IsCurrent)
Dispatcher.Post(action);
else
if (Dispatcher.IsCurrent)
action();
else
Dispatcher.Post(action);
}
/// <summary>
@ -140,7 +123,11 @@ namespace Stylet
{
EnsureDispatcher();
Exception exception = null;
if (!TestExecuteSynchronously && !Dispatcher.IsCurrent)
if (Dispatcher.IsCurrent)
{
action();
}
else
{
Dispatcher.Send(() =>
{
@ -157,10 +144,6 @@ namespace Stylet
if (exception != null)
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
}
else
{
action();
}
}
/// <summary>
@ -171,15 +154,15 @@ namespace Stylet
public static Task OnUIThreadAsync(Action action)
{
EnsureDispatcher();
if (!TestExecuteSynchronously && !Dispatcher.IsCurrent)
{
return PostOnUIThreadInternalAsync(action);
}
else
if (Dispatcher.IsCurrent)
{
action();
return Task.FromResult(false);
}
else
{
return PostOnUIThreadInternalAsync(action);
}
}
private static Task PostOnUIThreadInternalAsync(Action action)

View File

@ -19,7 +19,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[Test]

View File

@ -81,7 +81,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void FixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -46,7 +46,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void FixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -58,7 +58,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -20,7 +20,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -20,7 +20,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -49,7 +49,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -39,7 +39,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -17,11 +17,11 @@ namespace StyletUnitTests
public void SetUp()
{
// Dont want this being previously set by anything and messing us around
Execute.TestExecuteSynchronously = false;
Execute.Dispatcher = new SynchronousDispatcher();
}
[Test]
public void OnUIThreadSyncExecutesUsingDispatcher()
public void OnUIThreadSyncExecutesUsingDispatcherIfNotCurrent()
{
var sync = new Mock<IDispatcher>();
Execute.Dispatcher = sync.Object;
@ -106,7 +106,21 @@ namespace StyletUnitTests
}
[Test]
public void OnUIThreadAsyncExecutesAsynchronouslyIfDispatcherIsNotNull()
public void OnUIThreadExecutesSynchronouslyIfCurrent()
{
var sync = new Mock<IDispatcher>();
Execute.Dispatcher = sync.Object;
sync.SetupGet(x => x.IsCurrent).Returns(true);
bool actionCalled = false;
Execute.OnUIThread(() => actionCalled = true);
Assert.IsTrue(actionCalled);
}
[Test]
public void OnUIThreadAsyncExecutesAsynchronouslyIfNotCurrent()
{
var sync = new Mock<IDispatcher>();
Execute.Dispatcher = sync.Object;
@ -124,6 +138,21 @@ namespace StyletUnitTests
Assert.IsTrue(task.IsCompleted);
}
[Test]
public void OnUIThreadAsyncExecutesSynchronouslyIfCurrent()
{
var sync = new Mock<IDispatcher>();
Execute.Dispatcher = sync.Object;
sync.SetupGet(x => x.IsCurrent).Returns(true);
bool actionCalled = false;
var task = Execute.OnUIThreadAsync(() => actionCalled = true);
Assert.IsTrue(task.IsCompleted);
Assert.IsTrue(actionCalled);
}
[Test]
public void OnUIThreadSyncPropagatesException()
{
@ -210,51 +239,6 @@ namespace StyletUnitTests
Assert.Throws<InvalidOperationException>(() => Execute.OnUIThreadAsync(() => { }));
}
[Test]
public void PostToUIThreadExecutesSynchronouslyIfTestExecuteSynchronouslySet()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = null;
bool called = false;
Execute.PostToUIThread(() => called = true);
Assert.True(called);
}
[Test]
public void PostToUIThreadAsyncExecutesSynchronouslyIfTestExecuteSynchronouslySet()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = null;
bool called = false;
var task = Execute.PostToUIThreadAsync(() => called = true);
Assert.True(called);
Assert.True(task.IsCompleted);
}
[Test]
public void OnUIThreadSyncExecutesSynchronouslyIfTestExecuteSynchronouslySet()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = null;
bool called = false;
Execute.OnUIThreadSync(() => called = true);
Assert.True(called);
}
[Test]
public void OnUIThreadAsyncExecutesSynchronouslyIfTestExecuteSynchronouslySet()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = null;
bool called = false;
Execute.OnUIThreadAsync(() => called = true);
Assert.True(called);
}
[Test]
public void InDesignModeReturnsFalse()
{

View File

@ -37,7 +37,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[Test]

View File

@ -55,7 +55,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -68,7 +68,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}

View File

@ -67,6 +67,7 @@
<Compile Include="StyletIoC\StyletIoCFuncFactoryTests.cs" />
<Compile Include="StyletIoC\StyletIoCInstanceBindingTests.cs" />
<Compile Include="StyletIoC\StyletIoCModuleTests.cs" />
<Compile Include="SynchronousDispatcher.cs" />
<Compile Include="TraceLoggerTests.cs" />
<Compile Include="EqualityConverterTests.cs" />
<Compile Include="EventActionTests.cs" />

View File

@ -0,0 +1,27 @@
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StyletUnitTests
{
internal class SynchronousDispatcher : IDispatcher
{
public void Post(Action action)
{
throw new InvalidOperationException();
}
public void Send(Action action)
{
throw new InvalidOperationException();
}
public bool IsCurrent
{
get { return true; }
}
}
}

View File

@ -74,7 +74,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void SetUpFixture()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]

View File

@ -102,7 +102,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void FixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
}
[SetUp]
@ -175,7 +175,7 @@ namespace StyletUnitTests
var config = new Mock<IViewManagerConfig>();
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>() { Assembly.GetExecutingAssembly() });
var viewManager = new MyViewManager(config.Object);
Execute.TestExecuteSynchronously = true;
Execute.Dispatcher = new SynchronousDispatcher();
var viewType = viewManager.LocateViewForModel(typeof(ViewManagerTestsViewModel));
Assert.AreEqual(typeof(ViewManagerTestsView), viewType);
}