TextFieldTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. using System.Text;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class TextFieldTests
  4. {
  5. [Fact]
  6. public void Cancel_TextChanging_ThenBackspace ()
  7. {
  8. var tf = new TextField ();
  9. tf.SetFocus ();
  10. tf.NewKeyDownEvent (Key.A.WithShift);
  11. Assert.Equal ("A", tf.Text);
  12. // cancel the next keystroke
  13. tf.TextChanging += (s, e) => e.Cancel = e.NewValue == "AB";
  14. tf.NewKeyDownEvent (Key.B.WithShift);
  15. // B was canceled so should just be A
  16. Assert.Equal ("A", tf.Text);
  17. // now delete the A
  18. tf.NewKeyDownEvent (Key.Backspace);
  19. Assert.Equal ("", tf.Text);
  20. }
  21. [Fact]
  22. public void HistoryText_IsDirty_ClearHistoryChanges ()
  23. {
  24. var text = "Testing";
  25. var tf = new TextField { Text = text };
  26. tf.BeginInit ();
  27. tf.EndInit ();
  28. Assert.Equal (text, tf.Text);
  29. tf.ClearHistoryChanges ();
  30. Assert.False (tf.IsDirty);
  31. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift));
  32. Assert.Equal ($"{text}A", tf.Text);
  33. Assert.True (tf.IsDirty);
  34. }
  35. [Fact]
  36. public void Space_Does_Not_Raise_Selected ()
  37. {
  38. TextField tf = new ();
  39. tf.Selecting += (sender, args) => Assert.Fail ("Selected should not be raied.");
  40. Toplevel top = new ();
  41. top.Add (tf);
  42. tf.SetFocus ();
  43. top.NewKeyDownEvent (Key.Space);
  44. top.Dispose ();
  45. }
  46. [Fact]
  47. public void Enter_Does_Not_Raise_Selected ()
  48. {
  49. TextField tf = new ();
  50. var selectingCount = 0;
  51. tf.Selecting += (sender, args) => selectingCount++;
  52. Toplevel top = new ();
  53. top.Add (tf);
  54. tf.SetFocus ();
  55. top.NewKeyDownEvent (Key.Enter);
  56. Assert.Equal (0, selectingCount);
  57. top.Dispose ();
  58. }
  59. [Fact]
  60. public void Enter_Raises_Accepted ()
  61. {
  62. TextField tf = new ();
  63. var acceptedCount = 0;
  64. tf.Accepting += (sender, args) => acceptedCount++;
  65. Toplevel top = new ();
  66. top.Add (tf);
  67. tf.SetFocus ();
  68. top.NewKeyDownEvent (Key.Enter);
  69. Assert.Equal (1, acceptedCount);
  70. top.Dispose ();
  71. }
  72. [Fact]
  73. public void HotKey_Command_SetsFocus ()
  74. {
  75. var view = new TextField ();
  76. view.CanFocus = true;
  77. Assert.False (view.HasFocus);
  78. view.InvokeCommand (Command.HotKey);
  79. Assert.True (view.HasFocus);
  80. }
  81. [Fact]
  82. public void HotKey_Command_Does_Not_Accept ()
  83. {
  84. var view = new TextField ();
  85. var accepted = false;
  86. view.Accepting += OnAccept;
  87. view.InvokeCommand (Command.HotKey);
  88. Assert.False (accepted);
  89. return;
  90. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  91. }
  92. [Fact]
  93. public void Accepted_Command_Fires_Accept ()
  94. {
  95. var view = new TextField ();
  96. var accepted = false;
  97. view.Accepting += Accept;
  98. view.InvokeCommand (Command.Accept);
  99. Assert.True (accepted);
  100. return;
  101. void Accept (object sender, CommandEventArgs e) { accepted = true; }
  102. }
  103. [Fact]
  104. public void Accepted_No_Handler_Enables_Default_Button_Accept ()
  105. {
  106. var superView = new Window
  107. {
  108. Id = "superView"
  109. };
  110. var tf = new TextField
  111. {
  112. Id = "tf"
  113. };
  114. var button = new Button
  115. {
  116. Id = "button",
  117. IsDefault = true
  118. };
  119. superView.Add (tf, button);
  120. var buttonAccept = 0;
  121. button.Accepting += ButtonAccept;
  122. tf.SetFocus ();
  123. Assert.True (tf.HasFocus);
  124. superView.NewKeyDownEvent (Key.Enter);
  125. Assert.Equal (1, buttonAccept);
  126. button.SetFocus ();
  127. superView.NewKeyDownEvent (Key.Enter);
  128. Assert.Equal (2, buttonAccept);
  129. return;
  130. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  131. }
  132. [Fact]
  133. public void Accepted_Cancel_Event_HandlesCommand ()
  134. {
  135. //var super = new View ();
  136. var view = new TextField ();
  137. //super.Add (view);
  138. //var superAcceptedInvoked = false;
  139. var tfAcceptedInvoked = false;
  140. var handle = false;
  141. view.Accepting += TextViewAccept;
  142. Assert.False (view.InvokeCommand (Command.Accept));
  143. Assert.True (tfAcceptedInvoked);
  144. tfAcceptedInvoked = false;
  145. handle = true;
  146. view.Accepting += TextViewAccept;
  147. Assert.True (view.InvokeCommand (Command.Accept));
  148. Assert.True (tfAcceptedInvoked);
  149. return;
  150. void TextViewAccept (object sender, CommandEventArgs e)
  151. {
  152. tfAcceptedInvoked = true;
  153. e.Handled = handle;
  154. }
  155. }
  156. [Fact]
  157. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  158. {
  159. var top = new Toplevel ();
  160. var tf = new TextField { Width = 10 };
  161. top.Add (tf);
  162. Exception exception = Record.Exception (() => tf.SetFocus ());
  163. Assert.Null (exception);
  164. }
  165. [Fact]
  166. public void Backspace_From_End ()
  167. {
  168. var tf = new TextField { Text = "ABC" };
  169. tf.SetFocus ();
  170. Assert.Equal ("ABC", tf.Text);
  171. tf.BeginInit ();
  172. tf.EndInit ();
  173. Assert.Equal (3, tf.CursorPosition);
  174. // now delete the C
  175. tf.NewKeyDownEvent (Key.Backspace);
  176. Assert.Equal ("AB", tf.Text);
  177. Assert.Equal (2, tf.CursorPosition);
  178. // then delete the B
  179. tf.NewKeyDownEvent (Key.Backspace);
  180. Assert.Equal ("A", tf.Text);
  181. Assert.Equal (1, tf.CursorPosition);
  182. // then delete the A
  183. tf.NewKeyDownEvent (Key.Backspace);
  184. Assert.Equal ("", tf.Text);
  185. Assert.Equal (0, tf.CursorPosition);
  186. }
  187. [Fact]
  188. public void Backspace_From_Middle ()
  189. {
  190. var tf = new TextField { Text = "ABC" };
  191. tf.SetFocus ();
  192. tf.CursorPosition = 2;
  193. Assert.Equal ("ABC", tf.Text);
  194. // now delete the B
  195. tf.NewKeyDownEvent (Key.Backspace);
  196. Assert.Equal ("AC", tf.Text);
  197. // then delete the A
  198. tf.NewKeyDownEvent (Key.Backspace);
  199. Assert.Equal ("C", tf.Text);
  200. // then delete nothing
  201. tf.NewKeyDownEvent (Key.Backspace);
  202. Assert.Equal ("C", tf.Text);
  203. // now delete the C
  204. tf.CursorPosition = 1;
  205. tf.NewKeyDownEvent (Key.Backspace);
  206. Assert.Equal ("", tf.Text);
  207. }
  208. [Fact]
  209. public void KeyDown_Handled_Prevents_Input ()
  210. {
  211. var tf = new TextField ();
  212. tf.KeyDown += HandleJKey;
  213. tf.NewKeyDownEvent (Key.A);
  214. Assert.Equal ("a", tf.Text);
  215. // SuppressKey suppresses the 'j' key
  216. tf.NewKeyDownEvent (Key.J);
  217. Assert.Equal ("a", tf.Text);
  218. tf.KeyDown -= HandleJKey;
  219. // Now that the delegate has been removed we can type j again
  220. tf.NewKeyDownEvent (Key.J);
  221. Assert.Equal ("aj", tf.Text);
  222. return;
  223. void HandleJKey (object s, Key arg)
  224. {
  225. if (arg.AsRune == new Rune ('j'))
  226. {
  227. arg.Handled = true;
  228. }
  229. }
  230. }
  231. [InlineData ("a")] // Lower than selection
  232. [InlineData ("aaaaaaaaaaa")] // Greater than selection
  233. [InlineData ("aaaa")] // Equal than selection
  234. [Theory]
  235. public void SetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
  236. {
  237. var tf = new TextField ();
  238. tf.Text = "fish";
  239. tf.CursorPosition = tf.Text.Length;
  240. tf.NewKeyDownEvent (Key.CursorLeft);
  241. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  242. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  243. Assert.Equal (1, tf.CursorPosition);
  244. Assert.Equal (2, tf.SelectedLength);
  245. Assert.Equal ("is", tf.SelectedText);
  246. tf.Text = newText;
  247. tf.CursorPosition = tf.Text.Length;
  248. Assert.Equal (newText.Length, tf.CursorPosition);
  249. Assert.Equal (0, tf.SelectedLength);
  250. Assert.Null (tf.SelectedText);
  251. }
  252. [Fact]
  253. public void SpaceHandling ()
  254. {
  255. var tf = new TextField { Width = 10, Text = " " };
  256. var ev = new MouseEventArgs { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
  257. tf.NewMouseEvent (ev);
  258. Assert.Equal (1, tf.SelectedLength);
  259. ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
  260. tf.NewMouseEvent (ev);
  261. Assert.Equal (1, tf.SelectedLength);
  262. }
  263. [Fact]
  264. public void WordBackward_WordForward_Mixed ()
  265. {
  266. var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
  267. tf.BeginInit ();
  268. tf.EndInit ();
  269. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  270. Assert.Equal (15, tf.CursorPosition);
  271. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  272. Assert.Equal (12, tf.CursorPosition);
  273. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  274. Assert.Equal (10, tf.CursorPosition);
  275. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  276. Assert.Equal (5, tf.CursorPosition);
  277. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  278. Assert.Equal (0, tf.CursorPosition);
  279. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  280. Assert.Equal (5, tf.CursorPosition);
  281. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  282. Assert.Equal (10, tf.CursorPosition);
  283. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  284. Assert.Equal (12, tf.CursorPosition);
  285. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  286. Assert.Equal (15, tf.CursorPosition);
  287. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  288. Assert.Equal (22, tf.CursorPosition);
  289. }
  290. [Fact]
  291. public void WordBackward_WordForward_SelectedText_With_Accent ()
  292. {
  293. var text = "Les Misérables movie.";
  294. var tf = new TextField { Width = 30, Text = text };
  295. Assert.Equal (21, text.Length);
  296. Assert.Equal (21, tf.Text.GetRuneCount ());
  297. Assert.Equal (21, tf.Text.GetColumns ());
  298. List<Rune> runes = tf.Text.ToRuneList ();
  299. Assert.Equal (21, runes.Count);
  300. Assert.Equal (21, tf.Text.Length);
  301. for (var i = 0; i < runes.Count; i++)
  302. {
  303. char cs = text [i];
  304. var cus = (char)runes [i].Value;
  305. Assert.Equal (cs, cus);
  306. }
  307. var idx = 15;
  308. Assert.Equal ('m', text [idx]);
  309. Assert.Equal ('m', (char)runes [idx].Value);
  310. Assert.Equal ("m", runes [idx].ToString ());
  311. Assert.True (
  312. tf.NewMouseEvent (
  313. new () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  314. )
  315. );
  316. Assert.Equal ("movie.", tf.SelectedText);
  317. Assert.True (
  318. tf.NewMouseEvent (
  319. new () { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  320. )
  321. );
  322. Assert.Equal ("movie.", tf.SelectedText);
  323. }
  324. [Fact]
  325. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  326. {
  327. View superView = new ()
  328. {
  329. CanFocus = true
  330. };
  331. TextField t = new ();
  332. superView.Add (t);
  333. Assert.Single (superView.SubViews);
  334. superView.BeginInit ();
  335. superView.EndInit ();
  336. Assert.Equal (2, superView.SubViews.Count);
  337. }
  338. [Fact]
  339. public void Autocomplete__Added_To_SuperView_On_Add ()
  340. {
  341. View superView = new ()
  342. {
  343. CanFocus = true,
  344. Id = "superView"
  345. };
  346. superView.BeginInit ();
  347. superView.EndInit ();
  348. Assert.Empty (superView.SubViews);
  349. TextField t = new ()
  350. {
  351. Id = "t"
  352. };
  353. superView.Add (t);
  354. Assert.Equal (2, superView.SubViews.Count);
  355. }
  356. [Fact]
  357. public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
  358. {
  359. var tf = new TextField
  360. {
  361. Text = "Hello"
  362. };
  363. tf.SetFocus ();
  364. tf.SelectAll ();
  365. tf.CursorPosition = 5;
  366. // When there is selected text and the cursor is at the end of the text field
  367. Assert.Equal ("Hello", tf.SelectedText);
  368. // Pressing right should not move focus, instead it should clear selection
  369. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  370. Assert.Null (tf.SelectedText);
  371. // Now that the selection is cleared another right keypress should move focus
  372. Assert.False (tf.NewKeyDownEvent (Key.CursorRight));
  373. }
  374. [Fact]
  375. public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
  376. {
  377. var tf = new TextField
  378. {
  379. Text = "Hello"
  380. };
  381. tf.SetFocus ();
  382. tf.CursorPosition = 2;
  383. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  384. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  385. // When there is selected text and the cursor is at the start of the text field
  386. Assert.Equal ("He", tf.SelectedText);
  387. // Pressing left should not move focus, instead it should clear selection
  388. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  389. Assert.Null (tf.SelectedText);
  390. // When clearing selected text with left the cursor should be at the start of the selection
  391. Assert.Equal (0, tf.CursorPosition);
  392. // Now that the selection is cleared another left keypress should move focus
  393. Assert.False (tf.NewKeyDownEvent (Key.CursorLeft));
  394. }
  395. [Fact]
  396. public void Autocomplete_Visible_False_By_Default ()
  397. {
  398. View superView = new ()
  399. {
  400. CanFocus = true
  401. };
  402. TextField t = new ();
  403. superView.Add (t);
  404. superView.BeginInit ();
  405. superView.EndInit ();
  406. Assert.Equal (2, superView.SubViews.Count);
  407. Assert.True (t.Visible);
  408. Assert.False (t.Autocomplete.Visible);
  409. }
  410. [Fact]
  411. public void InsertText_Bmp_SurrogatePair_Non_Bmp_Invalid_SurrogatePair ()
  412. {
  413. var tf = new TextField ();
  414. //📄 == \ud83d\udcc4 == \U0001F4C4
  415. // � == Rune.ReplacementChar
  416. tf.InsertText ("aA,;\ud83d\udcc4\U0001F4C4\udcc4\ud83d");
  417. Assert.Equal ("aA,;📄📄��", tf.Text);
  418. }
  419. [Fact]
  420. public void PositionCursor_Respect_GetColumns ()
  421. {
  422. var tf = new TextField { Width = 5 };
  423. tf.BeginInit ();
  424. tf.EndInit ();
  425. tf.NewKeyDownEvent (new ("📄"));
  426. Assert.Equal (1, tf.CursorPosition);
  427. Assert.Equal (new (2, 0), tf.PositionCursor ());
  428. Assert.Equal ("📄", tf.Text);
  429. tf.NewKeyDownEvent (new (KeyCode.A));
  430. Assert.Equal (2, tf.CursorPosition);
  431. Assert.Equal (new (3, 0), tf.PositionCursor ());
  432. Assert.Equal ("📄a", tf.Text);
  433. }
  434. }