TextFieldTests.cs 60 KB

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