CanFocusTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnitTests;
  2. namespace Terminal.Gui.ViewTests;
  3. public class CanFocusTests
  4. {
  5. // TODO: Figure out what this test is supposed to be testing
  6. [Fact]
  7. public void CanFocus_Faced_With_Container_Before_Run ()
  8. {
  9. Application.Init (new FakeDriver ());
  10. Toplevel t = new ();
  11. var w = new Window ();
  12. var f = new FrameView ();
  13. var v = new View { CanFocus = true };
  14. f.Add (v);
  15. w.Add (f);
  16. t.Add (w);
  17. Assert.True (t.CanFocus);
  18. Assert.True (w.CanFocus);
  19. Assert.True (f.CanFocus);
  20. Assert.True (v.CanFocus);
  21. f.CanFocus = false;
  22. Assert.False (f.CanFocus);
  23. Assert.True (v.CanFocus);
  24. v.CanFocus = false;
  25. Assert.False (f.CanFocus);
  26. Assert.False (v.CanFocus);
  27. v.CanFocus = true;
  28. Assert.False (f.CanFocus);
  29. Assert.True (v.CanFocus);
  30. Application.Iteration += (s, a) => Application.RequestStop ();
  31. Application.Run (t);
  32. t.Dispose ();
  33. Application.Shutdown ();
  34. }
  35. //[Fact]
  36. //public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  37. //{
  38. // var r = new View ();
  39. // var v1 = new View { Text = "1" };
  40. // var v2 = new View { Text = "2" };
  41. // var v3 = new View { Text = "3" };
  42. // r.Add (v1, v2, v3);
  43. // v2.CanFocus = true;
  44. // Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  45. // Assert.Equal (0, v2.TabIndex);
  46. // Assert.Equal (TabBehavior.TabStop, v2.TabStop);
  47. // v1.CanFocus = true;
  48. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  49. // Assert.Equal (1, v1.TabIndex);
  50. // Assert.Equal (TabBehavior.TabStop, v1.TabStop);
  51. // v1.TabIndex = 2;
  52. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  53. // Assert.Equal (1, v1.TabIndex);
  54. // v3.CanFocus = true;
  55. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  56. // Assert.Equal (1, v1.TabIndex);
  57. // Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  58. // Assert.Equal (2, v3.TabIndex);
  59. // Assert.Equal (TabBehavior.TabStop, v3.TabStop);
  60. // v2.CanFocus = false;
  61. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  62. // Assert.Equal (1, v1.TabIndex);
  63. // Assert.Equal (TabBehavior.TabStop, v1.TabStop);
  64. // Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex); // TabIndex is not changed
  65. // Assert.NotEqual (-1, v2.TabIndex);
  66. // Assert.Equal (TabBehavior.TabStop, v2.TabStop); // TabStop is not changed
  67. // Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  68. // Assert.Equal (2, v3.TabIndex);
  69. // Assert.Equal (TabBehavior.TabStop, v3.TabStop);
  70. // r.Dispose ();
  71. //}
  72. [Fact]
  73. public void CanFocus_Set_True_Get_AdvanceFocus_Works ()
  74. {
  75. Label label = new () { Text = "label" };
  76. View view = new () { Text = "view", CanFocus = true };
  77. Application.Navigation = new ();
  78. Application.Top = new ();
  79. Application.Top.Add (label, view);
  80. Application.Top.SetFocus ();
  81. Assert.Equal (view, Application.Navigation.GetFocused ());
  82. Assert.False (label.CanFocus);
  83. Assert.False (label.HasFocus);
  84. Assert.True (view.CanFocus);
  85. Assert.True (view.HasFocus);
  86. Assert.False (Application.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
  87. Assert.False (label.HasFocus);
  88. Assert.True (view.HasFocus);
  89. // Set label CanFocus to true
  90. label.CanFocus = true;
  91. Assert.False (label.HasFocus);
  92. Assert.True (view.HasFocus);
  93. // label can now be focused, so AdvanceFocus should move to it.
  94. Assert.True (Application.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
  95. Assert.True (label.HasFocus);
  96. Assert.False (view.HasFocus);
  97. // Move back to view
  98. view.SetFocus ();
  99. Assert.False (label.HasFocus);
  100. Assert.True (view.HasFocus);
  101. Assert.True (Application.RaiseKeyDownEvent (Key.Tab));
  102. Assert.True (label.HasFocus);
  103. Assert.False (view.HasFocus);
  104. Application.Top.Dispose ();
  105. Application.ResetState ();
  106. }
  107. }