Application.NavigationTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Moq;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ApplicationTests;
  4. public class ApplicationNavigationTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void GetDeepestFocusedSubview_ShouldReturnNull_WhenViewIsNull ()
  9. {
  10. // Act
  11. var result = ApplicationNavigation.GetDeepestFocusedSubview (null);
  12. // Assert
  13. Assert.Null (result);
  14. }
  15. [Fact]
  16. public void GetDeepestFocusedSubview_ShouldReturnSameView_WhenNoSubviewsHaveFocus ()
  17. {
  18. // Arrange
  19. var view = new View () { Id = "view", CanFocus = true }; ;
  20. // Act
  21. var result = ApplicationNavigation.GetDeepestFocusedSubview (view);
  22. // Assert
  23. Assert.Equal (view, result);
  24. }
  25. [Fact]
  26. public void GetDeepestFocusedSubview_ShouldReturnFocusedSubview ()
  27. {
  28. // Arrange
  29. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  30. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  31. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  32. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  33. parentView.Add (childView1, childView2);
  34. childView2.Add (grandChildView);
  35. grandChildView.SetFocus ();
  36. // Act
  37. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  38. // Assert
  39. Assert.Equal (grandChildView, result);
  40. }
  41. [Fact]
  42. public void GetDeepestFocusedSubview_ShouldReturnDeepestFocusedSubview ()
  43. {
  44. // Arrange
  45. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  46. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  47. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  48. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  49. var greatGrandChildView = new View () { Id = "greatGrandChildView", CanFocus = true }; ;
  50. parentView.Add (childView1, childView2);
  51. childView2.Add (grandChildView);
  52. grandChildView.Add (greatGrandChildView);
  53. grandChildView.SetFocus ();
  54. // Act
  55. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  56. // Assert
  57. Assert.Equal (greatGrandChildView, result);
  58. // Arrange
  59. greatGrandChildView.CanFocus = false;
  60. grandChildView.SetFocus ();
  61. // Act
  62. result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  63. // Assert
  64. Assert.Equal (grandChildView, result);
  65. }
  66. [Fact]
  67. public void MoveNextView_ShouldMoveFocusToNextView ()
  68. {
  69. // Arrange
  70. var top = new Toplevel ();
  71. var view1 = new View () { Id = "view1", CanFocus = true };
  72. var view2 = new View () { Id = "view2", CanFocus = true };
  73. top.Add (view1, view2);
  74. Application.Top = top;
  75. Application.Current = top;
  76. view1.SetFocus ();
  77. // Act
  78. ApplicationNavigation.MoveNextView ();
  79. // Assert
  80. Assert.True (view2.HasFocus);
  81. top.Dispose ();
  82. }
  83. [Fact]
  84. public void MoveNextViewOrTop_ShouldMoveFocusToNextViewOrTop ()
  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.MoveNextViewOrTop ();
  96. // Assert
  97. Assert.True (view2.HasFocus);
  98. top.Dispose ();
  99. }
  100. [Fact]
  101. public void MovePreviousView_ShouldMoveFocusToPreviousView ()
  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. view2.SetFocus ();
  111. // Act
  112. ApplicationNavigation.MovePreviousView ();
  113. // Assert
  114. Assert.True (view1.HasFocus);
  115. top.Dispose ();
  116. }
  117. [Fact]
  118. public void MovePreviousViewOrTop_ShouldMoveFocusToPreviousViewOrTop ()
  119. {
  120. // Arrange
  121. var top = new Toplevel ();
  122. var view1 = new View () { Id = "view1", CanFocus = true, TabStop = TabBehavior.TabGroup };
  123. var view2 = new View () { Id = "view2", CanFocus = true, TabStop = TabBehavior.TabGroup };
  124. top.Add (view1, view2);
  125. Application.Top = top;
  126. Application.Current = top;
  127. view2.SetFocus ();
  128. // Act
  129. ApplicationNavigation.MovePreviousViewOrTop ();
  130. // Assert
  131. Assert.True (view1.HasFocus);
  132. top.Dispose ();
  133. }
  134. }