ShortcutTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using JetBrains.Annotations;
  2. namespace Terminal.Gui.ViewsTests;
  3. [TestSubject (typeof (Shortcut))]
  4. public class ShortcutTests
  5. {
  6. [Fact]
  7. public void Constructor_Defaults ()
  8. {
  9. var shortcut = new Shortcut ();
  10. Assert.NotNull (shortcut);
  11. Assert.True (shortcut.CanFocus);
  12. Assert.IsType<DimAuto> (shortcut.Width);
  13. Assert.IsType<DimAuto> (shortcut.Height);
  14. // TOOD: more
  15. }
  16. [Theory]
  17. [InlineData ("", "", KeyCode.Null, 2)]
  18. [InlineData ("C", "", KeyCode.Null, 3)]
  19. [InlineData ("", "H", KeyCode.Null, 5)]
  20. [InlineData ("", "", KeyCode.K, 5)]
  21. [InlineData ("C", "", KeyCode.K, 6)]
  22. [InlineData ("C", "H", KeyCode.Null, 6)]
  23. [InlineData ("", "H", KeyCode.K, 8)]
  24. [InlineData ("C", "H", KeyCode.K, 9)]
  25. public void NaturalSize (string command, string help, Key key, int expectedWidth)
  26. {
  27. var shortcut = new Shortcut
  28. {
  29. Title = command,
  30. HelpText = help,
  31. Key = key
  32. };
  33. Assert.IsType<DimAuto> (shortcut.Width);
  34. Assert.IsType<DimAuto> (shortcut.Height);
  35. shortcut.LayoutSubviews ();
  36. shortcut.SetRelativeLayout (new (100, 100));
  37. // |0123456789
  38. // | C H K |
  39. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  40. }
  41. [Theory]
  42. [InlineData (5, 0, 3, 6)]
  43. [InlineData (6, 0, 3, 6)]
  44. [InlineData (7, 0, 3, 6)]
  45. [InlineData (8, 0, 3, 6)]
  46. [InlineData (9, 0, 3, 6)]
  47. [InlineData (10, 0, 4, 7)]
  48. [InlineData (11, 0, 5, 8)]
  49. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  50. {
  51. var shortcut = new Shortcut
  52. {
  53. Width = width,
  54. Title = "C",
  55. Text = "H",
  56. Key = Key.K
  57. };
  58. shortcut.LayoutSubviews ();
  59. shortcut.SetRelativeLayout (new (100, 100));
  60. // 0123456789
  61. // -C--H--K-
  62. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  63. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  64. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  65. }
  66. [Fact]
  67. public void CommandView_Text_And_Title_Track ()
  68. {
  69. var shortcut = new Shortcut
  70. {
  71. Title = "T"
  72. };
  73. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  74. shortcut = new ();
  75. shortcut.CommandView = new()
  76. {
  77. Text = "T"
  78. };
  79. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  80. }
  81. [Fact]
  82. public void HelpText_And_Text_Are_The_Same ()
  83. {
  84. var shortcut = new Shortcut
  85. {
  86. Text = "H"
  87. };
  88. Assert.Equal (shortcut.Text, shortcut.HelpText);
  89. shortcut = new()
  90. {
  91. HelpText = "H"
  92. };
  93. Assert.Equal (shortcut.Text, shortcut.HelpText);
  94. }
  95. [Theory]
  96. [InlineData (KeyCode.Null, "")]
  97. [InlineData (KeyCode.F1, "F1")]
  98. public void KeyView_Text_Tracks_Key (Key key, string expected)
  99. {
  100. var shortcut = new Shortcut
  101. {
  102. Key = key
  103. };
  104. Assert.Equal (expected, shortcut.KeyView.Text);
  105. }
  106. // Test Key
  107. [Fact]
  108. public void Key_Defaults_To_Empty ()
  109. {
  110. var shortcut = new Shortcut ();
  111. Assert.Equal (Key.Empty, shortcut.Key);
  112. }
  113. [Fact]
  114. public void Key_Can_Be_Set ()
  115. {
  116. var shortcut = new Shortcut ();
  117. shortcut.Key = Key.F1;
  118. Assert.Equal (Key.F1, shortcut.Key);
  119. }
  120. [Fact]
  121. public void Key_Can_Be_Set_To_Empty ()
  122. {
  123. var shortcut = new Shortcut ();
  124. shortcut.Key = Key.Empty;
  125. Assert.Equal (Key.Empty, shortcut.Key);
  126. }
  127. // Test KeyBindingScope
  128. // Test Key gets bound correctly
  129. [Fact]
  130. public void KeyBindingScope_Defaults_To_HotKey ()
  131. {
  132. var shortcut = new Shortcut ();
  133. Assert.Equal (KeyBindingScope.HotKey, shortcut.KeyBindingScope);
  134. }
  135. [Fact]
  136. public void KeyBindingScope_Can_Be_Set ()
  137. {
  138. var shortcut = new Shortcut ();
  139. shortcut.KeyBindingScope = KeyBindingScope.Application;
  140. Assert.Equal (KeyBindingScope.Application, shortcut.KeyBindingScope);
  141. }
  142. [Fact]
  143. public void Setting_Key_Binds_Key_To_CommandView_Accept ()
  144. {
  145. var shortcut = new Shortcut ();
  146. shortcut.Key = Key.F1;
  147. // TODO:
  148. }
  149. [Theory]
  150. [InlineData (Orientation.Horizontal)]
  151. [InlineData (Orientation.Vertical)]
  152. public void Orientation_SetsCorrectly (Orientation orientation)
  153. {
  154. var shortcut = new Shortcut
  155. {
  156. Orientation = orientation
  157. };
  158. Assert.Equal (orientation, shortcut.Orientation);
  159. }
  160. [Theory]
  161. [InlineData (AlignmentModes.StartToEnd)]
  162. [InlineData (AlignmentModes.EndToStart)]
  163. public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
  164. {
  165. var shortcut = new Shortcut
  166. {
  167. AlignmentModes = alignmentModes
  168. };
  169. Assert.Equal (alignmentModes, shortcut.AlignmentModes);
  170. }
  171. [Fact]
  172. public void Action_SetsAndGetsCorrectly ()
  173. {
  174. var actionInvoked = false;
  175. var shortcut = new Shortcut
  176. {
  177. Action = () => { actionInvoked = true; }
  178. };
  179. shortcut.Action.Invoke ();
  180. Assert.True (actionInvoked);
  181. }
  182. [Fact]
  183. public void ColorScheme_SetsAndGetsCorrectly ()
  184. {
  185. var colorScheme = new ColorScheme ();
  186. var shortcut = new Shortcut
  187. {
  188. ColorScheme = colorScheme
  189. };
  190. Assert.Same (colorScheme, shortcut.ColorScheme);
  191. }
  192. [Fact]
  193. public void Subview_Visibility_Controlled_By_Removal ()
  194. {
  195. var shortcut = new Shortcut ();
  196. Assert.True (shortcut.CommandView.Visible);
  197. Assert.Contains (shortcut.CommandView, shortcut.Subviews);
  198. Assert.True (shortcut.HelpView.Visible);
  199. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  200. Assert.True (shortcut.KeyView.Visible);
  201. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  202. shortcut.HelpText = "help";
  203. Assert.True (shortcut.HelpView.Visible);
  204. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  205. Assert.True (shortcut.KeyView.Visible);
  206. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  207. shortcut.Key = Key.A;
  208. Assert.True (shortcut.HelpView.Visible);
  209. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  210. Assert.True (shortcut.KeyView.Visible);
  211. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  212. shortcut.HelpView.Visible = false;
  213. shortcut.ShowHide ();
  214. Assert.False (shortcut.HelpView.Visible);
  215. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  216. Assert.True (shortcut.KeyView.Visible);
  217. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  218. shortcut.KeyView.Visible = false;
  219. shortcut.ShowHide ();
  220. Assert.False (shortcut.HelpView.Visible);
  221. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  222. Assert.False (shortcut.KeyView.Visible);
  223. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  224. }
  225. }