TextFieldTests.cs 67 KB

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