TextFieldTests.cs 74 KB

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