Application.NavigationTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Moq;
  2. using Xunit.Abstractions;
  3. using Terminal.Gui;
  4. namespace Terminal.Gui.ApplicationTests.NavigationTests;
  5. public class ApplicationNavigationTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [Fact]
  9. public void Focused_Change_Raises_FocusedChanged ()
  10. {
  11. bool raised = false;
  12. Application.Navigation.FocusedChanged += ApplicationNavigationOnFocusedChanged;
  13. Application.Navigation.SetFocused(new View ());
  14. Assert.True (raised);
  15. Application.Navigation.GetFocused().Dispose ();
  16. Application.Navigation.SetFocused(null);
  17. Application.Navigation.FocusedChanged -= ApplicationNavigationOnFocusedChanged;
  18. return;
  19. void ApplicationNavigationOnFocusedChanged (object sender, EventArgs e)
  20. {
  21. raised = true;
  22. }
  23. }
  24. [Fact]
  25. public void GetDeepestFocusedSubview_ShouldReturnNull_WhenViewIsNull ()
  26. {
  27. // Act
  28. var result = ApplicationNavigation.GetDeepestFocusedSubview (null);
  29. // Assert
  30. Assert.Null (result);
  31. }
  32. [Fact]
  33. public void GetDeepestFocusedSubview_ShouldReturnSameView_WhenNoSubviewsHaveFocus ()
  34. {
  35. // Arrange
  36. var view = new View () { Id = "view", CanFocus = true }; ;
  37. // Act
  38. var result = ApplicationNavigation.GetDeepestFocusedSubview (view);
  39. // Assert
  40. Assert.Equal (view, result);
  41. }
  42. [Fact]
  43. public void GetDeepestFocusedSubview_ShouldReturnFocusedSubview ()
  44. {
  45. // Arrange
  46. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  47. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  48. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  49. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  50. parentView.Add (childView1, childView2);
  51. childView2.Add (grandChildView);
  52. grandChildView.SetFocus ();
  53. // Act
  54. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  55. // Assert
  56. Assert.Equal (grandChildView, result);
  57. }
  58. [Fact]
  59. public void GetDeepestFocusedSubview_ShouldReturnDeepestFocusedSubview ()
  60. {
  61. // Arrange
  62. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  63. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  64. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  65. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  66. var greatGrandChildView = new View () { Id = "greatGrandChildView", CanFocus = true }; ;
  67. parentView.Add (childView1, childView2);
  68. childView2.Add (grandChildView);
  69. grandChildView.Add (greatGrandChildView);
  70. grandChildView.SetFocus ();
  71. // Act
  72. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  73. // Assert
  74. Assert.Equal (greatGrandChildView, result);
  75. // Arrange
  76. greatGrandChildView.CanFocus = false;
  77. grandChildView.SetFocus ();
  78. // Act
  79. result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  80. // Assert
  81. Assert.Equal (grandChildView, result);
  82. }
  83. [Fact]
  84. public void MoveNextView_ShouldMoveFocusToNextView ()
  85. {
  86. // Arrange
  87. var top = new Toplevel ();
  88. var view1 = new View () { Id = "view1", CanFocus = true };
  89. var view2 = new View () { Id = "view2", CanFocus = true };
  90. top.Add (view1, view2);
  91. Application.Top = top;
  92. Application.Current = top;
  93. view1.SetFocus ();
  94. // Act
  95. ApplicationNavigation.MoveNextView ();
  96. // Assert
  97. Assert.True (view2.HasFocus);
  98. top.Dispose ();
  99. }
  100. [Fact]
  101. public void MoveNextViewOrTop_ShouldMoveFocusToNextViewOrTop ()
  102. {
  103. // Arrange
  104. var top = new Toplevel ();
  105. var view1 = new View () { Id = "view1", CanFocus = true };
  106. var view2 = new View () { Id = "view2", CanFocus = true };
  107. top.Add (view1, view2);
  108. Application.Top = top;
  109. Application.Current = top;
  110. view1.SetFocus ();
  111. // Act
  112. ApplicationNavigation.MoveNextViewOrTop ();
  113. // Assert
  114. Assert.True (view2.HasFocus);
  115. top.Dispose ();
  116. }
  117. [Fact]
  118. public void MovePreviousView_ShouldMoveFocusToPreviousView ()
  119. {
  120. // Arrange
  121. var top = new Toplevel ();
  122. var view1 = new View () { Id = "view1", CanFocus = true };
  123. var view2 = new View () { Id = "view2", CanFocus = true };
  124. top.Add (view1, view2);
  125. Application.Top = top;
  126. Application.Current = top;
  127. view2.SetFocus ();
  128. // Act
  129. ApplicationNavigation.MovePreviousView ();
  130. // Assert
  131. Assert.True (view1.HasFocus);
  132. top.Dispose ();
  133. }
  134. [Fact]
  135. public void MovePreviousViewOrTop_ShouldMoveFocusToPreviousViewOrTop ()
  136. {
  137. // Arrange
  138. var top = new Toplevel ();
  139. var view1 = new View () { Id = "view1", CanFocus = true, TabStop = TabBehavior.TabGroup };
  140. var view2 = new View () { Id = "view2", CanFocus = true, TabStop = TabBehavior.TabGroup };
  141. top.Add (view1, view2);
  142. Application.Top = top;
  143. Application.Current = top;
  144. view2.SetFocus ();
  145. // Act
  146. ApplicationNavigation.MovePreviousViewOrTop ();
  147. // Assert
  148. Assert.True (view1.HasFocus);
  149. top.Dispose ();
  150. }
  151. }