-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- so you can more easily run tests on the UI thread.
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters