TextFieldTests.cs 67 KB

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