TextFieldTests.cs 65 KB

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