TextFieldTests.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. using System.Globalization;
  2. using System.Reflection;
  3. using System.Text;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests;
  6. public class TextFieldTests
  7. {
  8. private static TextField _textField;
  9. private readonly ITestOutputHelper _output;
  10. public TextFieldTests (ITestOutputHelper output) { _output = output; }
  11. [Fact]
  12. [AutoInitShutdown]
  13. public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
  14. {
  15. var tf = new TextField { Width = 3, Text = "ắ" };
  16. Toplevel top = Application.Top;
  17. top.Add (tf);
  18. Application.Begin (top);
  19. TestHelpers.AssertDriverContentsWithFrameAre (
  20. @"
  21. ắ",
  22. _output
  23. );
  24. tf.Text = "\u1eaf";
  25. Application.Refresh ();
  26. TestHelpers.AssertDriverContentsWithFrameAre (
  27. @"
  28. ắ",
  29. _output
  30. );
  31. tf.Text = "\u0103\u0301";
  32. Application.Refresh ();
  33. TestHelpers.AssertDriverContentsWithFrameAre (
  34. @"
  35. ắ",
  36. _output
  37. );
  38. tf.Text = "\u0061\u0306\u0301";
  39. Application.Refresh ();
  40. TestHelpers.AssertDriverContentsWithFrameAre (
  41. @"
  42. ắ",
  43. _output
  44. );
  45. }
  46. [Fact]
  47. [AutoInitShutdown]
  48. public void Adjust_First ()
  49. {
  50. var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
  51. Application.Top.Add (tf);
  52. Application.Begin (Application.Top);
  53. Assert.Equal ("This is a test. ", GetContents ());
  54. string GetContents ()
  55. {
  56. var item = "";
  57. for (var i = 0; i < 16; i++)
  58. {
  59. item += Application.Driver.Contents [0, i].Rune;
  60. }
  61. return item;
  62. }
  63. }
  64. [Fact]
  65. public void Cancel_TextChanging_ThenBackspace ()
  66. {
  67. var tf = new TextField ();
  68. tf.EnsureFocus ();
  69. tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.ShiftMask));
  70. Assert.Equal ("A", tf.Text);
  71. // cancel the next keystroke
  72. tf.TextChanging += (s, e) => e.Cancel = e.NewText == "AB";
  73. tf.NewKeyDownEvent (new Key (KeyCode.B | KeyCode.ShiftMask));
  74. // B was canceled so should just be A
  75. Assert.Equal ("A", tf.Text);
  76. // now delete the A
  77. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  78. Assert.Equal ("", tf.Text);
  79. }
  80. [Fact]
  81. [TextFieldTestsAutoInitShutdown]
  82. public void CanFocus_False_Wont_Focus_With_Mouse ()
  83. {
  84. Toplevel top = Application.Top;
  85. var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  86. var fv = new FrameView
  87. {
  88. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  89. };
  90. fv.Add (tf);
  91. top.Add (fv);
  92. Application.Begin (top);
  93. Assert.False (tf.CanFocus);
  94. Assert.False (tf.HasFocus);
  95. Assert.False (fv.CanFocus);
  96. Assert.False (fv.HasFocus);
  97. tf.MouseEvent (
  98. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  99. );
  100. Assert.Null (tf.SelectedText);
  101. Assert.False (tf.CanFocus);
  102. Assert.False (tf.HasFocus);
  103. Assert.False (fv.CanFocus);
  104. Assert.False (fv.HasFocus);
  105. Assert.Throws<InvalidOperationException> (() => tf.CanFocus = true);
  106. fv.CanFocus = true;
  107. tf.CanFocus = true;
  108. tf.MouseEvent (
  109. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  110. );
  111. Assert.Equal ("some ", tf.SelectedText);
  112. Assert.True (tf.CanFocus);
  113. Assert.True (tf.HasFocus);
  114. Assert.True (fv.CanFocus);
  115. Assert.True (fv.HasFocus);
  116. fv.CanFocus = false;
  117. tf.MouseEvent (
  118. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  119. );
  120. Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
  121. Assert.False (tf.CanFocus);
  122. Assert.False (tf.HasFocus);
  123. Assert.False (fv.CanFocus);
  124. Assert.False (fv.HasFocus);
  125. }
  126. [Theory]
  127. [AutoInitShutdown]
  128. [InlineData ("0123456789", "0123456789")]
  129. [InlineData ("01234567890", "0123456789")]
  130. public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
  131. {
  132. TextField tf = GetTextFieldsInView ();
  133. // Caption has no effect when focused
  134. tf.Caption = caption;
  135. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  136. Assert.False (tf.HasFocus);
  137. tf.Draw ();
  138. TestHelpers.AssertDriverContentsAre (expectedRender, _output);
  139. }
  140. [Fact]
  141. [AutoInitShutdown]
  142. public void CaptionedTextField_DoesNotOverspillBounds_Unicode ()
  143. {
  144. string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  145. Assert.Equal (11, caption.Length);
  146. Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
  147. TextField tf = GetTextFieldsInView ();
  148. tf.Caption = caption;
  149. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  150. Assert.False (tf.HasFocus);
  151. tf.Draw ();
  152. TestHelpers.AssertDriverContentsAre ("Misérables", _output);
  153. }
  154. [Theory]
  155. [AutoInitShutdown]
  156. [InlineData ("blah")]
  157. [InlineData (" ")]
  158. public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
  159. {
  160. TextField tf = GetTextFieldsInView ();
  161. tf.Draw ();
  162. TestHelpers.AssertDriverContentsAre ("", _output);
  163. tf.Caption = "Enter txt";
  164. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  165. // Caption should appear when not focused and no text
  166. Assert.False (tf.HasFocus);
  167. tf.Draw ();
  168. TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
  169. // but disapear when text is added
  170. tf.Text = content;
  171. tf.Draw ();
  172. TestHelpers.AssertDriverContentsAre (content, _output);
  173. }
  174. [Fact]
  175. [AutoInitShutdown]
  176. public void CaptionedTextField_RendersCaption_WhenNotFocused ()
  177. {
  178. TextField tf = GetTextFieldsInView ();
  179. tf.Draw ();
  180. TestHelpers.AssertDriverContentsAre ("", _output);
  181. // Caption has no effect when focused
  182. tf.Caption = "Enter txt";
  183. Assert.True (tf.HasFocus);
  184. tf.Draw ();
  185. TestHelpers.AssertDriverContentsAre ("", _output);
  186. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  187. Assert.False (tf.HasFocus);
  188. tf.Draw ();
  189. TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
  190. }
  191. [Fact]
  192. [TextFieldTestsAutoInitShutdown]
  193. public void Changing_SelectedStart_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  194. {
  195. _textField.BeginInit ();
  196. _textField.EndInit ();
  197. _textField.SelectedStart = 2;
  198. Assert.Equal (32, _textField.CursorPosition);
  199. Assert.Equal (30, _textField.SelectedLength);
  200. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  201. _textField.CursorPosition = 20;
  202. Assert.Equal (2, _textField.SelectedStart);
  203. Assert.Equal (18, _textField.SelectedLength);
  204. Assert.Equal ("B to jump between ", _textField.SelectedText);
  205. }
  206. [Fact]
  207. [TextFieldTestsAutoInitShutdown]
  208. public void Copy_Or_Cut__Not_Allowed_If_Secret_Is_True ()
  209. {
  210. _textField.Secret = true;
  211. _textField.SelectedStart = 20;
  212. _textField.CursorPosition = 24;
  213. _textField.Copy ();
  214. Assert.Null (_textField.SelectedText);
  215. _textField.Cut ();
  216. Assert.Null (_textField.SelectedText);
  217. _textField.Secret = false;
  218. _textField.Copy ();
  219. Assert.Equal ("text", _textField.SelectedText);
  220. _textField.Cut ();
  221. Assert.Null (_textField.SelectedText);
  222. }
  223. [Fact]
  224. [TextFieldTestsAutoInitShutdown]
  225. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  226. {
  227. _textField.SelectedStart = 20;
  228. _textField.CursorPosition = 24;
  229. _textField.Copy ();
  230. Assert.Equal ("text", _textField.SelectedText);
  231. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  232. _textField.SelectedStart = -1;
  233. _textField.Paste ();
  234. Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
  235. _textField.SelectedStart = 24;
  236. _textField.Cut ();
  237. Assert.Null (_textField.SelectedText);
  238. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  239. _textField.SelectedStart = -1;
  240. _textField.Paste ();
  241. Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
  242. }
  243. [Fact]
  244. [TextFieldTestsAutoInitShutdown]
  245. public void Copy_Or_Cut_And_Paste_With_Selection ()
  246. {
  247. _textField.SelectedStart = 20;
  248. _textField.CursorPosition = 24;
  249. _textField.Copy ();
  250. Assert.Equal ("text", _textField.SelectedText);
  251. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  252. _textField.Paste ();
  253. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  254. _textField.SelectedStart = 20;
  255. _textField.Cut ();
  256. _textField.Paste ();
  257. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  258. }
  259. [Fact]
  260. [TextFieldTestsAutoInitShutdown]
  261. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  262. {
  263. _textField.SelectedStart = 20;
  264. _textField.CursorPosition = 24;
  265. _textField.Copy ();
  266. Assert.Equal ("text", _textField.SelectedText);
  267. _textField.Cut ();
  268. Assert.Null (_textField.SelectedText);
  269. }
  270. [Fact]
  271. [TextFieldTestsAutoInitShutdown]
  272. public void Copy_Or_Cut_Null_If_No_Selection ()
  273. {
  274. _textField.SelectedStart = -1;
  275. _textField.Copy ();
  276. Assert.Null (_textField.SelectedText);
  277. _textField.Cut ();
  278. Assert.Null (_textField.SelectedText);
  279. }
  280. [Fact]
  281. [TextFieldTestsAutoInitShutdown]
  282. public void Copy_Paste_Surrogate_Pairs ()
  283. {
  284. _textField.Text = "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
  285. _textField.SelectAll ();
  286. _textField.Cut ();
  287. Assert.Equal (
  288. "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!",
  289. Application.Driver.Clipboard.GetClipboardData ()
  290. );
  291. Assert.Equal (string.Empty, _textField.Text);
  292. _textField.Paste ();
  293. Assert.Equal ("TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textField.Text);
  294. }
  295. [Fact]
  296. [TextFieldTestsAutoInitShutdown]
  297. public void Copy_Paste_Text_Changing_Updates_Cursor_Position ()
  298. {
  299. _textField.BeginInit ();
  300. _textField.EndInit ();
  301. _textField.TextChanging += _textField_TextChanging;
  302. void _textField_TextChanging (object sender, TextChangingEventArgs e)
  303. {
  304. if (e.NewText.GetRuneCount () > 11)
  305. {
  306. e.NewText = e.NewText [..11];
  307. }
  308. }
  309. Assert.Equal (32, _textField.CursorPosition);
  310. _textField.SelectAll ();
  311. _textField.Cut ();
  312. Assert.Equal ("TAB to jump between text fields.", Application.Driver.Clipboard.GetClipboardData ());
  313. Assert.Equal (string.Empty, _textField.Text);
  314. Assert.Equal (0, _textField.CursorPosition);
  315. _textField.Paste ();
  316. Assert.Equal ("TAB to jump", _textField.Text);
  317. Assert.Equal (11, _textField.CursorPosition);
  318. _textField.TextChanging -= _textField_TextChanging;
  319. }
  320. [Fact]
  321. [TextFieldTestsAutoInitShutdown]
  322. public void Cursor_Position_Initialization ()
  323. {
  324. Assert.False (_textField.IsInitialized);
  325. // BUGBUG: IsInitialized is false and
  326. // CursorPosition wasn't calculated yet
  327. Assert.Equal (0, _textField.CursorPosition);
  328. _textField.BeginInit ();
  329. _textField.EndInit ();
  330. Assert.Equal (32, _textField.CursorPosition);
  331. Assert.Equal (0, _textField.SelectedLength);
  332. Assert.Null (_textField.SelectedText);
  333. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  334. }
  335. [Fact]
  336. [TextFieldTestsAutoInitShutdown]
  337. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  338. {
  339. _textField.CursorPosition = 33;
  340. Assert.Equal (32, _textField.CursorPosition);
  341. Assert.Equal (0, _textField.SelectedLength);
  342. Assert.Null (_textField.SelectedText);
  343. }
  344. [Fact]
  345. [TextFieldTestsAutoInitShutdown]
  346. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  347. {
  348. _textField.CursorPosition = -1;
  349. Assert.Equal (0, _textField.CursorPosition);
  350. Assert.Equal (0, _textField.SelectedLength);
  351. Assert.Null (_textField.SelectedText);
  352. }
  353. [Fact]
  354. [AutoInitShutdown]
  355. public void DeleteSelectedText_InsertText_DeleteCharLeft_DeleteCharRight_Cut ()
  356. {
  357. var newText = "";
  358. var oldText = "";
  359. var tf = new TextField { Width = 10, Text = "-1" };
  360. tf.TextChanging += (s, e) => newText = e.NewText;
  361. tf.TextChanged += (s, e) => oldText = e.OldValue;
  362. Application.Top.Add (tf);
  363. Application.Begin (Application.Top);
  364. Assert.Equal ("-1", tf.Text);
  365. // InsertText
  366. tf.SelectedStart = 1;
  367. tf.CursorPosition = 2;
  368. Assert.Equal (1, tf.SelectedLength);
  369. Assert.Equal ("1", tf.SelectedText);
  370. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.D2)));
  371. Assert.Equal ("-2", newText);
  372. Assert.Equal ("-1", oldText);
  373. Assert.Equal ("-2", tf.Text);
  374. // DeleteCharLeft
  375. tf.SelectedStart = 1;
  376. tf.CursorPosition = 2;
  377. Assert.Equal (1, tf.SelectedLength);
  378. Assert.Equal ("2", tf.SelectedText);
  379. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace)));
  380. Assert.Equal ("-", newText);
  381. Assert.Equal ("-2", oldText);
  382. Assert.Equal ("-", tf.Text);
  383. // DeleteCharRight
  384. tf.Text = "-1";
  385. tf.SelectedStart = 1;
  386. tf.CursorPosition = 2;
  387. Assert.Equal (1, tf.SelectedLength);
  388. Assert.Equal ("1", tf.SelectedText);
  389. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
  390. Assert.Equal ("-", newText);
  391. Assert.Equal ("-1", oldText);
  392. Assert.Equal ("-", tf.Text);
  393. // Cut
  394. tf.Text = "-1";
  395. tf.SelectedStart = 1;
  396. tf.CursorPosition = 2;
  397. Assert.Equal (1, tf.SelectedLength);
  398. Assert.Equal ("1", tf.SelectedText);
  399. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.X | KeyCode.CtrlMask)));
  400. Assert.Equal ("-", newText);
  401. Assert.Equal ("-1", oldText);
  402. Assert.Equal ("-", tf.Text);
  403. // Delete word with accented char
  404. tf.Text = "Les Misérables movie.";
  405. Assert.True (
  406. tf.MouseEvent (
  407. new MouseEvent { X = 7, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  408. )
  409. );
  410. Assert.Equal ("Misérables ", tf.SelectedText);
  411. Assert.Equal (11, tf.SelectedLength);
  412. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
  413. Assert.Equal ("Les movie.", newText);
  414. Assert.Equal ("Les Misérables movie.", oldText);
  415. Assert.Equal ("Les movie.", tf.Text);
  416. }
  417. [Fact]
  418. public void HistoryText_IsDirty_ClearHistoryChanges ()
  419. {
  420. var text = "Testing";
  421. var tf = new TextField { Text = text };
  422. tf.BeginInit ();
  423. tf.EndInit ();
  424. Assert.Equal (text, tf.Text);
  425. tf.ClearHistoryChanges ();
  426. Assert.False (tf.IsDirty);
  427. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.ShiftMask)));
  428. Assert.Equal ($"{text}A", tf.Text);
  429. Assert.True (tf.IsDirty);
  430. }
  431. [Fact]
  432. [AutoInitShutdown (useFakeClipboard: true)]
  433. public void KeyBindings_Command ()
  434. {
  435. var tf = new TextField { Width = 20, Text = "This is a test." };
  436. tf.BeginInit ();
  437. tf.EndInit ();
  438. Assert.Equal (15, tf.Text.Length);
  439. Assert.Equal (15, tf.CursorPosition);
  440. Assert.False (tf.ReadOnly);
  441. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
  442. Assert.Equal ("This is a test.", tf.Text);
  443. tf.CursorPosition = 0;
  444. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
  445. Assert.Equal ("his is a test.", tf.Text);
  446. tf.ReadOnly = true;
  447. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.D | KeyCode.CtrlMask)));
  448. Assert.Equal ("his is a test.", tf.Text);
  449. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete)));
  450. Assert.Equal ("his is a test.", tf.Text);
  451. tf.ReadOnly = false;
  452. tf.CursorPosition = 1;
  453. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace)));
  454. Assert.Equal ("is is a test.", tf.Text);
  455. tf.CursorPosition = 5;
  456. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Home | KeyCode.ShiftMask)));
  457. Assert.Equal ("is is a test.", tf.Text);
  458. Assert.Equal ("is is", tf.SelectedText);
  459. tf.CursorPosition = 5;
  460. tf.SelectedStart = -1;
  461. Assert.Null (tf.SelectedText);
  462. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Home | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  463. Assert.Equal ("is is a test.", tf.Text);
  464. Assert.Equal ("is is", tf.SelectedText);
  465. tf.CursorPosition = 5;
  466. tf.SelectedStart = -1;
  467. Assert.Null (tf.SelectedText);
  468. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  469. Assert.Equal ("is is a test.", tf.Text);
  470. Assert.Equal ("is is", tf.SelectedText);
  471. tf.CursorPosition = 5;
  472. tf.SelectedStart = -1;
  473. Assert.Null (tf.SelectedText);
  474. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End | KeyCode.ShiftMask)));
  475. Assert.Equal ("is is a test.", tf.Text);
  476. Assert.Equal (" a test.", tf.SelectedText);
  477. tf.CursorPosition = 5;
  478. tf.SelectedStart = -1;
  479. Assert.Null (tf.SelectedText);
  480. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  481. Assert.Equal ("is is a test.", tf.Text);
  482. Assert.Equal (" a test.", tf.SelectedText);
  483. tf.CursorPosition = 5;
  484. tf.SelectedStart = -1;
  485. Assert.Null (tf.SelectedText);
  486. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  487. Assert.Equal ("is is a test.", tf.Text);
  488. Assert.Equal (" a test.", tf.SelectedText);
  489. tf.CursorPosition = 5;
  490. tf.SelectedStart = -1;
  491. Assert.Null (tf.SelectedText);
  492. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Home)));
  493. Assert.Equal ("is is a test.", tf.Text);
  494. Assert.Equal (0, tf.CursorPosition);
  495. tf.CursorPosition = 5;
  496. Assert.Null (tf.SelectedText);
  497. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Home | KeyCode.CtrlMask)));
  498. Assert.Equal ("is is a test.", tf.Text);
  499. Assert.Equal (0, tf.CursorPosition);
  500. tf.CursorPosition = 5;
  501. Assert.Null (tf.SelectedText);
  502. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.CtrlMask)));
  503. Assert.Equal ("is is a test.", tf.Text);
  504. Assert.Equal (0, tf.CursorPosition);
  505. tf.CursorPosition = 5;
  506. tf.SelectedStart = -1;
  507. Assert.Null (tf.SelectedText);
  508. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask)));
  509. Assert.Equal ("is is a test.", tf.Text);
  510. Assert.Equal ("s", tf.SelectedText);
  511. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask)));
  512. Assert.Equal ("is is a test.", tf.Text);
  513. Assert.Equal ("is", tf.SelectedText);
  514. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.ShiftMask)));
  515. Assert.Equal ("is is a test.", tf.Text);
  516. Assert.Equal ("s", tf.SelectedText);
  517. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask)));
  518. Assert.Equal ("is is a test.", tf.Text);
  519. Assert.Null (tf.SelectedText);
  520. tf.CursorPosition = 7;
  521. tf.SelectedStart = -1;
  522. Assert.Null (tf.SelectedText);
  523. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  524. Assert.Equal ("is is a test.", tf.Text);
  525. Assert.Equal ("a", tf.SelectedText);
  526. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  527. Assert.Equal ("is is a test.", tf.Text);
  528. Assert.Equal ("is a", tf.SelectedText);
  529. Assert.True (tf.NewKeyDownEvent (new Key (('B' + KeyCode.ShiftMask) | KeyCode.AltMask)));
  530. Assert.Equal ("is is a test.", tf.Text);
  531. Assert.Equal ("is is a", tf.SelectedText);
  532. tf.CursorPosition = 3;
  533. tf.SelectedStart = -1;
  534. Assert.Null (tf.SelectedText);
  535. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  536. Assert.Equal ("is is a test.", tf.Text);
  537. Assert.Equal ("is ", tf.SelectedText);
  538. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask | KeyCode.CtrlMask)));
  539. Assert.Equal ("is is a test.", tf.Text);
  540. Assert.Equal ("is a ", tf.SelectedText);
  541. Assert.True (tf.NewKeyDownEvent (new Key (('F' + KeyCode.ShiftMask) | KeyCode.AltMask)));
  542. Assert.Equal ("is is a test.", tf.Text);
  543. Assert.Equal ("is a test.", tf.SelectedText);
  544. Assert.Equal (13, tf.CursorPosition);
  545. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
  546. Assert.Equal ("is is a test.", tf.Text);
  547. Assert.Null (tf.SelectedText);
  548. Assert.Equal (12, tf.CursorPosition);
  549. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft)));
  550. Assert.Equal ("is is a test.", tf.Text);
  551. Assert.Equal (11, tf.CursorPosition);
  552. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End)));
  553. Assert.Equal ("is is a test.", tf.Text);
  554. Assert.Equal (13, tf.CursorPosition);
  555. tf.CursorPosition = 0;
  556. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.End | KeyCode.CtrlMask)));
  557. Assert.Equal ("is is a test.", tf.Text);
  558. Assert.Equal (13, tf.CursorPosition);
  559. tf.CursorPosition = 0;
  560. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.CtrlMask)));
  561. Assert.Equal ("is is a test.", tf.Text);
  562. Assert.Equal (13, tf.CursorPosition);
  563. tf.CursorPosition = 0;
  564. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight)));
  565. Assert.Equal ("is is a test.", tf.Text);
  566. Assert.Equal (1, tf.CursorPosition);
  567. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.F | KeyCode.CtrlMask)));
  568. Assert.Equal ("is is a test.", tf.Text);
  569. Assert.Equal (2, tf.CursorPosition);
  570. tf.CursorPosition = 9;
  571. tf.ReadOnly = true;
  572. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.CtrlMask)));
  573. Assert.Equal ("is is a test.", tf.Text);
  574. tf.ReadOnly = false;
  575. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.CtrlMask)));
  576. Assert.Equal ("is is a t", tf.Text);
  577. Assert.Equal ("est.", Clipboard.Contents);
  578. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Z | KeyCode.CtrlMask)));
  579. Assert.Equal ("is is a test.", tf.Text);
  580. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Y | KeyCode.CtrlMask)));
  581. Assert.Equal ("is is a t", tf.Text);
  582. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace | KeyCode.AltMask)));
  583. Assert.Equal ("is is a test.", tf.Text);
  584. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Y | KeyCode.CtrlMask)));
  585. Assert.Equal ("is is a t", tf.Text);
  586. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask)));
  587. Assert.Equal ("is is a t", tf.Text);
  588. Assert.Equal (8, tf.CursorPosition);
  589. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.CtrlMask)));
  590. Assert.Equal ("is is a t", tf.Text);
  591. Assert.Equal (6, tf.CursorPosition);
  592. Assert.True (tf.NewKeyDownEvent (new Key ('B' + KeyCode.AltMask)));
  593. Assert.Equal ("is is a t", tf.Text);
  594. Assert.Equal (3, tf.CursorPosition);
  595. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask)));
  596. Assert.Equal ("is is a t", tf.Text);
  597. Assert.Equal (6, tf.CursorPosition);
  598. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.CtrlMask)));
  599. Assert.Equal ("is is a t", tf.Text);
  600. Assert.Equal (8, tf.CursorPosition);
  601. Assert.True (tf.NewKeyDownEvent (new Key ('F' + KeyCode.AltMask)));
  602. Assert.Equal ("is is a t", tf.Text);
  603. Assert.Equal (9, tf.CursorPosition);
  604. Assert.True (tf.Used);
  605. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Insert)));
  606. Assert.Equal ("is is a t", tf.Text);
  607. Assert.Equal (9, tf.CursorPosition);
  608. Assert.False (tf.Used);
  609. tf.SelectedStart = 3;
  610. tf.CursorPosition = 7;
  611. Assert.Equal ("is a", tf.SelectedText);
  612. Assert.Equal ("est.", Clipboard.Contents);
  613. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.C | KeyCode.CtrlMask)));
  614. Assert.Equal ("is is a t", tf.Text);
  615. Assert.Equal ("is a", Clipboard.Contents);
  616. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.X | KeyCode.CtrlMask)));
  617. Assert.Equal ("is t", tf.Text);
  618. Assert.Equal ("is a", Clipboard.Contents);
  619. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.V | KeyCode.CtrlMask)));
  620. Assert.Equal ("is is a t", tf.Text);
  621. Assert.Equal ("is a", Clipboard.Contents);
  622. Assert.Equal (7, tf.CursorPosition);
  623. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.K | KeyCode.AltMask)));
  624. Assert.Equal (" t", tf.Text);
  625. Assert.Equal ("is is a", Clipboard.Contents);
  626. tf.Text = "TAB to jump between text fields.";
  627. Assert.Equal (0, tf.CursorPosition);
  628. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Delete | KeyCode.CtrlMask)));
  629. Assert.Equal ("to jump between text fields.", tf.Text);
  630. tf.CursorPosition = tf.Text.Length;
  631. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.Backspace | KeyCode.CtrlMask)));
  632. Assert.Equal ("to jump between text fields", tf.Text);
  633. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.CtrlMask)));
  634. Assert.Equal ("to jump between text fields", tf.SelectedText);
  635. Assert.True (tf.NewKeyDownEvent (new Key (KeyCode.D | KeyCode.CtrlMask | KeyCode.ShiftMask)));
  636. Assert.Equal ("", tf.Text);
  637. }
  638. [Fact]
  639. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  640. {
  641. var top = new Toplevel ();
  642. var tf = new TextField { Width = 10 };
  643. top.Add (tf);
  644. Exception exception = Record.Exception (tf.SetFocus);
  645. Assert.Null (exception);
  646. }
  647. [Fact]
  648. [TextFieldTestsAutoInitShutdown]
  649. public void Paste_Always_Clear_The_SelectedText ()
  650. {
  651. _textField.SelectedStart = 20;
  652. _textField.CursorPosition = 24;
  653. _textField.Copy ();
  654. Assert.Equal ("text", _textField.SelectedText);
  655. _textField.Paste ();
  656. Assert.Null (_textField.SelectedText);
  657. }
  658. [Fact]
  659. public void ProcessKey_Backspace_From_End ()
  660. {
  661. var tf = new TextField { Text = "ABC" };
  662. tf.EnsureFocus ();
  663. Assert.Equal ("ABC", tf.Text);
  664. tf.BeginInit ();
  665. tf.EndInit ();
  666. Assert.Equal (3, tf.CursorPosition);
  667. // now delete the C
  668. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  669. Assert.Equal ("AB", tf.Text);
  670. Assert.Equal (2, tf.CursorPosition);
  671. // then delete the B
  672. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  673. Assert.Equal ("A", tf.Text);
  674. Assert.Equal (1, tf.CursorPosition);
  675. // then delete the A
  676. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  677. Assert.Equal ("", tf.Text);
  678. Assert.Equal (0, tf.CursorPosition);
  679. }
  680. [Fact]
  681. public void ProcessKey_Backspace_From_Middle ()
  682. {
  683. var tf = new TextField { Text = "ABC" };
  684. tf.EnsureFocus ();
  685. tf.CursorPosition = 2;
  686. Assert.Equal ("ABC", tf.Text);
  687. // now delete the B
  688. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  689. Assert.Equal ("AC", tf.Text);
  690. // then delete the A
  691. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  692. Assert.Equal ("C", tf.Text);
  693. // then delete nothing
  694. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  695. Assert.Equal ("C", tf.Text);
  696. // now delete the C
  697. tf.CursorPosition = 1;
  698. tf.NewKeyDownEvent (new Key (KeyCode.Backspace));
  699. Assert.Equal ("", tf.Text);
  700. }
  701. [Fact]
  702. [AutoInitShutdown]
  703. public void ScrollOffset_Initialize ()
  704. {
  705. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  706. tf.BeginInit ();
  707. tf.EndInit ();
  708. Assert.Equal (0, tf.ScrollOffset);
  709. Assert.Equal (16, tf.CursorPosition);
  710. Application.Top.Add (tf);
  711. Application.Begin (Application.Top);
  712. Assert.Equal (0, tf.ScrollOffset);
  713. Assert.Equal (16, tf.CursorPosition);
  714. }
  715. [Fact]
  716. [TextFieldTestsAutoInitShutdown]
  717. public void Selected_Text_Shows ()
  718. {
  719. // Proves #3022 is fixed (TextField selected text does not show in v2)
  720. _textField.CursorPosition = 0;
  721. Application.Top.Add (_textField);
  722. RunState rs = Application.Begin (Application.Top);
  723. Attribute [] attributes =
  724. {
  725. _textField.ColorScheme.Focus,
  726. new (
  727. _textField.ColorScheme.Focus.Background,
  728. _textField.ColorScheme.Focus.Foreground
  729. )
  730. };
  731. // TAB to jump between text fields.
  732. TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
  733. // Cursor is at the end
  734. Assert.Equal (32, _textField.CursorPosition);
  735. _textField.CursorPosition = 0;
  736. _textField.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask));
  737. var first = true;
  738. Application.RunIteration (ref rs, ref first);
  739. Assert.Equal (4, _textField.CursorPosition);
  740. // TAB to jump between text fields.
  741. TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
  742. }
  743. [Fact]
  744. [TextFieldTestsAutoInitShutdown]
  745. public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  746. {
  747. _textField.CursorPosition = 33;
  748. _textField.SelectedStart = 33;
  749. Assert.Equal (32, _textField.CursorPosition);
  750. Assert.Equal (32, _textField.SelectedStart);
  751. Assert.Equal (0, _textField.SelectedLength);
  752. Assert.Null (_textField.SelectedText);
  753. }
  754. [Fact]
  755. [TextFieldTestsAutoInitShutdown]
  756. public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
  757. {
  758. _textField.SelectedStart = 19;
  759. _textField.CursorPosition = 12;
  760. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  761. _textField.NewKeyDownEvent (new Key ((KeyCode)0x75)); // u
  762. Assert.Equal ("TAB to jump u text fields.", _textField.Text);
  763. }
  764. [Fact]
  765. [TextFieldTestsAutoInitShutdown]
  766. public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  767. {
  768. _textField.CursorPosition = 2;
  769. _textField.SelectedStart = 33;
  770. Assert.Equal (32, _textField.SelectedStart);
  771. Assert.Equal (30, _textField.SelectedLength);
  772. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  773. }
  774. [Fact]
  775. [TextFieldTestsAutoInitShutdown]
  776. public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
  777. {
  778. _textField.SelectedStart = -2;
  779. Assert.Equal (-1, _textField.SelectedStart);
  780. Assert.Equal (0, _textField.SelectedLength);
  781. Assert.Null (_textField.SelectedText);
  782. }
  783. [Fact]
  784. [AutoInitShutdown]
  785. public void Test_RootKeyEvent_Cancel ()
  786. {
  787. Application.KeyDown += SuppressKey;
  788. var tf = new TextField ();
  789. Application.Top.Add (tf);
  790. Application.Begin (Application.Top);
  791. Application.Driver.SendKeys ('a', ConsoleKey.A, false, false, false);
  792. Assert.Equal ("a", tf.Text);
  793. // SuppressKey suppresses the 'j' key
  794. Application.Driver.SendKeys ('j', ConsoleKey.J, false, false, false);
  795. Assert.Equal ("a", tf.Text);
  796. Application.KeyDown -= SuppressKey;
  797. // Now that the delegate has been removed we can type j again
  798. Application.Driver.SendKeys ('j', ConsoleKey.J, false, false, false);
  799. Assert.Equal ("aj", tf.Text);
  800. }
  801. [Fact]
  802. [AutoInitShutdown]
  803. public void Test_RootMouseKeyEvent_Cancel ()
  804. {
  805. Application.MouseEvent += SuppressRightClick;
  806. var tf = new TextField { Width = 10 };
  807. var clickCounter = 0;
  808. tf.MouseClick += (s, m) => { clickCounter++; };
  809. Application.Top.Add (tf);
  810. Application.Begin (Application.Top);
  811. var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
  812. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  813. Assert.Equal (1, clickCounter);
  814. // Get a fresh instance that represents a right click.
  815. // Should be ignored because of SuppressRightClick callback
  816. mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
  817. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  818. Assert.Equal (1, clickCounter);
  819. Application.MouseEvent -= SuppressRightClick;
  820. // Get a fresh instance that represents a right click.
  821. // Should no longer be ignored as the callback was removed
  822. mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
  823. // In #3183 OnMouseClicked is no longer called before MouseEvent().
  824. // This call causes the context menu to pop, and MouseEvent() returns true.
  825. // Thus, the clickCounter is NOT incremented.
  826. // Which is correct, because the user did NOT click with the left mouse button.
  827. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  828. Assert.Equal (1, clickCounter);
  829. }
  830. [InlineData ("a")] // Lower than selection
  831. [InlineData ("aaaaaaaaaaa")] // Greater than selection
  832. [InlineData ("aaaa")] // Equal than selection
  833. [Theory]
  834. public void TestSetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
  835. {
  836. var tf = new TextField ();
  837. tf.Text = "fish";
  838. tf.CursorPosition = tf.Text.Length;
  839. tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft));
  840. tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  841. tf.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  842. Assert.Equal (1, tf.CursorPosition);
  843. Assert.Equal (2, tf.SelectedLength);
  844. Assert.Equal ("is", tf.SelectedText);
  845. tf.Text = newText;
  846. tf.CursorPosition = tf.Text.Length;
  847. Assert.Equal (newText.Length, tf.CursorPosition);
  848. Assert.Equal (0, tf.SelectedLength);
  849. Assert.Null (tf.SelectedText);
  850. }
  851. [Fact]
  852. [TextFieldTestsAutoInitShutdown]
  853. public void Text_Replaces_Tabs_With_Empty_String ()
  854. {
  855. _textField.Text = "\t\tTAB to jump between text fields.";
  856. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  857. _textField.Text = "";
  858. Clipboard.Contents = "\t\tTAB to jump between text fields.";
  859. _textField.Paste ();
  860. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  861. }
  862. [Fact]
  863. [TextFieldTestsAutoInitShutdown]
  864. public void TextChanged_Event ()
  865. {
  866. _textField.TextChanged += (s, e) => { Assert.Equal ("TAB to jump between text fields.", e.OldValue); };
  867. _textField.Text = "changed";
  868. Assert.Equal ("changed", _textField.Text);
  869. }
  870. [Fact]
  871. [TextFieldTestsAutoInitShutdown]
  872. public void TextChanging_Event ()
  873. {
  874. var cancel = true;
  875. _textField.TextChanging += (s, e) =>
  876. {
  877. Assert.Equal ("changing", e.NewText);
  878. if (cancel)
  879. {
  880. e.Cancel = true;
  881. }
  882. };
  883. _textField.Text = "changing";
  884. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  885. cancel = false;
  886. _textField.Text = "changing";
  887. Assert.Equal ("changing", _textField.Text);
  888. }
  889. [Fact]
  890. [TextFieldTestsAutoInitShutdown]
  891. public void TextField_SpaceHandling ()
  892. {
  893. var tf = new TextField { Width = 10, Text = " " };
  894. var ev = new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
  895. tf.MouseEvent (ev);
  896. Assert.Equal (1, tf.SelectedLength);
  897. ev = new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
  898. tf.MouseEvent (ev);
  899. Assert.Equal (1, tf.SelectedLength);
  900. }
  901. [Fact]
  902. [TextFieldTestsAutoInitShutdown]
  903. public void Used_Is_False ()
  904. {
  905. _textField.Used = false;
  906. _textField.CursorPosition = 10;
  907. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  908. _textField.NewKeyDownEvent (new Key ((KeyCode)0x75)); // u
  909. Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
  910. _textField.NewKeyDownEvent (new Key ((KeyCode)0x73)); // s
  911. Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
  912. _textField.NewKeyDownEvent (new Key ((KeyCode)0x65)); // e
  913. Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
  914. _textField.NewKeyDownEvent (new Key ((KeyCode)0x64)); // d
  915. Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
  916. }
  917. [Fact]
  918. [TextFieldTestsAutoInitShutdown]
  919. public void Used_Is_True_By_Default ()
  920. {
  921. _textField.CursorPosition = 10;
  922. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  923. _textField.NewKeyDownEvent (new Key ((KeyCode)0x75)); // u
  924. Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
  925. _textField.NewKeyDownEvent (new Key ((KeyCode)0x73)); // s
  926. Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
  927. _textField.NewKeyDownEvent (new Key ((KeyCode)0x65)); // e
  928. Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
  929. _textField.NewKeyDownEvent (new Key ((KeyCode)0x64)); // d
  930. Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
  931. }
  932. [Fact]
  933. [TextFieldTestsAutoInitShutdown]
  934. public void WordBackward_With_No_Selection ()
  935. {
  936. _textField.CursorPosition = _textField.Text.Length;
  937. var iteration = 0;
  938. while (_textField.CursorPosition > 0)
  939. {
  940. _textField.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask));
  941. switch (iteration)
  942. {
  943. case 0:
  944. Assert.Equal (31, _textField.CursorPosition);
  945. Assert.Equal (-1, _textField.SelectedStart);
  946. Assert.Equal (0, _textField.SelectedLength);
  947. Assert.Null (_textField.SelectedText);
  948. break;
  949. case 1:
  950. Assert.Equal (25, _textField.CursorPosition);
  951. Assert.Equal (-1, _textField.SelectedStart);
  952. Assert.Equal (0, _textField.SelectedLength);
  953. Assert.Null (_textField.SelectedText);
  954. break;
  955. case 2:
  956. Assert.Equal (20, _textField.CursorPosition);
  957. Assert.Equal (-1, _textField.SelectedStart);
  958. Assert.Equal (0, _textField.SelectedLength);
  959. Assert.Null (_textField.SelectedText);
  960. break;
  961. case 3:
  962. Assert.Equal (12, _textField.CursorPosition);
  963. Assert.Equal (-1, _textField.SelectedStart);
  964. Assert.Equal (0, _textField.SelectedLength);
  965. Assert.Null (_textField.SelectedText);
  966. break;
  967. case 4:
  968. Assert.Equal (7, _textField.CursorPosition);
  969. Assert.Equal (-1, _textField.SelectedStart);
  970. Assert.Equal (0, _textField.SelectedLength);
  971. Assert.Null (_textField.SelectedText);
  972. break;
  973. case 5:
  974. Assert.Equal (4, _textField.CursorPosition);
  975. Assert.Equal (-1, _textField.SelectedStart);
  976. Assert.Equal (0, _textField.SelectedLength);
  977. Assert.Null (_textField.SelectedText);
  978. break;
  979. case 6:
  980. Assert.Equal (0, _textField.CursorPosition);
  981. Assert.Equal (-1, _textField.SelectedStart);
  982. Assert.Equal (0, _textField.SelectedLength);
  983. Assert.Null (_textField.SelectedText);
  984. break;
  985. }
  986. iteration++;
  987. }
  988. }
  989. [Fact]
  990. [TextFieldTestsAutoInitShutdown]
  991. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  992. {
  993. // 1 2 3 4 5
  994. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  995. _textField.Text = "TAB t o jump b etween t ext f ields .";
  996. _textField.CursorPosition = _textField.Text.Length;
  997. var iteration = 0;
  998. while (_textField.CursorPosition > 0)
  999. {
  1000. _textField.NewKeyDownEvent (new Key (KeyCode.CursorLeft | KeyCode.CtrlMask));
  1001. switch (iteration)
  1002. {
  1003. case 0:
  1004. Assert.Equal (54, _textField.CursorPosition);
  1005. Assert.Equal (-1, _textField.SelectedStart);
  1006. Assert.Equal (0, _textField.SelectedLength);
  1007. Assert.Null (_textField.SelectedText);
  1008. break;
  1009. case 1:
  1010. Assert.Equal (48, _textField.CursorPosition);
  1011. Assert.Equal (-1, _textField.SelectedStart);
  1012. Assert.Equal (0, _textField.SelectedLength);
  1013. Assert.Null (_textField.SelectedText);
  1014. break;
  1015. case 2:
  1016. Assert.Equal (46, _textField.CursorPosition);
  1017. Assert.Equal (-1, _textField.SelectedStart);
  1018. Assert.Equal (0, _textField.SelectedLength);
  1019. Assert.Null (_textField.SelectedText);
  1020. break;
  1021. case 3:
  1022. Assert.Equal (40, _textField.CursorPosition);
  1023. Assert.Equal (-1, _textField.SelectedStart);
  1024. Assert.Equal (0, _textField.SelectedLength);
  1025. Assert.Null (_textField.SelectedText);
  1026. break;
  1027. case 4:
  1028. Assert.Equal (38, _textField.CursorPosition);
  1029. Assert.Equal (-1, _textField.SelectedStart);
  1030. Assert.Equal (0, _textField.SelectedLength);
  1031. Assert.Null (_textField.SelectedText);
  1032. break;
  1033. case 5:
  1034. Assert.Equal (28, _textField.CursorPosition);
  1035. Assert.Equal (-1, _textField.SelectedStart);
  1036. Assert.Equal (0, _textField.SelectedLength);
  1037. Assert.Null (_textField.SelectedText);
  1038. break;
  1039. case 6:
  1040. Assert.Equal (25, _textField.CursorPosition);
  1041. Assert.Equal (-1, _textField.SelectedStart);
  1042. Assert.Equal (0, _textField.SelectedLength);
  1043. Assert.Null (_textField.SelectedText);
  1044. break;
  1045. case 7:
  1046. Assert.Equal (12, _textField.CursorPosition);
  1047. Assert.Equal (-1, _textField.SelectedStart);
  1048. Assert.Equal (0, _textField.SelectedLength);
  1049. Assert.Null (_textField.SelectedText);
  1050. break;
  1051. case 8:
  1052. Assert.Equal (9, _textField.CursorPosition);
  1053. Assert.Equal (-1, _textField.SelectedStart);
  1054. Assert.Equal (0, _textField.SelectedLength);
  1055. Assert.Null (_textField.SelectedText);
  1056. break;
  1057. case 9:
  1058. Assert.Equal (6, _textField.CursorPosition);
  1059. Assert.Equal (-1, _textField.SelectedStart);
  1060. Assert.Equal (0, _textField.SelectedLength);
  1061. Assert.Null (_textField.SelectedText);
  1062. break;
  1063. case 10:
  1064. Assert.Equal (0, _textField.CursorPosition);
  1065. Assert.Equal (-1, _textField.SelectedStart);
  1066. Assert.Equal (0, _textField.SelectedLength);
  1067. Assert.Null (_textField.SelectedText);
  1068. break;
  1069. }
  1070. iteration++;
  1071. }
  1072. }
  1073. [Fact]
  1074. [TextFieldTestsAutoInitShutdown]
  1075. public void WordBackward_With_Selection ()
  1076. {
  1077. _textField.CursorPosition = _textField.Text.Length;
  1078. _textField.SelectedStart = _textField.Text.Length;
  1079. var iteration = 0;
  1080. while (_textField.CursorPosition > 0)
  1081. {
  1082. _textField.NewKeyDownEvent (
  1083. new Key (
  1084. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1085. )
  1086. );
  1087. switch (iteration)
  1088. {
  1089. case 0:
  1090. Assert.Equal (31, _textField.CursorPosition);
  1091. Assert.Equal (32, _textField.SelectedStart);
  1092. Assert.Equal (1, _textField.SelectedLength);
  1093. Assert.Equal (".", _textField.SelectedText);
  1094. break;
  1095. case 1:
  1096. Assert.Equal (25, _textField.CursorPosition);
  1097. Assert.Equal (32, _textField.SelectedStart);
  1098. Assert.Equal (7, _textField.SelectedLength);
  1099. Assert.Equal ("fields.", _textField.SelectedText);
  1100. break;
  1101. case 2:
  1102. Assert.Equal (20, _textField.CursorPosition);
  1103. Assert.Equal (32, _textField.SelectedStart);
  1104. Assert.Equal (12, _textField.SelectedLength);
  1105. Assert.Equal ("text fields.", _textField.SelectedText);
  1106. break;
  1107. case 3:
  1108. Assert.Equal (12, _textField.CursorPosition);
  1109. Assert.Equal (32, _textField.SelectedStart);
  1110. Assert.Equal (20, _textField.SelectedLength);
  1111. Assert.Equal ("between text fields.", _textField.SelectedText);
  1112. break;
  1113. case 4:
  1114. Assert.Equal (7, _textField.CursorPosition);
  1115. Assert.Equal (32, _textField.SelectedStart);
  1116. Assert.Equal (25, _textField.SelectedLength);
  1117. Assert.Equal ("jump between text fields.", _textField.SelectedText);
  1118. break;
  1119. case 5:
  1120. Assert.Equal (4, _textField.CursorPosition);
  1121. Assert.Equal (32, _textField.SelectedStart);
  1122. Assert.Equal (28, _textField.SelectedLength);
  1123. Assert.Equal ("to jump between text fields.", _textField.SelectedText);
  1124. break;
  1125. case 6:
  1126. Assert.Equal (0, _textField.CursorPosition);
  1127. Assert.Equal (32, _textField.SelectedStart);
  1128. Assert.Equal (32, _textField.SelectedLength);
  1129. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1130. break;
  1131. }
  1132. iteration++;
  1133. }
  1134. }
  1135. [Fact]
  1136. [TextFieldTestsAutoInitShutdown]
  1137. public void
  1138. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1139. {
  1140. _textField.CursorPosition = 10;
  1141. _textField.SelectedStart = 10;
  1142. var iteration = 0;
  1143. while (_textField.CursorPosition > 0)
  1144. {
  1145. _textField.NewKeyDownEvent (
  1146. new Key (
  1147. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1148. )
  1149. );
  1150. switch (iteration)
  1151. {
  1152. case 0:
  1153. Assert.Equal (7, _textField.CursorPosition);
  1154. Assert.Equal (10, _textField.SelectedStart);
  1155. Assert.Equal (3, _textField.SelectedLength);
  1156. Assert.Equal ("jum", _textField.SelectedText);
  1157. break;
  1158. case 1:
  1159. Assert.Equal (4, _textField.CursorPosition);
  1160. Assert.Equal (10, _textField.SelectedStart);
  1161. Assert.Equal (6, _textField.SelectedLength);
  1162. Assert.Equal ("to jum", _textField.SelectedText);
  1163. break;
  1164. case 2:
  1165. Assert.Equal (0, _textField.CursorPosition);
  1166. Assert.Equal (10, _textField.SelectedStart);
  1167. Assert.Equal (10, _textField.SelectedLength);
  1168. Assert.Equal ("TAB to jum", _textField.SelectedText);
  1169. break;
  1170. }
  1171. iteration++;
  1172. }
  1173. }
  1174. [Fact]
  1175. public void WordBackward_WordForward_Mixed ()
  1176. {
  1177. var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
  1178. tf.BeginInit ();
  1179. tf.EndInit ();
  1180. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
  1181. Assert.Equal (15, tf.CursorPosition);
  1182. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
  1183. Assert.Equal (12, tf.CursorPosition);
  1184. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
  1185. Assert.Equal (10, tf.CursorPosition);
  1186. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
  1187. Assert.Equal (5, tf.CursorPosition);
  1188. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorLeft));
  1189. Assert.Equal (0, tf.CursorPosition);
  1190. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
  1191. Assert.Equal (5, tf.CursorPosition);
  1192. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
  1193. Assert.Equal (10, tf.CursorPosition);
  1194. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
  1195. Assert.Equal (12, tf.CursorPosition);
  1196. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
  1197. Assert.Equal (15, tf.CursorPosition);
  1198. tf.NewKeyDownEvent (new Key (KeyCode.CtrlMask | KeyCode.CursorRight));
  1199. Assert.Equal (22, tf.CursorPosition);
  1200. }
  1201. [Fact]
  1202. public void WordBackward_WordForward_SelectedText_With_Accent ()
  1203. {
  1204. var text = "Les Misérables movie.";
  1205. var tf = new TextField { Width = 30, Text = text };
  1206. Assert.Equal (21, text.Length);
  1207. Assert.Equal (21, tf.Text.GetRuneCount ());
  1208. Assert.Equal (21, tf.Text.GetColumns ());
  1209. List<Rune> runes = tf.Text.ToRuneList ();
  1210. Assert.Equal (21, runes.Count);
  1211. Assert.Equal (21, tf.Text.Length);
  1212. for (var i = 0; i < runes.Count; i++)
  1213. {
  1214. char cs = text [i];
  1215. var cus = (char)runes [i].Value;
  1216. Assert.Equal (cs, cus);
  1217. }
  1218. var idx = 15;
  1219. Assert.Equal ('m', text [idx]);
  1220. Assert.Equal ('m', (char)runes [idx].Value);
  1221. Assert.Equal ("m", runes [idx].ToString ());
  1222. Assert.True (
  1223. tf.MouseEvent (
  1224. new MouseEvent { X = idx, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1225. )
  1226. );
  1227. Assert.Equal ("movie.", tf.SelectedText);
  1228. Assert.True (
  1229. tf.MouseEvent (
  1230. new MouseEvent { X = idx + 1, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1231. )
  1232. );
  1233. Assert.Equal ("movie.", tf.SelectedText);
  1234. }
  1235. [Fact]
  1236. [TextFieldTestsAutoInitShutdown]
  1237. public void WordForward_With_No_Selection ()
  1238. {
  1239. _textField.CursorPosition = 0;
  1240. var iteration = 0;
  1241. while (_textField.CursorPosition < _textField.Text.Length)
  1242. {
  1243. _textField.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask));
  1244. switch (iteration)
  1245. {
  1246. case 0:
  1247. Assert.Equal (4, _textField.CursorPosition);
  1248. Assert.Equal (-1, _textField.SelectedStart);
  1249. Assert.Equal (0, _textField.SelectedLength);
  1250. Assert.Null (_textField.SelectedText);
  1251. break;
  1252. case 1:
  1253. Assert.Equal (7, _textField.CursorPosition);
  1254. Assert.Equal (-1, _textField.SelectedStart);
  1255. Assert.Equal (0, _textField.SelectedLength);
  1256. Assert.Null (_textField.SelectedText);
  1257. break;
  1258. case 2:
  1259. Assert.Equal (12, _textField.CursorPosition);
  1260. Assert.Equal (-1, _textField.SelectedStart);
  1261. Assert.Equal (0, _textField.SelectedLength);
  1262. Assert.Null (_textField.SelectedText);
  1263. break;
  1264. case 3:
  1265. Assert.Equal (20, _textField.CursorPosition);
  1266. Assert.Equal (-1, _textField.SelectedStart);
  1267. Assert.Equal (0, _textField.SelectedLength);
  1268. Assert.Null (_textField.SelectedText);
  1269. break;
  1270. case 4:
  1271. Assert.Equal (25, _textField.CursorPosition);
  1272. Assert.Equal (-1, _textField.SelectedStart);
  1273. Assert.Equal (0, _textField.SelectedLength);
  1274. Assert.Null (_textField.SelectedText);
  1275. break;
  1276. case 5:
  1277. Assert.Equal (32, _textField.CursorPosition);
  1278. Assert.Equal (-1, _textField.SelectedStart);
  1279. Assert.Equal (0, _textField.SelectedLength);
  1280. Assert.Null (_textField.SelectedText);
  1281. break;
  1282. }
  1283. iteration++;
  1284. }
  1285. }
  1286. [Fact]
  1287. [TextFieldTestsAutoInitShutdown]
  1288. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1289. {
  1290. // 1 2 3 4 5
  1291. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1292. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1293. _textField.CursorPosition = 0;
  1294. var iteration = 0;
  1295. while (_textField.CursorPosition < _textField.Text.Length)
  1296. {
  1297. _textField.NewKeyDownEvent (new Key (KeyCode.CursorRight | KeyCode.CtrlMask));
  1298. switch (iteration)
  1299. {
  1300. case 0:
  1301. Assert.Equal (6, _textField.CursorPosition);
  1302. Assert.Equal (-1, _textField.SelectedStart);
  1303. Assert.Equal (0, _textField.SelectedLength);
  1304. Assert.Null (_textField.SelectedText);
  1305. break;
  1306. case 1:
  1307. Assert.Equal (9, _textField.CursorPosition);
  1308. Assert.Equal (-1, _textField.SelectedStart);
  1309. Assert.Equal (0, _textField.SelectedLength);
  1310. Assert.Null (_textField.SelectedText);
  1311. break;
  1312. case 2:
  1313. Assert.Equal (12, _textField.CursorPosition);
  1314. Assert.Equal (-1, _textField.SelectedStart);
  1315. Assert.Equal (0, _textField.SelectedLength);
  1316. Assert.Null (_textField.SelectedText);
  1317. break;
  1318. case 3:
  1319. Assert.Equal (25, _textField.CursorPosition);
  1320. Assert.Equal (-1, _textField.SelectedStart);
  1321. Assert.Equal (0, _textField.SelectedLength);
  1322. Assert.Null (_textField.SelectedText);
  1323. break;
  1324. case 4:
  1325. Assert.Equal (28, _textField.CursorPosition);
  1326. Assert.Equal (-1, _textField.SelectedStart);
  1327. Assert.Equal (0, _textField.SelectedLength);
  1328. Assert.Null (_textField.SelectedText);
  1329. break;
  1330. case 5:
  1331. Assert.Equal (38, _textField.CursorPosition);
  1332. Assert.Equal (-1, _textField.SelectedStart);
  1333. Assert.Equal (0, _textField.SelectedLength);
  1334. Assert.Null (_textField.SelectedText);
  1335. break;
  1336. case 6:
  1337. Assert.Equal (40, _textField.CursorPosition);
  1338. Assert.Equal (-1, _textField.SelectedStart);
  1339. Assert.Equal (0, _textField.SelectedLength);
  1340. Assert.Null (_textField.SelectedText);
  1341. break;
  1342. case 7:
  1343. Assert.Equal (46, _textField.CursorPosition);
  1344. Assert.Equal (-1, _textField.SelectedStart);
  1345. Assert.Equal (0, _textField.SelectedLength);
  1346. Assert.Null (_textField.SelectedText);
  1347. break;
  1348. case 8:
  1349. Assert.Equal (48, _textField.CursorPosition);
  1350. Assert.Equal (-1, _textField.SelectedStart);
  1351. Assert.Equal (0, _textField.SelectedLength);
  1352. Assert.Null (_textField.SelectedText);
  1353. break;
  1354. case 9:
  1355. Assert.Equal (54, _textField.CursorPosition);
  1356. Assert.Equal (-1, _textField.SelectedStart);
  1357. Assert.Equal (0, _textField.SelectedLength);
  1358. Assert.Null (_textField.SelectedText);
  1359. break;
  1360. case 10:
  1361. Assert.Equal (55, _textField.CursorPosition);
  1362. Assert.Equal (-1, _textField.SelectedStart);
  1363. Assert.Equal (0, _textField.SelectedLength);
  1364. Assert.Null (_textField.SelectedText);
  1365. break;
  1366. }
  1367. iteration++;
  1368. }
  1369. }
  1370. [Fact]
  1371. [TextFieldTestsAutoInitShutdown]
  1372. public void WordForward_With_Selection ()
  1373. {
  1374. _textField.CursorPosition = 0;
  1375. _textField.SelectedStart = 0;
  1376. var iteration = 0;
  1377. while (_textField.CursorPosition < _textField.Text.Length)
  1378. {
  1379. _textField.NewKeyDownEvent (
  1380. new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask)
  1381. );
  1382. switch (iteration)
  1383. {
  1384. case 0:
  1385. Assert.Equal (4, _textField.CursorPosition);
  1386. Assert.Equal (0, _textField.SelectedStart);
  1387. Assert.Equal (4, _textField.SelectedLength);
  1388. Assert.Equal ("TAB ", _textField.SelectedText);
  1389. break;
  1390. case 1:
  1391. Assert.Equal (7, _textField.CursorPosition);
  1392. Assert.Equal (0, _textField.SelectedStart);
  1393. Assert.Equal (7, _textField.SelectedLength);
  1394. Assert.Equal ("TAB to ", _textField.SelectedText);
  1395. break;
  1396. case 2:
  1397. Assert.Equal (12, _textField.CursorPosition);
  1398. Assert.Equal (0, _textField.SelectedStart);
  1399. Assert.Equal (12, _textField.SelectedLength);
  1400. Assert.Equal ("TAB to jump ", _textField.SelectedText);
  1401. break;
  1402. case 3:
  1403. Assert.Equal (20, _textField.CursorPosition);
  1404. Assert.Equal (0, _textField.SelectedStart);
  1405. Assert.Equal (20, _textField.SelectedLength);
  1406. Assert.Equal ("TAB to jump between ", _textField.SelectedText);
  1407. break;
  1408. case 4:
  1409. Assert.Equal (25, _textField.CursorPosition);
  1410. Assert.Equal (0, _textField.SelectedStart);
  1411. Assert.Equal (25, _textField.SelectedLength);
  1412. Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
  1413. break;
  1414. case 5:
  1415. Assert.Equal (32, _textField.CursorPosition);
  1416. Assert.Equal (0, _textField.SelectedStart);
  1417. Assert.Equal (32, _textField.SelectedLength);
  1418. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1419. break;
  1420. }
  1421. iteration++;
  1422. }
  1423. }
  1424. [Fact]
  1425. [TextFieldTestsAutoInitShutdown]
  1426. public void
  1427. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1428. {
  1429. _textField.CursorPosition = 10;
  1430. _textField.SelectedStart = 10;
  1431. var iteration = 0;
  1432. while (_textField.CursorPosition < _textField.Text.Length)
  1433. {
  1434. _textField.NewKeyDownEvent (
  1435. new Key (KeyCode.CursorRight | KeyCode.CtrlMask | KeyCode.ShiftMask)
  1436. );
  1437. switch (iteration)
  1438. {
  1439. case 0:
  1440. Assert.Equal (12, _textField.CursorPosition);
  1441. Assert.Equal (10, _textField.SelectedStart);
  1442. Assert.Equal (2, _textField.SelectedLength);
  1443. Assert.Equal ("p ", _textField.SelectedText);
  1444. break;
  1445. case 1:
  1446. Assert.Equal (20, _textField.CursorPosition);
  1447. Assert.Equal (10, _textField.SelectedStart);
  1448. Assert.Equal (10, _textField.SelectedLength);
  1449. Assert.Equal ("p between ", _textField.SelectedText);
  1450. break;
  1451. case 2:
  1452. Assert.Equal (25, _textField.CursorPosition);
  1453. Assert.Equal (10, _textField.SelectedStart);
  1454. Assert.Equal (15, _textField.SelectedLength);
  1455. Assert.Equal ("p between text ", _textField.SelectedText);
  1456. break;
  1457. case 3:
  1458. Assert.Equal (32, _textField.CursorPosition);
  1459. Assert.Equal (10, _textField.SelectedStart);
  1460. Assert.Equal (22, _textField.SelectedLength);
  1461. Assert.Equal ("p between text fields.", _textField.SelectedText);
  1462. break;
  1463. }
  1464. iteration++;
  1465. }
  1466. }
  1467. [Fact]
  1468. [AutoInitShutdown]
  1469. public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
  1470. {
  1471. var tf = new TextField { Width = 30, Text = "Les Misérables" };
  1472. Toplevel top = Application.Top;
  1473. top.Add (tf);
  1474. Application.Begin (top);
  1475. TestHelpers.AssertDriverContentsWithFrameAre (
  1476. @"
  1477. Les Misérables",
  1478. _output
  1479. );
  1480. tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1481. Application.Refresh ();
  1482. TestHelpers.AssertDriverContentsWithFrameAre (
  1483. @"
  1484. Les Misérables",
  1485. _output
  1486. );
  1487. // incorrect order will result with a wrong accent place
  1488. tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
  1489. Application.Refresh ();
  1490. TestHelpers.AssertDriverContentsWithFrameAre (
  1491. @"
  1492. Les Miśerables",
  1493. _output
  1494. );
  1495. }
  1496. private TextField GetTextFieldsInView ()
  1497. {
  1498. var tf = new TextField { Width = 10 };
  1499. var tf2 = new TextField { Y = 1, Width = 10 };
  1500. Toplevel top = Application.Top;
  1501. top.Add (tf);
  1502. top.Add (tf2);
  1503. Application.Begin (top);
  1504. Assert.Same (tf, top.Focused);
  1505. return tf;
  1506. }
  1507. private void SuppressKey (object s, Key arg)
  1508. {
  1509. if (arg.AsRune == new Rune ('j'))
  1510. {
  1511. arg.Handled = true;
  1512. }
  1513. }
  1514. private void SuppressRightClick (object sender, MouseEventEventArgs arg)
  1515. {
  1516. if (arg.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
  1517. {
  1518. arg.Handled = true;
  1519. }
  1520. }
  1521. // This class enables test functions annotated with the [InitShutdown] attribute
  1522. // to have a function called before the test function is called and after.
  1523. //
  1524. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1525. // as a pair, and b) all unit test functions should be atomic.
  1526. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1527. public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
  1528. {
  1529. public override void After (MethodInfo methodUnderTest)
  1530. {
  1531. _textField.Dispose ();
  1532. _textField = null;
  1533. base.After (methodUnderTest);
  1534. }
  1535. public override void Before (MethodInfo methodUnderTest)
  1536. {
  1537. base.Before (methodUnderTest);
  1538. //Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
  1539. _textField = new TextField
  1540. {
  1541. ColorScheme = new ColorScheme (Colors.ColorSchemes ["Base"]),
  1542. // 1 2 3
  1543. // 01234567890123456789012345678901=32 (Length)
  1544. Text = "TAB to jump between text fields.",
  1545. Width = 32
  1546. };
  1547. }
  1548. }
  1549. }