TextFieldTests.cs 18 KB

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