AddRemoveTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllViews
  4. {
  5. [Fact]
  6. public void Add_Subview_Gets_Focus ()
  7. {
  8. View top = new View ()
  9. {
  10. Id = "top",
  11. CanFocus = true
  12. };
  13. top.SetFocus ();
  14. Assert.True (top.HasFocus);
  15. int nEnter = 0;
  16. View subView = new View ()
  17. {
  18. Id = "subView",
  19. CanFocus = true
  20. };
  21. subView.HasFocusChanging += (s, e) => nEnter++;
  22. top.Add (subView);
  23. Assert.True (top.HasFocus);
  24. Assert.Equal (subView, top.Focused);
  25. Assert.True (subView.HasFocus);
  26. Assert.Equal (1, nEnter);
  27. }
  28. [Fact]
  29. public void Add_Subview_Deepest_Gets_Focus ()
  30. {
  31. View top = new View ()
  32. {
  33. Id = "top",
  34. CanFocus = true
  35. };
  36. top.SetFocus ();
  37. Assert.True (top.HasFocus);
  38. View subView = new View ()
  39. {
  40. Id = "subView",
  41. CanFocus = true
  42. };
  43. View subSubView = new View ()
  44. {
  45. Id = "subSubView",
  46. CanFocus = true
  47. };
  48. subView.Add (subSubView);
  49. top.Add (subView);
  50. Assert.True (top.HasFocus);
  51. Assert.Equal (subView, top.Focused);
  52. Assert.True (subView.HasFocus);
  53. Assert.True (subSubView.HasFocus);
  54. }
  55. [Fact]
  56. public void Remove_Focused_Subview_Keeps_Focus_And_SubView_Looses_Focus ()
  57. {
  58. View top = new View ()
  59. {
  60. Id = "top",
  61. CanFocus = true
  62. };
  63. View subView = new View ()
  64. {
  65. Id = "subView",
  66. CanFocus = true
  67. };
  68. top.Add (subView);
  69. top.SetFocus ();
  70. Assert.True (top.HasFocus);
  71. Assert.Equal (subView, top.Focused);
  72. Assert.True (subView.HasFocus);
  73. top.Remove (subView);
  74. Assert.True (top.HasFocus);
  75. Assert.Equal (null, top.Focused);
  76. Assert.False (subView.HasFocus);
  77. }
  78. [Fact]
  79. public void Remove_Focused_Subview_Keeps_Focus_And_SubView_Looses_Focus_And_Next_Gets_Focus ()
  80. {
  81. View top = new View ()
  82. {
  83. Id = "top",
  84. CanFocus = true
  85. };
  86. View subView1 = new View ()
  87. {
  88. Id = "subView1",
  89. CanFocus = true
  90. };
  91. View subView2 = new View ()
  92. {
  93. Id = "subView2",
  94. CanFocus = true
  95. };
  96. top.Add (subView1, subView2);
  97. top.SetFocus ();
  98. Assert.True (top.HasFocus);
  99. Assert.Equal (subView1, top.Focused);
  100. Assert.True (subView1.HasFocus);
  101. Assert.False (subView2.HasFocus);
  102. top.Remove (subView1);
  103. Assert.True (top.HasFocus);
  104. Assert.True (subView2.HasFocus);
  105. Assert.Equal (subView2, top.Focused);
  106. Assert.False (subView1.HasFocus);
  107. }
  108. }