TextFieldTests.cs 72 KB

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