TextFieldTests.cs 74 KB

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