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