WindowTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. using NStack;
  8. namespace Terminal.Gui.Core {
  9. public class WindowTests {
  10. readonly ITestOutputHelper output;
  11. public WindowTests (ITestOutputHelper output)
  12. {
  13. this.output = output;
  14. }
  15. [Fact]
  16. public void New_Initializes ()
  17. {
  18. // Parameterless
  19. var r = new Window ();
  20. Assert.NotNull (r);
  21. Assert.Equal(ustring.Empty, r.Title);
  22. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  23. Assert.Equal ("Window()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  24. Assert.True (r.CanFocus);
  25. Assert.False (r.HasFocus);
  26. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  27. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  28. Assert.Null (r.Focused);
  29. Assert.NotNull (r.ColorScheme);
  30. Assert.Equal (Dim.Fill (0), r.Width);
  31. Assert.Equal (Dim.Fill (0), r.Height);
  32. Assert.Null (r.X);
  33. Assert.Null (r.Y);
  34. Assert.False (r.IsCurrentTop);
  35. Assert.Empty (r.Id);
  36. Assert.NotEmpty (r.Subviews);
  37. Assert.False (r.WantContinuousButtonPressed);
  38. Assert.False (r.WantMousePositionReports);
  39. Assert.Null (r.SuperView);
  40. Assert.Null (r.MostFocused);
  41. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  42. // Empty Rect
  43. r = new Window (Rect.Empty, "title");
  44. Assert.NotNull (r);
  45. Assert.Equal ("title", r.Title);
  46. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  47. Assert.Equal ("Window()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  48. Assert.True (r.CanFocus);
  49. Assert.False (r.HasFocus);
  50. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  51. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  52. Assert.Null (r.Focused);
  53. Assert.NotNull (r.ColorScheme);
  54. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  55. Assert.Null (r.Height); // avoiding Dim errors.
  56. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  57. Assert.Null (r.Y); // avoiding Pos errors.
  58. Assert.False (r.IsCurrentTop);
  59. Assert.Empty (r.Id);
  60. Assert.NotEmpty (r.Subviews);
  61. Assert.False (r.WantContinuousButtonPressed);
  62. Assert.False (r.WantMousePositionReports);
  63. Assert.Null (r.SuperView);
  64. Assert.Null (r.MostFocused);
  65. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  66. // Rect with values
  67. r = new Window (new Rect (1, 2, 3, 4), "title");
  68. Assert.Equal ("title", r.Title);
  69. Assert.NotNull (r);
  70. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  71. Assert.Equal ("Window()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  72. Assert.True (r.CanFocus);
  73. Assert.False (r.HasFocus);
  74. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  75. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  76. Assert.Null (r.Focused);
  77. Assert.NotNull (r.ColorScheme);
  78. Assert.Null (r.Width);
  79. Assert.Null (r.Height);
  80. Assert.Null (r.X);
  81. Assert.Null (r.Y);
  82. Assert.False (r.IsCurrentTop);
  83. Assert.Empty (r.Id);
  84. Assert.NotEmpty (r.Subviews);
  85. Assert.False (r.WantContinuousButtonPressed);
  86. Assert.False (r.WantMousePositionReports);
  87. Assert.Null (r.SuperView);
  88. Assert.Null (r.MostFocused);
  89. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  90. r.Dispose ();
  91. }
  92. [Fact]
  93. public void Set_Title_Fires_TitleChanging ()
  94. {
  95. var r = new Window ();
  96. Assert.Equal (ustring.Empty, r.Title);
  97. string expectedOld = null;
  98. string expectedDuring = null;
  99. string expectedAfter = null;
  100. bool cancel = false;
  101. r.TitleChanging += (args) => {
  102. Assert.Equal (expectedOld, args.OldTitle);
  103. Assert.Equal (expectedDuring, args.NewTitle);
  104. args.Cancel = cancel;
  105. };
  106. expectedOld = string.Empty;
  107. r.Title = expectedDuring = expectedAfter = "title";
  108. Assert.Equal (expectedAfter, r.Title.ToString ());
  109. expectedOld = r.Title.ToString();
  110. r.Title = expectedDuring = expectedAfter = "a different title";
  111. Assert.Equal (expectedAfter, r.Title.ToString ());
  112. // Now setup cancelling the change and change it back to "title"
  113. cancel = true;
  114. expectedOld = r.Title.ToString();
  115. r.Title = expectedDuring = "title";
  116. Assert.Equal (expectedAfter, r.Title.ToString ());
  117. r.Dispose ();
  118. }
  119. [Fact]
  120. public void Set_Title_Fires_TitleChanged ()
  121. {
  122. var r = new Window ();
  123. Assert.Equal (ustring.Empty, r.Title);
  124. string expectedOld = null;
  125. string expected = null;
  126. r.TitleChanged += (args) => {
  127. Assert.Equal (expectedOld, args.OldTitle);
  128. Assert.Equal (r.Title, args.NewTitle);
  129. };
  130. expected = "title";
  131. expectedOld = r.Title.ToString ();
  132. r.Title = expected;
  133. Assert.Equal (expected, r.Title.ToString ());
  134. expected = "another title";
  135. expectedOld = r.Title.ToString ();
  136. r.Title = expected;
  137. Assert.Equal (expected, r.Title.ToString ());
  138. r.Dispose ();
  139. }
  140. }
  141. }