TextFieldTests.cs 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. using System.ComponentModel;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using System.Text;
  5. using CsvHelper;
  6. using Xunit.Abstractions;
  7. namespace Terminal.Gui.ViewsTests;
  8. public class TextFieldTests
  9. {
  10. private static TextField _textField;
  11. private readonly ITestOutputHelper _output;
  12. public TextFieldTests (ITestOutputHelper output) { _output = output; }
  13. [Fact]
  14. [AutoInitShutdown]
  15. public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
  16. {
  17. var tf = new TextField { Width = 3, Text = "ắ" };
  18. Toplevel top = Application.Top;
  19. top.Add (tf);
  20. Application.Begin (top);
  21. TestHelpers.AssertDriverContentsWithFrameAre (
  22. @"
  23. ắ",
  24. _output
  25. );
  26. tf.Text = "\u1eaf";
  27. Application.Refresh ();
  28. TestHelpers.AssertDriverContentsWithFrameAre (
  29. @"
  30. ắ",
  31. _output
  32. );
  33. tf.Text = "\u0103\u0301";
  34. Application.Refresh ();
  35. TestHelpers.AssertDriverContentsWithFrameAre (
  36. @"
  37. ắ",
  38. _output
  39. );
  40. tf.Text = "\u0061\u0306\u0301";
  41. Application.Refresh ();
  42. TestHelpers.AssertDriverContentsWithFrameAre (
  43. @"
  44. ắ",
  45. _output
  46. );
  47. }
  48. [Fact]
  49. [AutoInitShutdown]
  50. public void Adjust_First ()
  51. {
  52. var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
  53. Application.Top.Add (tf);
  54. Application.Begin (Application.Top);
  55. Assert.Equal ("This is a test. ", GetContents ());
  56. string GetContents ()
  57. {
  58. var item = "";
  59. for (var i = 0; i < 16; i++)
  60. {
  61. item += Application.Driver.Contents [0, i].Rune;
  62. }
  63. return item;
  64. }
  65. }
  66. [Fact]
  67. public void Cancel_TextChanging_ThenBackspace ()
  68. {
  69. var tf = new TextField ();
  70. tf.EnsureFocus ();
  71. tf.NewKeyDownEvent (Key.A.WithShift);
  72. Assert.Equal ("A", tf.Text);
  73. // cancel the next keystroke
  74. tf.TextChanging += (s, e) => e.Cancel = e.NewValue == "AB";
  75. tf.NewKeyDownEvent (Key.B.WithShift);
  76. // B was canceled so should just be A
  77. Assert.Equal ("A", tf.Text);
  78. // now delete the A
  79. tf.NewKeyDownEvent (Key.Backspace);
  80. Assert.Equal ("", tf.Text);
  81. }
  82. [Fact]
  83. [TextFieldTestsAutoInitShutdown]
  84. public void CanFocus_False_Wont_Focus_With_Mouse ()
  85. {
  86. Toplevel top = Application.Top;
  87. var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  88. var fv = new FrameView
  89. {
  90. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  91. };
  92. fv.Add (tf);
  93. top.Add (fv);
  94. Application.Begin (top);
  95. Assert.False (tf.CanFocus);
  96. Assert.False (tf.HasFocus);
  97. Assert.False (fv.CanFocus);
  98. Assert.False (fv.HasFocus);
  99. tf.OnMouseEvent (
  100. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  101. );
  102. Assert.Null (tf.SelectedText);
  103. Assert.False (tf.CanFocus);
  104. Assert.False (tf.HasFocus);
  105. Assert.False (fv.CanFocus);
  106. Assert.False (fv.HasFocus);
  107. Assert.Throws<InvalidOperationException> (() => tf.CanFocus = true);
  108. fv.CanFocus = true;
  109. tf.CanFocus = true;
  110. tf.OnMouseEvent (
  111. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  112. );
  113. Assert.Equal ("some ", tf.SelectedText);
  114. Assert.True (tf.CanFocus);
  115. Assert.True (tf.HasFocus);
  116. Assert.True (fv.CanFocus);
  117. Assert.True (fv.HasFocus);
  118. fv.CanFocus = false;
  119. tf.OnMouseEvent (
  120. new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked }
  121. );
  122. Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
  123. Assert.False (tf.CanFocus);
  124. Assert.False (tf.HasFocus);
  125. Assert.False (fv.CanFocus);
  126. Assert.False (fv.HasFocus);
  127. }
  128. [Theory]
  129. [AutoInitShutdown]
  130. [InlineData ("0123456789", "0123456789")]
  131. [InlineData ("01234567890", "0123456789")]
  132. public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
  133. {
  134. TextField tf = GetTextFieldsInView ();
  135. // Caption has no effect when focused
  136. tf.Caption = caption;
  137. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  138. Assert.False (tf.HasFocus);
  139. tf.Draw ();
  140. TestHelpers.AssertDriverContentsAre (expectedRender, _output);
  141. }
  142. [Fact]
  143. [AutoInitShutdown]
  144. public void CaptionedTextField_DoesNotOverspillBounds_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. TestHelpers.AssertDriverContentsAre ("Misérables", _output);
  155. }
  156. [Theory]
  157. [AutoInitShutdown]
  158. [InlineData ("blah")]
  159. [InlineData (" ")]
  160. public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
  161. {
  162. TextField tf = GetTextFieldsInView ();
  163. tf.Draw ();
  164. TestHelpers.AssertDriverContentsAre ("", _output);
  165. tf.Caption = "Enter txt";
  166. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  167. // Caption should appear when not focused and no text
  168. Assert.False (tf.HasFocus);
  169. tf.Draw ();
  170. TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
  171. // but disapear when text is added
  172. tf.Text = content;
  173. tf.Draw ();
  174. TestHelpers.AssertDriverContentsAre (content, _output);
  175. }
  176. [Fact]
  177. [AutoInitShutdown]
  178. public void CaptionedTextField_RendersCaption_WhenNotFocused ()
  179. {
  180. TextField tf = GetTextFieldsInView ();
  181. tf.Draw ();
  182. TestHelpers.AssertDriverContentsAre ("", _output);
  183. // Caption has no effect when focused
  184. tf.Caption = "Enter txt";
  185. Assert.True (tf.HasFocus);
  186. tf.Draw ();
  187. TestHelpers.AssertDriverContentsAre ("", _output);
  188. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  189. Assert.False (tf.HasFocus);
  190. tf.Draw ();
  191. TestHelpers.AssertDriverContentsAre ("Enter txt", _output);
  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. Application.Top.Add (tf);
  365. Application.Begin (Application.Top);
  366. Assert.Equal ("-1", tf.Text);
  367. // InsertText
  368. tf.SelectedStart = 1;
  369. tf.CursorPosition = 2;
  370. Assert.Equal (1, tf.SelectedLength);
  371. Assert.Equal ("1", tf.SelectedText);
  372. Assert.True (tf.NewKeyDownEvent (Key.D2));
  373. Assert.Equal ("-2", newText);
  374. Assert.Equal ("-1", oldText);
  375. Assert.Equal ("-2", tf.Text);
  376. // DeleteCharLeft
  377. tf.SelectedStart = 1;
  378. tf.CursorPosition = 2;
  379. Assert.Equal (1, tf.SelectedLength);
  380. Assert.Equal ("2", tf.SelectedText);
  381. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  382. Assert.Equal ("-", newText);
  383. Assert.Equal ("-2", oldText);
  384. Assert.Equal ("-", tf.Text);
  385. // DeleteCharRight
  386. tf.Text = "-1";
  387. tf.SelectedStart = 1;
  388. tf.CursorPosition = 2;
  389. Assert.Equal (1, tf.SelectedLength);
  390. Assert.Equal ("1", tf.SelectedText);
  391. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  392. Assert.Equal ("-", newText);
  393. Assert.Equal ("-1", oldText);
  394. Assert.Equal ("-", tf.Text);
  395. // Cut
  396. tf.Text = "-1";
  397. tf.SelectedStart = 1;
  398. tf.CursorPosition = 2;
  399. Assert.Equal (1, tf.SelectedLength);
  400. Assert.Equal ("1", tf.SelectedText);
  401. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  402. Assert.Equal ("-", newText);
  403. Assert.Equal ("-1", oldText);
  404. Assert.Equal ("-", tf.Text);
  405. // Delete word with accented char
  406. tf.Text = "Les Misérables movie.";
  407. Assert.True (
  408. tf.OnMouseEvent (
  409. new MouseEvent { X = 7, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  410. )
  411. );
  412. Assert.Equal ("Misérables ", tf.SelectedText);
  413. Assert.Equal (11, tf.SelectedLength);
  414. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  415. Assert.Equal ("Les movie.", newText);
  416. Assert.Equal ("Les Misérables movie.", oldText);
  417. Assert.Equal ("Les movie.", tf.Text);
  418. }
  419. [Fact]
  420. public void HistoryText_IsDirty_ClearHistoryChanges ()
  421. {
  422. var text = "Testing";
  423. var tf = new TextField { Text = text };
  424. tf.BeginInit ();
  425. tf.EndInit ();
  426. Assert.Equal (text, tf.Text);
  427. tf.ClearHistoryChanges ();
  428. Assert.False (tf.IsDirty);
  429. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift));
  430. Assert.Equal ($"{text}A", tf.Text);
  431. Assert.True (tf.IsDirty);
  432. }
  433. [Fact]
  434. [AutoInitShutdown (useFakeClipboard: true)]
  435. public void KeyBindings_Command ()
  436. {
  437. var tf = new TextField { Width = 20, Text = "This is a test." };
  438. tf.BeginInit ();
  439. tf.EndInit ();
  440. Assert.Equal (15, tf.Text.Length);
  441. Assert.Equal (15, tf.CursorPosition);
  442. Assert.False (tf.ReadOnly);
  443. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  444. Assert.Equal ("This is a test.", tf.Text);
  445. tf.CursorPosition = 0;
  446. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  447. Assert.Equal ("his is a test.", tf.Text);
  448. tf.ReadOnly = true;
  449. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
  450. Assert.Equal ("his is a test.", tf.Text);
  451. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  452. Assert.Equal ("his is a test.", tf.Text);
  453. tf.ReadOnly = false;
  454. tf.CursorPosition = 1;
  455. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  456. Assert.Equal ("is is a test.", tf.Text);
  457. tf.CursorPosition = 5;
  458. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift));
  459. Assert.Equal ("is is a test.", tf.Text);
  460. Assert.Equal ("is is", tf.SelectedText);
  461. tf.CursorPosition = 5;
  462. tf.SelectedStart = -1;
  463. Assert.Null (tf.SelectedText);
  464. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
  465. Assert.Equal ("is is a test.", tf.Text);
  466. Assert.Equal ("is is", tf.SelectedText);
  467. tf.CursorPosition = 5;
  468. tf.SelectedStart = -1;
  469. Assert.Null (tf.SelectedText);
  470. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift.WithCtrl));
  471. Assert.Equal ("is is a test.", tf.Text);
  472. Assert.Equal ("is is", tf.SelectedText);
  473. tf.CursorPosition = 5;
  474. tf.SelectedStart = -1;
  475. Assert.Null (tf.SelectedText);
  476. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift));
  477. Assert.Equal ("is is a test.", tf.Text);
  478. Assert.Equal (" a test.", tf.SelectedText);
  479. tf.CursorPosition = 5;
  480. tf.SelectedStart = -1;
  481. Assert.Null (tf.SelectedText);
  482. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
  483. Assert.Equal ("is is a test.", tf.Text);
  484. Assert.Equal (" a test.", tf.SelectedText);
  485. tf.CursorPosition = 5;
  486. tf.SelectedStart = -1;
  487. Assert.Null (tf.SelectedText);
  488. Assert.True (tf.NewKeyDownEvent (Key.E.WithShift.WithCtrl));
  489. Assert.Equal ("is is a test.", tf.Text);
  490. Assert.Equal (" a test.", tf.SelectedText);
  491. tf.CursorPosition = 5;
  492. tf.SelectedStart = -1;
  493. Assert.Null (tf.SelectedText);
  494. Assert.True (tf.NewKeyDownEvent (Key.Home));
  495. Assert.Equal ("is is a test.", tf.Text);
  496. Assert.Equal (0, tf.CursorPosition);
  497. tf.CursorPosition = 5;
  498. Assert.Null (tf.SelectedText);
  499. Assert.True (tf.NewKeyDownEvent (Key.Home.WithCtrl));
  500. Assert.Equal ("is is a test.", tf.Text);
  501. Assert.Equal (0, tf.CursorPosition);
  502. tf.CursorPosition = 5;
  503. Assert.Null (tf.SelectedText);
  504. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  505. Assert.Equal ("is is a test.", tf.Text);
  506. Assert.Equal (0, tf.CursorPosition);
  507. tf.CursorPosition = 5;
  508. tf.SelectedStart = -1;
  509. Assert.Null (tf.SelectedText);
  510. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  511. Assert.Equal ("is is a test.", tf.Text);
  512. Assert.Equal ("s", tf.SelectedText);
  513. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift));
  514. Assert.Equal ("is is a test.", tf.Text);
  515. Assert.Equal ("is", tf.SelectedText);
  516. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  517. Assert.Equal ("is is a test.", tf.Text);
  518. Assert.Equal ("s", tf.SelectedText);
  519. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift));
  520. Assert.Equal ("is is a test.", tf.Text);
  521. Assert.Null (tf.SelectedText);
  522. tf.CursorPosition = 7;
  523. tf.SelectedStart = -1;
  524. Assert.Null (tf.SelectedText);
  525. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
  526. Assert.Equal ("is is a test.", tf.Text);
  527. Assert.Equal ("a", tf.SelectedText);
  528. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  529. Assert.Equal ("is is a test.", tf.Text);
  530. Assert.Equal ("is a", tf.SelectedText);
  531. #if UNIX_KEY_BINDINGS
  532. Assert.True (tf.NewKeyDownEvent (Key.B.WithShift.WithAlt));
  533. #else
  534. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  535. #endif
  536. Assert.Equal ("is is a test.", tf.Text);
  537. Assert.Equal ("is is a", tf.SelectedText);
  538. tf.CursorPosition = 3;
  539. tf.SelectedStart = -1;
  540. Assert.Null (tf.SelectedText);
  541. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  542. Assert.Equal ("is is a test.", tf.Text);
  543. Assert.Equal ("is ", tf.SelectedText);
  544. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift.WithCtrl));
  545. Assert.Equal ("is is a test.", tf.Text);
  546. Assert.Equal ("is a ", tf.SelectedText);
  547. #if UNIX_KEY_BINDINGS
  548. Assert.True (tf.NewKeyDownEvent (Key.F.WithShift.WithAlt));
  549. #else
  550. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  551. #endif
  552. Assert.Equal ("is is a test.", tf.Text);
  553. Assert.Equal ("is a test.", tf.SelectedText);
  554. Assert.Equal (13, tf.CursorPosition);
  555. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  556. Assert.Equal ("is is a test.", tf.Text);
  557. Assert.Null (tf.SelectedText);
  558. Assert.Equal (12, tf.CursorPosition);
  559. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  560. Assert.Equal ("is is a test.", tf.Text);
  561. Assert.Equal (11, tf.CursorPosition);
  562. Assert.True (tf.NewKeyDownEvent (Key.End));
  563. Assert.Equal ("is is a test.", tf.Text);
  564. Assert.Equal (13, tf.CursorPosition);
  565. tf.CursorPosition = 0;
  566. Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
  567. Assert.Equal ("is is a test.", tf.Text);
  568. Assert.Equal (13, tf.CursorPosition);
  569. tf.CursorPosition = 0;
  570. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  571. Assert.Equal ("is is a test.", tf.Text);
  572. Assert.Equal (13, tf.CursorPosition);
  573. tf.CursorPosition = 0;
  574. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  575. Assert.Equal ("is is a test.", tf.Text);
  576. Assert.Equal (1, tf.CursorPosition);
  577. Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
  578. Assert.Equal ("is is a test.", tf.Text);
  579. Assert.Equal (2, tf.CursorPosition);
  580. tf.CursorPosition = 9;
  581. tf.ReadOnly = true;
  582. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  583. Assert.Equal ("is is a test.", tf.Text);
  584. tf.ReadOnly = false;
  585. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  586. Assert.Equal ("is is a t", tf.Text);
  587. Assert.Equal ("est.", Clipboard.Contents);
  588. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  589. Assert.Equal ("is is a test.", tf.Text);
  590. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  591. Assert.Equal ("is is a t", tf.Text);
  592. #if UNIX_KEY_BINDINGS
  593. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
  594. #else
  595. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  596. #endif
  597. Assert.Equal ("is is a test.", tf.Text);
  598. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  599. Assert.Equal ("is is a t", tf.Text);
  600. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  601. Assert.Equal ("is is a t", tf.Text);
  602. Assert.Equal (8, tf.CursorPosition);
  603. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  604. Assert.Equal ("is is a t", tf.Text);
  605. Assert.Equal (6, tf.CursorPosition);
  606. #if UNIX_KEY_BINDINGS
  607. Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
  608. #else
  609. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  610. #endif
  611. Assert.Equal ("is is a t", tf.Text);
  612. Assert.Equal (3, tf.CursorPosition);
  613. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  614. Assert.Equal ("is is a t", tf.Text);
  615. Assert.Equal (6, tf.CursorPosition);
  616. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  617. Assert.Equal ("is is a t", tf.Text);
  618. Assert.Equal (8, tf.CursorPosition);
  619. #if UNIX_KEY_BINDINGS
  620. Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
  621. #else
  622. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  623. #endif
  624. Assert.Equal ("is is a t", tf.Text);
  625. Assert.Equal (9, tf.CursorPosition);
  626. Assert.True (tf.Used);
  627. Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
  628. Assert.Equal ("is is a t", tf.Text);
  629. Assert.Equal (9, tf.CursorPosition);
  630. Assert.False (tf.Used);
  631. tf.SelectedStart = 3;
  632. tf.CursorPosition = 7;
  633. Assert.Equal ("is a", tf.SelectedText);
  634. Assert.Equal ("est.", Clipboard.Contents);
  635. Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
  636. Assert.Equal ("is is a t", tf.Text);
  637. Assert.Equal ("is a", Clipboard.Contents);
  638. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  639. Assert.Equal ("is t", tf.Text);
  640. Assert.Equal ("is a", Clipboard.Contents);
  641. Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
  642. Assert.Equal ("is is a t", tf.Text);
  643. Assert.Equal ("is a", Clipboard.Contents);
  644. Assert.Equal (7, tf.CursorPosition);
  645. #if UNIX_KEY_BINDINGS
  646. Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
  647. #else
  648. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
  649. #endif
  650. Assert.Equal (" t", tf.Text);
  651. Assert.Equal ("is is a", Clipboard.Contents);
  652. tf.Text = "TAB to jump between text fields.";
  653. Assert.Equal (0, tf.CursorPosition);
  654. Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
  655. Assert.Equal ("to jump between text fields.", tf.Text);
  656. tf.CursorPosition = tf.Text.Length;
  657. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
  658. Assert.Equal ("to jump between text fields", tf.Text);
  659. Assert.True (tf.NewKeyDownEvent (Key.T.WithCtrl));
  660. Assert.Equal ("to jump between text fields", tf.SelectedText);
  661. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
  662. Assert.Equal ("", tf.Text);
  663. }
  664. [Fact]
  665. public void HotKey_Command_SetsFocus ()
  666. {
  667. var view = new TextField ();
  668. view.CanFocus = true;
  669. Assert.False (view.HasFocus);
  670. view.InvokeCommand (Command.HotKey);
  671. Assert.True (view.HasFocus);
  672. }
  673. [Fact]
  674. public void HotKey_Command_Does_Not_Accept ()
  675. {
  676. var view = new TextField ();
  677. var accepted = false;
  678. view.Accept += OnAccept;
  679. view.InvokeCommand (Command.HotKey);
  680. Assert.False (accepted);
  681. return;
  682. void OnAccept (object sender, CancelEventArgs e) { accepted = true; }
  683. }
  684. [Fact]
  685. public void Accept_Command_Fires_Accept ()
  686. {
  687. var view = new TextField ();
  688. var accepted = false;
  689. view.Accept += Accept;
  690. view.InvokeCommand (Command.Accept);
  691. Assert.True (accepted);
  692. return;
  693. void Accept (object sender, CancelEventArgs e) { accepted = true; }
  694. }
  695. [Fact]
  696. public void Accept_Cancel_Event_HandlesCommand ()
  697. {
  698. //var super = new View ();
  699. var view = new TextField ();
  700. //super.Add (view);
  701. //var superAcceptedInvoked = false;
  702. var tfAcceptedInvoked = false;
  703. var cancel = false;
  704. view.Accept += TextViewAccept;
  705. Assert.True (view.InvokeCommand (Command.Accept));
  706. Assert.True (tfAcceptedInvoked);
  707. tfAcceptedInvoked = false;
  708. cancel = true;
  709. view.Accept += TextViewAccept;
  710. Assert.False (view.InvokeCommand (Command.Accept));
  711. Assert.True (tfAcceptedInvoked);
  712. return;
  713. void TextViewAccept (object sender, CancelEventArgs e)
  714. {
  715. tfAcceptedInvoked = true;
  716. e.Cancel = cancel;
  717. }
  718. }
  719. [Fact]
  720. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  721. {
  722. var top = new Toplevel ();
  723. var tf = new TextField { Width = 10 };
  724. top.Add (tf);
  725. Exception exception = Record.Exception (tf.SetFocus);
  726. Assert.Null (exception);
  727. }
  728. [Fact]
  729. [TextFieldTestsAutoInitShutdown]
  730. public void Paste_Always_Clear_The_SelectedText ()
  731. {
  732. _textField.SelectedStart = 20;
  733. _textField.CursorPosition = 24;
  734. _textField.Copy ();
  735. Assert.Equal ("text", _textField.SelectedText);
  736. _textField.Paste ();
  737. Assert.Null (_textField.SelectedText);
  738. }
  739. [Fact]
  740. public void Backspace_From_End ()
  741. {
  742. var tf = new TextField { Text = "ABC" };
  743. tf.EnsureFocus ();
  744. Assert.Equal ("ABC", tf.Text);
  745. tf.BeginInit ();
  746. tf.EndInit ();
  747. Assert.Equal (3, tf.CursorPosition);
  748. // now delete the C
  749. tf.NewKeyDownEvent (Key.Backspace);
  750. Assert.Equal ("AB", tf.Text);
  751. Assert.Equal (2, tf.CursorPosition);
  752. // then delete the B
  753. tf.NewKeyDownEvent (Key.Backspace);
  754. Assert.Equal ("A", tf.Text);
  755. Assert.Equal (1, tf.CursorPosition);
  756. // then delete the A
  757. tf.NewKeyDownEvent (Key.Backspace);
  758. Assert.Equal ("", tf.Text);
  759. Assert.Equal (0, tf.CursorPosition);
  760. }
  761. [Fact]
  762. public void Backspace_From_Middle ()
  763. {
  764. var tf = new TextField { Text = "ABC" };
  765. tf.EnsureFocus ();
  766. tf.CursorPosition = 2;
  767. Assert.Equal ("ABC", tf.Text);
  768. // now delete the B
  769. tf.NewKeyDownEvent (Key.Backspace);
  770. Assert.Equal ("AC", tf.Text);
  771. // then delete the A
  772. tf.NewKeyDownEvent (Key.Backspace);
  773. Assert.Equal ("C", tf.Text);
  774. // then delete nothing
  775. tf.NewKeyDownEvent (Key.Backspace);
  776. Assert.Equal ("C", tf.Text);
  777. // now delete the C
  778. tf.CursorPosition = 1;
  779. tf.NewKeyDownEvent (Key.Backspace);
  780. Assert.Equal ("", tf.Text);
  781. }
  782. [Fact]
  783. [AutoInitShutdown]
  784. public void ScrollOffset_Initialize ()
  785. {
  786. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  787. tf.BeginInit ();
  788. tf.EndInit ();
  789. Assert.Equal (0, tf.ScrollOffset);
  790. Assert.Equal (16, tf.CursorPosition);
  791. Application.Top.Add (tf);
  792. Application.Begin (Application.Top);
  793. Assert.Equal (0, tf.ScrollOffset);
  794. Assert.Equal (16, tf.CursorPosition);
  795. }
  796. [Fact]
  797. [TextFieldTestsAutoInitShutdown]
  798. public void Selected_Text_Shows ()
  799. {
  800. // Proves #3022 is fixed (TextField selected text does not show in v2)
  801. _textField.CursorPosition = 0;
  802. Application.Top.Add (_textField);
  803. RunState rs = Application.Begin (Application.Top);
  804. Attribute [] attributes =
  805. {
  806. _textField.ColorScheme.Focus,
  807. new (
  808. _textField.ColorScheme.Focus.Background,
  809. _textField.ColorScheme.Focus.Foreground
  810. )
  811. };
  812. // TAB to jump between text fields.
  813. TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
  814. // Cursor is at the end
  815. Assert.Equal (32, _textField.CursorPosition);
  816. _textField.CursorPosition = 0;
  817. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  818. var first = true;
  819. Application.RunIteration (ref rs, ref first);
  820. Assert.Equal (4, _textField.CursorPosition);
  821. // TAB to jump between text fields.
  822. TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
  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. Application.Top.Add (tf);
  896. Application.Begin (Application.Top);
  897. var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
  898. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  899. Assert.Equal (1, clickCounter);
  900. // Get a fresh instance that represents a right click.
  901. // Should be ignored because of SuppressRightClick callback
  902. mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
  903. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  904. Assert.Equal (1, clickCounter);
  905. Application.MouseEvent -= HandleRightClick;
  906. // Get a fresh instance that represents a right click.
  907. // Should no longer be ignored as the callback was removed
  908. mouseEvent = new MouseEvent { Flags = MouseFlags.Button3Clicked, View = tf };
  909. // In #3183 OnMouseClicked is no longer called before MouseEvent().
  910. // This call causes the context menu to pop, and MouseEvent() returns true.
  911. // Thus, the clickCounter is NOT incremented.
  912. // Which is correct, because the user did NOT click with the left mouse button.
  913. Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
  914. Assert.Equal (1, clickCounter);
  915. return;
  916. void HandleRightClick (object sender, MouseEventEventArgs arg)
  917. {
  918. if (arg.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
  919. {
  920. arg.Handled = true;
  921. }
  922. }
  923. }
  924. [InlineData ("a")] // Lower than selection
  925. [InlineData ("aaaaaaaaaaa")] // Greater than selection
  926. [InlineData ("aaaa")] // Equal than selection
  927. [Theory]
  928. public void SetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
  929. {
  930. var tf = new TextField ();
  931. tf.Text = "fish";
  932. tf.CursorPosition = tf.Text.Length;
  933. tf.NewKeyDownEvent (Key.CursorLeft);
  934. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  935. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  936. Assert.Equal (1, tf.CursorPosition);
  937. Assert.Equal (2, tf.SelectedLength);
  938. Assert.Equal ("is", tf.SelectedText);
  939. tf.Text = newText;
  940. tf.CursorPosition = tf.Text.Length;
  941. Assert.Equal (newText.Length, tf.CursorPosition);
  942. Assert.Equal (0, tf.SelectedLength);
  943. Assert.Null (tf.SelectedText);
  944. }
  945. [Fact]
  946. [TextFieldTestsAutoInitShutdown]
  947. public void Text_Replaces_Tabs_With_Empty_String ()
  948. {
  949. _textField.Text = "\t\tTAB to jump between text fields.";
  950. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  951. _textField.Text = "";
  952. Clipboard.Contents = "\t\tTAB to jump between text fields.";
  953. _textField.Paste ();
  954. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  955. }
  956. [Fact]
  957. [TextFieldTestsAutoInitShutdown]
  958. public void TextChanged_Event ()
  959. {
  960. _textField.TextChanged += (s, e) => { Assert.Equal ("TAB to jump between text fields.", e.OldValue); };
  961. _textField.Text = "changed";
  962. Assert.Equal ("changed", _textField.Text);
  963. }
  964. [Fact]
  965. [TextFieldTestsAutoInitShutdown]
  966. public void TextChanging_Event ()
  967. {
  968. var cancel = true;
  969. _textField.TextChanging += (s, e) =>
  970. {
  971. Assert.Equal ("changing", e.NewValue);
  972. if (cancel)
  973. {
  974. e.Cancel = true;
  975. }
  976. };
  977. _textField.Text = "changing";
  978. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  979. cancel = false;
  980. _textField.Text = "changing";
  981. Assert.Equal ("changing", _textField.Text);
  982. }
  983. [Fact]
  984. [TextFieldTestsAutoInitShutdown]
  985. public void SpaceHandling ()
  986. {
  987. var tf = new TextField { Width = 10, Text = " " };
  988. var ev = new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
  989. tf.OnMouseEvent (ev);
  990. Assert.Equal (1, tf.SelectedLength);
  991. ev = new MouseEvent { X = 1, Y = 0, Flags = MouseFlags.Button1DoubleClicked };
  992. tf.OnMouseEvent (ev);
  993. Assert.Equal (1, tf.SelectedLength);
  994. }
  995. [Fact]
  996. [TextFieldTestsAutoInitShutdown]
  997. public void Used_Is_False ()
  998. {
  999. _textField.Used = false;
  1000. _textField.CursorPosition = 10;
  1001. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1002. _textField.NewKeyDownEvent (Key.U); // u
  1003. Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
  1004. _textField.NewKeyDownEvent (Key.S); // s
  1005. Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
  1006. _textField.NewKeyDownEvent (Key.E); // e
  1007. Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
  1008. _textField.NewKeyDownEvent (Key.D); // d
  1009. Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
  1010. }
  1011. [Fact]
  1012. [TextFieldTestsAutoInitShutdown]
  1013. public void Used_Is_True_By_Default ()
  1014. {
  1015. _textField.CursorPosition = 10;
  1016. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1017. _textField.NewKeyDownEvent (Key.U); // u
  1018. Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
  1019. _textField.NewKeyDownEvent (Key.S); // s
  1020. Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
  1021. _textField.NewKeyDownEvent (Key.E); // e
  1022. Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
  1023. _textField.NewKeyDownEvent (Key.D); // d
  1024. Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
  1025. }
  1026. [Fact]
  1027. [TextFieldTestsAutoInitShutdown]
  1028. public void WordBackward_With_No_Selection ()
  1029. {
  1030. _textField.CursorPosition = _textField.Text.Length;
  1031. var iteration = 0;
  1032. while (_textField.CursorPosition > 0)
  1033. {
  1034. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1035. switch (iteration)
  1036. {
  1037. case 0:
  1038. Assert.Equal (31, _textField.CursorPosition);
  1039. Assert.Equal (-1, _textField.SelectedStart);
  1040. Assert.Equal (0, _textField.SelectedLength);
  1041. Assert.Null (_textField.SelectedText);
  1042. break;
  1043. case 1:
  1044. Assert.Equal (25, _textField.CursorPosition);
  1045. Assert.Equal (-1, _textField.SelectedStart);
  1046. Assert.Equal (0, _textField.SelectedLength);
  1047. Assert.Null (_textField.SelectedText);
  1048. break;
  1049. case 2:
  1050. Assert.Equal (20, _textField.CursorPosition);
  1051. Assert.Equal (-1, _textField.SelectedStart);
  1052. Assert.Equal (0, _textField.SelectedLength);
  1053. Assert.Null (_textField.SelectedText);
  1054. break;
  1055. case 3:
  1056. Assert.Equal (12, _textField.CursorPosition);
  1057. Assert.Equal (-1, _textField.SelectedStart);
  1058. Assert.Equal (0, _textField.SelectedLength);
  1059. Assert.Null (_textField.SelectedText);
  1060. break;
  1061. case 4:
  1062. Assert.Equal (7, _textField.CursorPosition);
  1063. Assert.Equal (-1, _textField.SelectedStart);
  1064. Assert.Equal (0, _textField.SelectedLength);
  1065. Assert.Null (_textField.SelectedText);
  1066. break;
  1067. case 5:
  1068. Assert.Equal (4, _textField.CursorPosition);
  1069. Assert.Equal (-1, _textField.SelectedStart);
  1070. Assert.Equal (0, _textField.SelectedLength);
  1071. Assert.Null (_textField.SelectedText);
  1072. break;
  1073. case 6:
  1074. Assert.Equal (0, _textField.CursorPosition);
  1075. Assert.Equal (-1, _textField.SelectedStart);
  1076. Assert.Equal (0, _textField.SelectedLength);
  1077. Assert.Null (_textField.SelectedText);
  1078. break;
  1079. }
  1080. iteration++;
  1081. }
  1082. }
  1083. [Fact]
  1084. [TextFieldTestsAutoInitShutdown]
  1085. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1086. {
  1087. // 1 2 3 4 5
  1088. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1089. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1090. _textField.CursorPosition = _textField.Text.Length;
  1091. var iteration = 0;
  1092. while (_textField.CursorPosition > 0)
  1093. {
  1094. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1095. switch (iteration)
  1096. {
  1097. case 0:
  1098. Assert.Equal (54, _textField.CursorPosition);
  1099. Assert.Equal (-1, _textField.SelectedStart);
  1100. Assert.Equal (0, _textField.SelectedLength);
  1101. Assert.Null (_textField.SelectedText);
  1102. break;
  1103. case 1:
  1104. Assert.Equal (48, _textField.CursorPosition);
  1105. Assert.Equal (-1, _textField.SelectedStart);
  1106. Assert.Equal (0, _textField.SelectedLength);
  1107. Assert.Null (_textField.SelectedText);
  1108. break;
  1109. case 2:
  1110. Assert.Equal (46, _textField.CursorPosition);
  1111. Assert.Equal (-1, _textField.SelectedStart);
  1112. Assert.Equal (0, _textField.SelectedLength);
  1113. Assert.Null (_textField.SelectedText);
  1114. break;
  1115. case 3:
  1116. Assert.Equal (40, _textField.CursorPosition);
  1117. Assert.Equal (-1, _textField.SelectedStart);
  1118. Assert.Equal (0, _textField.SelectedLength);
  1119. Assert.Null (_textField.SelectedText);
  1120. break;
  1121. case 4:
  1122. Assert.Equal (38, _textField.CursorPosition);
  1123. Assert.Equal (-1, _textField.SelectedStart);
  1124. Assert.Equal (0, _textField.SelectedLength);
  1125. Assert.Null (_textField.SelectedText);
  1126. break;
  1127. case 5:
  1128. Assert.Equal (28, _textField.CursorPosition);
  1129. Assert.Equal (-1, _textField.SelectedStart);
  1130. Assert.Equal (0, _textField.SelectedLength);
  1131. Assert.Null (_textField.SelectedText);
  1132. break;
  1133. case 6:
  1134. Assert.Equal (25, _textField.CursorPosition);
  1135. Assert.Equal (-1, _textField.SelectedStart);
  1136. Assert.Equal (0, _textField.SelectedLength);
  1137. Assert.Null (_textField.SelectedText);
  1138. break;
  1139. case 7:
  1140. Assert.Equal (12, _textField.CursorPosition);
  1141. Assert.Equal (-1, _textField.SelectedStart);
  1142. Assert.Equal (0, _textField.SelectedLength);
  1143. Assert.Null (_textField.SelectedText);
  1144. break;
  1145. case 8:
  1146. Assert.Equal (9, _textField.CursorPosition);
  1147. Assert.Equal (-1, _textField.SelectedStart);
  1148. Assert.Equal (0, _textField.SelectedLength);
  1149. Assert.Null (_textField.SelectedText);
  1150. break;
  1151. case 9:
  1152. Assert.Equal (6, _textField.CursorPosition);
  1153. Assert.Equal (-1, _textField.SelectedStart);
  1154. Assert.Equal (0, _textField.SelectedLength);
  1155. Assert.Null (_textField.SelectedText);
  1156. break;
  1157. case 10:
  1158. Assert.Equal (0, _textField.CursorPosition);
  1159. Assert.Equal (-1, _textField.SelectedStart);
  1160. Assert.Equal (0, _textField.SelectedLength);
  1161. Assert.Null (_textField.SelectedText);
  1162. break;
  1163. }
  1164. iteration++;
  1165. }
  1166. }
  1167. [Fact]
  1168. [TextFieldTestsAutoInitShutdown]
  1169. public void WordBackward_With_Selection ()
  1170. {
  1171. _textField.CursorPosition = _textField.Text.Length;
  1172. _textField.SelectedStart = _textField.Text.Length;
  1173. var iteration = 0;
  1174. while (_textField.CursorPosition > 0)
  1175. {
  1176. _textField.NewKeyDownEvent (
  1177. new Key (
  1178. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1179. )
  1180. );
  1181. switch (iteration)
  1182. {
  1183. case 0:
  1184. Assert.Equal (31, _textField.CursorPosition);
  1185. Assert.Equal (32, _textField.SelectedStart);
  1186. Assert.Equal (1, _textField.SelectedLength);
  1187. Assert.Equal (".", _textField.SelectedText);
  1188. break;
  1189. case 1:
  1190. Assert.Equal (25, _textField.CursorPosition);
  1191. Assert.Equal (32, _textField.SelectedStart);
  1192. Assert.Equal (7, _textField.SelectedLength);
  1193. Assert.Equal ("fields.", _textField.SelectedText);
  1194. break;
  1195. case 2:
  1196. Assert.Equal (20, _textField.CursorPosition);
  1197. Assert.Equal (32, _textField.SelectedStart);
  1198. Assert.Equal (12, _textField.SelectedLength);
  1199. Assert.Equal ("text fields.", _textField.SelectedText);
  1200. break;
  1201. case 3:
  1202. Assert.Equal (12, _textField.CursorPosition);
  1203. Assert.Equal (32, _textField.SelectedStart);
  1204. Assert.Equal (20, _textField.SelectedLength);
  1205. Assert.Equal ("between text fields.", _textField.SelectedText);
  1206. break;
  1207. case 4:
  1208. Assert.Equal (7, _textField.CursorPosition);
  1209. Assert.Equal (32, _textField.SelectedStart);
  1210. Assert.Equal (25, _textField.SelectedLength);
  1211. Assert.Equal ("jump between text fields.", _textField.SelectedText);
  1212. break;
  1213. case 5:
  1214. Assert.Equal (4, _textField.CursorPosition);
  1215. Assert.Equal (32, _textField.SelectedStart);
  1216. Assert.Equal (28, _textField.SelectedLength);
  1217. Assert.Equal ("to jump between text fields.", _textField.SelectedText);
  1218. break;
  1219. case 6:
  1220. Assert.Equal (0, _textField.CursorPosition);
  1221. Assert.Equal (32, _textField.SelectedStart);
  1222. Assert.Equal (32, _textField.SelectedLength);
  1223. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1224. break;
  1225. }
  1226. iteration++;
  1227. }
  1228. }
  1229. [Fact]
  1230. [TextFieldTestsAutoInitShutdown]
  1231. public void
  1232. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1233. {
  1234. _textField.CursorPosition = 10;
  1235. _textField.SelectedStart = 10;
  1236. var iteration = 0;
  1237. while (_textField.CursorPosition > 0)
  1238. {
  1239. _textField.NewKeyDownEvent (
  1240. new Key (
  1241. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1242. )
  1243. );
  1244. switch (iteration)
  1245. {
  1246. case 0:
  1247. Assert.Equal (7, _textField.CursorPosition);
  1248. Assert.Equal (10, _textField.SelectedStart);
  1249. Assert.Equal (3, _textField.SelectedLength);
  1250. Assert.Equal ("jum", _textField.SelectedText);
  1251. break;
  1252. case 1:
  1253. Assert.Equal (4, _textField.CursorPosition);
  1254. Assert.Equal (10, _textField.SelectedStart);
  1255. Assert.Equal (6, _textField.SelectedLength);
  1256. Assert.Equal ("to jum", _textField.SelectedText);
  1257. break;
  1258. case 2:
  1259. Assert.Equal (0, _textField.CursorPosition);
  1260. Assert.Equal (10, _textField.SelectedStart);
  1261. Assert.Equal (10, _textField.SelectedLength);
  1262. Assert.Equal ("TAB to jum", _textField.SelectedText);
  1263. break;
  1264. }
  1265. iteration++;
  1266. }
  1267. }
  1268. [Fact]
  1269. public void WordBackward_WordForward_Mixed ()
  1270. {
  1271. var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
  1272. tf.BeginInit ();
  1273. tf.EndInit ();
  1274. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1275. Assert.Equal (15, tf.CursorPosition);
  1276. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1277. Assert.Equal (12, tf.CursorPosition);
  1278. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1279. Assert.Equal (10, tf.CursorPosition);
  1280. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1281. Assert.Equal (5, tf.CursorPosition);
  1282. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1283. Assert.Equal (0, tf.CursorPosition);
  1284. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1285. Assert.Equal (5, tf.CursorPosition);
  1286. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1287. Assert.Equal (10, tf.CursorPosition);
  1288. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1289. Assert.Equal (12, tf.CursorPosition);
  1290. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1291. Assert.Equal (15, tf.CursorPosition);
  1292. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1293. Assert.Equal (22, tf.CursorPosition);
  1294. }
  1295. [Fact]
  1296. public void WordBackward_WordForward_SelectedText_With_Accent ()
  1297. {
  1298. var text = "Les Misérables movie.";
  1299. var tf = new TextField { Width = 30, Text = text };
  1300. Assert.Equal (21, text.Length);
  1301. Assert.Equal (21, tf.Text.GetRuneCount ());
  1302. Assert.Equal (21, tf.Text.GetColumns ());
  1303. List<Rune> runes = tf.Text.ToRuneList ();
  1304. Assert.Equal (21, runes.Count);
  1305. Assert.Equal (21, tf.Text.Length);
  1306. for (var i = 0; i < runes.Count; i++)
  1307. {
  1308. char cs = text [i];
  1309. var cus = (char)runes [i].Value;
  1310. Assert.Equal (cs, cus);
  1311. }
  1312. var idx = 15;
  1313. Assert.Equal ('m', text [idx]);
  1314. Assert.Equal ('m', (char)runes [idx].Value);
  1315. Assert.Equal ("m", runes [idx].ToString ());
  1316. Assert.True (
  1317. tf.OnMouseEvent (
  1318. new MouseEvent { X = idx, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1319. )
  1320. );
  1321. Assert.Equal ("movie.", tf.SelectedText);
  1322. Assert.True (
  1323. tf.OnMouseEvent (
  1324. new MouseEvent { X = idx + 1, Y = 1, Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1325. )
  1326. );
  1327. Assert.Equal ("movie.", tf.SelectedText);
  1328. }
  1329. [Fact]
  1330. [TextFieldTestsAutoInitShutdown]
  1331. public void WordForward_With_No_Selection ()
  1332. {
  1333. _textField.CursorPosition = 0;
  1334. var iteration = 0;
  1335. while (_textField.CursorPosition < _textField.Text.Length)
  1336. {
  1337. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1338. switch (iteration)
  1339. {
  1340. case 0:
  1341. Assert.Equal (4, _textField.CursorPosition);
  1342. Assert.Equal (-1, _textField.SelectedStart);
  1343. Assert.Equal (0, _textField.SelectedLength);
  1344. Assert.Null (_textField.SelectedText);
  1345. break;
  1346. case 1:
  1347. Assert.Equal (7, _textField.CursorPosition);
  1348. Assert.Equal (-1, _textField.SelectedStart);
  1349. Assert.Equal (0, _textField.SelectedLength);
  1350. Assert.Null (_textField.SelectedText);
  1351. break;
  1352. case 2:
  1353. Assert.Equal (12, _textField.CursorPosition);
  1354. Assert.Equal (-1, _textField.SelectedStart);
  1355. Assert.Equal (0, _textField.SelectedLength);
  1356. Assert.Null (_textField.SelectedText);
  1357. break;
  1358. case 3:
  1359. Assert.Equal (20, _textField.CursorPosition);
  1360. Assert.Equal (-1, _textField.SelectedStart);
  1361. Assert.Equal (0, _textField.SelectedLength);
  1362. Assert.Null (_textField.SelectedText);
  1363. break;
  1364. case 4:
  1365. Assert.Equal (25, _textField.CursorPosition);
  1366. Assert.Equal (-1, _textField.SelectedStart);
  1367. Assert.Equal (0, _textField.SelectedLength);
  1368. Assert.Null (_textField.SelectedText);
  1369. break;
  1370. case 5:
  1371. Assert.Equal (32, _textField.CursorPosition);
  1372. Assert.Equal (-1, _textField.SelectedStart);
  1373. Assert.Equal (0, _textField.SelectedLength);
  1374. Assert.Null (_textField.SelectedText);
  1375. break;
  1376. }
  1377. iteration++;
  1378. }
  1379. }
  1380. [Fact]
  1381. [TextFieldTestsAutoInitShutdown]
  1382. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1383. {
  1384. // 1 2 3 4 5
  1385. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1386. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1387. _textField.CursorPosition = 0;
  1388. var iteration = 0;
  1389. while (_textField.CursorPosition < _textField.Text.Length)
  1390. {
  1391. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1392. switch (iteration)
  1393. {
  1394. case 0:
  1395. Assert.Equal (6, _textField.CursorPosition);
  1396. Assert.Equal (-1, _textField.SelectedStart);
  1397. Assert.Equal (0, _textField.SelectedLength);
  1398. Assert.Null (_textField.SelectedText);
  1399. break;
  1400. case 1:
  1401. Assert.Equal (9, _textField.CursorPosition);
  1402. Assert.Equal (-1, _textField.SelectedStart);
  1403. Assert.Equal (0, _textField.SelectedLength);
  1404. Assert.Null (_textField.SelectedText);
  1405. break;
  1406. case 2:
  1407. Assert.Equal (12, _textField.CursorPosition);
  1408. Assert.Equal (-1, _textField.SelectedStart);
  1409. Assert.Equal (0, _textField.SelectedLength);
  1410. Assert.Null (_textField.SelectedText);
  1411. break;
  1412. case 3:
  1413. Assert.Equal (25, _textField.CursorPosition);
  1414. Assert.Equal (-1, _textField.SelectedStart);
  1415. Assert.Equal (0, _textField.SelectedLength);
  1416. Assert.Null (_textField.SelectedText);
  1417. break;
  1418. case 4:
  1419. Assert.Equal (28, _textField.CursorPosition);
  1420. Assert.Equal (-1, _textField.SelectedStart);
  1421. Assert.Equal (0, _textField.SelectedLength);
  1422. Assert.Null (_textField.SelectedText);
  1423. break;
  1424. case 5:
  1425. Assert.Equal (38, _textField.CursorPosition);
  1426. Assert.Equal (-1, _textField.SelectedStart);
  1427. Assert.Equal (0, _textField.SelectedLength);
  1428. Assert.Null (_textField.SelectedText);
  1429. break;
  1430. case 6:
  1431. Assert.Equal (40, _textField.CursorPosition);
  1432. Assert.Equal (-1, _textField.SelectedStart);
  1433. Assert.Equal (0, _textField.SelectedLength);
  1434. Assert.Null (_textField.SelectedText);
  1435. break;
  1436. case 7:
  1437. Assert.Equal (46, _textField.CursorPosition);
  1438. Assert.Equal (-1, _textField.SelectedStart);
  1439. Assert.Equal (0, _textField.SelectedLength);
  1440. Assert.Null (_textField.SelectedText);
  1441. break;
  1442. case 8:
  1443. Assert.Equal (48, _textField.CursorPosition);
  1444. Assert.Equal (-1, _textField.SelectedStart);
  1445. Assert.Equal (0, _textField.SelectedLength);
  1446. Assert.Null (_textField.SelectedText);
  1447. break;
  1448. case 9:
  1449. Assert.Equal (54, _textField.CursorPosition);
  1450. Assert.Equal (-1, _textField.SelectedStart);
  1451. Assert.Equal (0, _textField.SelectedLength);
  1452. Assert.Null (_textField.SelectedText);
  1453. break;
  1454. case 10:
  1455. Assert.Equal (55, _textField.CursorPosition);
  1456. Assert.Equal (-1, _textField.SelectedStart);
  1457. Assert.Equal (0, _textField.SelectedLength);
  1458. Assert.Null (_textField.SelectedText);
  1459. break;
  1460. }
  1461. iteration++;
  1462. }
  1463. }
  1464. [Fact]
  1465. [TextFieldTestsAutoInitShutdown]
  1466. public void WordForward_With_Selection ()
  1467. {
  1468. _textField.CursorPosition = 0;
  1469. _textField.SelectedStart = 0;
  1470. var iteration = 0;
  1471. while (_textField.CursorPosition < _textField.Text.Length)
  1472. {
  1473. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1474. switch (iteration)
  1475. {
  1476. case 0:
  1477. Assert.Equal (4, _textField.CursorPosition);
  1478. Assert.Equal (0, _textField.SelectedStart);
  1479. Assert.Equal (4, _textField.SelectedLength);
  1480. Assert.Equal ("TAB ", _textField.SelectedText);
  1481. break;
  1482. case 1:
  1483. Assert.Equal (7, _textField.CursorPosition);
  1484. Assert.Equal (0, _textField.SelectedStart);
  1485. Assert.Equal (7, _textField.SelectedLength);
  1486. Assert.Equal ("TAB to ", _textField.SelectedText);
  1487. break;
  1488. case 2:
  1489. Assert.Equal (12, _textField.CursorPosition);
  1490. Assert.Equal (0, _textField.SelectedStart);
  1491. Assert.Equal (12, _textField.SelectedLength);
  1492. Assert.Equal ("TAB to jump ", _textField.SelectedText);
  1493. break;
  1494. case 3:
  1495. Assert.Equal (20, _textField.CursorPosition);
  1496. Assert.Equal (0, _textField.SelectedStart);
  1497. Assert.Equal (20, _textField.SelectedLength);
  1498. Assert.Equal ("TAB to jump between ", _textField.SelectedText);
  1499. break;
  1500. case 4:
  1501. Assert.Equal (25, _textField.CursorPosition);
  1502. Assert.Equal (0, _textField.SelectedStart);
  1503. Assert.Equal (25, _textField.SelectedLength);
  1504. Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
  1505. break;
  1506. case 5:
  1507. Assert.Equal (32, _textField.CursorPosition);
  1508. Assert.Equal (0, _textField.SelectedStart);
  1509. Assert.Equal (32, _textField.SelectedLength);
  1510. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1511. break;
  1512. }
  1513. iteration++;
  1514. }
  1515. }
  1516. [Fact]
  1517. [TextFieldTestsAutoInitShutdown]
  1518. public void
  1519. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1520. {
  1521. _textField.CursorPosition = 10;
  1522. _textField.SelectedStart = 10;
  1523. var iteration = 0;
  1524. while (_textField.CursorPosition < _textField.Text.Length)
  1525. {
  1526. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1527. switch (iteration)
  1528. {
  1529. case 0:
  1530. Assert.Equal (12, _textField.CursorPosition);
  1531. Assert.Equal (10, _textField.SelectedStart);
  1532. Assert.Equal (2, _textField.SelectedLength);
  1533. Assert.Equal ("p ", _textField.SelectedText);
  1534. break;
  1535. case 1:
  1536. Assert.Equal (20, _textField.CursorPosition);
  1537. Assert.Equal (10, _textField.SelectedStart);
  1538. Assert.Equal (10, _textField.SelectedLength);
  1539. Assert.Equal ("p between ", _textField.SelectedText);
  1540. break;
  1541. case 2:
  1542. Assert.Equal (25, _textField.CursorPosition);
  1543. Assert.Equal (10, _textField.SelectedStart);
  1544. Assert.Equal (15, _textField.SelectedLength);
  1545. Assert.Equal ("p between text ", _textField.SelectedText);
  1546. break;
  1547. case 3:
  1548. Assert.Equal (32, _textField.CursorPosition);
  1549. Assert.Equal (10, _textField.SelectedStart);
  1550. Assert.Equal (22, _textField.SelectedLength);
  1551. Assert.Equal ("p between text fields.", _textField.SelectedText);
  1552. break;
  1553. }
  1554. iteration++;
  1555. }
  1556. }
  1557. [Fact]
  1558. [AutoInitShutdown]
  1559. public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
  1560. {
  1561. var tf = new TextField { Width = 30, Text = "Les Misérables" };
  1562. Toplevel top = Application.Top;
  1563. top.Add (tf);
  1564. Application.Begin (top);
  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. Application.Refresh ();
  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. Application.Refresh ();
  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 = Application.Top;
  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 TextField
  1616. {
  1617. ColorScheme = new ColorScheme (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. }