TextFieldTests.cs 69 KB

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