TextFieldTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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.Handled = e.Result == "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. Assert.False (tf.UseSameRuneTypeForWords);
  270. Assert.Equal (22, tf.CursorPosition);
  271. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  272. Assert.Equal (15, tf.CursorPosition);
  273. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  274. Assert.Equal (12, tf.CursorPosition);
  275. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  276. Assert.Equal (10, tf.CursorPosition);
  277. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  278. Assert.Equal (5, tf.CursorPosition);
  279. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  280. Assert.Equal (0, tf.CursorPosition);
  281. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  282. Assert.Equal (5, tf.CursorPosition);
  283. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  284. Assert.Equal (10, tf.CursorPosition);
  285. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  286. Assert.Equal (12, tf.CursorPosition);
  287. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  288. Assert.Equal (15, tf.CursorPosition);
  289. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  290. Assert.Equal (22, tf.CursorPosition);
  291. }
  292. [Fact]
  293. public void WordBackward_WordForward_SelectedText_With_Accent ()
  294. {
  295. var text = "Les Misérables movie.";
  296. var tf = new TextField { Width = 30, Text = text };
  297. Assert.Equal (21, text.Length);
  298. Assert.Equal (21, tf.Text.GetRuneCount ());
  299. Assert.Equal (21, tf.Text.GetColumns ());
  300. List<Rune> runes = tf.Text.ToRuneList ();
  301. Assert.Equal (21, runes.Count);
  302. Assert.Equal (21, tf.Text.Length);
  303. for (var i = 0; i < runes.Count; i++)
  304. {
  305. char cs = text [i];
  306. var cus = (char)runes [i].Value;
  307. Assert.Equal (cs, cus);
  308. }
  309. var idx = 15;
  310. Assert.Equal ('m', text [idx]);
  311. Assert.Equal ('m', (char)runes [idx].Value);
  312. Assert.Equal ("m", runes [idx].ToString ());
  313. Assert.True (
  314. tf.NewMouseEvent (
  315. new () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  316. )
  317. );
  318. Assert.Equal ("movie", tf.SelectedText);
  319. Assert.True (
  320. tf.NewMouseEvent (
  321. new () { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  322. )
  323. );
  324. Assert.Equal ("movie", tf.SelectedText);
  325. }
  326. [Fact]
  327. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  328. {
  329. View superView = new ()
  330. {
  331. CanFocus = true
  332. };
  333. TextField t = new ();
  334. superView.Add (t);
  335. Assert.Single (superView.SubViews);
  336. superView.BeginInit ();
  337. superView.EndInit ();
  338. Assert.Equal (2, superView.SubViews.Count);
  339. }
  340. [Fact]
  341. public void Autocomplete__Added_To_SuperView_On_Add ()
  342. {
  343. View superView = new ()
  344. {
  345. CanFocus = true,
  346. Id = "superView"
  347. };
  348. superView.BeginInit ();
  349. superView.EndInit ();
  350. Assert.Empty (superView.SubViews);
  351. TextField t = new ()
  352. {
  353. Id = "t"
  354. };
  355. superView.Add (t);
  356. Assert.Equal (2, superView.SubViews.Count);
  357. }
  358. [Fact]
  359. public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
  360. {
  361. var tf = new TextField
  362. {
  363. Text = "Hello"
  364. };
  365. tf.SetFocus ();
  366. tf.SelectAll ();
  367. tf.CursorPosition = 5;
  368. // When there is selected text and the cursor is at the end of the text field
  369. Assert.Equal ("Hello", tf.SelectedText);
  370. // Pressing right should not move focus, instead it should clear selection
  371. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  372. Assert.Null (tf.SelectedText);
  373. // Now that the selection is cleared another right keypress should move focus
  374. Assert.False (tf.NewKeyDownEvent (Key.CursorRight));
  375. }
  376. [Fact]
  377. public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
  378. {
  379. var tf = new TextField
  380. {
  381. Text = "Hello"
  382. };
  383. tf.SetFocus ();
  384. tf.CursorPosition = 2;
  385. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  386. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  387. // When there is selected text and the cursor is at the start of the text field
  388. Assert.Equal ("He", tf.SelectedText);
  389. // Pressing left should not move focus, instead it should clear selection
  390. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  391. Assert.Null (tf.SelectedText);
  392. // When clearing selected text with left the cursor should be at the start of the selection
  393. Assert.Equal (0, tf.CursorPosition);
  394. // Now that the selection is cleared another left keypress should move focus
  395. Assert.False (tf.NewKeyDownEvent (Key.CursorLeft));
  396. }
  397. [Fact]
  398. public void Autocomplete_Visible_False_By_Default ()
  399. {
  400. View superView = new ()
  401. {
  402. CanFocus = true
  403. };
  404. TextField t = new ();
  405. superView.Add (t);
  406. superView.BeginInit ();
  407. superView.EndInit ();
  408. Assert.Equal (2, superView.SubViews.Count);
  409. Assert.True (t.Visible);
  410. Assert.False (t.Autocomplete.Visible);
  411. }
  412. [Fact]
  413. public void InsertText_Bmp_SurrogatePair_Non_Bmp_Invalid_SurrogatePair ()
  414. {
  415. var tf = new TextField ();
  416. //📄 == \ud83d\udcc4 == \U0001F4C4
  417. // � == Rune.ReplacementChar
  418. tf.InsertText ("aA,;\ud83d\udcc4\U0001F4C4\udcc4\ud83d");
  419. Assert.Equal ("aA,;📄📄��", tf.Text);
  420. }
  421. [Fact]
  422. public void PositionCursor_Respect_GetColumns ()
  423. {
  424. var tf = new TextField { Width = 5 };
  425. tf.BeginInit ();
  426. tf.EndInit ();
  427. tf.NewKeyDownEvent (new ("📄"));
  428. Assert.Equal (1, tf.CursorPosition);
  429. Assert.Equal (new (2, 0), tf.PositionCursor ());
  430. Assert.Equal ("📄", tf.Text);
  431. tf.NewKeyDownEvent (new (KeyCode.A));
  432. Assert.Equal (2, tf.CursorPosition);
  433. Assert.Equal (new (3, 0), tf.PositionCursor ());
  434. Assert.Equal ("📄a", tf.Text);
  435. }
  436. }