SynchronizatonContextTests.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. using UnitTests;
  3. namespace Terminal.Gui.ApplicationTests;
  4. public class SyncrhonizationContextTests
  5. {
  6. [Fact]
  7. public void SynchronizationContext_CreateCopy ()
  8. {
  9. ConsoleDriver.RunningUnitTests = true;
  10. Application.Init ();
  11. SynchronizationContext context = SynchronizationContext.Current;
  12. Assert.NotNull (context);
  13. SynchronizationContext contextCopy = context.CreateCopy ();
  14. Assert.NotNull (contextCopy);
  15. Assert.NotEqual (context, contextCopy);
  16. Application.Shutdown ();
  17. }
  18. [Theory]
  19. [InlineData (typeof (FakeDriver))]
  20. [InlineData (typeof (NetDriver))]
  21. [InlineData (typeof (WindowsDriver))]
  22. [InlineData (typeof (CursesDriver))]
  23. public void SynchronizationContext_Post (Type driverType)
  24. {
  25. ConsoleDriver.RunningUnitTests = true;
  26. Application.Init (driverName: driverType.Name);
  27. SynchronizationContext context = SynchronizationContext.Current;
  28. var success = false;
  29. Task.Run (
  30. () =>
  31. {
  32. Thread.Sleep (500);
  33. // non blocking
  34. context.Post (
  35. delegate
  36. {
  37. success = true;
  38. // then tell the application to quit
  39. Application.Invoke (() => Application.RequestStop ());
  40. },
  41. null
  42. );
  43. Assert.False (success);
  44. }
  45. );
  46. // blocks here until the RequestStop is processed at the end of the test
  47. Application.Run ().Dispose ();
  48. Assert.True (success);
  49. Application.Shutdown ();
  50. }
  51. [Fact]
  52. [AutoInitShutdown]
  53. public void SynchronizationContext_Send ()
  54. {
  55. ConsoleDriver.RunningUnitTests = true;
  56. Application.Init ();
  57. SynchronizationContext context = SynchronizationContext.Current;
  58. var success = false;
  59. Task.Run (
  60. () =>
  61. {
  62. Thread.Sleep (500);
  63. // blocking
  64. context.Send (
  65. delegate
  66. {
  67. success = true;
  68. // then tell the application to quit
  69. Application.Invoke (() => Application.RequestStop ());
  70. },
  71. null
  72. );
  73. Assert.True (success);
  74. }
  75. );
  76. // blocks here until the RequestStop is processed at the end of the test
  77. Application.Run ().Dispose ();
  78. Assert.True (success);
  79. Application.Shutdown ();
  80. }
  81. }