Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Commands;
  8.  
  9. /// <summary>
  10. /// Requires NUnit 3.11 or later.
  11. /// </summary>
  12. [AttributeUsage(AttributeTargets.Method)]
  13. public sealed class WindowsFormsMessagePumpAttribute : PropertyAttribute, IWrapSetUpTearDown
  14. {
  15. public WindowsFormsMessagePumpAttribute()
  16. {
  17. Properties.Add(PropertyNames.ApartmentState, ApartmentState.STA);
  18. }
  19.  
  20. public TestCommand Wrap(TestCommand command) => new Command(command);
  21.  
  22. private sealed class Command : DelegatingTestCommand
  23. {
  24. public Command(TestCommand innerCommand) : base(innerCommand)
  25. {
  26. }
  27.  
  28. /* NUnit 3.12 version (change class <summary> element if this version is used)
  29. public override TestResult Execute(TestExecutionContext context)
  30. {
  31. SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
  32. return innerCommand.Execute(context);
  33. }
  34. */
  35.  
  36. // NUnit 3.11 version
  37. public override TestResult Execute(TestExecutionContext context)
  38. {
  39. var previousSyncContext = SynchronizationContext.Current;
  40. if (!(previousSyncContext is WindowsFormsSynchronizationContext winFormsSyncContext))
  41. {
  42. winFormsSyncContext = new WindowsFormsSynchronizationContext();
  43. SynchronizationContext.SetSynchronizationContext(winFormsSyncContext);
  44. }
  45.  
  46. try
  47. {
  48. return innerCommand.Execute(context);
  49. }
  50. finally
  51. {
  52. if (previousSyncContext != winFormsSyncContext)
  53. SynchronizationContext.SetSynchronizationContext(previousSyncContext);
  54. }
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement