TextFieldTests.cs 74 KB

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