OutputBaseTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. namespace DriverTests;
  2. public class OutputBaseTests
  3. {
  4. [Fact]
  5. public void ToAnsi_SingleCell_NoAttribute_ReturnsGraphemeAndNewline ()
  6. {
  7. // Arrange
  8. var output = new FakeOutput ();
  9. IOutputBuffer buffer = output.GetLastBuffer ()!;
  10. buffer.SetSize (1, 1);
  11. // Act
  12. buffer.AddStr ("A");
  13. string ansi = output.ToAnsi (buffer);
  14. // Assert: single grapheme plus newline (BuildAnsiForRegion appends a newline per row)
  15. Assert.Contains ("A" + Environment.NewLine, ansi);
  16. }
  17. [Theory]
  18. [InlineData (true, false)]
  19. [InlineData (true, true)]
  20. [InlineData (false, false)]
  21. [InlineData (false, true)]
  22. public void ToAnsi_WithAttribute_AppendsCorrectColorSequence_BasedOnIsLegacyConsole_And_Force16Colors (bool isLegacyConsole, bool force16Colors)
  23. {
  24. // Arrange
  25. var output = new FakeOutput { IsLegacyConsole = isLegacyConsole };
  26. // Create DriverImpl and associate it with the FakeOutput to test Sixel output
  27. IDriver driver = new DriverImpl (
  28. new FakeInputProcessor (null!),
  29. new OutputBufferImpl (),
  30. output,
  31. new (new AnsiResponseParser ()),
  32. new SizeMonitorImpl (output));
  33. driver.Force16Colors = force16Colors;
  34. IOutputBuffer buffer = output.GetLastBuffer ()!;
  35. buffer.SetSize (1, 1);
  36. // Use a known RGB color and attribute
  37. var fg = new Color (1, 2, 3);
  38. var bg = new Color (4, 5, 6);
  39. buffer.CurrentAttribute = new (fg, bg);
  40. buffer.AddStr ("X");
  41. // Act
  42. string ansi = output.ToAnsi (buffer);
  43. // Assert: when true color expected, we should see the RGB CSI; otherwise we should see the 16-color CSI
  44. if (!isLegacyConsole && !force16Colors)
  45. {
  46. Assert.Contains ("\u001b[38;2;1;2;3m", ansi);
  47. }
  48. else if (!isLegacyConsole && force16Colors)
  49. {
  50. string expected16 = EscSeqUtils.CSI_SetForegroundColor (fg.GetAnsiColorCode ());
  51. Assert.Contains (expected16, ansi);
  52. }
  53. else
  54. {
  55. var expected16 = (ConsoleColor)fg.GetClosestNamedColor16 ();
  56. Assert.Equal (ConsoleColor.Black, expected16);
  57. Assert.DoesNotContain ('\u001b', ansi);
  58. }
  59. // Grapheme and newline should always be present
  60. Assert.Contains ("X" + Environment.NewLine, ansi);
  61. }
  62. [Fact]
  63. public void Write_WritesDirtyCellsAndClearsDirtyFlags ()
  64. {
  65. // Arrange
  66. var output = new FakeOutput ();
  67. IOutputBuffer buffer = output.GetLastBuffer ()!;
  68. buffer.SetSize (2, 1);
  69. // Mark two characters as dirty by writing them into the buffer
  70. buffer.AddStr ("AB");
  71. // Sanity: ensure cells are dirty before calling Write
  72. Assert.True (buffer.Contents! [0, 0].IsDirty);
  73. Assert.True (buffer.Contents! [0, 1].IsDirty);
  74. // Act
  75. output.Write (buffer); // calls OutputBase.Write via FakeOutput
  76. // Assert: content was written to the fake output and dirty flags cleared
  77. Assert.Contains ("AB", output.GetLastOutput ());
  78. Assert.False (buffer.Contents! [0, 0].IsDirty);
  79. Assert.False (buffer.Contents! [0, 1].IsDirty);
  80. }
  81. [Theory]
  82. [InlineData (true)]
  83. [InlineData (false)]
  84. public void Write_Virtual_Or_NonVirtual_Uses_WriteToConsole_And_Clears_Dirty_Flags (bool isLegacyConsole)
  85. {
  86. // Arrange
  87. // FakeOutput exposes this because it's in test scope
  88. var output = new FakeOutput { IsLegacyConsole = isLegacyConsole };
  89. IOutputBuffer buffer = output.GetLastBuffer ()!;
  90. buffer.SetSize (3, 1);
  91. // Write 'A' at col 0 and 'C' at col 2; leave col 1 untouched (not dirty)
  92. buffer.Move (0, 0);
  93. buffer.AddStr ("A");
  94. buffer.Move (2, 0);
  95. buffer.AddStr ("C");
  96. // Confirm some dirtiness before to write
  97. Assert.True (buffer.Contents! [0, 0].IsDirty);
  98. Assert.True (buffer.Contents! [0, 2].IsDirty);
  99. // Act
  100. output.Write (buffer);
  101. // Assert: both characters were written (use Contains to avoid CI side effects)
  102. Assert.Contains ("A", output.GetLastOutput ());
  103. Assert.Contains ("C", output.GetLastOutput ());
  104. // Dirty flags cleared for the written cells
  105. Assert.False (buffer.Contents! [0, 0].IsDirty);
  106. Assert.False (buffer.Contents! [0, 2].IsDirty);
  107. // Verify SetCursorPositionImpl was invoked by WriteToConsole (cursor set to a written column)
  108. Assert.Equal (new (0, 0), output.GetCursorPosition ());
  109. // Now write 'X' at col 0 to verify subsequent writes also work
  110. buffer.Move (0, 0);
  111. buffer.AddStr ("X");
  112. // Confirm dirtiness state before to write
  113. Assert.True (buffer.Contents! [0, 0].IsDirty);
  114. Assert.False (buffer.Contents! [0, 2].IsDirty);
  115. output.Write (buffer);
  116. // Assert: both characters were written (use Contains to avoid CI side effects)
  117. Assert.Contains ("A", output.GetLastOutput ());
  118. Assert.Contains ("C", output.GetLastOutput ());
  119. // Dirty flags cleared for the written cells
  120. Assert.False (buffer.Contents! [0, 0].IsDirty);
  121. Assert.False (buffer.Contents! [0, 2].IsDirty);
  122. // Verify SetCursorPositionImpl was invoked by WriteToConsole (cursor set to a written column)
  123. Assert.Equal (new (2, 0), output.GetCursorPosition ());
  124. }
  125. [Theory]
  126. [InlineData (true)]
  127. [InlineData (false)]
  128. public void Write_Virtual_Or_NonVirtual_Uses_WriteToConsole_And_Clears_Dirty_Flags_Mixed_Graphemes (bool isLegacyConsole)
  129. {
  130. // Arrange
  131. // FakeOutput exposes this because it's in test scope
  132. var output = new FakeOutput { IsLegacyConsole = isLegacyConsole };
  133. IOutputBuffer buffer = output.GetLastBuffer ()!;
  134. buffer.SetSize (3, 1);
  135. // Write '🦮' at col 0 and 'A' at col 2
  136. buffer.Move (0, 0);
  137. buffer.AddStr ("🦮A");
  138. // After the fix for https://github.com/gui-cs/Terminal.Gui/issues/4258:
  139. // Writing a wide glyph at column 0 no longer sets column 1 to IsDirty = false.
  140. // Column 1 retains whatever state it had (in this case, it was initialized as dirty
  141. // by ClearContents, but may have been cleared by a previous Write call).
  142. //
  143. // What we care about is that wide glyphs work correctly and don't prevent
  144. // other content from being drawn at odd columns.
  145. Assert.True (buffer.Contents! [0, 0].IsDirty);
  146. // Column 1 state depends on whether it was cleared by a previous Write - don't assert
  147. Assert.True (buffer.Contents! [0, 2].IsDirty);
  148. // Act
  149. output.Write (buffer);
  150. Assert.Contains ("🦮", output.GetLastOutput ());
  151. Assert.Contains ("A", output.GetLastOutput ());
  152. // Dirty flags cleared for the written cells
  153. // Column 0 was written (wide glyph)
  154. Assert.False (buffer.Contents! [0, 0].IsDirty);
  155. // Column 1 was skipped by OutputBase.Write because column 0 had a wide glyph
  156. // So its dirty flag remains true (it was initialized as dirty by ClearContents)
  157. Assert.True (buffer.Contents! [0, 1].IsDirty);
  158. // Column 2 was written ('A')
  159. Assert.False (buffer.Contents! [0, 2].IsDirty);
  160. Assert.Equal (new (0, 0), output.GetCursorPosition ());
  161. // Now write 'X' at col 1 which invalidates the wide glyph at col 0
  162. buffer.Move (1, 0);
  163. buffer.AddStr ("X");
  164. // Confirm dirtiness state before to write
  165. Assert.True (buffer.Contents! [0, 0].IsDirty); // Invalidated by writing at col 1
  166. Assert.True (buffer.Contents! [0, 1].IsDirty); // Just written
  167. Assert.True (buffer.Contents! [0, 2].IsDirty); // Marked dirty by writing at col 1
  168. output.Write (buffer);
  169. Assert.Contains ("�", output.GetLastOutput ());
  170. Assert.Contains ("X", output.GetLastOutput ());
  171. // Dirty flags cleared for the written cells
  172. Assert.False (buffer.Contents! [0, 0].IsDirty);
  173. Assert.False (buffer.Contents! [0, 1].IsDirty);
  174. Assert.False (buffer.Contents! [0, 2].IsDirty);
  175. // Verify SetCursorPositionImpl was invoked by WriteToConsole (cursor set to a written column)
  176. Assert.Equal (new (0, 0), output.GetCursorPosition ());
  177. }
  178. [Theory]
  179. [InlineData (true)]
  180. [InlineData (false)]
  181. public void Write_EmitsSixelDataAndPositionsCursor (bool isLegacyConsole)
  182. {
  183. // Arrange
  184. var output = new FakeOutput ();
  185. IOutputBuffer buffer = output.GetLastBuffer ()!;
  186. buffer.SetSize (1, 1);
  187. // Ensure the buffer has some content so Write traverses rows
  188. buffer.AddStr (".");
  189. // Create a Sixel to render
  190. var s = new SixelToRender
  191. {
  192. SixelData = "SIXEL-DATA",
  193. ScreenPosition = new (4, 2)
  194. };
  195. // Create DriverImpl and associate it with the FakeOutput to test Sixel output
  196. IDriver driver = new DriverImpl (
  197. new FakeInputProcessor (null!),
  198. new OutputBufferImpl (),
  199. output,
  200. new (new AnsiResponseParser ()),
  201. new SizeMonitorImpl (output));
  202. // Add the Sixel to the driver
  203. driver.GetSixels ().Enqueue (s);
  204. // FakeOutput exposes this because it's in test scope
  205. output.IsLegacyConsole = isLegacyConsole;
  206. // Act
  207. output.Write (buffer);
  208. if (!isLegacyConsole)
  209. {
  210. // Assert: Sixel data was emitted (use Contains to avoid equality/side-effects)
  211. Assert.Contains ("SIXEL-DATA", output.GetLastOutput ());
  212. // Cursor was moved to Sixel position
  213. Assert.Equal (s.ScreenPosition, output.GetCursorPosition ());
  214. }
  215. else
  216. {
  217. // Assert: Sixel data was NOT emitted
  218. Assert.DoesNotContain ("SIXEL-DATA", output.GetLastOutput ());
  219. // Cursor was NOT moved to Sixel position
  220. Assert.NotEqual (s.ScreenPosition, output.GetCursorPosition ());
  221. }
  222. IApplication app = Application.Create ();
  223. app.Driver = driver;
  224. Assert.Equal (driver.GetSixels (), app.Driver.GetSixels ());
  225. app.Dispose ();
  226. }
  227. }