TextFieldTests.cs 70 KB

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