TextFieldTests.cs 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  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. [AutoInitShutdown (useFakeClipboard: true)]
  439. public void KeyBindings_Command ()
  440. {
  441. var tf = new TextField { Width = 20, Text = "This is a test." };
  442. tf.BeginInit ();
  443. tf.EndInit ();
  444. Assert.Equal (15, tf.Text.Length);
  445. Assert.Equal (15, tf.CursorPosition);
  446. Assert.False (tf.ReadOnly);
  447. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  448. Assert.Equal ("This is a test.", tf.Text);
  449. tf.CursorPosition = 0;
  450. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  451. Assert.Equal ("his is a test.", tf.Text);
  452. tf.ReadOnly = true;
  453. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
  454. Assert.Equal ("his is a test.", tf.Text);
  455. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  456. Assert.Equal ("his is a test.", tf.Text);
  457. tf.ReadOnly = false;
  458. tf.CursorPosition = 1;
  459. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  460. Assert.Equal ("is is a test.", tf.Text);
  461. tf.CursorPosition = 5;
  462. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift));
  463. Assert.Equal ("is is a test.", tf.Text);
  464. Assert.Equal ("is is", tf.SelectedText);
  465. tf.CursorPosition = 5;
  466. tf.SelectedStart = -1;
  467. Assert.Null (tf.SelectedText);
  468. Assert.True (tf.NewKeyDownEvent (Key.Home.WithShift.WithCtrl));
  469. Assert.Equal ("is is a test.", tf.Text);
  470. Assert.Equal ("is is", tf.SelectedText);
  471. tf.CursorPosition = 5;
  472. tf.SelectedStart = -1;
  473. Assert.Null (tf.SelectedText);
  474. Assert.True (tf.NewKeyDownEvent (Key.A.WithShift.WithCtrl));
  475. Assert.Equal ("is is a test.", tf.Text);
  476. Assert.Equal ("is is", tf.SelectedText);
  477. tf.CursorPosition = 5;
  478. tf.SelectedStart = -1;
  479. Assert.Null (tf.SelectedText);
  480. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift));
  481. Assert.Equal ("is is a test.", tf.Text);
  482. Assert.Equal (" a test.", tf.SelectedText);
  483. tf.CursorPosition = 5;
  484. tf.SelectedStart = -1;
  485. Assert.Null (tf.SelectedText);
  486. Assert.True (tf.NewKeyDownEvent (Key.End.WithShift.WithCtrl));
  487. Assert.Equal ("is is a test.", tf.Text);
  488. Assert.Equal (" a test.", tf.SelectedText);
  489. tf.CursorPosition = 5;
  490. tf.SelectedStart = -1;
  491. Assert.Null (tf.SelectedText);
  492. Assert.True (tf.NewKeyDownEvent (Key.E.WithShift.WithCtrl));
  493. Assert.Equal ("is is a test.", tf.Text);
  494. Assert.Equal (" a test.", tf.SelectedText);
  495. tf.CursorPosition = 5;
  496. tf.SelectedStart = -1;
  497. Assert.Null (tf.SelectedText);
  498. Assert.True (tf.NewKeyDownEvent (Key.Home));
  499. Assert.Equal ("is is a test.", tf.Text);
  500. Assert.Equal (0, tf.CursorPosition);
  501. tf.CursorPosition = 5;
  502. Assert.Null (tf.SelectedText);
  503. Assert.True (tf.NewKeyDownEvent (Key.Home.WithCtrl));
  504. Assert.Equal ("is is a test.", tf.Text);
  505. Assert.Equal (0, tf.CursorPosition);
  506. tf.CursorPosition = 5;
  507. Assert.Null (tf.SelectedText);
  508. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  509. Assert.Equal ("is is a test.", tf.Text);
  510. Assert.Equal (0, tf.CursorPosition);
  511. tf.CursorPosition = 5;
  512. tf.SelectedStart = -1;
  513. Assert.Null (tf.SelectedText);
  514. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  515. Assert.Equal ("is is a test.", tf.Text);
  516. Assert.Equal ("s", tf.SelectedText);
  517. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift));
  518. Assert.Equal ("is is a test.", tf.Text);
  519. Assert.Equal ("is", tf.SelectedText);
  520. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  521. Assert.Equal ("is is a test.", tf.Text);
  522. Assert.Equal ("s", tf.SelectedText);
  523. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift));
  524. Assert.Equal ("is is a test.", tf.Text);
  525. Assert.Null (tf.SelectedText);
  526. tf.CursorPosition = 7;
  527. tf.SelectedStart = -1;
  528. Assert.Null (tf.SelectedText);
  529. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift.WithCtrl));
  530. Assert.Equal ("is is a test.", tf.Text);
  531. Assert.Equal ("a", tf.SelectedText);
  532. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  533. Assert.Equal ("is is a test.", tf.Text);
  534. Assert.Equal ("is a", tf.SelectedText);
  535. #if UNIX_KEY_BINDINGS
  536. Assert.True (tf.NewKeyDownEvent (Key.B.WithShift.WithAlt));
  537. #else
  538. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithShift.WithCtrl));
  539. #endif
  540. Assert.Equal ("is is a test.", tf.Text);
  541. Assert.Equal ("is is a", tf.SelectedText);
  542. tf.CursorPosition = 3;
  543. tf.SelectedStart = -1;
  544. Assert.Null (tf.SelectedText);
  545. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  546. Assert.Equal ("is is a test.", tf.Text);
  547. Assert.Equal ("is ", tf.SelectedText);
  548. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithShift.WithCtrl));
  549. Assert.Equal ("is is a test.", tf.Text);
  550. Assert.Equal ("is a ", tf.SelectedText);
  551. #if UNIX_KEY_BINDINGS
  552. Assert.True (tf.NewKeyDownEvent (Key.F.WithShift.WithAlt));
  553. #else
  554. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  555. #endif
  556. Assert.Equal ("is is a test.", tf.Text);
  557. Assert.Equal ("is a test.", tf.SelectedText);
  558. Assert.Equal (13, tf.CursorPosition);
  559. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  560. Assert.Equal ("is is a test.", tf.Text);
  561. Assert.Null (tf.SelectedText);
  562. Assert.Equal (12, tf.CursorPosition);
  563. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  564. Assert.Equal ("is is a test.", tf.Text);
  565. Assert.Equal (11, tf.CursorPosition);
  566. Assert.True (tf.NewKeyDownEvent (Key.End));
  567. Assert.Equal ("is is a test.", tf.Text);
  568. Assert.Equal (13, tf.CursorPosition);
  569. tf.CursorPosition = 0;
  570. Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
  571. Assert.Equal ("is is a test.", tf.Text);
  572. Assert.Equal (13, tf.CursorPosition);
  573. tf.CursorPosition = 0;
  574. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  575. Assert.Equal ("is is a test.", tf.Text);
  576. Assert.Equal (13, tf.CursorPosition);
  577. tf.CursorPosition = 0;
  578. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  579. Assert.Equal ("is is a test.", tf.Text);
  580. Assert.Equal (1, tf.CursorPosition);
  581. Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
  582. Assert.Equal ("is is a test.", tf.Text);
  583. Assert.Equal (2, tf.CursorPosition);
  584. tf.CursorPosition = 9;
  585. tf.ReadOnly = true;
  586. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  587. Assert.Equal ("is is a test.", tf.Text);
  588. tf.ReadOnly = false;
  589. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  590. Assert.Equal ("is is a t", tf.Text);
  591. Assert.Equal ("est.", Clipboard.Contents);
  592. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  593. Assert.Equal ("is is a test.", tf.Text);
  594. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  595. Assert.Equal ("is is a t", tf.Text);
  596. #if UNIX_KEY_BINDINGS
  597. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
  598. #else
  599. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  600. #endif
  601. Assert.Equal ("is is a test.", tf.Text);
  602. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  603. Assert.Equal ("is is a t", tf.Text);
  604. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  605. Assert.Equal ("is is a t", tf.Text);
  606. Assert.Equal (8, tf.CursorPosition);
  607. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  608. Assert.Equal ("is is a t", tf.Text);
  609. Assert.Equal (6, tf.CursorPosition);
  610. #if UNIX_KEY_BINDINGS
  611. Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
  612. #else
  613. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  614. #endif
  615. Assert.Equal ("is is a t", tf.Text);
  616. Assert.Equal (3, tf.CursorPosition);
  617. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  618. Assert.Equal ("is is a t", tf.Text);
  619. Assert.Equal (6, tf.CursorPosition);
  620. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  621. Assert.Equal ("is is a t", tf.Text);
  622. Assert.Equal (8, tf.CursorPosition);
  623. #if UNIX_KEY_BINDINGS
  624. Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
  625. #else
  626. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  627. #endif
  628. Assert.Equal ("is is a t", tf.Text);
  629. Assert.Equal (9, tf.CursorPosition);
  630. Assert.True (tf.Used);
  631. Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
  632. Assert.Equal ("is is a t", tf.Text);
  633. Assert.Equal (9, tf.CursorPosition);
  634. Assert.False (tf.Used);
  635. tf.SelectedStart = 3;
  636. tf.CursorPosition = 7;
  637. Assert.Equal ("is a", tf.SelectedText);
  638. Assert.Equal ("est.", Clipboard.Contents);
  639. Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
  640. Assert.Equal ("is is a t", tf.Text);
  641. Assert.Equal ("is a", Clipboard.Contents);
  642. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  643. Assert.Equal ("is t", tf.Text);
  644. Assert.Equal ("is a", Clipboard.Contents);
  645. Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
  646. Assert.Equal ("is is a t", tf.Text);
  647. Assert.Equal ("is a", Clipboard.Contents);
  648. Assert.Equal (7, tf.CursorPosition);
  649. #if UNIX_KEY_BINDINGS
  650. Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
  651. #else
  652. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
  653. #endif
  654. Assert.Equal (" t", tf.Text);
  655. Assert.Equal ("is is a", Clipboard.Contents);
  656. tf.Text = "TAB to jump between text fields.";
  657. Assert.Equal (0, tf.CursorPosition);
  658. Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
  659. Assert.Equal ("to jump between text fields.", tf.Text);
  660. tf.CursorPosition = tf.Text.Length;
  661. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
  662. Assert.Equal ("to jump between text fields", tf.Text);
  663. Assert.True (tf.NewKeyDownEvent (Key.T.WithCtrl));
  664. Assert.Equal ("to jump between text fields", tf.SelectedText);
  665. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
  666. Assert.Equal ("", tf.Text);
  667. }
  668. [Fact]
  669. public void HotKey_Command_SetsFocus ()
  670. {
  671. var view = new TextField ();
  672. view.CanFocus = true;
  673. Assert.False (view.HasFocus);
  674. view.InvokeCommand (Command.HotKey);
  675. Assert.True (view.HasFocus);
  676. }
  677. [Fact]
  678. public void HotKey_Command_Does_Not_Accept ()
  679. {
  680. var view = new TextField ();
  681. var accepted = false;
  682. view.Accepted += OnAccept;
  683. view.InvokeCommand (Command.HotKey);
  684. Assert.False (accepted);
  685. return;
  686. void OnAccept (object sender, HandledEventArgs e) { accepted = true; }
  687. }
  688. [Fact]
  689. public void Accept_Command_Fires_Accept ()
  690. {
  691. var view = new TextField ();
  692. var accepted = false;
  693. view.Accepted += Accept;
  694. view.InvokeCommand (Command.Accept);
  695. Assert.True (accepted);
  696. return;
  697. void Accept (object sender, HandledEventArgs e) { accepted = true; }
  698. }
  699. [Theory]
  700. [InlineData (false, 1)]
  701. [InlineData (true, 0)]
  702. public void Accept_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
  703. {
  704. var superView = new Window ()
  705. {
  706. Id = "superView"
  707. };
  708. var tf = new TextField ()
  709. {
  710. Id = "tf"
  711. };
  712. var button = new Button ()
  713. {
  714. Id = "button",
  715. IsDefault = true,
  716. };
  717. superView.Add (tf, button);
  718. var buttonAccept = 0;
  719. button.Accepted += ButtonAccept;
  720. var textFieldAccept = 0;
  721. tf.Accepted += TextFieldAccept;
  722. tf.SetFocus ();
  723. Assert.True (tf.HasFocus);
  724. superView.NewKeyDownEvent (Key.Enter);
  725. Assert.Equal (1, textFieldAccept);
  726. Assert.Equal (expectedButtonAccepts, buttonAccept);
  727. button.SetFocus ();
  728. superView.NewKeyDownEvent (Key.Enter);
  729. Assert.Equal (1, textFieldAccept);
  730. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  731. return;
  732. void TextFieldAccept (object sender, HandledEventArgs e)
  733. {
  734. textFieldAccept++;
  735. e.Handled = handleAccept;
  736. }
  737. void ButtonAccept (object sender, HandledEventArgs e)
  738. {
  739. buttonAccept++;
  740. }
  741. }
  742. [Fact]
  743. public void Accept_No_Handler_Enables_Default_Button_Accept ()
  744. {
  745. var superView = new Window ()
  746. {
  747. Id = "superView"
  748. };
  749. var tf = new TextField ()
  750. {
  751. Id = "tf"
  752. };
  753. var button = new Button ()
  754. {
  755. Id="button",
  756. IsDefault = true,
  757. };
  758. superView.Add (tf, button);
  759. var buttonAccept = 0;
  760. button.Accepted += ButtonAccept;
  761. tf.SetFocus ();
  762. Assert.True (tf.HasFocus);
  763. superView.NewKeyDownEvent (Key.Enter);
  764. Assert.Equal (1, buttonAccept);
  765. button.SetFocus ();
  766. superView.NewKeyDownEvent (Key.Enter);
  767. Assert.Equal (2, buttonAccept);
  768. return;
  769. void ButtonAccept (object sender, HandledEventArgs e)
  770. {
  771. buttonAccept++;
  772. }
  773. }
  774. [Fact]
  775. public void Accept_Cancel_Event_HandlesCommand ()
  776. {
  777. //var super = new View ();
  778. var view = new TextField ();
  779. //super.Add (view);
  780. //var superAcceptedInvoked = false;
  781. var tfAcceptedInvoked = false;
  782. var handle = false;
  783. view.Accepted += TextViewAccept;
  784. Assert.False (view.InvokeCommand (Command.Accept));
  785. Assert.True (tfAcceptedInvoked);
  786. tfAcceptedInvoked = false;
  787. handle = true;
  788. view.Accepted += TextViewAccept;
  789. Assert.True (view.InvokeCommand (Command.Accept));
  790. Assert.True (tfAcceptedInvoked);
  791. return;
  792. void TextViewAccept (object sender, HandledEventArgs e)
  793. {
  794. tfAcceptedInvoked = true;
  795. e.Handled = handle;
  796. }
  797. }
  798. [Fact]
  799. public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
  800. {
  801. var top = new Toplevel ();
  802. var tf = new TextField { Width = 10 };
  803. top.Add (tf);
  804. Exception exception = Record.Exception (() => tf.SetFocus ());
  805. Assert.Null (exception);
  806. }
  807. [Fact]
  808. [TextFieldTestsAutoInitShutdown]
  809. public void Paste_Always_Clear_The_SelectedText ()
  810. {
  811. _textField.SelectedStart = 20;
  812. _textField.CursorPosition = 24;
  813. _textField.Copy ();
  814. Assert.Equal ("text", _textField.SelectedText);
  815. _textField.Paste ();
  816. Assert.Null (_textField.SelectedText);
  817. }
  818. [Fact]
  819. public void Backspace_From_End ()
  820. {
  821. var tf = new TextField { Text = "ABC" };
  822. tf.SetFocus ();
  823. Assert.Equal ("ABC", tf.Text);
  824. tf.BeginInit ();
  825. tf.EndInit ();
  826. Assert.Equal (3, tf.CursorPosition);
  827. // now delete the C
  828. tf.NewKeyDownEvent (Key.Backspace);
  829. Assert.Equal ("AB", tf.Text);
  830. Assert.Equal (2, tf.CursorPosition);
  831. // then delete the B
  832. tf.NewKeyDownEvent (Key.Backspace);
  833. Assert.Equal ("A", tf.Text);
  834. Assert.Equal (1, tf.CursorPosition);
  835. // then delete the A
  836. tf.NewKeyDownEvent (Key.Backspace);
  837. Assert.Equal ("", tf.Text);
  838. Assert.Equal (0, tf.CursorPosition);
  839. }
  840. [Fact]
  841. public void Backspace_From_Middle ()
  842. {
  843. var tf = new TextField { Text = "ABC" };
  844. tf.SetFocus ();
  845. tf.CursorPosition = 2;
  846. Assert.Equal ("ABC", tf.Text);
  847. // now delete the B
  848. tf.NewKeyDownEvent (Key.Backspace);
  849. Assert.Equal ("AC", tf.Text);
  850. // then delete the A
  851. tf.NewKeyDownEvent (Key.Backspace);
  852. Assert.Equal ("C", tf.Text);
  853. // then delete nothing
  854. tf.NewKeyDownEvent (Key.Backspace);
  855. Assert.Equal ("C", tf.Text);
  856. // now delete the C
  857. tf.CursorPosition = 1;
  858. tf.NewKeyDownEvent (Key.Backspace);
  859. Assert.Equal ("", tf.Text);
  860. }
  861. [Fact]
  862. [AutoInitShutdown]
  863. public void ScrollOffset_Initialize ()
  864. {
  865. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  866. tf.BeginInit ();
  867. tf.EndInit ();
  868. Assert.Equal (0, tf.ScrollOffset);
  869. Assert.Equal (16, tf.CursorPosition);
  870. }
  871. [Fact]
  872. [TextFieldTestsAutoInitShutdown]
  873. public void Selected_Text_Shows ()
  874. {
  875. // Proves #3022 is fixed (TextField selected text does not show in v2)
  876. _textField.CursorPosition = 0;
  877. var top = new Toplevel ();
  878. top.Add (_textField);
  879. RunState rs = Application.Begin (top);
  880. Attribute [] attributes =
  881. {
  882. _textField.ColorScheme.Focus,
  883. new (
  884. _textField.ColorScheme.Focus.Background,
  885. _textField.ColorScheme.Focus.Foreground
  886. )
  887. };
  888. // TAB to jump between text fields.
  889. TestHelpers.AssertDriverAttributesAre ("0000000", Application.Driver, attributes);
  890. // Cursor is at the end
  891. Assert.Equal (32, _textField.CursorPosition);
  892. _textField.CursorPosition = 0;
  893. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  894. var first = true;
  895. Application.RunIteration (ref rs, ref first);
  896. Assert.Equal (4, _textField.CursorPosition);
  897. // TAB to jump between text fields.
  898. TestHelpers.AssertDriverAttributesAre ("1111000", Application.Driver, attributes);
  899. top.Dispose ();
  900. }
  901. [Fact]
  902. [TextFieldTestsAutoInitShutdown]
  903. public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  904. {
  905. _textField.CursorPosition = 33;
  906. _textField.SelectedStart = 33;
  907. Assert.Equal (32, _textField.CursorPosition);
  908. Assert.Equal (32, _textField.SelectedStart);
  909. Assert.Equal (0, _textField.SelectedLength);
  910. Assert.Null (_textField.SelectedText);
  911. }
  912. [Fact]
  913. [TextFieldTestsAutoInitShutdown]
  914. public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
  915. {
  916. _textField.SelectedStart = 19;
  917. _textField.CursorPosition = 12;
  918. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  919. _textField.NewKeyDownEvent (Key.U); // u
  920. Assert.Equal ("TAB to jump u text fields.", _textField.Text);
  921. }
  922. [Fact]
  923. [TextFieldTestsAutoInitShutdown]
  924. public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  925. {
  926. _textField.CursorPosition = 2;
  927. _textField.SelectedStart = 33;
  928. Assert.Equal (32, _textField.SelectedStart);
  929. Assert.Equal (30, _textField.SelectedLength);
  930. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  931. }
  932. [Fact]
  933. [TextFieldTestsAutoInitShutdown]
  934. public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
  935. {
  936. _textField.SelectedStart = -2;
  937. Assert.Equal (-1, _textField.SelectedStart);
  938. Assert.Equal (0, _textField.SelectedLength);
  939. Assert.Null (_textField.SelectedText);
  940. }
  941. [Fact]
  942. public void KeyDown_Handled_Prevents_Input ()
  943. {
  944. var tf = new TextField ();
  945. tf.KeyDown += HandleJKey;
  946. tf.NewKeyDownEvent (Key.A);
  947. Assert.Equal ("a", tf.Text);
  948. // SuppressKey suppresses the 'j' key
  949. tf.NewKeyDownEvent (Key.J);
  950. Assert.Equal ("a", tf.Text);
  951. tf.KeyDown -= HandleJKey;
  952. // Now that the delegate has been removed we can type j again
  953. tf.NewKeyDownEvent (Key.J);
  954. Assert.Equal ("aj", tf.Text);
  955. return;
  956. void HandleJKey (object s, Key arg)
  957. {
  958. if (arg.AsRune == new Rune ('j'))
  959. {
  960. arg.Handled = true;
  961. }
  962. }
  963. }
  964. [Fact]
  965. [AutoInitShutdown]
  966. public void MouseEvent_Handled_Prevents_RightClick ()
  967. {
  968. Application.MouseEvent += HandleRightClick;
  969. var tf = new TextField { Width = 10 };
  970. var clickCounter = 0;
  971. tf.MouseClick += (s, m) => { clickCounter++; };
  972. var top = new Toplevel ();
  973. top.Add (tf);
  974. Application.Begin (top);
  975. var mouseEvent = new MouseEvent { Flags = MouseFlags.Button1Clicked, View = tf };
  976. Application.OnMouseEvent (mouseEvent);
  977. Assert.Equal (1, clickCounter);
  978. // Get a fresh instance that represents a right click.
  979. // Should be ignored because of SuppressRightClick callback
  980. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  981. Application.OnMouseEvent (mouseEvent);
  982. Assert.Equal (1, clickCounter);
  983. Application.MouseEvent -= HandleRightClick;
  984. // Get a fresh instance that represents a right click.
  985. // Should no longer be ignored as the callback was removed
  986. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  987. // In #3183 OnMouseClicked is no longer called before MouseEvent().
  988. // This call causes the context menu to pop, and MouseEvent() returns true.
  989. // Thus, the clickCounter is NOT incremented.
  990. // Which is correct, because the user did NOT click with the left mouse button.
  991. Application.OnMouseEvent (mouseEvent);
  992. Assert.Equal (1, clickCounter);
  993. top.Dispose ();
  994. return;
  995. void HandleRightClick (object sender, MouseEvent arg)
  996. {
  997. if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
  998. {
  999. arg.Handled = true;
  1000. }
  1001. }
  1002. }
  1003. [InlineData ("a")] // Lower than selection
  1004. [InlineData ("aaaaaaaaaaa")] // Greater than selection
  1005. [InlineData ("aaaa")] // Equal than selection
  1006. [Theory]
  1007. public void SetTextAndMoveCursorToEnd_WhenExistingSelection (string newText)
  1008. {
  1009. var tf = new TextField ();
  1010. tf.Text = "fish";
  1011. tf.CursorPosition = tf.Text.Length;
  1012. tf.NewKeyDownEvent (Key.CursorLeft);
  1013. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1014. tf.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1015. Assert.Equal (1, tf.CursorPosition);
  1016. Assert.Equal (2, tf.SelectedLength);
  1017. Assert.Equal ("is", tf.SelectedText);
  1018. tf.Text = newText;
  1019. tf.CursorPosition = tf.Text.Length;
  1020. Assert.Equal (newText.Length, tf.CursorPosition);
  1021. Assert.Equal (0, tf.SelectedLength);
  1022. Assert.Null (tf.SelectedText);
  1023. }
  1024. [Fact]
  1025. [TextFieldTestsAutoInitShutdown]
  1026. public void Text_Replaces_Tabs_With_Empty_String ()
  1027. {
  1028. _textField.Text = "\t\tTAB to jump between text fields.";
  1029. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1030. _textField.Text = "";
  1031. Clipboard.Contents = "\t\tTAB to jump between text fields.";
  1032. _textField.Paste ();
  1033. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1034. }
  1035. [Fact]
  1036. [TextFieldTestsAutoInitShutdown]
  1037. public void TextChanged_Event ()
  1038. {
  1039. bool eventFired = false;
  1040. _textField.TextChanged += (s, e) => eventFired = true;
  1041. _textField.Text = "changed";
  1042. Assert.True (eventFired);
  1043. Assert.Equal ("changed", _textField.Text);
  1044. }
  1045. [Fact]
  1046. [TextFieldTestsAutoInitShutdown]
  1047. public void TextChanging_Event ()
  1048. {
  1049. var cancel = true;
  1050. _textField.TextChanging += (s, e) =>
  1051. {
  1052. Assert.Equal ("changing", e.NewValue);
  1053. if (cancel)
  1054. {
  1055. e.Cancel = true;
  1056. }
  1057. };
  1058. _textField.Text = "changing";
  1059. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1060. cancel = false;
  1061. _textField.Text = "changing";
  1062. Assert.Equal ("changing", _textField.Text);
  1063. }
  1064. [Fact]
  1065. public void SpaceHandling ()
  1066. {
  1067. var tf = new TextField { Width = 10, Text = " " };
  1068. var ev = new MouseEvent { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked };
  1069. tf.NewMouseEvent (ev);
  1070. Assert.Equal (1, tf.SelectedLength);
  1071. ev = new () { Position = new (1, 0), Flags = MouseFlags.Button1DoubleClicked };
  1072. tf.NewMouseEvent (ev);
  1073. Assert.Equal (1, tf.SelectedLength);
  1074. }
  1075. [Fact]
  1076. [TextFieldTestsAutoInitShutdown]
  1077. public void Used_Is_False ()
  1078. {
  1079. _textField.Used = false;
  1080. _textField.CursorPosition = 10;
  1081. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1082. _textField.NewKeyDownEvent (Key.U); // u
  1083. Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
  1084. _textField.NewKeyDownEvent (Key.S); // s
  1085. Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
  1086. _textField.NewKeyDownEvent (Key.E); // e
  1087. Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
  1088. _textField.NewKeyDownEvent (Key.D); // d
  1089. Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
  1090. }
  1091. [Fact]
  1092. [TextFieldTestsAutoInitShutdown]
  1093. public void Used_Is_True_By_Default ()
  1094. {
  1095. _textField.CursorPosition = 10;
  1096. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  1097. _textField.NewKeyDownEvent (Key.U); // u
  1098. Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
  1099. _textField.NewKeyDownEvent (Key.S); // s
  1100. Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
  1101. _textField.NewKeyDownEvent (Key.E); // e
  1102. Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
  1103. _textField.NewKeyDownEvent (Key.D); // d
  1104. Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
  1105. }
  1106. [Fact]
  1107. [TextFieldTestsAutoInitShutdown]
  1108. public void WordBackward_With_No_Selection ()
  1109. {
  1110. _textField.CursorPosition = _textField.Text.Length;
  1111. var iteration = 0;
  1112. while (_textField.CursorPosition > 0)
  1113. {
  1114. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1115. switch (iteration)
  1116. {
  1117. case 0:
  1118. Assert.Equal (31, _textField.CursorPosition);
  1119. Assert.Equal (-1, _textField.SelectedStart);
  1120. Assert.Equal (0, _textField.SelectedLength);
  1121. Assert.Null (_textField.SelectedText);
  1122. break;
  1123. case 1:
  1124. Assert.Equal (25, _textField.CursorPosition);
  1125. Assert.Equal (-1, _textField.SelectedStart);
  1126. Assert.Equal (0, _textField.SelectedLength);
  1127. Assert.Null (_textField.SelectedText);
  1128. break;
  1129. case 2:
  1130. Assert.Equal (20, _textField.CursorPosition);
  1131. Assert.Equal (-1, _textField.SelectedStart);
  1132. Assert.Equal (0, _textField.SelectedLength);
  1133. Assert.Null (_textField.SelectedText);
  1134. break;
  1135. case 3:
  1136. Assert.Equal (12, _textField.CursorPosition);
  1137. Assert.Equal (-1, _textField.SelectedStart);
  1138. Assert.Equal (0, _textField.SelectedLength);
  1139. Assert.Null (_textField.SelectedText);
  1140. break;
  1141. case 4:
  1142. Assert.Equal (7, _textField.CursorPosition);
  1143. Assert.Equal (-1, _textField.SelectedStart);
  1144. Assert.Equal (0, _textField.SelectedLength);
  1145. Assert.Null (_textField.SelectedText);
  1146. break;
  1147. case 5:
  1148. Assert.Equal (4, _textField.CursorPosition);
  1149. Assert.Equal (-1, _textField.SelectedStart);
  1150. Assert.Equal (0, _textField.SelectedLength);
  1151. Assert.Null (_textField.SelectedText);
  1152. break;
  1153. case 6:
  1154. Assert.Equal (0, _textField.CursorPosition);
  1155. Assert.Equal (-1, _textField.SelectedStart);
  1156. Assert.Equal (0, _textField.SelectedLength);
  1157. Assert.Null (_textField.SelectedText);
  1158. break;
  1159. }
  1160. iteration++;
  1161. }
  1162. }
  1163. [Fact]
  1164. [TextFieldTestsAutoInitShutdown]
  1165. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1166. {
  1167. // 1 2 3 4 5
  1168. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1169. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1170. _textField.CursorPosition = _textField.Text.Length;
  1171. var iteration = 0;
  1172. while (_textField.CursorPosition > 0)
  1173. {
  1174. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1175. switch (iteration)
  1176. {
  1177. case 0:
  1178. Assert.Equal (54, _textField.CursorPosition);
  1179. Assert.Equal (-1, _textField.SelectedStart);
  1180. Assert.Equal (0, _textField.SelectedLength);
  1181. Assert.Null (_textField.SelectedText);
  1182. break;
  1183. case 1:
  1184. Assert.Equal (48, _textField.CursorPosition);
  1185. Assert.Equal (-1, _textField.SelectedStart);
  1186. Assert.Equal (0, _textField.SelectedLength);
  1187. Assert.Null (_textField.SelectedText);
  1188. break;
  1189. case 2:
  1190. Assert.Equal (46, _textField.CursorPosition);
  1191. Assert.Equal (-1, _textField.SelectedStart);
  1192. Assert.Equal (0, _textField.SelectedLength);
  1193. Assert.Null (_textField.SelectedText);
  1194. break;
  1195. case 3:
  1196. Assert.Equal (40, _textField.CursorPosition);
  1197. Assert.Equal (-1, _textField.SelectedStart);
  1198. Assert.Equal (0, _textField.SelectedLength);
  1199. Assert.Null (_textField.SelectedText);
  1200. break;
  1201. case 4:
  1202. Assert.Equal (38, _textField.CursorPosition);
  1203. Assert.Equal (-1, _textField.SelectedStart);
  1204. Assert.Equal (0, _textField.SelectedLength);
  1205. Assert.Null (_textField.SelectedText);
  1206. break;
  1207. case 5:
  1208. Assert.Equal (28, _textField.CursorPosition);
  1209. Assert.Equal (-1, _textField.SelectedStart);
  1210. Assert.Equal (0, _textField.SelectedLength);
  1211. Assert.Null (_textField.SelectedText);
  1212. break;
  1213. case 6:
  1214. Assert.Equal (25, _textField.CursorPosition);
  1215. Assert.Equal (-1, _textField.SelectedStart);
  1216. Assert.Equal (0, _textField.SelectedLength);
  1217. Assert.Null (_textField.SelectedText);
  1218. break;
  1219. case 7:
  1220. Assert.Equal (12, _textField.CursorPosition);
  1221. Assert.Equal (-1, _textField.SelectedStart);
  1222. Assert.Equal (0, _textField.SelectedLength);
  1223. Assert.Null (_textField.SelectedText);
  1224. break;
  1225. case 8:
  1226. Assert.Equal (9, _textField.CursorPosition);
  1227. Assert.Equal (-1, _textField.SelectedStart);
  1228. Assert.Equal (0, _textField.SelectedLength);
  1229. Assert.Null (_textField.SelectedText);
  1230. break;
  1231. case 9:
  1232. Assert.Equal (6, _textField.CursorPosition);
  1233. Assert.Equal (-1, _textField.SelectedStart);
  1234. Assert.Equal (0, _textField.SelectedLength);
  1235. Assert.Null (_textField.SelectedText);
  1236. break;
  1237. case 10:
  1238. Assert.Equal (0, _textField.CursorPosition);
  1239. Assert.Equal (-1, _textField.SelectedStart);
  1240. Assert.Equal (0, _textField.SelectedLength);
  1241. Assert.Null (_textField.SelectedText);
  1242. break;
  1243. }
  1244. iteration++;
  1245. }
  1246. }
  1247. [Fact]
  1248. [TextFieldTestsAutoInitShutdown]
  1249. public void WordBackward_With_Selection ()
  1250. {
  1251. _textField.CursorPosition = _textField.Text.Length;
  1252. _textField.SelectedStart = _textField.Text.Length;
  1253. var iteration = 0;
  1254. while (_textField.CursorPosition > 0)
  1255. {
  1256. _textField.NewKeyDownEvent (
  1257. new (
  1258. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1259. )
  1260. );
  1261. switch (iteration)
  1262. {
  1263. case 0:
  1264. Assert.Equal (31, _textField.CursorPosition);
  1265. Assert.Equal (32, _textField.SelectedStart);
  1266. Assert.Equal (1, _textField.SelectedLength);
  1267. Assert.Equal (".", _textField.SelectedText);
  1268. break;
  1269. case 1:
  1270. Assert.Equal (25, _textField.CursorPosition);
  1271. Assert.Equal (32, _textField.SelectedStart);
  1272. Assert.Equal (7, _textField.SelectedLength);
  1273. Assert.Equal ("fields.", _textField.SelectedText);
  1274. break;
  1275. case 2:
  1276. Assert.Equal (20, _textField.CursorPosition);
  1277. Assert.Equal (32, _textField.SelectedStart);
  1278. Assert.Equal (12, _textField.SelectedLength);
  1279. Assert.Equal ("text fields.", _textField.SelectedText);
  1280. break;
  1281. case 3:
  1282. Assert.Equal (12, _textField.CursorPosition);
  1283. Assert.Equal (32, _textField.SelectedStart);
  1284. Assert.Equal (20, _textField.SelectedLength);
  1285. Assert.Equal ("between text fields.", _textField.SelectedText);
  1286. break;
  1287. case 4:
  1288. Assert.Equal (7, _textField.CursorPosition);
  1289. Assert.Equal (32, _textField.SelectedStart);
  1290. Assert.Equal (25, _textField.SelectedLength);
  1291. Assert.Equal ("jump between text fields.", _textField.SelectedText);
  1292. break;
  1293. case 5:
  1294. Assert.Equal (4, _textField.CursorPosition);
  1295. Assert.Equal (32, _textField.SelectedStart);
  1296. Assert.Equal (28, _textField.SelectedLength);
  1297. Assert.Equal ("to jump between text fields.", _textField.SelectedText);
  1298. break;
  1299. case 6:
  1300. Assert.Equal (0, _textField.CursorPosition);
  1301. Assert.Equal (32, _textField.SelectedStart);
  1302. Assert.Equal (32, _textField.SelectedLength);
  1303. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1304. break;
  1305. }
  1306. iteration++;
  1307. }
  1308. }
  1309. [Fact]
  1310. [TextFieldTestsAutoInitShutdown]
  1311. public void
  1312. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1313. {
  1314. _textField.CursorPosition = 10;
  1315. _textField.SelectedStart = 10;
  1316. var iteration = 0;
  1317. while (_textField.CursorPosition > 0)
  1318. {
  1319. _textField.NewKeyDownEvent (
  1320. new (
  1321. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1322. )
  1323. );
  1324. switch (iteration)
  1325. {
  1326. case 0:
  1327. Assert.Equal (7, _textField.CursorPosition);
  1328. Assert.Equal (10, _textField.SelectedStart);
  1329. Assert.Equal (3, _textField.SelectedLength);
  1330. Assert.Equal ("jum", _textField.SelectedText);
  1331. break;
  1332. case 1:
  1333. Assert.Equal (4, _textField.CursorPosition);
  1334. Assert.Equal (10, _textField.SelectedStart);
  1335. Assert.Equal (6, _textField.SelectedLength);
  1336. Assert.Equal ("to jum", _textField.SelectedText);
  1337. break;
  1338. case 2:
  1339. Assert.Equal (0, _textField.CursorPosition);
  1340. Assert.Equal (10, _textField.SelectedStart);
  1341. Assert.Equal (10, _textField.SelectedLength);
  1342. Assert.Equal ("TAB to jum", _textField.SelectedText);
  1343. break;
  1344. }
  1345. iteration++;
  1346. }
  1347. }
  1348. [Fact]
  1349. public void WordBackward_WordForward_Mixed ()
  1350. {
  1351. var tf = new TextField { Width = 30, Text = "Test with0. and!.?;-@+" };
  1352. tf.BeginInit ();
  1353. tf.EndInit ();
  1354. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1355. Assert.Equal (15, tf.CursorPosition);
  1356. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1357. Assert.Equal (12, tf.CursorPosition);
  1358. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1359. Assert.Equal (10, tf.CursorPosition);
  1360. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1361. Assert.Equal (5, tf.CursorPosition);
  1362. tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  1363. Assert.Equal (0, tf.CursorPosition);
  1364. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1365. Assert.Equal (5, tf.CursorPosition);
  1366. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1367. Assert.Equal (10, tf.CursorPosition);
  1368. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1369. Assert.Equal (12, tf.CursorPosition);
  1370. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1371. Assert.Equal (15, tf.CursorPosition);
  1372. tf.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1373. Assert.Equal (22, tf.CursorPosition);
  1374. }
  1375. [Fact]
  1376. public void WordBackward_WordForward_SelectedText_With_Accent ()
  1377. {
  1378. var text = "Les Misérables movie.";
  1379. var tf = new TextField { Width = 30, Text = text };
  1380. Assert.Equal (21, text.Length);
  1381. Assert.Equal (21, tf.Text.GetRuneCount ());
  1382. Assert.Equal (21, tf.Text.GetColumns ());
  1383. List<Rune> runes = tf.Text.ToRuneList ();
  1384. Assert.Equal (21, runes.Count);
  1385. Assert.Equal (21, tf.Text.Length);
  1386. for (var i = 0; i < runes.Count; i++)
  1387. {
  1388. char cs = text [i];
  1389. var cus = (char)runes [i].Value;
  1390. Assert.Equal (cs, cus);
  1391. }
  1392. var idx = 15;
  1393. Assert.Equal ('m', text [idx]);
  1394. Assert.Equal ('m', (char)runes [idx].Value);
  1395. Assert.Equal ("m", runes [idx].ToString ());
  1396. Assert.True (
  1397. tf.NewMouseEvent (
  1398. new () { Position = new (idx, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1399. )
  1400. );
  1401. Assert.Equal ("movie.", tf.SelectedText);
  1402. Assert.True (
  1403. tf.NewMouseEvent (
  1404. new () { Position = new (idx + 1, 1), Flags = MouseFlags.Button1DoubleClicked, View = tf }
  1405. )
  1406. );
  1407. Assert.Equal ("movie.", tf.SelectedText);
  1408. }
  1409. [Fact]
  1410. [TextFieldTestsAutoInitShutdown]
  1411. public void WordForward_With_No_Selection ()
  1412. {
  1413. _textField.CursorPosition = 0;
  1414. var iteration = 0;
  1415. while (_textField.CursorPosition < _textField.Text.Length)
  1416. {
  1417. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1418. switch (iteration)
  1419. {
  1420. case 0:
  1421. Assert.Equal (4, _textField.CursorPosition);
  1422. Assert.Equal (-1, _textField.SelectedStart);
  1423. Assert.Equal (0, _textField.SelectedLength);
  1424. Assert.Null (_textField.SelectedText);
  1425. break;
  1426. case 1:
  1427. Assert.Equal (7, _textField.CursorPosition);
  1428. Assert.Equal (-1, _textField.SelectedStart);
  1429. Assert.Equal (0, _textField.SelectedLength);
  1430. Assert.Null (_textField.SelectedText);
  1431. break;
  1432. case 2:
  1433. Assert.Equal (12, _textField.CursorPosition);
  1434. Assert.Equal (-1, _textField.SelectedStart);
  1435. Assert.Equal (0, _textField.SelectedLength);
  1436. Assert.Null (_textField.SelectedText);
  1437. break;
  1438. case 3:
  1439. Assert.Equal (20, _textField.CursorPosition);
  1440. Assert.Equal (-1, _textField.SelectedStart);
  1441. Assert.Equal (0, _textField.SelectedLength);
  1442. Assert.Null (_textField.SelectedText);
  1443. break;
  1444. case 4:
  1445. Assert.Equal (25, _textField.CursorPosition);
  1446. Assert.Equal (-1, _textField.SelectedStart);
  1447. Assert.Equal (0, _textField.SelectedLength);
  1448. Assert.Null (_textField.SelectedText);
  1449. break;
  1450. case 5:
  1451. Assert.Equal (32, _textField.CursorPosition);
  1452. Assert.Equal (-1, _textField.SelectedStart);
  1453. Assert.Equal (0, _textField.SelectedLength);
  1454. Assert.Null (_textField.SelectedText);
  1455. break;
  1456. }
  1457. iteration++;
  1458. }
  1459. }
  1460. [Fact]
  1461. [TextFieldTestsAutoInitShutdown]
  1462. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1463. {
  1464. // 1 2 3 4 5
  1465. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1466. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1467. _textField.CursorPosition = 0;
  1468. var iteration = 0;
  1469. while (_textField.CursorPosition < _textField.Text.Length)
  1470. {
  1471. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1472. switch (iteration)
  1473. {
  1474. case 0:
  1475. Assert.Equal (6, _textField.CursorPosition);
  1476. Assert.Equal (-1, _textField.SelectedStart);
  1477. Assert.Equal (0, _textField.SelectedLength);
  1478. Assert.Null (_textField.SelectedText);
  1479. break;
  1480. case 1:
  1481. Assert.Equal (9, _textField.CursorPosition);
  1482. Assert.Equal (-1, _textField.SelectedStart);
  1483. Assert.Equal (0, _textField.SelectedLength);
  1484. Assert.Null (_textField.SelectedText);
  1485. break;
  1486. case 2:
  1487. Assert.Equal (12, _textField.CursorPosition);
  1488. Assert.Equal (-1, _textField.SelectedStart);
  1489. Assert.Equal (0, _textField.SelectedLength);
  1490. Assert.Null (_textField.SelectedText);
  1491. break;
  1492. case 3:
  1493. Assert.Equal (25, _textField.CursorPosition);
  1494. Assert.Equal (-1, _textField.SelectedStart);
  1495. Assert.Equal (0, _textField.SelectedLength);
  1496. Assert.Null (_textField.SelectedText);
  1497. break;
  1498. case 4:
  1499. Assert.Equal (28, _textField.CursorPosition);
  1500. Assert.Equal (-1, _textField.SelectedStart);
  1501. Assert.Equal (0, _textField.SelectedLength);
  1502. Assert.Null (_textField.SelectedText);
  1503. break;
  1504. case 5:
  1505. Assert.Equal (38, _textField.CursorPosition);
  1506. Assert.Equal (-1, _textField.SelectedStart);
  1507. Assert.Equal (0, _textField.SelectedLength);
  1508. Assert.Null (_textField.SelectedText);
  1509. break;
  1510. case 6:
  1511. Assert.Equal (40, _textField.CursorPosition);
  1512. Assert.Equal (-1, _textField.SelectedStart);
  1513. Assert.Equal (0, _textField.SelectedLength);
  1514. Assert.Null (_textField.SelectedText);
  1515. break;
  1516. case 7:
  1517. Assert.Equal (46, _textField.CursorPosition);
  1518. Assert.Equal (-1, _textField.SelectedStart);
  1519. Assert.Equal (0, _textField.SelectedLength);
  1520. Assert.Null (_textField.SelectedText);
  1521. break;
  1522. case 8:
  1523. Assert.Equal (48, _textField.CursorPosition);
  1524. Assert.Equal (-1, _textField.SelectedStart);
  1525. Assert.Equal (0, _textField.SelectedLength);
  1526. Assert.Null (_textField.SelectedText);
  1527. break;
  1528. case 9:
  1529. Assert.Equal (54, _textField.CursorPosition);
  1530. Assert.Equal (-1, _textField.SelectedStart);
  1531. Assert.Equal (0, _textField.SelectedLength);
  1532. Assert.Null (_textField.SelectedText);
  1533. break;
  1534. case 10:
  1535. Assert.Equal (55, _textField.CursorPosition);
  1536. Assert.Equal (-1, _textField.SelectedStart);
  1537. Assert.Equal (0, _textField.SelectedLength);
  1538. Assert.Null (_textField.SelectedText);
  1539. break;
  1540. }
  1541. iteration++;
  1542. }
  1543. }
  1544. [Fact]
  1545. [TextFieldTestsAutoInitShutdown]
  1546. public void WordForward_With_Selection ()
  1547. {
  1548. _textField.CursorPosition = 0;
  1549. _textField.SelectedStart = 0;
  1550. var iteration = 0;
  1551. while (_textField.CursorPosition < _textField.Text.Length)
  1552. {
  1553. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1554. switch (iteration)
  1555. {
  1556. case 0:
  1557. Assert.Equal (4, _textField.CursorPosition);
  1558. Assert.Equal (0, _textField.SelectedStart);
  1559. Assert.Equal (4, _textField.SelectedLength);
  1560. Assert.Equal ("TAB ", _textField.SelectedText);
  1561. break;
  1562. case 1:
  1563. Assert.Equal (7, _textField.CursorPosition);
  1564. Assert.Equal (0, _textField.SelectedStart);
  1565. Assert.Equal (7, _textField.SelectedLength);
  1566. Assert.Equal ("TAB to ", _textField.SelectedText);
  1567. break;
  1568. case 2:
  1569. Assert.Equal (12, _textField.CursorPosition);
  1570. Assert.Equal (0, _textField.SelectedStart);
  1571. Assert.Equal (12, _textField.SelectedLength);
  1572. Assert.Equal ("TAB to jump ", _textField.SelectedText);
  1573. break;
  1574. case 3:
  1575. Assert.Equal (20, _textField.CursorPosition);
  1576. Assert.Equal (0, _textField.SelectedStart);
  1577. Assert.Equal (20, _textField.SelectedLength);
  1578. Assert.Equal ("TAB to jump between ", _textField.SelectedText);
  1579. break;
  1580. case 4:
  1581. Assert.Equal (25, _textField.CursorPosition);
  1582. Assert.Equal (0, _textField.SelectedStart);
  1583. Assert.Equal (25, _textField.SelectedLength);
  1584. Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
  1585. break;
  1586. case 5:
  1587. Assert.Equal (32, _textField.CursorPosition);
  1588. Assert.Equal (0, _textField.SelectedStart);
  1589. Assert.Equal (32, _textField.SelectedLength);
  1590. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1591. break;
  1592. }
  1593. iteration++;
  1594. }
  1595. }
  1596. [Fact]
  1597. [TextFieldTestsAutoInitShutdown]
  1598. public void
  1599. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1600. {
  1601. _textField.CursorPosition = 10;
  1602. _textField.SelectedStart = 10;
  1603. var iteration = 0;
  1604. while (_textField.CursorPosition < _textField.Text.Length)
  1605. {
  1606. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1607. switch (iteration)
  1608. {
  1609. case 0:
  1610. Assert.Equal (12, _textField.CursorPosition);
  1611. Assert.Equal (10, _textField.SelectedStart);
  1612. Assert.Equal (2, _textField.SelectedLength);
  1613. Assert.Equal ("p ", _textField.SelectedText);
  1614. break;
  1615. case 1:
  1616. Assert.Equal (20, _textField.CursorPosition);
  1617. Assert.Equal (10, _textField.SelectedStart);
  1618. Assert.Equal (10, _textField.SelectedLength);
  1619. Assert.Equal ("p between ", _textField.SelectedText);
  1620. break;
  1621. case 2:
  1622. Assert.Equal (25, _textField.CursorPosition);
  1623. Assert.Equal (10, _textField.SelectedStart);
  1624. Assert.Equal (15, _textField.SelectedLength);
  1625. Assert.Equal ("p between text ", _textField.SelectedText);
  1626. break;
  1627. case 3:
  1628. Assert.Equal (32, _textField.CursorPosition);
  1629. Assert.Equal (10, _textField.SelectedStart);
  1630. Assert.Equal (22, _textField.SelectedLength);
  1631. Assert.Equal ("p between text fields.", _textField.SelectedText);
  1632. break;
  1633. }
  1634. iteration++;
  1635. }
  1636. }
  1637. [Fact]
  1638. [SetupFakeDriver]
  1639. public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
  1640. {
  1641. var tf = new TextField { Width = 30, Text = "Les Misérables" };
  1642. tf.SetRelativeLayout (new Size (100, 100));
  1643. tf.Draw ();
  1644. TestHelpers.AssertDriverContentsWithFrameAre (
  1645. @"
  1646. Les Misérables",
  1647. output
  1648. );
  1649. tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1650. tf.Draw ();
  1651. TestHelpers.AssertDriverContentsWithFrameAre (
  1652. @"
  1653. Les Misérables",
  1654. output
  1655. );
  1656. // incorrect order will result with a wrong accent place
  1657. tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
  1658. tf.Draw ();
  1659. TestHelpers.AssertDriverContentsWithFrameAre (
  1660. @"
  1661. Les Miśerables",
  1662. output
  1663. );
  1664. }
  1665. private TextField GetTextFieldsInView ()
  1666. {
  1667. var tf = new TextField { Width = 10 };
  1668. var tf2 = new TextField { Y = 1, Width = 10 };
  1669. Toplevel top = new ();
  1670. top.Add (tf);
  1671. top.Add (tf2);
  1672. Application.Begin (top);
  1673. Assert.Same (tf, top.Focused);
  1674. return tf;
  1675. }
  1676. // This class enables test functions annotated with the [InitShutdown] attribute
  1677. // to have a function called before the test function is called and after.
  1678. //
  1679. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1680. // as a pair, and b) all unit test functions should be atomic.
  1681. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1682. public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
  1683. {
  1684. public override void After (MethodInfo methodUnderTest)
  1685. {
  1686. _textField.Dispose ();
  1687. _textField = null;
  1688. base.After (methodUnderTest);
  1689. }
  1690. public override void Before (MethodInfo methodUnderTest)
  1691. {
  1692. base.Before (methodUnderTest);
  1693. //Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
  1694. _textField = new ()
  1695. {
  1696. ColorScheme = new (Colors.ColorSchemes ["Base"]),
  1697. // 1 2 3
  1698. // 01234567890123456789012345678901=32 (Length)
  1699. Text = "TAB to jump between text fields.",
  1700. Width = 32
  1701. };
  1702. }
  1703. }
  1704. [Fact]
  1705. public void Autocomplete_Popup_Added_To_SuperView_On_Init ()
  1706. {
  1707. View superView = new ()
  1708. {
  1709. CanFocus = true,
  1710. };
  1711. TextField t = new ();
  1712. superView.Add (t);
  1713. Assert.Single (superView.Subviews);
  1714. superView.BeginInit ();
  1715. superView.EndInit ();
  1716. Assert.Equal (2, superView.Subviews.Count);
  1717. }
  1718. [Fact]
  1719. public void Autocomplete__Added_To_SuperView_On_Add ()
  1720. {
  1721. View superView = new ()
  1722. {
  1723. CanFocus = true,
  1724. Id = "superView",
  1725. };
  1726. superView.BeginInit ();
  1727. superView.EndInit ();
  1728. Assert.Empty (superView.Subviews);
  1729. TextField t = new ()
  1730. {
  1731. Id = "t"
  1732. };
  1733. superView.Add (t);
  1734. Assert.Equal (2, superView.Subviews.Count);
  1735. }
  1736. [Fact]
  1737. public void Autocomplete_Visible_False_By_Default ()
  1738. {
  1739. View superView = new ()
  1740. {
  1741. CanFocus = true,
  1742. };
  1743. TextField t = new ();
  1744. superView.Add (t);
  1745. superView.BeginInit ();
  1746. superView.EndInit ();
  1747. Assert.Equal (2, superView.Subviews.Count);
  1748. Assert.True (t.Visible);
  1749. Assert.False (t.Autocomplete.Visible);
  1750. }
  1751. }