SynchronizatonContextTests.cs 3.0 KB

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