Skip to content

Commit

Permalink
Add InvokeOnUIAttribute for NUnit
Browse files Browse the repository at this point in the history
- so you can more easily run tests on the UI thread.
  • Loading branch information
cwensley committed Feb 6, 2020
1 parent e421724 commit 7ca23c7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Eto.UnitTest.NUnit/Eto.UnitTest.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
Expand Down
58 changes: 58 additions & 0 deletions src/Eto.UnitTest.NUnit/InvokeOnUIAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Runtime.ExceptionServices;
using Eto.Forms;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;

namespace Eto.UnitTest.NUnit
{

/// <summary>
/// Runs the test on the UI thread
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class InvokeOnUIAttribute : Attribute, IWrapSetUpTearDown
{
/// <summary>
/// Wraps the specified command
/// </summary>
/// <param name="command">command to wrap</param>
/// <returns>A new command that wraps the specified command in a UI invoke</returns>
public TestCommand Wrap(TestCommand command) => new RunOnUICommand(command);

class RunOnUICommand : DelegatingTestCommand
{
public RunOnUICommand(TestCommand innerCommand)
: base(innerCommand)
{
}

public override TestResult Execute(TestExecutionContext context)
{
Exception exception = null;

var result = Application.Instance.Invoke(() =>
{
try
{
context.EstablishExecutionEnvironment();
return innerCommand.Execute(context);
}
catch (Exception ex)
{
exception = ex;
return null;
}
});

if (exception != null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}

return result;
}
}
}
}
35 changes: 35 additions & 0 deletions src/Eto.UnitTest.NUnit/MyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using NUnit.Framework;
using System.Threading;
using Eto.Forms;
using System.Threading.Tasks;

#if INCLUDE_TESTS

Expand Down Expand Up @@ -121,6 +123,39 @@ public void Test4()
Thread.Sleep(1000);
}
}

[TestFixture]
public class UITests
{
[Test]
[InvokeOnUI]
public void RunThisOnUIThread()
{
Thread currentThread = Thread.CurrentThread;
Thread uiThread = null;
Application.Instance.Invoke(() =>
{
uiThread = Thread.CurrentThread;
});

Assert.IsNotNull(uiThread, "#1. UI thread was not found");
Assert.AreEqual(currentThread.ManagedThreadId, uiThread.ManagedThreadId, "#2. UI Thread should be the same as the test thread");
}

[Test]
public void RunThisOnTestThread()
{
Thread currentThread = Thread.CurrentThread;
Thread uiThread = null;
Application.Instance.Invoke(() =>
{
uiThread = Thread.CurrentThread;
});

Assert.IsNotNull(uiThread, "#1. UI thread was not found");
Assert.AreNotEqual(currentThread.ManagedThreadId, uiThread.ManagedThreadId, "#2. UI Thread should not be the same as the test thread");
}
}
}

#endif

0 comments on commit 7ca23c7

Please sign in to comment.