TextFieldTests.cs 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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 (ITestOutputHelper output)
  7. {
  8. private static TextField _textField;
  9. [Fact]
  10. [SetupFakeDriver]
  11. public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
  12. {
  13. var tf = new TextField { Width = 3, Text = "ắ" };
  14. tf.Draw ();
  15. TestHelpers.AssertDriverContentsWithFrameAre (
  16. @"
  17. ắ",
  18. output
  19. );
  20. tf.Text = "\u1eaf";
  21. tf.Draw ();
  22. TestHelpers.AssertDriverContentsWithFrameAre (
  23. @"
  24. ắ",
  25. output
  26. );
  27. tf.Text = "\u0103\u0301";
  28. tf.Draw ();
  29. TestHelpers.AssertDriverContentsWithFrameAre (
  30. @"
  31. ắ",
  32. output
  33. );
  34. tf.Text = "\u0061\u0306\u0301";
  35. tf.Draw ();
  36. TestHelpers.AssertDriverContentsWithFrameAre (
  37. @"
  38. ắ",
  39. output
  40. );
  41. }
  42. [Fact]
  43. [SetupFakeDriver]
  44. public void Adjust_First ()
  45. {
  46. var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
  47. tf.SetRelativeLayout (new (20, 20));
  48. tf.Draw ();
  49. Assert.Equal ("This is a test. ", GetContents ());
  50. string GetContents ()
  51. {
  52. var item = "";
  53. for (var i = 0; i < 16; i++)
  54. {
  55. item += Application.Driver?.Contents [0, i].Rune;
  56. }
  57. return item;
  58. }
  59. }
  60. [Fact]
  61. public void Cancel_TextChanging_ThenBackspace ()
  62. {
  63. var tf = new TextField ();
  64. tf.SetFocus ();
  65. tf.NewKeyDownEvent (Key.A.WithShift);
  66. Assert.Equal ("A", tf.Text);
  67. // cancel the next keystroke
  68. tf.TextChanging += (s, e) => e.Cancel = e.NewValue == "AB";
  69. tf.NewKeyDownEvent (Key.B.WithShift);
  70. // B was canceled so should just be A
  71. Assert.Equal ("A", tf.Text);
  72. // now delete the A
  73. tf.NewKeyDownEvent (Key.Backspace);
  74. Assert.Equal ("", tf.Text);
  75. }
  76. [Fact]
  77. [TextFieldTestsAutoInitShutdown]
  78. public void CanFocus_False_Wont_Focus_With_Mouse ()
  79. {
  80. Toplevel top = new ();
  81. var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  82. var fv = new FrameView
  83. {
  84. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  85. };
  86. fv.Add (tf);
  87. top.Add (fv);
  88. Application.Begin (top);
  89. Assert.False (tf.CanFocus);
  90. Assert.False (tf.HasFocus);
  91. Assert.False (fv.CanFocus);
  92. Assert.False (fv.HasFocus);
  93. tf.NewMouseEvent (
  94. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  95. );
  96. Assert.Null (tf.SelectedText);
  97. Assert.False (tf.CanFocus);
  98. Assert.False (tf.HasFocus);
  99. Assert.False (fv.CanFocus);
  100. Assert.False (fv.HasFocus);
  101. fv.CanFocus = true;
  102. tf.CanFocus = true;
  103. tf.NewMouseEvent (
  104. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  105. );
  106. Assert.Equal ("some ", tf.SelectedText);
  107. Assert.True (tf.CanFocus);
  108. Assert.True (tf.HasFocus);
  109. Assert.True (fv.CanFocus);
  110. Assert.True (fv.HasFocus);
  111. fv.CanFocus = false;
  112. tf.NewMouseEvent (
  113. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  114. );
  115. Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
  116. Assert.True (tf.CanFocus); // v2: CanFocus is not longer automatically changed
  117. Assert.False (tf.HasFocus);
  118. Assert.False (fv.CanFocus);
  119. Assert.False (fv.HasFocus);
  120. top.Dispose ();
  121. }
  122. [Theory]
  123. [AutoInitShutdown]
  124. [InlineData ("0123456789", "0123456789")]
  125. [InlineData ("01234567890", "0123456789")]
  126. public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
  127. {
  128. TextField tf = GetTextFieldsInView ();
  129. // Caption has no effect when focused
  130. tf.Caption = caption;
  131. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  132. Assert.False (tf.HasFocus);
  133. tf.Draw ();
  134. TestHelpers.AssertDriverContentsAre (expectedRender, output);
  135. Application.Top.Dispose ();
  136. }
  137. [Fact]
  138. [AutoInitShutdown]
  139. public void CaptionedTextField_DoesNotOverspillViewport_Unicode ()
  140. {
  141. string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  142. Assert.Equal (11, caption.Length);
  143. Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
  144. TextField tf = GetTextFieldsInView ();
  145. tf.Caption = caption;
  146. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  147. Assert.False (tf.HasFocus);
  148. tf.Draw ();
  149. TestHelpers.AssertDriverContentsAre ("Misérables", output);
  150. Application.Top.Dispose ();
  151. }
  152. [Theory]
  153. [AutoInitShutdown]
  154. [InlineData ("blah")]
  155. [InlineData (" ")]
  156. public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
  157. {
  158. TextField tf = GetTextFieldsInView ();
  159. tf.Draw ();
  160. TestHelpers.AssertDriverContentsAre ("", output);
  161. tf.Caption = "Enter txt";
  162. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  163. // Caption should appear when not focused and no text
  164. Assert.False (tf.HasFocus);
  165. tf.Draw ();
  166. TestHelpers.AssertDriverContentsAre ("Enter txt", output);
  167. // but disapear when text is added
  168. tf.Text = content;
  169. tf.Draw ();
  170. TestHelpers.AssertDriverContentsAre (content, output);
  171. Application.Top.Dispose ();
  172. }
  173. [Fact]
  174. [AutoInitShutdown]
  175. public void CaptionedTextField_RendersCaption_WhenNotFocused ()
  176. {
  177. TextField tf = GetTextFieldsInView ();
  178. tf.Draw ();
  179. TestHelpers.AssertDriverContentsAre ("", output);
  180. // Caption has no effect when focused
  181. tf.Caption = "Enter txt";
  182. Assert.True (tf.HasFocus);
  183. tf.Draw ();
  184. TestHelpers.AssertDriverContentsAre ("", output);
  185. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  186. Assert.False (tf.HasFocus);
  187. tf.Draw ();
  188. TestHelpers.AssertDriverContentsAre ("Enter txt", output);
  189. Application.Top.Dispose ();
  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, CancelEventArgs<string> e)
  303. {
  304. if (e.NewValue.GetRuneCount () > 11)
  305. {
  306. e.NewValue = e.NewValue [..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) =>
  361. {
  362. newText = e.NewValue;
  363. oldText = e.CurrentValue;
  364. };
  365. var top = new Toplevel ();
  366. top.Add (tf);
  367. Application.Begin (top);
  368. Assert.Equal ("-1", tf.Text);
  369. // InsertText
  370. tf.SelectedStart = 1;
  371. tf.CursorPosition = 2;
  372. Assert.Equal (1, tf.SelectedLength);
  373. Assert.Equal ("1", tf.SelectedText);
  374. Assert.True (tf.NewKeyDownEvent (Key.D2));
  375. Assert.Equal ("-2", newText);
  376. Assert.Equal ("-1", oldText);
  377. Assert.Equal ("-2", tf.Text);
  378. // DeleteCharLeft
  379. tf.SelectedStart = 1;
  380. tf.CursorPosition = 2;
  381. Assert.Equal (1, tf.SelectedLength);
  382. Assert.Equal ("2", tf.SelectedText);
  383. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  384. Assert.Equal ("-", newText);
  385. Assert.Equal ("-2", oldText);
  386. Assert.Equal ("-", tf.Text);
  387. // DeleteCharRight
  388. tf.Text = "-1";
  389. tf.SelectedStart = 1;
  390. tf.CursorPosition = 2;
  391. Assert.Equal (1, tf.SelectedLength);
  392. Assert.Equal ("1", tf.SelectedText);
  393. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  394. Assert.Equal ("-", newText);
  395. Assert.Equal ("-1", oldText);
  396. Assert.Equal ("-", tf.Text);
  397. // Cut
  398. tf.Text = "-1";
  399. tf.SelectedStart = 1;
  400. tf.CursorPosition = 2;
  401. Assert.Equal (1, tf.SelectedLength);
  402. Assert.Equal ("1", tf.SelectedText);
  403. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  404. Assert.Equal ("-", newText);
  405. Assert.Equal ("-1", oldText);
  406. Assert.Equal ("-", tf.Text);
  407. // Delete word with accented char
  408. tf.Text = "Les Misérables movie.";
  409. Assert.True (
  410. tf.NewMouseEvent (
  411. new () { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  412. )
  413. );
  414. Assert.Equal ("Misérables ", tf.SelectedText);
  415. Assert.Equal (11, tf.SelectedLength);
  416. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  417. Assert.Equal ("Les movie.", newText);
  418. Assert.Equal ("Les Misérables movie.", oldText);
  419. Assert.Equal ("Les movie.", tf.Text);
  420. top.Dispose ();
  421. }
  422. [Fact]
  423. public void HistoryText_IsDirty_ClearHistoryChanges ()
  424. {
  425. var text = "Testing";
  426. var tf = new TextField { Text = text };
  427. tf.BeginInit ();
  428. tf.EndInit ();
  429. Assert.Equal (text, tf.Text);
  430. tf.ClearHistoryChanges ();
  431. Assert.False (tf.IsDirty);
  432. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift));
  433. Assert.Equal ($"{text}A", tf.Text);
  434. Assert.True (tf.IsDirty);
  435. }
  436. [Fact]
  437. public void Space_Does_Not_Raise_Selected ()
  438. {
  439. TextField tf = new ();
  440. tf.Selecting += (sender, args) => Assert.Fail ("Selected should not be raied.");
  441. Application.Top = new ();
  442. Application.Top.Add (tf);
  443. tf.SetFocus ();
  444. Application.OnKeyDown (Key.Space);
  445. Application.Top.Dispose ();
  446. Application.ResetState (true);
  447. }
  448. [Fact]
  449. public void Enter_Does_Not_Raise_Selected ()
  450. {
  451. TextField tf = new ();
  452. var selectingCount = 0;
  453. tf.Selecting += (sender, args) => selectingCount++;
  454. Application.Top = new ();
  455. Application.Top.Add (tf);
  456. tf.SetFocus ();
  457. Application.OnKeyDown (Key.Enter);
  458. Assert.Equal (0, selectingCount);
  459. Application.Top.Dispose ();
  460. Application.ResetState (true);
  461. }
  462. [Fact]
  463. public void Enter_Raises_Accepted ()
  464. {
  465. TextField tf = new ();
  466. var acceptedCount = 0;
  467. tf.Accepting += (sender, args) => acceptedCount++;
  468. Application.Top = new ();
  469. Application.Top.Add (tf);
  470. tf.SetFocus ();
  471. Application.OnKeyDown (Key.Enter);
  472. Assert.Equal (1, acceptedCount);
  473. Application.Top.Dispose ();
  474. Application.ResetState (true);
  475. }
  476. [Fact]
  477. [AutoInitShutdown (useFakeClipboard: true)]
  478. public void KeyBindings_Command ()
  479. {
  480. var tf = new TextField { Width = 20, Text = "This is a test." };
  481. tf.BeginInit ();
  482. tf.EndInit ();
  483. Assert.Equal (15, tf.Text.Length);
  484. Assert.Equal (15, tf.CursorPosition);
  485. Assert.False (tf.ReadOnly);
  486. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  487. Assert.Equal ("This is a test.", tf.Text);
  488. tf.CursorPosition = 0;
  489. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  490. Assert.Equal ("his is a test.", tf.Text);
  491. tf.ReadOnly = true;
  492. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
  493. Assert.Equal ("his is a test.", tf.Text);
  494. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  495. Assert.Equal ("his is a test.", tf.Text);
  496. tf.ReadOnly = false;
  497. tf.CursorPosition = 1;
  498. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  499. Assert.Equal ("is is a test.", tf.Text);
  500. tf.CursorPosition = 5;
  501. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift));
  502. Assert.Equal ("is is a test.", tf.Text);
  503. Assert.Equal ("is is", tf.SelectedText);
  504. tf.CursorPosition = 5;
  505. tf.SelectedStart = -1;
  506. Assert.Null (tf.SelectedText);
  507. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
  508. Assert.Equal ("is is a test.", tf.Text);
  509. Assert.Equal ("is is", tf.SelectedText);
  510. tf.CursorPosition = 5;
  511. tf.SelectedStart = -1;
  512. Assert.Null (tf.SelectedText);
  513. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift.WithCtrl));
  514. Assert.Equal ("is is a test.", tf.Text);
  515. Assert.Equal ("is is", tf.SelectedText);
  516. tf.CursorPosition = 5;
  517. tf.SelectedStart = -1;
  518. Assert.Null (tf.SelectedText);
  519. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift));
  520. Assert.Equal ("is is a test.", tf.Text);
  521. Assert.Equal (" a test.", tf.SelectedText);
  522. tf.CursorPosition = 5;
  523. tf.SelectedStart = -1;
  524. Assert.Null (tf.SelectedText);
  525. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
  526. Assert.Equal ("is is a test.", tf.Text);
  527. Assert.Equal (" a test.", tf.SelectedText);
  528. tf.CursorPosition = 5;
  529. tf.SelectedStart = -1;
  530. Assert.Null (tf.SelectedText);
  531. Assert.True (tf.NewKeyDownEvent (Key.E.WithShift.WithCtrl));
  532. Assert.Equal ("is is a test.", tf.Text);
  533. Assert.Equal (" a test.", tf.SelectedText);
  534. tf.CursorPosition = 5;
  535. tf.SelectedStart = -1;
  536. Assert.Null (tf.SelectedText);
  537. Assert.True (tf.NewKeyDownEvent (Key.Home));
  538. Assert.Equal ("is is a test.", tf.Text);
  539. Assert.Equal (0, tf.CursorPosition);
  540. tf.CursorPosition = 5;
  541. Assert.Null (tf.SelectedText);
  542. Assert.True (tf.NewKeyDownEvent (Key.Home.WithCtrl));
  543. Assert.Equal ("is is a test.", tf.Text);
  544. Assert.Equal (0, tf.CursorPosition);
  545. tf.CursorPosition = 5;
  546. Assert.Null (tf.SelectedText);
  547. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  548. Assert.Equal ("is is a test.", tf.Text);
  549. Assert.Equal (0, tf.CursorPosition);
  550. tf.CursorPosition = 5;
  551. tf.SelectedStart = -1;
  552. Assert.Null (tf.SelectedText);
  553. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  554. Assert.Equal ("is is a test.", tf.Text);
  555. Assert.Equal ("s", tf.SelectedText);
  556. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift));
  557. Assert.Equal ("is is a test.", tf.Text);
  558. Assert.Equal ("is", tf.SelectedText);
  559. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  560. Assert.Equal ("is is a test.", tf.Text);
  561. Assert.Equal ("s", tf.SelectedText);
  562. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift));
  563. Assert.Equal ("is is a test.", tf.Text);
  564. Assert.Null (tf.SelectedText);
  565. tf.CursorPosition = 7;
  566. tf.SelectedStart = -1;
  567. Assert.Null (tf.SelectedText);
  568. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
  569. Assert.Equal ("is is a test.", tf.Text);
  570. Assert.Equal ("a", tf.SelectedText);
  571. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  572. Assert.Equal ("is is a test.", tf.Text);
  573. Assert.Equal ("is a", tf.SelectedText);
  574. #if UNIX_KEY_BINDINGS
  575. Assert.True (tf.NewKeyDownEvent (Key.B.WithShift.WithAlt));
  576. #else
  577. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  578. #endif
  579. Assert.Equal ("is is a test.", tf.Text);
  580. Assert.Equal ("is is a", tf.SelectedText);
  581. tf.CursorPosition = 3;
  582. tf.SelectedStart = -1;
  583. Assert.Null (tf.SelectedText);
  584. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  585. Assert.Equal ("is is a test.", tf.Text);
  586. Assert.Equal ("is ", tf.SelectedText);
  587. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift.WithCtrl));
  588. Assert.Equal ("is is a test.", tf.Text);
  589. Assert.Equal ("is a ", tf.SelectedText);
  590. #if UNIX_KEY_BINDINGS
  591. Assert.True (tf.NewKeyDownEvent (Key.F.WithShift.WithAlt));
  592. #else
  593. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  594. #endif
  595. Assert.Equal ("is is a test.", tf.Text);
  596. Assert.Equal ("is a test.", tf.SelectedText);
  597. Assert.Equal (13, tf.CursorPosition);
  598. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  599. Assert.Equal ("is is a test.", tf.Text);
  600. Assert.Null (tf.SelectedText);
  601. Assert.Equal (12, tf.CursorPosition);
  602. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  603. Assert.Equal ("is is a test.", tf.Text);
  604. Assert.Equal (11, tf.CursorPosition);
  605. Assert.True (tf.NewKeyDownEvent (Key.End));
  606. Assert.Equal ("is is a test.", tf.Text);
  607. Assert.Equal (13, tf.CursorPosition);
  608. tf.CursorPosition = 0;
  609. Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
  610. Assert.Equal ("is is a test.", tf.Text);
  611. Assert.Equal (13, tf.CursorPosition);
  612. tf.CursorPosition = 0;
  613. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  614. Assert.Equal ("is is a test.", tf.Text);
  615. Assert.Equal (13, tf.CursorPosition);
  616. tf.CursorPosition = 0;
  617. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  618. Assert.Equal ("is is a test.", tf.Text);
  619. Assert.Equal (1, tf.CursorPosition);
  620. Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
  621. Assert.Equal ("is is a test.", tf.Text);
  622. Assert.Equal (2, tf.CursorPosition);
  623. tf.CursorPosition = 9;
  624. tf.ReadOnly = true;
  625. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  626. Assert.Equal ("is is a test.", tf.Text);
  627. tf.ReadOnly = false;
  628. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  629. Assert.Equal ("is is a t", tf.Text);
  630. Assert.Equal ("est.", Clipboard.Contents);
  631. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  632. Assert.Equal ("is is a test.", tf.Text);
  633. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  634. Assert.Equal ("is is a t", tf.Text);
  635. #if UNIX_KEY_BINDINGS
  636. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
  637. #else
  638. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  639. #endif
  640. Assert.Equal ("is is a test.", tf.Text);
  641. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  642. Assert.Equal ("is is a t", tf.Text);
  643. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  644. Assert.Equal ("is is a t", tf.Text);
  645. Assert.Equal (8, tf.CursorPosition);
  646. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  647. Assert.Equal ("is is a t", tf.Text);
  648. Assert.Equal (6, tf.CursorPosition);
  649. #if UNIX_KEY_BINDINGS
  650. Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
  651. #else
  652. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  653. #endif
  654. Assert.Equal ("is is a t", tf.Text);
  655. Assert.Equal (3, tf.CursorPosition);
  656. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  657. Assert.Equal ("is is a t", tf.Text);
  658. Assert.Equal (6, tf.CursorPosition);
  659. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  660. Assert.Equal ("is is a t", tf.Text);
  661. Assert.Equal (8, tf.CursorPosition);
  662. #if UNIX_KEY_BINDINGS
  663. Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
  664. #else
  665. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  666. #endif
  667. Assert.Equal ("is is a t", tf.Text);
  668. Assert.Equal (9, tf.CursorPosition);
  669. Assert.True (tf.Used);
  670. Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
  671. Assert.Equal ("is is a t", tf.Text);
  672. Assert.Equal (9, tf.CursorPosition);
  673. Assert.False (tf.Used);
  674. tf.SelectedStart = 3;
  675. tf.CursorPosition = 7;
  676. Assert.Equal ("is a", tf.SelectedText);
  677. Assert.Equal ("est.", Clipboard.Contents);
  678. Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
  679. Assert.Equal ("is is a t", tf.Text);
  680. Assert.Equal ("is a", Clipboard.Contents);
  681. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  682. Assert.Equal ("is t", tf.Text);
  683. Assert.Equal ("is a", Clipboard.Contents);
  684. Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
  685. Assert.Equal ("is is a t", tf.Text);
  686. Assert.Equal ("is a", Clipboard.Contents);
  687. Assert.Equal (7, tf.CursorPosition);
  688. #if UNIX_KEY_BINDINGS
  689. Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
  690. #else
  691. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
  692. #endif
  693. Assert.Equal (" t", tf.Text);
  694. Assert.Equal ("is is a", Clipboard.Contents);
  695. tf.Text = "TAB to jump between text fields.";
  696. Assert.Equal (0, tf.CursorPosition);
  697. Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
  698. Assert.Equal ("to jump between text fields.", tf.Text);
  699. tf.CursorPosition = tf.Text.Length;
  700. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
  701. Assert.Equal ("to jump between text fields", tf.Text);
  702. Assert.True (tf.NewKeyDownEvent (Key.T.WithCtrl));
  703. Assert.Equal ("to jump between text fields", tf.SelectedText);
  704. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
  705. Assert.Equal ("", tf.Text);
  706. }
  707. [Fact]
  708. public void HotKey_Command_SetsFocus ()
  709. {
  710. var view = new TextField ();
  711. view.CanFocus = true;
  712. Assert.False (view.HasFocus);
  713. view.InvokeCommand (Command.HotKey);
  714. Assert.True (view.HasFocus);
  715. }
  716. [Fact]
  717. public void HotKey_Command_Does_Not_Accept ()
  718. {
  719. var view = new TextField ();
  720. var accepted = false;
  721. view.Accepting += OnAccept;
  722. view.InvokeCommand (Command.HotKey);
  723. Assert.False (accepted);
  724. return;
  725. void OnAccept (object sender, CommandEventArgs e) { accepted = true; }
  726. }
  727. [Fact]
  728. public void Accepted_Command_Fires_Accept ()
  729. {
  730. var view = new TextField ();
  731. var accepted = false;
  732. view.Accepting += Accept;
  733. view.InvokeCommand (Command.Accept);
  734. Assert.True (accepted);
  735. return;
  736. void Accept (object sender, CommandEventArgs e) { accepted = true; }
  737. }
  738. [Theory]
  739. [InlineData (false, 1)]
  740. [InlineData (true, 0)]
  741. public void Accepted_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
  742. {
  743. var superView = new Window
  744. {
  745. Id = "superView"
  746. };
  747. var tf = new TextField
  748. {
  749. Id = "tf"
  750. };
  751. var button = new Button
  752. {
  753. Id = "button",
  754. IsDefault = true
  755. };
  756. superView.Add (tf, button);
  757. var buttonAccept = 0;
  758. button.Accepting += ButtonAccept;
  759. var textFieldAccept = 0;
  760. tf.Accepting += TextFieldAccept;
  761. tf.SetFocus ();
  762. Assert.True (tf.HasFocus);
  763. superView.NewKeyDownEvent (Key.Enter);
  764. Assert.Equal (1, textFieldAccept);
  765. Assert.Equal (expectedButtonAccepts, buttonAccept);
  766. button.SetFocus ();
  767. superView.NewKeyDownEvent (Key.Enter);
  768. Assert.Equal (1, textFieldAccept);
  769. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  770. return;
  771. void TextFieldAccept (object sender, CommandEventArgs e)
  772. {
  773. textFieldAccept++;
  774. e.Cancel = handleAccept;
  775. }
  776. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  777. }
  778. [Fact]
  779. public void Accepted_No_Handler_Enables_Default_Button_Accept ()
  780. {
  781. var superView = new Window
  782. {
  783. Id = "superView"
  784. };
  785. var tf = new TextField
  786. {
  787. Id = "tf"
  788. };
  789. var button = new Button
  790. {
  791. Id = "button",
  792. IsDefault = true
  793. };
  794. superView.Add (tf, button);
  795. var buttonAccept = 0;
  796. button.Accepting += ButtonAccept;
  797. tf.SetFocus ();
  798. Assert.True (tf.HasFocus);
  799. superView.NewKeyDownEvent (Key.Enter);
  800. Assert.Equal (1, buttonAccept);
  801. button.SetFocus ();
  802. superView.NewKeyDownEvent (Key.Enter);
  803. Assert.Equal (2, buttonAccept);
  804. return;
  805. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  806. }
  807. [Fact]
  808. public void Accepted_Cancel_Event_HandlesCommand ()
  809. {
  810. //var super = new View ();
  811. var view = new TextField ();
  812. //super.Add (view);
  813. //var superAcceptedInvoked = false;
  814. var tfAcceptedInvoked = false;
  815. var handle = false;
  816. view.Accepting += TextViewAccept;
  817. Assert.False (view.InvokeCommand (Command.Accept));
  818. Assert.True (tfAcceptedInvoked);
  819. tfAcceptedInvoked = false;
  820. handle = true;
  821. view.Accepting += TextViewAccept;
  822. Assert.True (view.InvokeCommand (Command.Accept));
  823. Assert.True (tfAcceptedInvoked);
  824. return;
  825. void TextViewAccept (object sender, CommandEventArgs e)
  826. {
  827. tfAcceptedInvoked = true;
  828. e.Cancel = handle;
  829. }
  830. }
  831. [Fact]
  832. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  833. {
  834. var top = new Toplevel ();
  835. var tf = new TextField { Width = 10 };
  836. top.Add (tf);
  837. Exception exception = Record.Exception (() => tf.SetFocus ());
  838. Assert.Null (exception);
  839. }
  840. [Fact]
  841. [TextFieldTestsAutoInitShutdown]
  842. public void Paste_Always_Clear_The_SelectedText ()
  843. {
  844. _textField.SelectedStart = 20;
  845. _textField.CursorPosition = 24;
  846. _textField.Copy ();
  847. Assert.Equal ("text", _textField.SelectedText);
  848. _textField.Paste ();
  849. Assert.Null (_textField.SelectedText);
  850. }
  851. [Fact]
  852. public void Backspace_From_End ()
  853. {
  854. var tf = new TextField { Text = "ABC" };
  855. tf.SetFocus ();
  856. Assert.Equal ("ABC", tf.Text);
  857. tf.BeginInit ();
  858. tf.EndInit ();
  859. Assert.Equal (3, tf.CursorPosition);
  860. // now delete the C
  861. tf.NewKeyDownEvent (Key.Backspace);
  862. Assert.Equal ("AB", tf.Text);
  863. Assert.Equal (2, tf.CursorPosition);
  864. // then delete the B
  865. tf.NewKeyDownEvent (Key.Backspace);
  866. Assert.Equal ("A", tf.Text);
  867. Assert.Equal (1, tf.CursorPosition);
  868. // then delete the A
  869. tf.NewKeyDownEvent (Key.Backspace);
  870. Assert.Equal ("", tf.Text);
  871. Assert.Equal (0, tf.CursorPosition);
  872. }
  873. [Fact]
  874. public void Backspace_From_Middle ()
  875. {
  876. var tf = new TextField { Text = "ABC" };
  877. tf.SetFocus ();
  878. tf.CursorPosition = 2;
  879. Assert.Equal ("ABC", tf.Text);
  880. // now delete the B
  881. tf.NewKeyDownEvent (Key.Backspace);
  882. Assert.Equal ("AC", tf.Text);
  883. // then delete the A
  884. tf.NewKeyDownEvent (Key.Backspace);
  885. Assert.Equal ("C", tf.Text);
  886. // then delete nothing
  887. tf.NewKeyDownEvent (Key.Backspace);
  888. Assert.Equal ("C", tf.Text);
  889. // now delete the C
  890. tf.CursorPosition = 1;
  891. tf.NewKeyDownEvent (Key.Backspace);
  892. Assert.Equal ("", tf.Text);
  893. }
  894. [Fact]
  895. [AutoInitShutdown]
  896. public void ScrollOffset_Initialize ()
  897. {
  898. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  899. tf.BeginInit ();
  900. tf.EndInit ();
  901. Assert.Equal (0, tf.ScrollOffset);
  902. Assert.Equal (16, tf.CursorPosition);
  903. }
  904. [Fact]
  905. [TextFieldTestsAutoInitShutdown]
  906. public void Selected_Text_Shows ()
  907. {
  908. // Proves #3022 is fixed (TextField selected text does not show in v2)
  909. _textField.CursorPosition = 0;
  910. var top = new Toplevel ();
  911. top.Add (_textField);
  912. RunState rs = Application.Begin (top);
  913. Attribute [] attributes =
  914. {
  915. _textField.ColorScheme.Focus,
  916. new (
  917. _textField.ColorScheme.Focus.Background,
  918. _textField.ColorScheme.Focus.Foreground
  919. )
  920. };
  921. // TAB to jump between text fields.
  922. TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
  923. // Cursor is at the end
  924. Assert.Equal (32, _textField.CursorPosition);
  925. _textField.CursorPosition = 0;
  926. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  927. var first = true;
  928. Application.RunIteration (ref rs, ref first);
  929. Assert.Equal (4, _textField.CursorPosition);
  930. // TAB to jump between text fields.
  931. TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
  932. top.Dispose ();
  933. }
  934. [Fact]
  935. [TextFieldTestsAutoInitShutdown]
  936. public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  937. {
  938. _textField.CursorPosition = 33;
  939. _textField.SelectedStart = 33;
  940. Assert.Equal (32, _textField.CursorPosition);
  941. Assert.Equal (32, _textField.SelectedStart);
  942. Assert.Equal (0, _textField.SelectedLength);
  943. Assert.Null (_textField.SelectedText);
  944. }
  945. [Fact]
  946. [TextFieldTestsAutoInitShutdown]
  947. public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
  948. {
  949. _textField.SelectedStart = 19;
  950. _textField.CursorPosition = 12;
  951. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  952. _textField.NewKeyDownEvent (Key.U); // u
  953. Assert.Equal ("TAB to jump u text fields.", _textField.Text);
  954. }
  955. [Fact]
  956. [TextFieldTestsAutoInitShutdown]
  957. public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  958. {
  959. _textField.CursorPosition = 2;
  960. _textField.SelectedStart = 33;
  961. Assert.Equal (32, _textField.SelectedStart);
  962. Assert.Equal (30, _textField.SelectedLength);
  963. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  964. }
  965. [Fact]
  966. [TextFieldTestsAutoInitShutdown]
  967. public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
  968. {
  969. _textField.SelectedStart = -2;
  970. Assert.Equal (-1, _textField.SelectedStart);
  971. Assert.Equal (0, _textField.SelectedLength);
  972. Assert.Null (_textField.SelectedText);
  973. }
  974. [Fact]
  975. public void KeyDown_Handled_Prevents_Input ()
  976. {
  977. var tf = new TextField ();
  978. tf.KeyDown += HandleJKey;
  979. tf.NewKeyDownEvent (Key.A);
  980. Assert.Equal ("a", tf.Text);
  981. // SuppressKey suppresses the 'j' key
  982. tf.NewKeyDownEvent (Key.J);
  983. Assert.Equal ("a", tf.Text);
  984. tf.KeyDown -= HandleJKey;
  985. // Now that the delegate has been removed we can type j again
  986. tf.NewKeyDownEvent (Key.J);
  987. Assert.Equal ("aj", tf.Text);
  988. return;
  989. void HandleJKey (object s, Key arg)
  990. {
  991. if (arg.AsRune == new Rune ('j'))
  992. {
  993. arg.Handled = true;
  994. }
  995. }
  996. }
  997. [Fact]
  998. [AutoInitShutdown]
  999. public void MouseEvent_Handled_Prevents_RightClick ()
  1000. {
  1001. Application.MouseEvent += HandleRightClick;
  1002. var tf = new TextField { Width = 10 };
  1003. var clickCounter = 0;
  1004. tf.MouseClick += (s, m) => { clickCounter++; };
  1005. var top = new Toplevel ();
  1006. top.Add (tf);
  1007. Application.Begin (top);
  1008. var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
  1009. Application.OnMouseEvent (mouseEvent);
  1010. Assert.Equal (1, clickCounter);
  1011. // Get a fresh instance that represents a right click.
  1012. // Should be ignored because of SuppressRightClick callback
  1013. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  1014. Application.OnMouseEvent (mouseEvent);
  1015. Assert.Equal (1, clickCounter);
  1016. Application.MouseEvent -= HandleRightClick;
  1017. // Get a fresh instance that represents a right click.
  1018. // Should no longer be ignored as the callback was removed
  1019. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  1020. // In #3183 OnMouseClicked is no longer called before MouseEvent().
  1021. // This call causes the context menu to pop, and MouseEvent() returns true.
  1022. // Thus, the clickCounter is NOT incremented.
  1023. // Which is correct, because the user did NOT click with the left mouse button.
  1024. Application.OnMouseEvent (mouseEvent);
  1025. Assert.Equal (1, clickCounter);
  1026. top.Dispose ();
  1027. return;
  1028. void HandleRightClick (object sender, MouseEvent arg)
  1029. {
  1030. if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
  1031. {
  1032. arg.Handled = true;
  1033. }
  1034. }
  1035. }
  1036. [InlineData ("a")] // Lower than selection
  1037. [InlineData ("aaaaaaaaaaa")] // Greater than selection
  1038. [InlineData ("aaaa")] // Equal than selection
  1039. [Theory]
  1040. public void SetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
  1041. {
  1042. var tf = new TextField ();
  1043. tf.Text = "fish";
  1044. tf.CursorPosition = tf.Text.Length;
  1045. tf.NewKeyDownEvent (Key.CursorLeft);
  1046. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1047. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1048. Assert.Equal (1, tf.CursorPosition);
  1049. Assert.Equal (2, tf.SelectedLength);
  1050. Assert.Equal ("is", tf.SelectedText);
  1051. tf.Text = newText;
  1052. tf.CursorPosition = tf.Text.Length;
  1053. Assert.Equal (newText.Length, tf.CursorPosition);
  1054. Assert.Equal (0, tf.SelectedLength);
  1055. Assert.Null (tf.SelectedText);
  1056. }
  1057. [Fact]
  1058. [TextFieldTestsAutoInitShutdown]
  1059. public void Text_Replaces_Tabs_With_Empty_String ()
  1060. {
  1061. _textField.Text = "\t\tTAB to jump between text fields.";
  1062. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1063. _textField.Text = "";
  1064. Clipboard.Contents = "\t\tTAB to jump between text fields.";
  1065. _textField.Paste ();
  1066. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1067. }
  1068. [Fact]
  1069. [TextFieldTestsAutoInitShutdown]
  1070. public void TextChanged_Event ()
  1071. {
  1072. var eventFired = false;
  1073. _textField.TextChanged += (s, e) => eventFired = true;
  1074. _textField.Text = "changed";
  1075. Assert.True (eventFired);
  1076. Assert.Equal ("changed", _textField.Text);
  1077. }
  1078. [Fact]
  1079. [TextFieldTestsAutoInitShutdown]
  1080. public void TextChanging_Event ()
  1081. {
  1082. var cancel = true;
  1083. _textField.TextChanging += (s, e) =>
  1084. {
  1085. Assert.Equal ("changing", e.NewValue);
  1086. if (cancel)
  1087. {
  1088. e.Cancel = true;
  1089. }
  1090. };
  1091. _textField.Text = "changing";
  1092. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1093. cancel = false;
  1094. _textField.Text = "changing";
  1095. Assert.Equal ("changing", _textField.Text);
  1096. }
  1097. [Fact]
  1098. public void SpaceHandling ()
  1099. {
  1100. var tf = new TextField { Width = 10, Text = " " };
  1101. var ev = new MouseEvent { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
  1102. tf.NewMouseEvent (ev);
  1103. Assert.Equal (1, tf.SelectedLength);
  1104. ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
  1105. tf.NewMouseEvent (ev);
  1106. Assert.Equal (1, tf.SelectedLength);
  1107. }
  1108. [Fact]
  1109. [TextFieldTestsAutoInitShutdown]
  1110. public void Used_Is_False ()
  1111. {
  1112. _textField.Used = false;
  1113. _textField.CursorPosition = 10;
  1114. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1115. _textField.NewKeyDownEvent (Key.U); // u
  1116. Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
  1117. _textField.NewKeyDownEvent (Key.S); // s
  1118. Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
  1119. _textField.NewKeyDownEvent (Key.E); // e
  1120. Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
  1121. _textField.NewKeyDownEvent (Key.D); // d
  1122. Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
  1123. }
  1124. [Fact]
  1125. [TextFieldTestsAutoInitShutdown]
  1126. public void Used_Is_True_By_Default ()
  1127. {
  1128. _textField.CursorPosition = 10;
  1129. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1130. _textField.NewKeyDownEvent (Key.U); // u
  1131. Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
  1132. _textField.NewKeyDownEvent (Key.S); // s
  1133. Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
  1134. _textField.NewKeyDownEvent (Key.E); // e
  1135. Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
  1136. _textField.NewKeyDownEvent (Key.D); // d
  1137. Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
  1138. }
  1139. [Fact]
  1140. [TextFieldTestsAutoInitShutdown]
  1141. public void WordBackward_With_No_Selection ()
  1142. {
  1143. _textField.CursorPosition = _textField.Text.Length;
  1144. var iteration = 0;
  1145. while (_textField.CursorPosition > 0)
  1146. {
  1147. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1148. switch (iteration)
  1149. {
  1150. case 0:
  1151. Assert.Equal (31, _textField.CursorPosition);
  1152. Assert.Equal (-1, _textField.SelectedStart);
  1153. Assert.Equal (0, _textField.SelectedLength);
  1154. Assert.Null (_textField.SelectedText);
  1155. break;
  1156. case 1:
  1157. Assert.Equal (25, _textField.CursorPosition);
  1158. Assert.Equal (-1, _textField.SelectedStart);
  1159. Assert.Equal (0, _textField.SelectedLength);
  1160. Assert.Null (_textField.SelectedText);
  1161. break;
  1162. case 2:
  1163. Assert.Equal (20, _textField.CursorPosition);
  1164. Assert.Equal (-1, _textField.SelectedStart);
  1165. Assert.Equal (0, _textField.SelectedLength);
  1166. Assert.Null (_textField.SelectedText);
  1167. break;
  1168. case 3:
  1169. Assert.Equal (12, _textField.CursorPosition);
  1170. Assert.Equal (-1, _textField.SelectedStart);
  1171. Assert.Equal (0, _textField.SelectedLength);
  1172. Assert.Null (_textField.SelectedText);
  1173. break;
  1174. case 4:
  1175. Assert.Equal (7, _textField.CursorPosition);
  1176. Assert.Equal (-1, _textField.SelectedStart);
  1177. Assert.Equal (0, _textField.SelectedLength);
  1178. Assert.Null (_textField.SelectedText);
  1179. break;
  1180. case 5:
  1181. Assert.Equal (4, _textField.CursorPosition);
  1182. Assert.Equal (-1, _textField.SelectedStart);
  1183. Assert.Equal (0, _textField.SelectedLength);
  1184. Assert.Null (_textField.SelectedText);
  1185. break;
  1186. case 6:
  1187. Assert.Equal (0, _textField.CursorPosition);
  1188. Assert.Equal (-1, _textField.SelectedStart);
  1189. Assert.Equal (0, _textField.SelectedLength);
  1190. Assert.Null (_textField.SelectedText);
  1191. break;
  1192. }
  1193. iteration++;
  1194. }
  1195. }
  1196. [Fact]
  1197. [TextFieldTestsAutoInitShutdown]
  1198. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1199. {
  1200. // 1 2 3 4 5
  1201. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1202. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1203. _textField.CursorPosition = _textField.Text.Length;
  1204. var iteration = 0;
  1205. while (_textField.CursorPosition > 0)
  1206. {
  1207. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1208. switch (iteration)
  1209. {
  1210. case 0:
  1211. Assert.Equal (54, _textField.CursorPosition);
  1212. Assert.Equal (-1, _textField.SelectedStart);
  1213. Assert.Equal (0, _textField.SelectedLength);
  1214. Assert.Null (_textField.SelectedText);
  1215. break;
  1216. case 1:
  1217. Assert.Equal (48, _textField.CursorPosition);
  1218. Assert.Equal (-1, _textField.SelectedStart);
  1219. Assert.Equal (0, _textField.SelectedLength);
  1220. Assert.Null (_textField.SelectedText);
  1221. break;
  1222. case 2:
  1223. Assert.Equal (46, _textField.CursorPosition);
  1224. Assert.Equal (-1, _textField.SelectedStart);
  1225. Assert.Equal (0, _textField.SelectedLength);
  1226. Assert.Null (_textField.SelectedText);
  1227. break;
  1228. case 3:
  1229. Assert.Equal (40, _textField.CursorPosition);
  1230. Assert.Equal (-1, _textField.SelectedStart);
  1231. Assert.Equal (0, _textField.SelectedLength);
  1232. Assert.Null (_textField.SelectedText);
  1233. break;
  1234. case 4:
  1235. Assert.Equal (38, _textField.CursorPosition);
  1236. Assert.Equal (-1, _textField.SelectedStart);
  1237. Assert.Equal (0, _textField.SelectedLength);
  1238. Assert.Null (_textField.SelectedText);
  1239. break;
  1240. case 5:
  1241. Assert.Equal (28, _textField.CursorPosition);
  1242. Assert.Equal (-1, _textField.SelectedStart);
  1243. Assert.Equal (0, _textField.SelectedLength);
  1244. Assert.Null (_textField.SelectedText);
  1245. break;
  1246. case 6:
  1247. Assert.Equal (25, _textField.CursorPosition);
  1248. Assert.Equal (-1, _textField.SelectedStart);
  1249. Assert.Equal (0, _textField.SelectedLength);
  1250. Assert.Null (_textField.SelectedText);
  1251. break;
  1252. case 7:
  1253. Assert.Equal (12, _textField.CursorPosition);
  1254. Assert.Equal (-1, _textField.SelectedStart);
  1255. Assert.Equal (0, _textField.SelectedLength);
  1256. Assert.Null (_textField.SelectedText);
  1257. break;
  1258. case 8:
  1259. Assert.Equal (9, _textField.CursorPosition);
  1260. Assert.Equal (-1, _textField.SelectedStart);
  1261. Assert.Equal (0, _textField.SelectedLength);
  1262. Assert.Null (_textField.SelectedText);
  1263. break;
  1264. case 9:
  1265. Assert.Equal (6, _textField.CursorPosition);
  1266. Assert.Equal (-1, _textField.SelectedStart);
  1267. Assert.Equal (0, _textField.SelectedLength);
  1268. Assert.Null (_textField.SelectedText);
  1269. break;
  1270. case 10:
  1271. Assert.Equal (0, _textField.CursorPosition);
  1272. Assert.Equal (-1, _textField.SelectedStart);
  1273. Assert.Equal (0, _textField.SelectedLength);
  1274. Assert.Null (_textField.SelectedText);
  1275. break;
  1276. }
  1277. iteration++;
  1278. }
  1279. }
  1280. [Fact]
  1281. [TextFieldTestsAutoInitShutdown]
  1282. public void WordBackward_With_Selection ()
  1283. {
  1284. _textField.CursorPosition = _textField.Text.Length;
  1285. _textField.SelectedStart = _textField.Text.Length;
  1286. var iteration = 0;
  1287. while (_textField.CursorPosition > 0)
  1288. {
  1289. _textField.NewKeyDownEvent (
  1290. new (
  1291. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1292. )
  1293. );
  1294. switch (iteration)
  1295. {
  1296. case 0:
  1297. Assert.Equal (31, _textField.CursorPosition);
  1298. Assert.Equal (32, _textField.SelectedStart);
  1299. Assert.Equal (1, _textField.SelectedLength);
  1300. Assert.Equal (".", _textField.SelectedText);
  1301. break;
  1302. case 1:
  1303. Assert.Equal (25, _textField.CursorPosition);
  1304. Assert.Equal (32, _textField.SelectedStart);
  1305. Assert.Equal (7, _textField.SelectedLength);
  1306. Assert.Equal ("fields.", _textField.SelectedText);
  1307. break;
  1308. case 2:
  1309. Assert.Equal (20, _textField.CursorPosition);
  1310. Assert.Equal (32, _textField.SelectedStart);
  1311. Assert.Equal (12, _textField.SelectedLength);
  1312. Assert.Equal ("text fields.", _textField.SelectedText);
  1313. break;
  1314. case 3:
  1315. Assert.Equal (12, _textField.CursorPosition);
  1316. Assert.Equal (32, _textField.SelectedStart);
  1317. Assert.Equal (20, _textField.SelectedLength);
  1318. Assert.Equal ("between text fields.", _textField.SelectedText);
  1319. break;
  1320. case 4:
  1321. Assert.Equal (7, _textField.CursorPosition);
  1322. Assert.Equal (32, _textField.SelectedStart);
  1323. Assert.Equal (25, _textField.SelectedLength);
  1324. Assert.Equal ("jump between text fields.", _textField.SelectedText);
  1325. break;
  1326. case 5:
  1327. Assert.Equal (4, _textField.CursorPosition);
  1328. Assert.Equal (32, _textField.SelectedStart);
  1329. Assert.Equal (28, _textField.SelectedLength);
  1330. Assert.Equal ("to jump between text fields.", _textField.SelectedText);
  1331. break;
  1332. case 6:
  1333. Assert.Equal (0, _textField.CursorPosition);
  1334. Assert.Equal (32, _textField.SelectedStart);
  1335. Assert.Equal (32, _textField.SelectedLength);
  1336. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1337. break;
  1338. }
  1339. iteration++;
  1340. }
  1341. }
  1342. [Fact]
  1343. [TextFieldTestsAutoInitShutdown]
  1344. public void
  1345. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1346. {
  1347. _textField.CursorPosition = 10;
  1348. _textField.SelectedStart = 10;
  1349. var iteration = 0;
  1350. while (_textField.CursorPosition > 0)
  1351. {
  1352. _textField.NewKeyDownEvent (
  1353. new (
  1354. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1355. )
  1356. );
  1357. switch (iteration)
  1358. {
  1359. case 0:
  1360. Assert.Equal (7, _textField.CursorPosition);
  1361. Assert.Equal (10, _textField.SelectedStart);
  1362. Assert.Equal (3, _textField.SelectedLength);
  1363. Assert.Equal ("jum", _textField.SelectedText);
  1364. break;
  1365. case 1:
  1366. Assert.Equal (4, _textField.CursorPosition);
  1367. Assert.Equal (10, _textField.SelectedStart);
  1368. Assert.Equal (6, _textField.SelectedLength);
  1369. Assert.Equal ("to jum", _textField.SelectedText);
  1370. break;
  1371. case 2:
  1372. Assert.Equal (0, _textField.CursorPosition);
  1373. Assert.Equal (10, _textField.SelectedStart);
  1374. Assert.Equal (10, _textField.SelectedLength);
  1375. Assert.Equal ("TAB to jum", _textField.SelectedText);
  1376. break;
  1377. }
  1378. iteration++;
  1379. }
  1380. }
  1381. [Fact]
  1382. public void WordBackward_WordForward_Mixed ()
  1383. {
  1384. var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
  1385. tf.BeginInit ();
  1386. tf.EndInit ();
  1387. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1388. Assert.Equal (15, tf.CursorPosition);
  1389. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1390. Assert.Equal (12, tf.CursorPosition);
  1391. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1392. Assert.Equal (10, tf.CursorPosition);
  1393. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1394. Assert.Equal (5, tf.CursorPosition);
  1395. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1396. Assert.Equal (0, tf.CursorPosition);
  1397. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1398. Assert.Equal (5, tf.CursorPosition);
  1399. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1400. Assert.Equal (10, tf.CursorPosition);
  1401. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1402. Assert.Equal (12, tf.CursorPosition);
  1403. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1404. Assert.Equal (15, tf.CursorPosition);
  1405. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1406. Assert.Equal (22, tf.CursorPosition);
  1407. }
  1408. [Fact]
  1409. public void WordBackward_WordForward_SelectedText_With_Accent ()
  1410. {
  1411. var text = "Les Misérables movie.";
  1412. var tf = new TextField { Width = 30, Text = text };
  1413. Assert.Equal (21, text.Length);
  1414. Assert.Equal (21, tf.Text.GetRuneCount ());
  1415. Assert.Equal (21, tf.Text.GetColumns ());
  1416. List<Rune> runes = tf.Text.ToRuneList ();
  1417. Assert.Equal (21, runes.Count);
  1418. Assert.Equal (21, tf.Text.Length);
  1419. for (var i = 0; i < runes.Count; i++)
  1420. {
  1421. char cs = text [i];
  1422. var cus = (char)runes [i].Value;
  1423. Assert.Equal (cs, cus);
  1424. }
  1425. var idx = 15;
  1426. Assert.Equal ('m', text [idx]);
  1427. Assert.Equal ('m', (char)runes [idx].Value);
  1428. Assert.Equal ("m", runes [idx].ToString ());
  1429. Assert.True (
  1430. tf.NewMouseEvent (
  1431. new () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1432. )
  1433. );
  1434. Assert.Equal ("movie.", tf.SelectedText);
  1435. Assert.True (
  1436. tf.NewMouseEvent (
  1437. new () { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1438. )
  1439. );
  1440. Assert.Equal ("movie.", tf.SelectedText);
  1441. }
  1442. [Fact]
  1443. [TextFieldTestsAutoInitShutdown]
  1444. public void WordForward_With_No_Selection ()
  1445. {
  1446. _textField.CursorPosition = 0;
  1447. var iteration = 0;
  1448. while (_textField.CursorPosition < _textField.Text.Length)
  1449. {
  1450. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1451. switch (iteration)
  1452. {
  1453. case 0:
  1454. Assert.Equal (4, _textField.CursorPosition);
  1455. Assert.Equal (-1, _textField.SelectedStart);
  1456. Assert.Equal (0, _textField.SelectedLength);
  1457. Assert.Null (_textField.SelectedText);
  1458. break;
  1459. case 1:
  1460. Assert.Equal (7, _textField.CursorPosition);
  1461. Assert.Equal (-1, _textField.SelectedStart);
  1462. Assert.Equal (0, _textField.SelectedLength);
  1463. Assert.Null (_textField.SelectedText);
  1464. break;
  1465. case 2:
  1466. Assert.Equal (12, _textField.CursorPosition);
  1467. Assert.Equal (-1, _textField.SelectedStart);
  1468. Assert.Equal (0, _textField.SelectedLength);
  1469. Assert.Null (_textField.SelectedText);
  1470. break;
  1471. case 3:
  1472. Assert.Equal (20, _textField.CursorPosition);
  1473. Assert.Equal (-1, _textField.SelectedStart);
  1474. Assert.Equal (0, _textField.SelectedLength);
  1475. Assert.Null (_textField.SelectedText);
  1476. break;
  1477. case 4:
  1478. Assert.Equal (25, _textField.CursorPosition);
  1479. Assert.Equal (-1, _textField.SelectedStart);
  1480. Assert.Equal (0, _textField.SelectedLength);
  1481. Assert.Null (_textField.SelectedText);
  1482. break;
  1483. case 5:
  1484. Assert.Equal (32, _textField.CursorPosition);
  1485. Assert.Equal (-1, _textField.SelectedStart);
  1486. Assert.Equal (0, _textField.SelectedLength);
  1487. Assert.Null (_textField.SelectedText);
  1488. break;
  1489. }
  1490. iteration++;
  1491. }
  1492. }
  1493. [Fact]
  1494. [TextFieldTestsAutoInitShutdown]
  1495. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1496. {
  1497. // 1 2 3 4 5
  1498. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1499. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1500. _textField.CursorPosition = 0;
  1501. var iteration = 0;
  1502. while (_textField.CursorPosition < _textField.Text.Length)
  1503. {
  1504. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1505. switch (iteration)
  1506. {
  1507. case 0:
  1508. Assert.Equal (6, _textField.CursorPosition);
  1509. Assert.Equal (-1, _textField.SelectedStart);
  1510. Assert.Equal (0, _textField.SelectedLength);
  1511. Assert.Null (_textField.SelectedText);
  1512. break;
  1513. case 1:
  1514. Assert.Equal (9, _textField.CursorPosition);
  1515. Assert.Equal (-1, _textField.SelectedStart);
  1516. Assert.Equal (0, _textField.SelectedLength);
  1517. Assert.Null (_textField.SelectedText);
  1518. break;
  1519. case 2:
  1520. Assert.Equal (12, _textField.CursorPosition);
  1521. Assert.Equal (-1, _textField.SelectedStart);
  1522. Assert.Equal (0, _textField.SelectedLength);
  1523. Assert.Null (_textField.SelectedText);
  1524. break;
  1525. case 3:
  1526. Assert.Equal (25, _textField.CursorPosition);
  1527. Assert.Equal (-1, _textField.SelectedStart);
  1528. Assert.Equal (0, _textField.SelectedLength);
  1529. Assert.Null (_textField.SelectedText);
  1530. break;
  1531. case 4:
  1532. Assert.Equal (28, _textField.CursorPosition);
  1533. Assert.Equal (-1, _textField.SelectedStart);
  1534. Assert.Equal (0, _textField.SelectedLength);
  1535. Assert.Null (_textField.SelectedText);
  1536. break;
  1537. case 5:
  1538. Assert.Equal (38, _textField.CursorPosition);
  1539. Assert.Equal (-1, _textField.SelectedStart);
  1540. Assert.Equal (0, _textField.SelectedLength);
  1541. Assert.Null (_textField.SelectedText);
  1542. break;
  1543. case 6:
  1544. Assert.Equal (40, _textField.CursorPosition);
  1545. Assert.Equal (-1, _textField.SelectedStart);
  1546. Assert.Equal (0, _textField.SelectedLength);
  1547. Assert.Null (_textField.SelectedText);
  1548. break;
  1549. case 7:
  1550. Assert.Equal (46, _textField.CursorPosition);
  1551. Assert.Equal (-1, _textField.SelectedStart);
  1552. Assert.Equal (0, _textField.SelectedLength);
  1553. Assert.Null (_textField.SelectedText);
  1554. break;
  1555. case 8:
  1556. Assert.Equal (48, _textField.CursorPosition);
  1557. Assert.Equal (-1, _textField.SelectedStart);
  1558. Assert.Equal (0, _textField.SelectedLength);
  1559. Assert.Null (_textField.SelectedText);
  1560. break;
  1561. case 9:
  1562. Assert.Equal (54, _textField.CursorPosition);
  1563. Assert.Equal (-1, _textField.SelectedStart);
  1564. Assert.Equal (0, _textField.SelectedLength);
  1565. Assert.Null (_textField.SelectedText);
  1566. break;
  1567. case 10:
  1568. Assert.Equal (55, _textField.CursorPosition);
  1569. Assert.Equal (-1, _textField.SelectedStart);
  1570. Assert.Equal (0, _textField.SelectedLength);
  1571. Assert.Null (_textField.SelectedText);
  1572. break;
  1573. }
  1574. iteration++;
  1575. }
  1576. }
  1577. [Fact]
  1578. [TextFieldTestsAutoInitShutdown]
  1579. public void WordForward_With_Selection ()
  1580. {
  1581. _textField.CursorPosition = 0;
  1582. _textField.SelectedStart = 0;
  1583. var iteration = 0;
  1584. while (_textField.CursorPosition < _textField.Text.Length)
  1585. {
  1586. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1587. switch (iteration)
  1588. {
  1589. case 0:
  1590. Assert.Equal (4, _textField.CursorPosition);
  1591. Assert.Equal (0, _textField.SelectedStart);
  1592. Assert.Equal (4, _textField.SelectedLength);
  1593. Assert.Equal ("TAB ", _textField.SelectedText);
  1594. break;
  1595. case 1:
  1596. Assert.Equal (7, _textField.CursorPosition);
  1597. Assert.Equal (0, _textField.SelectedStart);
  1598. Assert.Equal (7, _textField.SelectedLength);
  1599. Assert.Equal ("TAB to ", _textField.SelectedText);
  1600. break;
  1601. case 2:
  1602. Assert.Equal (12, _textField.CursorPosition);
  1603. Assert.Equal (0, _textField.SelectedStart);
  1604. Assert.Equal (12, _textField.SelectedLength);
  1605. Assert.Equal ("TAB to jump ", _textField.SelectedText);
  1606. break;
  1607. case 3:
  1608. Assert.Equal (20, _textField.CursorPosition);
  1609. Assert.Equal (0, _textField.SelectedStart);
  1610. Assert.Equal (20, _textField.SelectedLength);
  1611. Assert.Equal ("TAB to jump between ", _textField.SelectedText);
  1612. break;
  1613. case 4:
  1614. Assert.Equal (25, _textField.CursorPosition);
  1615. Assert.Equal (0, _textField.SelectedStart);
  1616. Assert.Equal (25, _textField.SelectedLength);
  1617. Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
  1618. break;
  1619. case 5:
  1620. Assert.Equal (32, _textField.CursorPosition);
  1621. Assert.Equal (0, _textField.SelectedStart);
  1622. Assert.Equal (32, _textField.SelectedLength);
  1623. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1624. break;
  1625. }
  1626. iteration++;
  1627. }
  1628. }
  1629. [Fact]
  1630. [TextFieldTestsAutoInitShutdown]
  1631. public void
  1632. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1633. {
  1634. _textField.CursorPosition = 10;
  1635. _textField.SelectedStart = 10;
  1636. var iteration = 0;
  1637. while (_textField.CursorPosition < _textField.Text.Length)
  1638. {
  1639. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1640. switch (iteration)
  1641. {
  1642. case 0:
  1643. Assert.Equal (12, _textField.CursorPosition);
  1644. Assert.Equal (10, _textField.SelectedStart);
  1645. Assert.Equal (2, _textField.SelectedLength);
  1646. Assert.Equal ("p ", _textField.SelectedText);
  1647. break;
  1648. case 1:
  1649. Assert.Equal (20, _textField.CursorPosition);
  1650. Assert.Equal (10, _textField.SelectedStart);
  1651. Assert.Equal (10, _textField.SelectedLength);
  1652. Assert.Equal ("p between ", _textField.SelectedText);
  1653. break;
  1654. case 2:
  1655. Assert.Equal (25, _textField.CursorPosition);
  1656. Assert.Equal (10, _textField.SelectedStart);
  1657. Assert.Equal (15, _textField.SelectedLength);
  1658. Assert.Equal ("p between text ", _textField.SelectedText);
  1659. break;
  1660. case 3:
  1661. Assert.Equal (32, _textField.CursorPosition);
  1662. Assert.Equal (10, _textField.SelectedStart);
  1663. Assert.Equal (22, _textField.SelectedLength);
  1664. Assert.Equal ("p between text fields.", _textField.SelectedText);
  1665. break;
  1666. }
  1667. iteration++;
  1668. }
  1669. }
  1670. [Fact]
  1671. [SetupFakeDriver]
  1672. public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
  1673. {
  1674. var tf = new TextField { Width = 30, Text = "Les Misérables" };
  1675. tf.SetRelativeLayout (new (100, 100));
  1676. tf.Draw ();
  1677. TestHelpers.AssertDriverContentsWithFrameAre (
  1678. @"
  1679. Les Misérables",
  1680. output
  1681. );
  1682. tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1683. tf.Draw ();
  1684. TestHelpers.AssertDriverContentsWithFrameAre (
  1685. @"
  1686. Les Misérables",
  1687. output
  1688. );
  1689. // incorrect order will result with a wrong accent place
  1690. tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
  1691. tf.Draw ();
  1692. TestHelpers.AssertDriverContentsWithFrameAre (
  1693. @"
  1694. Les Miśerables",
  1695. output
  1696. );
  1697. }
  1698. private TextField GetTextFieldsInView ()
  1699. {
  1700. var tf = new TextField { Width = 10 };
  1701. var tf2 = new TextField { Y = 1, Width = 10 };
  1702. Toplevel top = new ();
  1703. top.Add (tf);
  1704. top.Add (tf2);
  1705. Application.Begin (top);
  1706. Assert.Same (tf, top.Focused);
  1707. return tf;
  1708. }
  1709. // This class enables test functions annotated with the [InitShutdown] attribute
  1710. // to have a function called before the test function is called and after.
  1711. //
  1712. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1713. // as a pair, and b) all unit test functions should be atomic.
  1714. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1715. public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
  1716. {
  1717. public override void After (MethodInfo methodUnderTest)
  1718. {
  1719. _textField.Dispose ();
  1720. _textField = null;
  1721. base.After (methodUnderTest);
  1722. }
  1723. public override void Before (MethodInfo methodUnderTest)
  1724. {
  1725. base.Before (methodUnderTest);
  1726. //Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
  1727. _textField = new ()
  1728. {
  1729. ColorScheme = new (Colors.ColorSchemes ["Base"]),
  1730. // 1 2 3
  1731. // 01234567890123456789012345678901=32 (Length)
  1732. Text = "TAB to jump between text fields.",
  1733. Width = 32
  1734. };
  1735. }
  1736. }
  1737. [Fact]
  1738. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  1739. {
  1740. View superView = new ()
  1741. {
  1742. CanFocus = true
  1743. };
  1744. TextField t = new ();
  1745. superView.Add (t);
  1746. Assert.Single (superView.Subviews);
  1747. superView.BeginInit ();
  1748. superView.EndInit ();
  1749. Assert.Equal (2, superView.Subviews.Count);
  1750. }
  1751. [Fact]
  1752. public void Autocomplete__Added_To_SuperView_On_Add ()
  1753. {
  1754. View superView = new ()
  1755. {
  1756. CanFocus = true,
  1757. Id = "superView"
  1758. };
  1759. superView.BeginInit ();
  1760. superView.EndInit ();
  1761. Assert.Empty (superView.Subviews);
  1762. TextField t = new ()
  1763. {
  1764. Id = "t"
  1765. };
  1766. superView.Add (t);
  1767. Assert.Equal (2, superView.Subviews.Count);
  1768. }
  1769. [Fact]
  1770. public void Autocomplete_Visible_False_By_Default ()
  1771. {
  1772. View superView = new ()
  1773. {
  1774. CanFocus = true
  1775. };
  1776. TextField t = new ();
  1777. superView.Add (t);
  1778. superView.BeginInit ();
  1779. superView.EndInit ();
  1780. Assert.Equal (2, superView.Subviews.Count);
  1781. Assert.True (t.Visible);
  1782. Assert.False (t.Autocomplete.Visible);
  1783. }
  1784. [Fact]
  1785. [AutoInitShutdown]
  1786. public void Draw_Esc_Rune ()
  1787. {
  1788. var tf = new TextField { Width = 5, Text = "\u001b" };
  1789. tf.BeginInit ();
  1790. tf.EndInit ();
  1791. tf.Draw ();
  1792. TestHelpers.AssertDriverContentsWithFrameAre ("\u241b", output);
  1793. tf.Dispose ();
  1794. }
  1795. }