DriverTests.cs 5.0 KB

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