TextFieldTests.cs 17 KB

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