TextFieldTests.cs 69 KB

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