TextFieldTests.cs 74 KB

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