TextFormatterNewArchitectureTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Drawing;
  2. using Terminal.Gui.Text;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.TextTests;
  6. /// <summary>
  7. /// Tests for the new TextFormatter architecture that separates formatting from rendering.
  8. /// </summary>
  9. public class TextFormatterNewArchitectureTests
  10. {
  11. private readonly ITestOutputHelper _output;
  12. public TextFormatterNewArchitectureTests(ITestOutputHelper output)
  13. {
  14. _output = output;
  15. }
  16. [Fact]
  17. public void TextFormatter_NewArchitecture_BasicFormatting_Works()
  18. {
  19. Application.Init(new FakeDriver());
  20. var tf = new TextFormatter
  21. {
  22. Text = "Hello World"
  23. };
  24. // Test the new architecture method
  25. Size size = tf.GetFormattedSizeWithNewArchitecture();
  26. Assert.True(size.Width > 0);
  27. Assert.True(size.Height > 0);
  28. Application.Shutdown();
  29. }
  30. [Fact]
  31. public void TextFormatter_NewArchitecture_WithAlignment_Works()
  32. {
  33. Application.Init(new FakeDriver());
  34. var tf = new TextFormatter
  35. {
  36. Text = "Hello World",
  37. Alignment = Alignment.Center,
  38. VerticalAlignment = Alignment.Center
  39. };
  40. // Test that properties are synchronized
  41. Size size = tf.GetFormattedSizeWithNewArchitecture();
  42. Assert.True(size.Width > 0);
  43. Assert.True(size.Height > 0);
  44. Application.Shutdown();
  45. }
  46. [Fact]
  47. public void TextFormatter_NewArchitecture_Performance_IsBetter()
  48. {
  49. Application.Init(new FakeDriver());
  50. var tf = new TextFormatter
  51. {
  52. Text = "This is a long text that will be formatted multiple times to test performance improvements"
  53. };
  54. // Warm up
  55. tf.GetFormattedSizeWithNewArchitecture();
  56. // Test multiple calls - should use caching
  57. var sw = System.Diagnostics.Stopwatch.StartNew();
  58. for (int i = 0; i < 100; i++)
  59. {
  60. tf.GetFormattedSizeWithNewArchitecture();
  61. }
  62. sw.Stop();
  63. _output.WriteLine($"New architecture: 100 calls took {sw.ElapsedMilliseconds}ms");
  64. // The new architecture should be fast due to caching
  65. Assert.True(sw.ElapsedMilliseconds < 100, "New architecture should be fast due to caching");
  66. Application.Shutdown();
  67. }
  68. [Fact]
  69. public void TextFormatter_NewArchitecture_DrawRegion_Works()
  70. {
  71. Application.Init(new FakeDriver());
  72. var tf = new TextFormatter
  73. {
  74. Text = "Hello\nWorld"
  75. };
  76. Region region = tf.GetDrawRegionWithNewArchitecture(new Rectangle(0, 0, 10, 10));
  77. Assert.NotNull(region);
  78. Application.Shutdown();
  79. }
  80. [Fact]
  81. public void StandardTextFormatter_DirectlyUsed_Works()
  82. {
  83. var formatter = new StandardTextFormatter
  84. {
  85. Text = "Test Text",
  86. Alignment = Alignment.Center
  87. };
  88. FormattedText result = formatter.Format();
  89. Assert.NotNull(result);
  90. Assert.NotEmpty(result.Lines);
  91. Assert.True(result.RequiredSize.Width > 0);
  92. Assert.True(result.RequiredSize.Height > 0);
  93. }
  94. [Fact]
  95. public void StandardTextRenderer_DirectlyUsed_Works()
  96. {
  97. Application.Init(new FakeDriver());
  98. var formatter = new StandardTextFormatter
  99. {
  100. Text = "Test Text"
  101. };
  102. var renderer = new StandardTextRenderer();
  103. FormattedText formattedText = formatter.Format();
  104. // Should not throw
  105. renderer.Draw(
  106. formattedText,
  107. new Rectangle(0, 0, 10, 1),
  108. Attribute.Default,
  109. Attribute.Default);
  110. Region region = renderer.GetDrawRegion(
  111. formattedText,
  112. new Rectangle(0, 0, 10, 1));
  113. Assert.NotNull(region);
  114. Application.Shutdown();
  115. }
  116. [Fact]
  117. public void TextFormatter_UseNewArchitecture_Flag_Works()
  118. {
  119. Application.Init(new FakeDriver());
  120. var tf = new TextFormatter
  121. {
  122. Text = "Hello World",
  123. UseNewArchitecture = true // Enable new architecture
  124. };
  125. // This should now use the new architecture via the Draw method
  126. tf.Draw(new Rectangle(0, 0, 10, 1), Attribute.Default, Attribute.Default);
  127. // Test that the new architecture produces results
  128. Size size = tf.GetFormattedSizeWithNewArchitecture();
  129. Assert.True(size.Width > 0);
  130. Assert.True(size.Height > 0);
  131. Application.Shutdown();
  132. }
  133. }