DriverTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #nullable enable
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace DriverTests;
  5. public class DriverTests (ITestOutputHelper output) : FakeDriverBase
  6. {
  7. [Theory]
  8. [InlineData ("", true)]
  9. [InlineData ("a", true)]
  10. [InlineData ("👩‍❤️‍💋‍👨", false)]
  11. public void IsValidLocation (string text, bool positive)
  12. {
  13. IDriver driver = CreateFakeDriver ();
  14. driver.SetScreenSize (10, 10);
  15. // positive
  16. Assert.True (driver.IsValidLocation (text, 0, 0));
  17. Assert.True (driver.IsValidLocation (text, 1, 1));
  18. Assert.Equal (positive, driver.IsValidLocation (text, driver.Cols - 1, driver.Rows - 1));
  19. // negative
  20. Assert.False (driver.IsValidLocation (text, -1, 0));
  21. Assert.False (driver.IsValidLocation (text, 0, -1));
  22. Assert.False (driver.IsValidLocation (text, -1, -1));
  23. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows - 1));
  24. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows - 1));
  25. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows));
  26. // Define a clip rectangle
  27. driver.Clip = new (new Rectangle (5, 5, 5, 5));
  28. // positive
  29. Assert.True (driver.IsValidLocation (text, 5, 5));
  30. Assert.Equal (positive, driver.IsValidLocation (text, 9, 9));
  31. // negative
  32. Assert.False (driver.IsValidLocation (text, 4, 5));
  33. Assert.False (driver.IsValidLocation (text, 5, 4));
  34. Assert.False (driver.IsValidLocation (text, 10, 9));
  35. Assert.False (driver.IsValidLocation (text, 9, 10));
  36. Assert.False (driver.IsValidLocation (text, -1, 0));
  37. Assert.False (driver.IsValidLocation (text, 0, -1));
  38. Assert.False (driver.IsValidLocation (text, -1, -1));
  39. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows - 1));
  40. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows - 1));
  41. Assert.False (driver.IsValidLocation (text, driver.Cols, driver.Rows));
  42. driver.Dispose ();
  43. }
  44. [Theory]
  45. [InlineData ("fake")]
  46. [InlineData ("windows")]
  47. [InlineData ("dotnet")]
  48. [InlineData ("unix")]
  49. public void All_Drivers_Init_Dispose_Cross_Platform (string driverName)
  50. {
  51. IApplication? app = Application.Create ();
  52. app.Init (driverName);
  53. app.Dispose ();
  54. }
  55. [Theory]
  56. [InlineData ("fake")]
  57. [InlineData ("windows")]
  58. [InlineData ("dotnet")]
  59. [InlineData ("unix")]
  60. public void All_Drivers_Run_Cross_Platform (string driverName)
  61. {
  62. IApplication? app = Application.Create ();
  63. app.Init (driverName);
  64. app.StopAfterFirstIteration = true;
  65. app.Run<Runnable<bool>> ();
  66. app.Dispose ();
  67. }
  68. [Theory]
  69. [InlineData ("fake")]
  70. [InlineData ("windows")]
  71. [InlineData ("dotnet")]
  72. [InlineData ("unix")]
  73. public void All_Drivers_LayoutAndDraw_Cross_Platform (string driverName)
  74. {
  75. IApplication? app = Application.Create ();
  76. app.Init (driverName);
  77. app.StopAfterFirstIteration = true;
  78. app.Run<TestTop> ();
  79. DriverAssert.AssertDriverContentsWithFrameAre (driverName!, output, app.Driver);
  80. app.Dispose ();
  81. }
  82. // Tests fix for https://github.com/gui-cs/Terminal.Gui/issues/4258
  83. [Theory]
  84. [InlineData ("fake")]
  85. [InlineData ("windows")]
  86. [InlineData ("dotnet")]
  87. [InlineData ("unix")]
  88. public void All_Drivers_When_Clipped_AddStr_Glyph_On_Second_Cell_Of_Wide_Glyph_Outputs_Correctly (string driverName)
  89. {
  90. IApplication? app = Application.Create ();
  91. app.Init (driverName);
  92. IDriver driver = app.Driver!;
  93. // Need to force "windows" driver to override legacy console mode for this test
  94. driver.IsLegacyConsole = false;
  95. driver.Force16Colors = false;
  96. driver.SetScreenSize (6, 3);
  97. driver!.Clip = new (driver.Screen);
  98. driver.Move (1, 0);
  99. driver.AddStr ("┌");
  100. driver.Move (2, 0);
  101. driver.AddStr ("─");
  102. driver.Move (3, 0);
  103. driver.AddStr ("┐");
  104. driver.Clip.Exclude (new Region (new (1, 0, 3, 1)));
  105. driver.Move (0, 0);
  106. driver.AddStr ("🍎🍎🍎🍎");
  107. DriverAssert.AssertDriverContentsAre (
  108. """
  109. �┌─┐🍎
  110. """,
  111. output,
  112. driver);
  113. driver.Refresh ();
  114. DriverAssert.AssertDriverOutputIs (@"\x1b[38;2;0;0;0m\x1b[48;2;0;0;0m�┌─┐🍎\x1b[38;2;255;255;255m\x1b[48;2;0;0;0m",
  115. output, driver);
  116. }
  117. }
  118. public class TestTop : Runnable
  119. {
  120. /// <inheritdoc/>
  121. public override void BeginInit ()
  122. {
  123. Text = Driver!.GetName ()!;
  124. BorderStyle = LineStyle.None;
  125. base.BeginInit ();
  126. }
  127. }