TitleTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (expectedOld, args.CurrentValue);
  47. Assert.Equal (r.Title, args.NewValue);
  48. };
  49. expected = "title";
  50. expectedOld = r.Title;
  51. r.Title = expected;
  52. Assert.Equal (expected, r.Title);
  53. r.Dispose ();
  54. }
  55. [Fact]
  56. public void Set_Title_Fires_TitleChanging ()
  57. {
  58. var r = new View ();
  59. Assert.Equal (string.Empty, r.Title);
  60. string expectedOld = null;
  61. string expectedDuring = null;
  62. string expectedAfter = null;
  63. var cancel = false;
  64. r.TitleChanging += (s, args) =>
  65. {
  66. Assert.Equal (expectedOld, args.CurrentValue);
  67. Assert.Equal (expectedDuring, args.NewValue);
  68. args.Cancel = cancel;
  69. };
  70. expectedOld = string.Empty;
  71. r.Title = expectedDuring = expectedAfter = "title";
  72. Assert.Equal (expectedAfter, r.Title);
  73. expectedOld = r.Title;
  74. r.Title = expectedDuring = expectedAfter = "a different title";
  75. Assert.Equal (expectedAfter, r.Title);
  76. // Now setup cancelling the change and change it back to "title"
  77. cancel = true;
  78. expectedOld = r.Title;
  79. r.Title = expectedDuring = "title";
  80. Assert.Equal (expectedAfter, r.Title);
  81. r.Dispose ();
  82. }
  83. // Setting Text does NOT set the HotKey
  84. [Fact]
  85. public void Title_Does_Set_HotKey ()
  86. {
  87. var view = new View { HotKeySpecifier = (Rune)'_', Title = "_Hello World" };
  88. Assert.Equal (Key.H, view.HotKey);
  89. }
  90. }