ShortcutTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System.CommandLine;
  2. using JetBrains.Annotations;
  3. using UICatalog.Scenarios;
  4. namespace Terminal.Gui.ViewsTests;
  5. [TestSubject (typeof (Shortcut))]
  6. public class ShortcutTests
  7. {
  8. [Fact]
  9. public void Constructor_Defaults ()
  10. {
  11. Shortcut shortcut = new Shortcut ();
  12. Assert.NotNull (shortcut);
  13. Assert.True (shortcut.CanFocus);
  14. Assert.IsType<DimAuto> (shortcut.Width);
  15. Assert.IsType<DimAuto> (shortcut.Height);
  16. // TOOD: more
  17. }
  18. [Theory]
  19. [InlineData ("", "", KeyCode.Null, 2)]
  20. [InlineData ("C", "", KeyCode.Null, 3)]
  21. [InlineData ("", "H", KeyCode.Null, 5)]
  22. [InlineData ("", "", KeyCode.K, 5)]
  23. [InlineData ("C", "", KeyCode.K, 6)]
  24. [InlineData ("C", "H", KeyCode.Null, 6)]
  25. [InlineData ("", "H", KeyCode.K, 8)]
  26. [InlineData ("C", "H", KeyCode.K, 9)]
  27. public void NaturalSize (string command, string help, Key key, int expectedWidth)
  28. {
  29. Shortcut shortcut = new Shortcut ()
  30. {
  31. Title = command,
  32. HelpText = help,
  33. Key = key,
  34. };
  35. Assert.IsType<DimAuto> (shortcut.Width);
  36. Assert.IsType<DimAuto> (shortcut.Height);
  37. shortcut.LayoutSubviews ();
  38. shortcut.SetRelativeLayout (new Size (100, 100));
  39. // |0123456789
  40. // | C H K |
  41. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  42. }
  43. [Theory]
  44. [InlineData (5, 0, 3, 6)]
  45. [InlineData (6, 0, 3, 6)]
  46. [InlineData (7, 0, 3, 6)]
  47. [InlineData (8, 0, 3, 6)]
  48. [InlineData (9, 0, 3, 6)]
  49. [InlineData (10, 0, 4, 7)]
  50. [InlineData (11, 0, 5, 8)]
  51. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  52. {
  53. Shortcut shortcut = new Shortcut ()
  54. {
  55. Width = width,
  56. Title = "C",
  57. Text = "H",
  58. Key = Key.K
  59. };
  60. shortcut.LayoutSubviews ();
  61. shortcut.SetRelativeLayout (new Size (100, 100));
  62. // 0123456789
  63. // -C--H--K-
  64. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  65. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  66. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  67. }
  68. [Fact]
  69. public void CommandView_Text_And_Title_Track ()
  70. {
  71. Shortcut shortcut = new Shortcut ()
  72. {
  73. Title = "T",
  74. };
  75. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  76. shortcut = new Shortcut ()
  77. {
  78. };
  79. shortcut.CommandView = new View ()
  80. {
  81. Text = "T"
  82. };
  83. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  84. }
  85. [Fact]
  86. public void HelpText_And_Text_Are_The_Same ()
  87. {
  88. Shortcut shortcut = new Shortcut ()
  89. {
  90. Text = "H",
  91. };
  92. Assert.Equal (shortcut.Text, shortcut.HelpText);
  93. shortcut = new Shortcut ()
  94. {
  95. HelpText = "H",
  96. };
  97. Assert.Equal (shortcut.Text, shortcut.HelpText);
  98. }
  99. [Theory]
  100. [InlineData (KeyCode.Null, "")]
  101. [InlineData (KeyCode.F1, "F1")]
  102. public void KeyView_Text_Tracks_Key (Key key, string expected)
  103. {
  104. Shortcut shortcut = new Shortcut ()
  105. {
  106. Key = key,
  107. };
  108. Assert.Equal (expected, shortcut.KeyView.Text);
  109. }
  110. // Test Key
  111. [Fact]
  112. public void Key_Defaults_To_Empty ()
  113. {
  114. Shortcut shortcut = new Shortcut ();
  115. Assert.Equal (Key.Empty, shortcut.Key);
  116. }
  117. [Fact]
  118. public void Key_Can_Be_Set ()
  119. {
  120. Shortcut shortcut = new Shortcut ();
  121. shortcut.Key = Key.F1;
  122. Assert.Equal (Key.F1, shortcut.Key);
  123. }
  124. [Fact]
  125. public void Key_Can_Be_Set_To_Empty ()
  126. {
  127. Shortcut shortcut = new Shortcut ();
  128. shortcut.Key = Key.Empty;
  129. Assert.Equal (Key.Empty, shortcut.Key);
  130. }
  131. // Test KeyBindingScope
  132. // Test Key gets bound correctly
  133. [Fact]
  134. public void KeyBindingScope_Defaults_To_HotKey ()
  135. {
  136. Shortcut shortcut = new Shortcut ();
  137. Assert.Equal (KeyBindingScope.HotKey, shortcut.KeyBindingScope);
  138. }
  139. [Fact]
  140. public void KeyBindingScope_Can_Be_Set ()
  141. {
  142. Shortcut shortcut = new Shortcut ();
  143. shortcut.KeyBindingScope = KeyBindingScope.Application;
  144. Assert.Equal (KeyBindingScope.Application, shortcut.KeyBindingScope);
  145. }
  146. [Fact]
  147. public void Setting_Key_Binds_Key_To_CommandView_Accept ()
  148. {
  149. Shortcut shortcut = new Shortcut ();
  150. shortcut.Key = Key.F1;
  151. // Assert.Equal (Command.Accept, shortcut.CommandView.Get);
  152. }
  153. }