Application.NavigationTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = new ApplicationNavigation();
  13. Application.Navigation.FocusedChanged += ApplicationNavigationOnFocusedChanged;
  14. Application.Navigation.SetFocused(new View ());
  15. Assert.True (raised);
  16. Application.Navigation.GetFocused().Dispose ();
  17. Application.Navigation.SetFocused(null);
  18. Application.Navigation.FocusedChanged -= ApplicationNavigationOnFocusedChanged;
  19. Application.Navigation = null;
  20. return;
  21. void ApplicationNavigationOnFocusedChanged (object sender, EventArgs e)
  22. {
  23. raised = true;
  24. }
  25. }
  26. [Fact]
  27. public void GetDeepestFocusedSubview_ShouldReturnNull_WhenViewIsNull ()
  28. {
  29. // Act
  30. var result = ApplicationNavigation.GetDeepestFocusedSubview (null);
  31. // Assert
  32. Assert.Null (result);
  33. }
  34. [Fact]
  35. public void GetDeepestFocusedSubview_ShouldReturnSameView_WhenNoSubviewsHaveFocus ()
  36. {
  37. // Arrange
  38. var view = new View () { Id = "view", CanFocus = true }; ;
  39. // Act
  40. var result = ApplicationNavigation.GetDeepestFocusedSubview (view);
  41. // Assert
  42. Assert.Equal (view, result);
  43. }
  44. [Fact]
  45. public void GetDeepestFocusedSubview_ShouldReturnFocusedSubview ()
  46. {
  47. // Arrange
  48. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  49. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  50. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  51. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  52. parentView.Add (childView1, childView2);
  53. childView2.Add (grandChildView);
  54. grandChildView.SetFocus ();
  55. // Act
  56. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  57. // Assert
  58. Assert.Equal (grandChildView, result);
  59. }
  60. [Fact]
  61. public void GetDeepestFocusedSubview_ShouldReturnDeepestFocusedSubview ()
  62. {
  63. // Arrange
  64. var parentView = new View () { Id = "parentView", CanFocus = true }; ;
  65. var childView1 = new View () { Id = "childView1", CanFocus = true }; ;
  66. var childView2 = new View () { Id = "childView2", CanFocus = true }; ;
  67. var grandChildView = new View () { Id = "grandChildView", CanFocus = true }; ;
  68. var greatGrandChildView = new View () { Id = "greatGrandChildView", CanFocus = true }; ;
  69. parentView.Add (childView1, childView2);
  70. childView2.Add (grandChildView);
  71. grandChildView.Add (greatGrandChildView);
  72. grandChildView.SetFocus ();
  73. // Act
  74. var result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  75. // Assert
  76. Assert.Equal (greatGrandChildView, result);
  77. // Arrange
  78. greatGrandChildView.CanFocus = false;
  79. grandChildView.SetFocus ();
  80. // Act
  81. result = ApplicationNavigation.GetDeepestFocusedSubview (parentView);
  82. // Assert
  83. Assert.Equal (grandChildView, result);
  84. }
  85. //[Fact]
  86. //public void MoveNextViewOrTop_ShouldMoveFocusToNextViewOrTop ()
  87. //{
  88. // // Arrange
  89. // var top = new Toplevel ();
  90. // var view1 = new View () { Id = "view1", CanFocus = true };
  91. // var view2 = new View () { Id = "view2", CanFocus = true };
  92. // top.Add (view1, view2);
  93. // Application.Top = top;
  94. // Application.Current = top;
  95. // view1.SetFocus ();
  96. // // Act
  97. // ApplicationNavigation.MoveNextViewOrTop ();
  98. // // Assert
  99. // Assert.True (view2.HasFocus);
  100. // top.Dispose ();
  101. //}
  102. //[Fact]
  103. //public void MovePreviousViewOrTop_ShouldMoveFocusToPreviousViewOrTop ()
  104. //{
  105. // // Arrange
  106. // var top = new Toplevel ();
  107. // var view1 = new View () { Id = "view1", CanFocus = true, TabStop = TabBehavior.TabGroup };
  108. // var view2 = new View () { Id = "view2", CanFocus = true, TabStop = TabBehavior.TabGroup };
  109. // top.Add (view1, view2);
  110. // Application.Top = top;
  111. // Application.Current = top;
  112. // view2.SetFocus ();
  113. // // Act
  114. // ApplicationNavigation.MovePreviousViewOrTop ();
  115. // // Assert
  116. // Assert.True (view1.HasFocus);
  117. // top.Dispose ();
  118. //}
  119. }