TitleTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. public class TitleTests (ITestOutputHelper output)
  5. {
  6. [SetupFakeDriver]
  7. [Fact]
  8. public void Change_View_Size_Update_Title_Size ()
  9. {
  10. var view = new View
  11. {
  12. Title = "_Hello World",
  13. Width = Dim.Auto (),
  14. Height = Dim.Auto (),
  15. BorderStyle = LineStyle.Single
  16. };
  17. var top = new Toplevel ();
  18. top.Add (view);
  19. top.BeginInit ();
  20. top.EndInit ();
  21. Assert.Equal (string.Empty, view.Text);
  22. Assert.Equal (new (2, 2), view.Frame.Size);
  23. top.Draw ();
  24. TestHelpers.AssertDriverContentsWithFrameAre (
  25. @"
  26. ┌┐
  27. └┘",
  28. output);
  29. var text = "This text will increment the view size and display the title.";
  30. view.Text = text;
  31. top.Draw ();
  32. Assert.Equal (text, view.Text);
  33. // SetupFakeDriver only create a screen with 25 cols and 25 rows
  34. Assert.Equal (new (text.Length, 1), view.GetContentSize ());
  35. top.Dispose ();
  36. }
  37. [Fact]
  38. public void Set_Title_Fires_TitleChanged ()
  39. {
  40. var r = new View ();
  41. Assert.Equal (string.Empty, r.Title);
  42. string expectedOld = null;
  43. string expected = null;
  44. r.TitleChanged += (s, args) =>
  45. {
  46. Assert.Equal (r.Title, args.CurrentValue);
  47. };
  48. expected = "title";
  49. expectedOld = r.Title;
  50. r.Title = expected;
  51. Assert.Equal (expected, r.Title);
  52. r.Dispose ();
  53. }
  54. [Fact]
  55. public void Set_Title_Fires_TitleChanging ()
  56. {
  57. var r = new View ();
  58. Assert.Equal (string.Empty, r.Title);
  59. string expectedOld = null;
  60. string expectedDuring = null;
  61. string expectedAfter = null;
  62. var cancel = false;
  63. r.TitleChanging += (s, args) =>
  64. {
  65. Assert.Equal (expectedOld, args.CurrentValue);
  66. Assert.Equal (expectedDuring, args.NewValue);
  67. args.Cancel = cancel;
  68. };
  69. expectedOld = string.Empty;
  70. r.Title = expectedDuring = expectedAfter = "title";
  71. Assert.Equal (expectedAfter, r.Title);
  72. expectedOld = r.Title;
  73. r.Title = expectedDuring = expectedAfter = "a different title";
  74. Assert.Equal (expectedAfter, r.Title);
  75. // Now setup cancelling the change and change it back to "title"
  76. cancel = true;
  77. expectedOld = r.Title;
  78. r.Title = expectedDuring = "title";
  79. Assert.Equal (expectedAfter, r.Title);
  80. r.Dispose ();
  81. }
  82. // Setting Text does NOT set the HotKey
  83. [Fact]
  84. public void Title_Does_Set_HotKey ()
  85. {
  86. var view = new View { HotKeySpecifier = (Rune)'_', Title = "_Hello World" };
  87. Assert.Equal (Key.H, view.HotKey);
  88. }
  89. }