TextFieldTests.cs 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. using System.ComponentModel;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using System.Text;
  5. using Xunit.Abstractions;
  6. namespace Terminal.Gui.ViewsTests;
  7. public class TextFieldTests (ITestOutputHelper output)
  8. {
  9. private static TextField _textField;
  10. [Fact]
  11. [SetupFakeDriver]
  12. public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
  13. {
  14. var tf = new TextField { Width = 3, Text = "ắ" };
  15. tf.Draw ();
  16. TestHelpers.AssertDriverContentsWithFrameAre (
  17. @"
  18. ắ",
  19. output
  20. );
  21. tf.Text = "\u1eaf";
  22. tf.Draw ();
  23. TestHelpers.AssertDriverContentsWithFrameAre (
  24. @"
  25. ắ",
  26. output
  27. );
  28. tf.Text = "\u0103\u0301";
  29. tf.Draw ();
  30. TestHelpers.AssertDriverContentsWithFrameAre (
  31. @"
  32. ắ",
  33. output
  34. );
  35. tf.Text = "\u0061\u0306\u0301";
  36. tf.Draw ();
  37. TestHelpers.AssertDriverContentsWithFrameAre (
  38. @"
  39. ắ",
  40. output
  41. );
  42. }
  43. [Fact]
  44. [SetupFakeDriver]
  45. public void Adjust_First ()
  46. {
  47. var tf = new TextField { Width = Dim.Fill (), Text = "This is a test." };
  48. tf.SetRelativeLayout (new (20, 20));
  49. tf.Draw ();
  50. Assert.Equal ("This is a test. ", GetContents ());
  51. string GetContents ()
  52. {
  53. var item = "";
  54. for (var i = 0; i < 16; i++)
  55. {
  56. item += Application.Driver?.Contents [0, i].Rune;
  57. }
  58. return item;
  59. }
  60. }
  61. [Fact]
  62. public void Cancel_TextChanging_ThenBackspace ()
  63. {
  64. var tf = new TextField ();
  65. tf.SetFocus ();
  66. tf.NewKeyDownEvent (Key.A.WithShift);
  67. Assert.Equal ("A", tf.Text);
  68. // cancel the next keystroke
  69. tf.TextChanging += (s, e) => e.Cancel = e.NewValue == "AB";
  70. tf.NewKeyDownEvent (Key.B.WithShift);
  71. // B was canceled so should just be A
  72. Assert.Equal ("A", tf.Text);
  73. // now delete the A
  74. tf.NewKeyDownEvent (Key.Backspace);
  75. Assert.Equal ("", tf.Text);
  76. }
  77. [Fact]
  78. [TextFieldTestsAutoInitShutdown]
  79. public void CanFocus_False_Wont_Focus_With_Mouse ()
  80. {
  81. Toplevel top = new ();
  82. var tf = new TextField { Width = Dim.Fill (), CanFocus = false, ReadOnly = true, Text = "some text" };
  83. var fv = new FrameView
  84. {
  85. Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = false, Title = "I shouldn't get focus"
  86. };
  87. fv.Add (tf);
  88. top.Add (fv);
  89. Application.Begin (top);
  90. Assert.False (tf.CanFocus);
  91. Assert.False (tf.HasFocus);
  92. Assert.False (fv.CanFocus);
  93. Assert.False (fv.HasFocus);
  94. tf.NewMouseEvent (
  95. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  96. );
  97. Assert.Null (tf.SelectedText);
  98. Assert.False (tf.CanFocus);
  99. Assert.False (tf.HasFocus);
  100. Assert.False (fv.CanFocus);
  101. Assert.False (fv.HasFocus);
  102. fv.CanFocus = true;
  103. tf.CanFocus = true;
  104. tf.NewMouseEvent (
  105. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  106. );
  107. Assert.Equal ("some ", tf.SelectedText);
  108. Assert.True (tf.CanFocus);
  109. Assert.True (tf.HasFocus);
  110. Assert.True (fv.CanFocus);
  111. Assert.True (fv.HasFocus);
  112. fv.CanFocus = false;
  113. tf.NewMouseEvent (
  114. new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked }
  115. );
  116. Assert.Equal ("some ", tf.SelectedText); // Setting CanFocus to false don't change the SelectedText
  117. Assert.True (tf.CanFocus); // v2: CanFocus is not longer automatically changed
  118. Assert.False (tf.HasFocus);
  119. Assert.False (fv.CanFocus);
  120. Assert.False (fv.HasFocus);
  121. top.Dispose ();
  122. }
  123. [Theory]
  124. [AutoInitShutdown]
  125. [InlineData ("0123456789", "0123456789")]
  126. [InlineData ("01234567890", "0123456789")]
  127. public void CaptionedTextField_DoesNotOverspillBounds (string caption, string expectedRender)
  128. {
  129. TextField tf = GetTextFieldsInView ();
  130. // Caption has no effect when focused
  131. tf.Caption = caption;
  132. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  133. Assert.False (tf.HasFocus);
  134. tf.Draw ();
  135. TestHelpers.AssertDriverContentsAre (expectedRender, output);
  136. Application.Top.Dispose ();
  137. }
  138. [Fact]
  139. [AutoInitShutdown]
  140. public void CaptionedTextField_DoesNotOverspillViewport_Unicode ()
  141. {
  142. string caption = "Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  143. Assert.Equal (11, caption.Length);
  144. Assert.Equal (10, caption.EnumerateRunes ().Sum (c => c.GetColumns ()));
  145. TextField tf = GetTextFieldsInView ();
  146. tf.Caption = caption;
  147. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  148. Assert.False (tf.HasFocus);
  149. tf.Draw ();
  150. TestHelpers.AssertDriverContentsAre ("Misérables", output);
  151. Application.Top.Dispose ();
  152. }
  153. [Theory]
  154. [AutoInitShutdown]
  155. [InlineData ("blah")]
  156. [InlineData (" ")]
  157. public void CaptionedTextField_DoNotRenderCaption_WhenTextPresent (string content)
  158. {
  159. TextField tf = GetTextFieldsInView ();
  160. tf.Draw ();
  161. TestHelpers.AssertDriverContentsAre ("", output);
  162. tf.Caption = "Enter txt";
  163. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  164. // Caption should appear when not focused and no text
  165. Assert.False (tf.HasFocus);
  166. tf.Draw ();
  167. TestHelpers.AssertDriverContentsAre ("Enter txt", output);
  168. // but disapear when text is added
  169. tf.Text = content;
  170. tf.Draw ();
  171. TestHelpers.AssertDriverContentsAre (content, output);
  172. Application.Top.Dispose ();
  173. }
  174. [Fact]
  175. [AutoInitShutdown]
  176. public void CaptionedTextField_RendersCaption_WhenNotFocused ()
  177. {
  178. TextField tf = GetTextFieldsInView ();
  179. tf.Draw ();
  180. TestHelpers.AssertDriverContentsAre ("", output);
  181. // Caption has no effect when focused
  182. tf.Caption = "Enter txt";
  183. Assert.True (tf.HasFocus);
  184. tf.Draw ();
  185. TestHelpers.AssertDriverContentsAre ("", output);
  186. Application.Driver?.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  187. Assert.False (tf.HasFocus);
  188. tf.Draw ();
  189. TestHelpers.AssertDriverContentsAre ("Enter txt", output);
  190. Application.Top.Dispose ();
  191. }
  192. [Fact]
  193. [TextFieldTestsAutoInitShutdown]
  194. public void Changing_SelectedStart_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  195. {
  196. _textField.BeginInit ();
  197. _textField.EndInit ();
  198. _textField.SelectedStart = 2;
  199. Assert.Equal (32, _textField.CursorPosition);
  200. Assert.Equal (30, _textField.SelectedLength);
  201. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  202. _textField.CursorPosition = 20;
  203. Assert.Equal (2, _textField.SelectedStart);
  204. Assert.Equal (18, _textField.SelectedLength);
  205. Assert.Equal ("B to jump between ", _textField.SelectedText);
  206. }
  207. [Fact]
  208. [TextFieldTestsAutoInitShutdown]
  209. public void Copy_Or_Cut__Not_Allowed_If_Secret_Is_True ()
  210. {
  211. _textField.Secret = true;
  212. _textField.SelectedStart = 20;
  213. _textField.CursorPosition = 24;
  214. _textField.Copy ();
  215. Assert.Null (_textField.SelectedText);
  216. _textField.Cut ();
  217. Assert.Null (_textField.SelectedText);
  218. _textField.Secret = false;
  219. _textField.Copy ();
  220. Assert.Equal ("text", _textField.SelectedText);
  221. _textField.Cut ();
  222. Assert.Null (_textField.SelectedText);
  223. }
  224. [Fact]
  225. [TextFieldTestsAutoInitShutdown]
  226. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  227. {
  228. _textField.SelectedStart = 20;
  229. _textField.CursorPosition = 24;
  230. _textField.Copy ();
  231. Assert.Equal ("text", _textField.SelectedText);
  232. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  233. _textField.SelectedStart = -1;
  234. _textField.Paste ();
  235. Assert.Equal ("TAB to jump between texttext fields.", _textField.Text);
  236. _textField.SelectedStart = 24;
  237. _textField.Cut ();
  238. Assert.Null (_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. }
  244. [Fact]
  245. [TextFieldTestsAutoInitShutdown]
  246. public void Copy_Or_Cut_And_Paste_With_Selection ()
  247. {
  248. _textField.SelectedStart = 20;
  249. _textField.CursorPosition = 24;
  250. _textField.Copy ();
  251. Assert.Equal ("text", _textField.SelectedText);
  252. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  253. _textField.Paste ();
  254. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  255. _textField.SelectedStart = 20;
  256. _textField.Cut ();
  257. _textField.Paste ();
  258. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  259. }
  260. [Fact]
  261. [TextFieldTestsAutoInitShutdown]
  262. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  263. {
  264. _textField.SelectedStart = 20;
  265. _textField.CursorPosition = 24;
  266. _textField.Copy ();
  267. Assert.Equal ("text", _textField.SelectedText);
  268. _textField.Cut ();
  269. Assert.Null (_textField.SelectedText);
  270. }
  271. [Fact]
  272. [TextFieldTestsAutoInitShutdown]
  273. public void Copy_Or_Cut_Null_If_No_Selection ()
  274. {
  275. _textField.SelectedStart = -1;
  276. _textField.Copy ();
  277. Assert.Null (_textField.SelectedText);
  278. _textField.Cut ();
  279. Assert.Null (_textField.SelectedText);
  280. }
  281. [Fact]
  282. [TextFieldTestsAutoInitShutdown]
  283. public void Copy_Paste_Surrogate_Pairs ()
  284. {
  285. _textField.Text = "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
  286. _textField.SelectAll ();
  287. _textField.Cut ();
  288. Assert.Equal (
  289. "TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!",
  290. Application.Driver?.Clipboard.GetClipboardData ()
  291. );
  292. Assert.Equal (string.Empty, _textField.Text);
  293. _textField.Paste ();
  294. Assert.Equal ("TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textField.Text);
  295. }
  296. [Fact]
  297. [TextFieldTestsAutoInitShutdown]
  298. public void Copy_Paste_Text_Changing_Updates_Cursor_Position ()
  299. {
  300. _textField.BeginInit ();
  301. _textField.EndInit ();
  302. _textField.TextChanging += _textField_TextChanging;
  303. void _textField_TextChanging (object sender, CancelEventArgs<string> e)
  304. {
  305. if (e.NewValue.GetRuneCount () > 11)
  306. {
  307. e.NewValue = e.NewValue [..11];
  308. }
  309. }
  310. Assert.Equal (32, _textField.CursorPosition);
  311. _textField.SelectAll ();
  312. _textField.Cut ();
  313. Assert.Equal ("TAB to jump between text fields.", Application.Driver?.Clipboard.GetClipboardData ());
  314. Assert.Equal (string.Empty, _textField.Text);
  315. Assert.Equal (0, _textField.CursorPosition);
  316. _textField.Paste ();
  317. Assert.Equal ("TAB to jump", _textField.Text);
  318. Assert.Equal (11, _textField.CursorPosition);
  319. _textField.TextChanging -= _textField_TextChanging;
  320. }
  321. [Fact]
  322. [TextFieldTestsAutoInitShutdown]
  323. public void Cursor_Position_Initialization ()
  324. {
  325. Assert.False (_textField.IsInitialized);
  326. // BUGBUG: IsInitialized is false and
  327. // CursorPosition wasn't calculated yet
  328. Assert.Equal (0, _textField.CursorPosition);
  329. _textField.BeginInit ();
  330. _textField.EndInit ();
  331. Assert.Equal (32, _textField.CursorPosition);
  332. Assert.Equal (0, _textField.SelectedLength);
  333. Assert.Null (_textField.SelectedText);
  334. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  335. }
  336. [Fact]
  337. [TextFieldTestsAutoInitShutdown]
  338. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  339. {
  340. _textField.CursorPosition = 33;
  341. Assert.Equal (32, _textField.CursorPosition);
  342. Assert.Equal (0, _textField.SelectedLength);
  343. Assert.Null (_textField.SelectedText);
  344. }
  345. [Fact]
  346. [TextFieldTestsAutoInitShutdown]
  347. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  348. {
  349. _textField.CursorPosition = -1;
  350. Assert.Equal (0, _textField.CursorPosition);
  351. Assert.Equal (0, _textField.SelectedLength);
  352. Assert.Null (_textField.SelectedText);
  353. }
  354. [Fact]
  355. [AutoInitShutdown]
  356. public void DeleteSelectedText_InsertText_DeleteCharLeft_DeleteCharRight_Cut ()
  357. {
  358. var newText = "";
  359. var oldText = "";
  360. var tf = new TextField { Width = 10, Text = "-1" };
  361. tf.TextChanging += (s, e) =>
  362. {
  363. newText = e.NewValue;
  364. oldText = e.CurrentValue;
  365. };
  366. var top = new Toplevel ();
  367. top.Add (tf);
  368. Application.Begin (top);
  369. Assert.Equal ("-1", tf.Text);
  370. // InsertText
  371. tf.SelectedStart = 1;
  372. tf.CursorPosition = 2;
  373. Assert.Equal (1, tf.SelectedLength);
  374. Assert.Equal ("1", tf.SelectedText);
  375. Assert.True (tf.NewKeyDownEvent (Key.D2));
  376. Assert.Equal ("-2", newText);
  377. Assert.Equal ("-1", oldText);
  378. Assert.Equal ("-2", tf.Text);
  379. // DeleteCharLeft
  380. tf.SelectedStart = 1;
  381. tf.CursorPosition = 2;
  382. Assert.Equal (1, tf.SelectedLength);
  383. Assert.Equal ("2", tf.SelectedText);
  384. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  385. Assert.Equal ("-", newText);
  386. Assert.Equal ("-2", oldText);
  387. Assert.Equal ("-", tf.Text);
  388. // DeleteCharRight
  389. tf.Text = "-1";
  390. tf.SelectedStart = 1;
  391. tf.CursorPosition = 2;
  392. Assert.Equal (1, tf.SelectedLength);
  393. Assert.Equal ("1", tf.SelectedText);
  394. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  395. Assert.Equal ("-", newText);
  396. Assert.Equal ("-1", oldText);
  397. Assert.Equal ("-", tf.Text);
  398. // Cut
  399. tf.Text = "-1";
  400. tf.SelectedStart = 1;
  401. tf.CursorPosition = 2;
  402. Assert.Equal (1, tf.SelectedLength);
  403. Assert.Equal ("1", tf.SelectedText);
  404. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  405. Assert.Equal ("-", newText);
  406. Assert.Equal ("-1", oldText);
  407. Assert.Equal ("-", tf.Text);
  408. // Delete word with accented char
  409. tf.Text = "Les Misérables movie.";
  410. Assert.True (
  411. tf.NewMouseEvent (
  412. new () { Position = new (7, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  413. )
  414. );
  415. Assert.Equal ("Misérables ", tf.SelectedText);
  416. Assert.Equal (11, tf.SelectedLength);
  417. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  418. Assert.Equal ("Les movie.", newText);
  419. Assert.Equal ("Les Misérables movie.", oldText);
  420. Assert.Equal ("Les movie.", tf.Text);
  421. top.Dispose ();
  422. }
  423. [Fact]
  424. public void HistoryText_IsDirty_ClearHistoryChanges ()
  425. {
  426. var text = "Testing";
  427. var tf = new TextField { Text = text };
  428. tf.BeginInit ();
  429. tf.EndInit ();
  430. Assert.Equal (text, tf.Text);
  431. tf.ClearHistoryChanges ();
  432. Assert.False (tf.IsDirty);
  433. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift));
  434. Assert.Equal ($"{text}A", tf.Text);
  435. Assert.True (tf.IsDirty);
  436. }
  437. [Fact]
  438. public void Space_Does_Not_Raise_Selected ()
  439. {
  440. TextField tf = new ();
  441. tf.Selecting += (sender, args) => Assert.Fail ("Selected should not be raied.");
  442. Application.Top = new Toplevel ();
  443. Application.Top.Add (tf);
  444. tf.SetFocus ();
  445. Application.OnKeyDown (Key.Space);
  446. Application.Top.Dispose ();
  447. Application.ResetState (ignoreDisposed: true);
  448. }
  449. [Fact]
  450. public void Enter_Does_Not_Raise_Selected ()
  451. {
  452. TextField tf = new ();
  453. int selectingCount = 0;
  454. tf.Selecting += (sender, args) => selectingCount++;
  455. Application.Top = new Toplevel ();
  456. Application.Top.Add (tf);
  457. tf.SetFocus ();
  458. Application.OnKeyDown (Key.Enter);
  459. Assert.Equal (0, selectingCount);
  460. Application.Top.Dispose ();
  461. Application.ResetState (ignoreDisposed: true);
  462. }
  463. [Fact]
  464. public void Enter_Raises_Accepted ()
  465. {
  466. TextField tf = new ();
  467. int acceptedCount = 0;
  468. tf.Accepted += (sender, args) => acceptedCount++;
  469. Application.Top = new Toplevel ();
  470. Application.Top.Add (tf);
  471. tf.SetFocus ();
  472. Application.OnKeyDown (Key.Enter);
  473. Assert.Equal (1, acceptedCount);
  474. Application.Top.Dispose ();
  475. Application.ResetState (ignoreDisposed: true);
  476. }
  477. [Fact]
  478. [AutoInitShutdown (useFakeClipboard: true)]
  479. public void KeyBindings_Command ()
  480. {
  481. var tf = new TextField { Width = 20, Text = "This is a test." };
  482. tf.BeginInit ();
  483. tf.EndInit ();
  484. Assert.Equal (15, tf.Text.Length);
  485. Assert.Equal (15, tf.CursorPosition);
  486. Assert.False (tf.ReadOnly);
  487. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  488. Assert.Equal ("This is a test.", tf.Text);
  489. tf.CursorPosition = 0;
  490. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  491. Assert.Equal ("his is a test.", tf.Text);
  492. tf.ReadOnly = true;
  493. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
  494. Assert.Equal ("his is a test.", tf.Text);
  495. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  496. Assert.Equal ("his is a test.", tf.Text);
  497. tf.ReadOnly = false;
  498. tf.CursorPosition = 1;
  499. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  500. Assert.Equal ("is is a test.", tf.Text);
  501. tf.CursorPosition = 5;
  502. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift));
  503. Assert.Equal ("is is a test.", tf.Text);
  504. Assert.Equal ("is is", tf.SelectedText);
  505. tf.CursorPosition = 5;
  506. tf.SelectedStart = -1;
  507. Assert.Null (tf.SelectedText);
  508. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
  509. Assert.Equal ("is is a test.", tf.Text);
  510. Assert.Equal ("is is", tf.SelectedText);
  511. tf.CursorPosition = 5;
  512. tf.SelectedStart = -1;
  513. Assert.Null (tf.SelectedText);
  514. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift.WithCtrl));
  515. Assert.Equal ("is is a test.", tf.Text);
  516. Assert.Equal ("is is", tf.SelectedText);
  517. tf.CursorPosition = 5;
  518. tf.SelectedStart = -1;
  519. Assert.Null (tf.SelectedText);
  520. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift));
  521. Assert.Equal ("is is a test.", tf.Text);
  522. Assert.Equal (" a test.", tf.SelectedText);
  523. tf.CursorPosition = 5;
  524. tf.SelectedStart = -1;
  525. Assert.Null (tf.SelectedText);
  526. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
  527. Assert.Equal ("is is a test.", tf.Text);
  528. Assert.Equal (" a test.", tf.SelectedText);
  529. tf.CursorPosition = 5;
  530. tf.SelectedStart = -1;
  531. Assert.Null (tf.SelectedText);
  532. Assert.True (tf.NewKeyDownEvent (Key.E.WithShift.WithCtrl));
  533. Assert.Equal ("is is a test.", tf.Text);
  534. Assert.Equal (" a test.", tf.SelectedText);
  535. tf.CursorPosition = 5;
  536. tf.SelectedStart = -1;
  537. Assert.Null (tf.SelectedText);
  538. Assert.True (tf.NewKeyDownEvent (Key.Home));
  539. Assert.Equal ("is is a test.", tf.Text);
  540. Assert.Equal (0, tf.CursorPosition);
  541. tf.CursorPosition = 5;
  542. Assert.Null (tf.SelectedText);
  543. Assert.True (tf.NewKeyDownEvent (Key.Home.WithCtrl));
  544. Assert.Equal ("is is a test.", tf.Text);
  545. Assert.Equal (0, tf.CursorPosition);
  546. tf.CursorPosition = 5;
  547. Assert.Null (tf.SelectedText);
  548. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  549. Assert.Equal ("is is a test.", tf.Text);
  550. Assert.Equal (0, tf.CursorPosition);
  551. tf.CursorPosition = 5;
  552. tf.SelectedStart = -1;
  553. Assert.Null (tf.SelectedText);
  554. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  555. Assert.Equal ("is is a test.", tf.Text);
  556. Assert.Equal ("s", tf.SelectedText);
  557. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift));
  558. Assert.Equal ("is is a test.", tf.Text);
  559. Assert.Equal ("is", tf.SelectedText);
  560. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  561. Assert.Equal ("is is a test.", tf.Text);
  562. Assert.Equal ("s", tf.SelectedText);
  563. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift));
  564. Assert.Equal ("is is a test.", tf.Text);
  565. Assert.Null (tf.SelectedText);
  566. tf.CursorPosition = 7;
  567. tf.SelectedStart = -1;
  568. Assert.Null (tf.SelectedText);
  569. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
  570. Assert.Equal ("is is a test.", tf.Text);
  571. Assert.Equal ("a", tf.SelectedText);
  572. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  573. Assert.Equal ("is is a test.", tf.Text);
  574. Assert.Equal ("is a", tf.SelectedText);
  575. #if UNIX_KEY_BINDINGS
  576. Assert.True (tf.NewKeyDownEvent (Key.B.WithShift.WithAlt));
  577. #else
  578. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  579. #endif
  580. Assert.Equal ("is is a test.", tf.Text);
  581. Assert.Equal ("is is a", tf.SelectedText);
  582. tf.CursorPosition = 3;
  583. tf.SelectedStart = -1;
  584. Assert.Null (tf.SelectedText);
  585. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  586. Assert.Equal ("is is a test.", tf.Text);
  587. Assert.Equal ("is ", tf.SelectedText);
  588. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift.WithCtrl));
  589. Assert.Equal ("is is a test.", tf.Text);
  590. Assert.Equal ("is a ", tf.SelectedText);
  591. #if UNIX_KEY_BINDINGS
  592. Assert.True (tf.NewKeyDownEvent (Key.F.WithShift.WithAlt));
  593. #else
  594. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  595. #endif
  596. Assert.Equal ("is is a test.", tf.Text);
  597. Assert.Equal ("is a test.", tf.SelectedText);
  598. Assert.Equal (13, tf.CursorPosition);
  599. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  600. Assert.Equal ("is is a test.", tf.Text);
  601. Assert.Null (tf.SelectedText);
  602. Assert.Equal (12, tf.CursorPosition);
  603. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  604. Assert.Equal ("is is a test.", tf.Text);
  605. Assert.Equal (11, tf.CursorPosition);
  606. Assert.True (tf.NewKeyDownEvent (Key.End));
  607. Assert.Equal ("is is a test.", tf.Text);
  608. Assert.Equal (13, tf.CursorPosition);
  609. tf.CursorPosition = 0;
  610. Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
  611. Assert.Equal ("is is a test.", tf.Text);
  612. Assert.Equal (13, tf.CursorPosition);
  613. tf.CursorPosition = 0;
  614. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  615. Assert.Equal ("is is a test.", tf.Text);
  616. Assert.Equal (13, tf.CursorPosition);
  617. tf.CursorPosition = 0;
  618. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  619. Assert.Equal ("is is a test.", tf.Text);
  620. Assert.Equal (1, tf.CursorPosition);
  621. Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
  622. Assert.Equal ("is is a test.", tf.Text);
  623. Assert.Equal (2, tf.CursorPosition);
  624. tf.CursorPosition = 9;
  625. tf.ReadOnly = true;
  626. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  627. Assert.Equal ("is is a test.", tf.Text);
  628. tf.ReadOnly = false;
  629. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  630. Assert.Equal ("is is a t", tf.Text);
  631. Assert.Equal ("est.", Clipboard.Contents);
  632. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  633. Assert.Equal ("is is a test.", tf.Text);
  634. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  635. Assert.Equal ("is is a t", tf.Text);
  636. #if UNIX_KEY_BINDINGS
  637. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
  638. #else
  639. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  640. #endif
  641. Assert.Equal ("is is a test.", tf.Text);
  642. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  643. Assert.Equal ("is is a t", tf.Text);
  644. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  645. Assert.Equal ("is is a t", tf.Text);
  646. Assert.Equal (8, tf.CursorPosition);
  647. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  648. Assert.Equal ("is is a t", tf.Text);
  649. Assert.Equal (6, tf.CursorPosition);
  650. #if UNIX_KEY_BINDINGS
  651. Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
  652. #else
  653. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  654. #endif
  655. Assert.Equal ("is is a t", tf.Text);
  656. Assert.Equal (3, tf.CursorPosition);
  657. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  658. Assert.Equal ("is is a t", tf.Text);
  659. Assert.Equal (6, tf.CursorPosition);
  660. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  661. Assert.Equal ("is is a t", tf.Text);
  662. Assert.Equal (8, tf.CursorPosition);
  663. #if UNIX_KEY_BINDINGS
  664. Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
  665. #else
  666. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  667. #endif
  668. Assert.Equal ("is is a t", tf.Text);
  669. Assert.Equal (9, tf.CursorPosition);
  670. Assert.True (tf.Used);
  671. Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
  672. Assert.Equal ("is is a t", tf.Text);
  673. Assert.Equal (9, tf.CursorPosition);
  674. Assert.False (tf.Used);
  675. tf.SelectedStart = 3;
  676. tf.CursorPosition = 7;
  677. Assert.Equal ("is a", tf.SelectedText);
  678. Assert.Equal ("est.", Clipboard.Contents);
  679. Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
  680. Assert.Equal ("is is a t", tf.Text);
  681. Assert.Equal ("is a", Clipboard.Contents);
  682. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  683. Assert.Equal ("is t", tf.Text);
  684. Assert.Equal ("is a", Clipboard.Contents);
  685. Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
  686. Assert.Equal ("is is a t", tf.Text);
  687. Assert.Equal ("is a", Clipboard.Contents);
  688. Assert.Equal (7, tf.CursorPosition);
  689. #if UNIX_KEY_BINDINGS
  690. Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
  691. #else
  692. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
  693. #endif
  694. Assert.Equal (" t", tf.Text);
  695. Assert.Equal ("is is a", Clipboard.Contents);
  696. tf.Text = "TAB to jump between text fields.";
  697. Assert.Equal (0, tf.CursorPosition);
  698. Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
  699. Assert.Equal ("to jump between text fields.", tf.Text);
  700. tf.CursorPosition = tf.Text.Length;
  701. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
  702. Assert.Equal ("to jump between text fields", tf.Text);
  703. Assert.True (tf.NewKeyDownEvent (Key.T.WithCtrl));
  704. Assert.Equal ("to jump between text fields", tf.SelectedText);
  705. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
  706. Assert.Equal ("", tf.Text);
  707. }
  708. [Fact]
  709. public void HotKey_Command_SetsFocus ()
  710. {
  711. var view = new TextField ();
  712. view.CanFocus = true;
  713. Assert.False (view.HasFocus);
  714. view.InvokeCommand (Command.HotKey);
  715. Assert.True (view.HasFocus);
  716. }
  717. [Fact]
  718. public void HotKey_Command_Does_Not_Accept ()
  719. {
  720. var view = new TextField ();
  721. var accepted = false;
  722. view.Accepted += OnAccept;
  723. view.InvokeCommand (Command.HotKey);
  724. Assert.False (accepted);
  725. return;
  726. void OnAccept (object sender, HandledEventArgs e) { accepted = true; }
  727. }
  728. [Fact]
  729. public void Accepted_Command_Fires_Accept ()
  730. {
  731. var view = new TextField ();
  732. var accepted = false;
  733. view.Accepted += Accept;
  734. view.InvokeCommand (Command.Accept);
  735. Assert.True (accepted);
  736. return;
  737. void Accept (object sender, HandledEventArgs e) { accepted = true; }
  738. }
  739. [Theory]
  740. [InlineData (false, 1)]
  741. [InlineData (true, 0)]
  742. public void Accepted_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
  743. {
  744. var superView = new Window ()
  745. {
  746. Id = "superView"
  747. };
  748. var tf = new TextField ()
  749. {
  750. Id = "tf"
  751. };
  752. var button = new Button ()
  753. {
  754. Id = "button",
  755. IsDefault = true,
  756. };
  757. superView.Add (tf, button);
  758. var buttonAccept = 0;
  759. button.Accepted += ButtonAccept;
  760. var textFieldAccept = 0;
  761. tf.Accepted += TextFieldAccept;
  762. tf.SetFocus ();
  763. Assert.True (tf.HasFocus);
  764. superView.NewKeyDownEvent (Key.Enter);
  765. Assert.Equal (1, textFieldAccept);
  766. Assert.Equal (expectedButtonAccepts, buttonAccept);
  767. button.SetFocus ();
  768. superView.NewKeyDownEvent (Key.Enter);
  769. Assert.Equal (1, textFieldAccept);
  770. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  771. return;
  772. void TextFieldAccept (object sender, HandledEventArgs e)
  773. {
  774. textFieldAccept++;
  775. e.Handled = handleAccept;
  776. }
  777. void ButtonAccept (object sender, HandledEventArgs e)
  778. {
  779. buttonAccept++;
  780. }
  781. }
  782. [Fact]
  783. public void Accepted_No_Handler_Enables_Default_Button_Accept ()
  784. {
  785. var superView = new Window ()
  786. {
  787. Id = "superView"
  788. };
  789. var tf = new TextField ()
  790. {
  791. Id = "tf"
  792. };
  793. var button = new Button ()
  794. {
  795. Id="button",
  796. IsDefault = true,
  797. };
  798. superView.Add (tf, button);
  799. var buttonAccept = 0;
  800. button.Accepted += ButtonAccept;
  801. tf.SetFocus ();
  802. Assert.True (tf.HasFocus);
  803. superView.NewKeyDownEvent (Key.Enter);
  804. Assert.Equal (1, buttonAccept);
  805. button.SetFocus ();
  806. superView.NewKeyDownEvent (Key.Enter);
  807. Assert.Equal (2, buttonAccept);
  808. return;
  809. void ButtonAccept (object sender, HandledEventArgs e)
  810. {
  811. buttonAccept++;
  812. }
  813. }
  814. [Fact]
  815. public void Accepted_Cancel_Event_HandlesCommand ()
  816. {
  817. //var super = new View ();
  818. var view = new TextField ();
  819. //super.Add (view);
  820. //var superAcceptedInvoked = false;
  821. var tfAcceptedInvoked = false;
  822. var handle = false;
  823. view.Accepted += TextViewAccept;
  824. Assert.False (view.InvokeCommand (Command.Accept));
  825. Assert.True (tfAcceptedInvoked);
  826. tfAcceptedInvoked = false;
  827. handle = true;
  828. view.Accepted += TextViewAccept;
  829. Assert.True (view.InvokeCommand (Command.Accept));
  830. Assert.True (tfAcceptedInvoked);
  831. return;
  832. void TextViewAccept (object sender, HandledEventArgs e)
  833. {
  834. tfAcceptedInvoked = true;
  835. e.Handled = handle;
  836. }
  837. }
  838. [Fact]
  839. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  840. {
  841. var top = new Toplevel ();
  842. var tf = new TextField { Width = 10 };
  843. top.Add (tf);
  844. Exception exception = Record.Exception (() => tf.SetFocus ());
  845. Assert.Null (exception);
  846. }
  847. [Fact]
  848. [TextFieldTestsAutoInitShutdown]
  849. public void Paste_Always_Clear_The_SelectedText ()
  850. {
  851. _textField.SelectedStart = 20;
  852. _textField.CursorPosition = 24;
  853. _textField.Copy ();
  854. Assert.Equal ("text", _textField.SelectedText);
  855. _textField.Paste ();
  856. Assert.Null (_textField.SelectedText);
  857. }
  858. [Fact]
  859. public void Backspace_From_End ()
  860. {
  861. var tf = new TextField { Text = "ABC" };
  862. tf.SetFocus ();
  863. Assert.Equal ("ABC", tf.Text);
  864. tf.BeginInit ();
  865. tf.EndInit ();
  866. Assert.Equal (3, tf.CursorPosition);
  867. // now delete the C
  868. tf.NewKeyDownEvent (Key.Backspace);
  869. Assert.Equal ("AB", tf.Text);
  870. Assert.Equal (2, tf.CursorPosition);
  871. // then delete the B
  872. tf.NewKeyDownEvent (Key.Backspace);
  873. Assert.Equal ("A", tf.Text);
  874. Assert.Equal (1, tf.CursorPosition);
  875. // then delete the A
  876. tf.NewKeyDownEvent (Key.Backspace);
  877. Assert.Equal ("", tf.Text);
  878. Assert.Equal (0, tf.CursorPosition);
  879. }
  880. [Fact]
  881. public void Backspace_From_Middle ()
  882. {
  883. var tf = new TextField { Text = "ABC" };
  884. tf.SetFocus ();
  885. tf.CursorPosition = 2;
  886. Assert.Equal ("ABC", tf.Text);
  887. // now delete the B
  888. tf.NewKeyDownEvent (Key.Backspace);
  889. Assert.Equal ("AC", tf.Text);
  890. // then delete the A
  891. tf.NewKeyDownEvent (Key.Backspace);
  892. Assert.Equal ("C", tf.Text);
  893. // then delete nothing
  894. tf.NewKeyDownEvent (Key.Backspace);
  895. Assert.Equal ("C", tf.Text);
  896. // now delete the C
  897. tf.CursorPosition = 1;
  898. tf.NewKeyDownEvent (Key.Backspace);
  899. Assert.Equal ("", tf.Text);
  900. }
  901. [Fact]
  902. [AutoInitShutdown]
  903. public void ScrollOffset_Initialize ()
  904. {
  905. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  906. tf.BeginInit ();
  907. tf.EndInit ();
  908. Assert.Equal (0, tf.ScrollOffset);
  909. Assert.Equal (16, tf.CursorPosition);
  910. }
  911. [Fact]
  912. [TextFieldTestsAutoInitShutdown]
  913. public void Selected_Text_Shows ()
  914. {
  915. // Proves #3022 is fixed (TextField selected text does not show in v2)
  916. _textField.CursorPosition = 0;
  917. var top = new Toplevel ();
  918. top.Add (_textField);
  919. RunState rs = Application.Begin (top);
  920. Attribute [] attributes =
  921. {
  922. _textField.ColorScheme.Focus,
  923. new (
  924. _textField.ColorScheme.Focus.Background,
  925. _textField.ColorScheme.Focus.Foreground
  926. )
  927. };
  928. // TAB to jump between text fields.
  929. TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
  930. // Cursor is at the end
  931. Assert.Equal (32, _textField.CursorPosition);
  932. _textField.CursorPosition = 0;
  933. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  934. var first = true;
  935. Application.RunIteration (ref rs, ref first);
  936. Assert.Equal (4, _textField.CursorPosition);
  937. // TAB to jump between text fields.
  938. TestHelpers.AssertDriverAttributesAre ("1111000", 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 MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
  1016. Application.OnMouseEvent (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.OnMouseEvent (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.OnMouseEvent (mouseEvent);
  1032. Assert.Equal (1, clickCounter);
  1033. top.Dispose ();
  1034. return;
  1035. void HandleRightClick (object sender, MouseEvent 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. bool 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 MouseEvent { 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 Size (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. tf.Draw ();
  1699. TestHelpers.AssertDriverContentsWithFrameAre (
  1700. @"
  1701. Les Miśerables",
  1702. output
  1703. );
  1704. }
  1705. private TextField GetTextFieldsInView ()
  1706. {
  1707. var tf = new TextField { Width = 10 };
  1708. var tf2 = new TextField { Y = 1, Width = 10 };
  1709. Toplevel top = new ();
  1710. top.Add (tf);
  1711. top.Add (tf2);
  1712. Application.Begin (top);
  1713. Assert.Same (tf, top.Focused);
  1714. return tf;
  1715. }
  1716. // This class enables test functions annotated with the [InitShutdown] attribute
  1717. // to have a function called before the test function is called and after.
  1718. //
  1719. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1720. // as a pair, and b) all unit test functions should be atomic.
  1721. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1722. public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
  1723. {
  1724. public override void After (MethodInfo methodUnderTest)
  1725. {
  1726. _textField.Dispose ();
  1727. _textField = null;
  1728. base.After (methodUnderTest);
  1729. }
  1730. public override void Before (MethodInfo methodUnderTest)
  1731. {
  1732. base.Before (methodUnderTest);
  1733. //Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
  1734. _textField = new ()
  1735. {
  1736. ColorScheme = new (Colors.ColorSchemes ["Base"]),
  1737. // 1 2 3
  1738. // 01234567890123456789012345678901=32 (Length)
  1739. Text = "TAB to jump between text fields.",
  1740. Width = 32
  1741. };
  1742. }
  1743. }
  1744. [Fact]
  1745. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  1746. {
  1747. View superView = new ()
  1748. {
  1749. CanFocus = true,
  1750. };
  1751. TextField t = new ();
  1752. superView.Add (t);
  1753. Assert.Single (superView.Subviews);
  1754. superView.BeginInit ();
  1755. superView.EndInit ();
  1756. Assert.Equal (2, superView.Subviews.Count);
  1757. }
  1758. [Fact]
  1759. public void Autocomplete__Added_To_SuperView_On_Add ()
  1760. {
  1761. View superView = new ()
  1762. {
  1763. CanFocus = true,
  1764. Id = "superView",
  1765. };
  1766. superView.BeginInit ();
  1767. superView.EndInit ();
  1768. Assert.Empty (superView.Subviews);
  1769. TextField t = new ()
  1770. {
  1771. Id = "t"
  1772. };
  1773. superView.Add (t);
  1774. Assert.Equal (2, superView.Subviews.Count);
  1775. }
  1776. [Fact]
  1777. public void Autocomplete_Visible_False_By_Default ()
  1778. {
  1779. View superView = new ()
  1780. {
  1781. CanFocus = true,
  1782. };
  1783. TextField t = new ();
  1784. superView.Add (t);
  1785. superView.BeginInit ();
  1786. superView.EndInit ();
  1787. Assert.Equal (2, superView.Subviews.Count);
  1788. Assert.True (t.Visible);
  1789. Assert.False (t.Autocomplete.Visible);
  1790. }
  1791. }