TextFieldTests.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. using System.Globalization;
  2. using System.Reflection;
  3. using System.Text;
  4. using UnitTests;
  5. using Xunit.Abstractions;
  6. namespace UnitTests.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.RaiseKeyDownEvent ('\t');
  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.RaiseKeyDownEvent ('\t');
  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.RaiseKeyDownEvent ('\t');
  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.RaiseKeyDownEvent ('\t');
  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, ResultEventArgs<string> e)
  296. {
  297. if (e.Result.GetRuneCount () > 11)
  298. {
  299. e.Result = e.Result [..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.Result;
  356. oldText = tf.Text;
  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.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift.WithCtrl));
  537. Assert.Equal ("is a test.", tf.SelectedText);
  538. Assert.Equal (13, tf.CursorPosition);
  539. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  540. Assert.Equal ("is is a test.", tf.Text);
  541. Assert.Null (tf.SelectedText);
  542. Assert.Equal (12, tf.CursorPosition);
  543. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  544. Assert.Equal ("is is a test.", tf.Text);
  545. Assert.Equal (11, tf.CursorPosition);
  546. Assert.True (tf.NewKeyDownEvent (Key.End));
  547. Assert.Equal ("is is a test.", tf.Text);
  548. Assert.Equal (13, tf.CursorPosition);
  549. tf.CursorPosition = 0;
  550. Assert.True (tf.NewKeyDownEvent (Key.End.WithCtrl));
  551. Assert.Equal ("is is a test.", tf.Text);
  552. Assert.Equal (13, tf.CursorPosition);
  553. tf.CursorPosition = 0;
  554. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  555. Assert.Equal ("is is a test.", tf.Text);
  556. Assert.Equal (13, tf.CursorPosition);
  557. tf.CursorPosition = 0;
  558. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  559. Assert.Equal ("is is a test.", tf.Text);
  560. Assert.Equal (1, tf.CursorPosition);
  561. Assert.True (tf.NewKeyDownEvent (Key.F.WithCtrl));
  562. Assert.Equal ("is is a test.", tf.Text);
  563. Assert.Equal (2, tf.CursorPosition);
  564. tf.CursorPosition = 9;
  565. tf.ReadOnly = true;
  566. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  567. Assert.Equal ("is is a test.", tf.Text);
  568. tf.ReadOnly = false;
  569. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl));
  570. Assert.Equal ("is is a t", tf.Text);
  571. Assert.Equal ("est.", Clipboard.Contents);
  572. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  573. Assert.Equal ("is is a test.", tf.Text);
  574. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  575. Assert.Equal ("is is a t", tf.Text);
  576. #if UNIX_KEY_BINDINGS
  577. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithAlt));
  578. #else
  579. Assert.True (tf.NewKeyDownEvent (Key.Z.WithCtrl));
  580. #endif
  581. Assert.Equal ("is is a test.", tf.Text);
  582. Assert.True (tf.NewKeyDownEvent (Key.Y.WithCtrl));
  583. Assert.Equal ("is is a t", tf.Text);
  584. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  585. Assert.Equal ("is is a t", tf.Text);
  586. Assert.Equal (8, tf.CursorPosition);
  587. Assert.True (tf.NewKeyDownEvent (Key.CursorUp.WithCtrl));
  588. Assert.Equal ("is is a t", tf.Text);
  589. Assert.Equal (6, tf.CursorPosition);
  590. #if UNIX_KEY_BINDINGS
  591. Assert.True (tf.NewKeyDownEvent (Key.B.WithAlt));
  592. #else
  593. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithCtrl));
  594. #endif
  595. Assert.Equal ("is is a t", tf.Text);
  596. Assert.Equal (3, tf.CursorPosition);
  597. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  598. Assert.Equal ("is is a t", tf.Text);
  599. Assert.Equal (6, tf.CursorPosition);
  600. Assert.True (tf.NewKeyDownEvent (Key.CursorDown.WithCtrl));
  601. Assert.Equal ("is is a t", tf.Text);
  602. Assert.Equal (8, tf.CursorPosition);
  603. #if UNIX_KEY_BINDINGS
  604. Assert.True (tf.NewKeyDownEvent (Key.F.WithAlt));
  605. #else
  606. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithCtrl));
  607. #endif
  608. Assert.Equal ("is is a t", tf.Text);
  609. Assert.Equal (9, tf.CursorPosition);
  610. Assert.True (tf.Used);
  611. Assert.True (tf.NewKeyDownEvent (Key.InsertChar));
  612. Assert.Equal ("is is a t", tf.Text);
  613. Assert.Equal (9, tf.CursorPosition);
  614. Assert.False (tf.Used);
  615. tf.SelectedStart = 3;
  616. tf.CursorPosition = 7;
  617. Assert.Equal ("is a", tf.SelectedText);
  618. Assert.Equal ("est.", Clipboard.Contents);
  619. Assert.True (tf.NewKeyDownEvent (Key.C.WithCtrl));
  620. Assert.Equal ("is is a t", tf.Text);
  621. Assert.Equal ("is a", Clipboard.Contents);
  622. Assert.True (tf.NewKeyDownEvent (Key.X.WithCtrl));
  623. Assert.Equal ("is t", tf.Text);
  624. Assert.Equal ("is a", Clipboard.Contents);
  625. Assert.True (tf.NewKeyDownEvent (Key.V.WithCtrl));
  626. Assert.Equal ("is is a t", tf.Text);
  627. Assert.Equal ("is a", Clipboard.Contents);
  628. Assert.Equal (7, tf.CursorPosition);
  629. #if UNIX_KEY_BINDINGS
  630. Assert.True (tf.NewKeyDownEvent (Key.K.WithAlt));
  631. #else
  632. Assert.True (tf.NewKeyDownEvent (Key.K.WithCtrl.WithShift));
  633. #endif
  634. Assert.Equal (" t", tf.Text);
  635. Assert.Equal ("is is a", Clipboard.Contents);
  636. tf.Text = "TAB to jump between text fields.";
  637. Assert.Equal (0, tf.CursorPosition);
  638. Assert.True (tf.NewKeyDownEvent (Key.Delete.WithCtrl));
  639. Assert.Equal ("to jump between text fields.", tf.Text);
  640. tf.CursorPosition = tf.Text.Length;
  641. Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
  642. Assert.Equal ("to jump between text fields", tf.Text);
  643. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  644. Assert.Equal ("to jump between text fields", tf.SelectedText);
  645. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
  646. Assert.Equal ("", tf.Text);
  647. }
  648. [Theory]
  649. [InlineData (false, 1)]
  650. [InlineData (true, 0)]
  651. public void Accepted_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts)
  652. {
  653. var superView = new Window
  654. {
  655. Id = "superView"
  656. };
  657. var tf = new TextField
  658. {
  659. Id = "tf"
  660. };
  661. var button = new Button
  662. {
  663. Id = "button",
  664. IsDefault = true
  665. };
  666. superView.Add (tf, button);
  667. var buttonAccept = 0;
  668. button.Accepting += ButtonAccept;
  669. var textFieldAccept = 0;
  670. tf.Accepting += TextFieldAccept;
  671. tf.SetFocus ();
  672. Assert.True (tf.HasFocus);
  673. superView.NewKeyDownEvent (Key.Enter);
  674. Assert.Equal (1, textFieldAccept);
  675. Assert.Equal (expectedButtonAccepts, buttonAccept);
  676. button.SetFocus ();
  677. superView.NewKeyDownEvent (Key.Enter);
  678. Assert.Equal (1, textFieldAccept);
  679. Assert.Equal (expectedButtonAccepts + 1, buttonAccept);
  680. return;
  681. void TextFieldAccept (object sender, CommandEventArgs e)
  682. {
  683. textFieldAccept++;
  684. e.Handled = handleAccept;
  685. }
  686. void ButtonAccept (object sender, CommandEventArgs e) { buttonAccept++; }
  687. }
  688. [Fact]
  689. [TextFieldTestsAutoInitShutdown]
  690. public void Paste_Always_Clear_The_SelectedText ()
  691. {
  692. _textField.SelectedStart = 20;
  693. _textField.CursorPosition = 24;
  694. _textField.Copy ();
  695. Assert.Equal ("text", _textField.SelectedText);
  696. _textField.Paste ();
  697. Assert.Null (_textField.SelectedText);
  698. }
  699. [Fact]
  700. [AutoInitShutdown]
  701. public void ScrollOffset_Initialize ()
  702. {
  703. var tf = new TextField { X = 1, Y = 1, Width = 20, Text = "Testing Scrolls." };
  704. tf.BeginInit ();
  705. tf.EndInit ();
  706. Assert.Equal (0, tf.ScrollOffset);
  707. Assert.Equal (16, tf.CursorPosition);
  708. }
  709. [Fact]
  710. [TextFieldTestsAutoInitShutdown]
  711. public void Selected_Text_Shows ()
  712. {
  713. // Proves #3022 is fixed (TextField selected text does not show in v2)
  714. _textField.CursorPosition = 0;
  715. var top = new Toplevel ();
  716. top.Add (_textField);
  717. RunState rs = Application.Begin (top);
  718. Attribute [] attributes =
  719. {
  720. _textField.GetAttributeForRole (VisualRole.Focus),
  721. new (
  722. _textField.GetAttributeForRole (VisualRole.Focus).Background,
  723. _textField.GetAttributeForRole (VisualRole.Focus).Foreground
  724. )
  725. };
  726. // TAB to jump between text fields.
  727. DriverAssert.AssertDriverAttributesAre ("0000000", output, Application.Driver, attributes);
  728. // Cursor is at the end
  729. Assert.Equal (32, _textField.CursorPosition);
  730. _textField.CursorPosition = 0;
  731. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  732. AutoInitShutdownAttribute.RunIteration ();
  733. Assert.Equal (4, _textField.CursorPosition);
  734. // TAB to jump between text fields.
  735. DriverAssert.AssertDriverAttributesAre ("1111000", output, Application.Driver, attributes);
  736. top.Dispose ();
  737. }
  738. [Fact]
  739. [TextFieldTestsAutoInitShutdown]
  740. public void SelectedStart_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  741. {
  742. _textField.CursorPosition = 33;
  743. _textField.SelectedStart = 33;
  744. Assert.Equal (32, _textField.CursorPosition);
  745. Assert.Equal (32, _textField.SelectedStart);
  746. Assert.Equal (0, _textField.SelectedLength);
  747. Assert.Null (_textField.SelectedText);
  748. }
  749. [Fact]
  750. [TextFieldTestsAutoInitShutdown]
  751. public void SelectedStart_Greater_Than_CursorPosition_All_Selection_Is_Overwritten_On_Typing ()
  752. {
  753. _textField.SelectedStart = 19;
  754. _textField.CursorPosition = 12;
  755. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  756. _textField.NewKeyDownEvent (Key.U); // u
  757. Assert.Equal ("TAB to jump u text fields.", _textField.Text);
  758. }
  759. [Fact]
  760. [TextFieldTestsAutoInitShutdown]
  761. public void SelectedStart_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  762. {
  763. _textField.CursorPosition = 2;
  764. _textField.SelectedStart = 33;
  765. Assert.Equal (32, _textField.SelectedStart);
  766. Assert.Equal (30, _textField.SelectedLength);
  767. Assert.Equal ("B to jump between text fields.", _textField.SelectedText);
  768. }
  769. [Fact]
  770. [TextFieldTestsAutoInitShutdown]
  771. public void SelectedStart_With_Value_Less_Than_Minus_One_Changes_To_Minus_One ()
  772. {
  773. _textField.SelectedStart = -2;
  774. Assert.Equal (-1, _textField.SelectedStart);
  775. Assert.Equal (0, _textField.SelectedLength);
  776. Assert.Null (_textField.SelectedText);
  777. }
  778. [Fact]
  779. [AutoInitShutdown]
  780. public void MouseEvent_Handled_Prevents_RightClick ()
  781. {
  782. Application.MouseEvent += HandleRightClick;
  783. var tf = new TextField { Width = 10 };
  784. var clickCounter = 0;
  785. tf.MouseClick += (s, m) => { clickCounter++; };
  786. var top = new Toplevel ();
  787. top.Add (tf);
  788. Application.Begin (top);
  789. var mouseEvent = new MouseEventArgs { Flags = MouseFlags.Button1Clicked, View = tf };
  790. Application.RaiseMouseEvent (mouseEvent);
  791. Assert.Equal (1, clickCounter);
  792. // Get a fresh instance that represents a right click.
  793. // Should be ignored because of SuppressRightClick callback
  794. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  795. Application.RaiseMouseEvent (mouseEvent);
  796. Assert.Equal (1, clickCounter);
  797. Application.MouseEvent -= HandleRightClick;
  798. // Get a fresh instance that represents a right click.
  799. // Should no longer be ignored as the callback was removed
  800. mouseEvent = new () { Flags = MouseFlags.Button3Clicked, View = tf };
  801. // In #3183 OnMouseClicked is no longer called before MouseEvent().
  802. // This call causes the context menu to pop, and MouseEvent() returns true.
  803. // Thus, the clickCounter is NOT incremented.
  804. // Which is correct, because the user did NOT click with the left mouse button.
  805. Application.RaiseMouseEvent (mouseEvent);
  806. Assert.Equal (1, clickCounter);
  807. top.Dispose ();
  808. return;
  809. void HandleRightClick (object sender, MouseEventArgs arg)
  810. {
  811. if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
  812. {
  813. arg.Handled = true;
  814. }
  815. }
  816. }
  817. [Fact]
  818. [TextFieldTestsAutoInitShutdown]
  819. public void Text_Replaces_Tabs_With_Empty_String ()
  820. {
  821. _textField.Text = "\t\tTAB to jump between text fields.";
  822. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  823. _textField.Text = "";
  824. Clipboard.Contents = "\t\tTAB to jump between text fields.";
  825. _textField.Paste ();
  826. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  827. }
  828. [Fact]
  829. [TextFieldTestsAutoInitShutdown]
  830. public void TextChanged_Event ()
  831. {
  832. var eventFired = false;
  833. _textField.TextChanged += (s, e) => eventFired = true;
  834. _textField.Text = "changed";
  835. Assert.True (eventFired);
  836. Assert.Equal ("changed", _textField.Text);
  837. }
  838. [Fact]
  839. [TextFieldTestsAutoInitShutdown]
  840. public void TextChanging_Event ()
  841. {
  842. var cancel = true;
  843. _textField.TextChanging += (s, e) =>
  844. {
  845. Assert.Equal ("changing", e.Result);
  846. if (cancel)
  847. {
  848. e.Handled = true;
  849. }
  850. };
  851. _textField.Text = "changing";
  852. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  853. cancel = false;
  854. _textField.Text = "changing";
  855. Assert.Equal ("changing", _textField.Text);
  856. }
  857. [Fact]
  858. [TextFieldTestsAutoInitShutdown]
  859. public void Used_Is_False ()
  860. {
  861. _textField.Used = false;
  862. _textField.CursorPosition = 10;
  863. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  864. _textField.NewKeyDownEvent (Key.U); // u
  865. Assert.Equal ("TAB to jumu between text fields.", _textField.Text);
  866. _textField.NewKeyDownEvent (Key.S); // s
  867. Assert.Equal ("TAB to jumusbetween text fields.", _textField.Text);
  868. _textField.NewKeyDownEvent (Key.E); // e
  869. Assert.Equal ("TAB to jumuseetween text fields.", _textField.Text);
  870. _textField.NewKeyDownEvent (Key.D); // d
  871. Assert.Equal ("TAB to jumusedtween text fields.", _textField.Text);
  872. }
  873. [Fact]
  874. [TextFieldTestsAutoInitShutdown]
  875. public void Used_Is_True_By_Default ()
  876. {
  877. _textField.CursorPosition = 10;
  878. Assert.Equal ("TAB to jump between text fields.", _textField.Text);
  879. _textField.NewKeyDownEvent (Key.U); // u
  880. Assert.Equal ("TAB to jumup between text fields.", _textField.Text);
  881. _textField.NewKeyDownEvent (Key.S); // s
  882. Assert.Equal ("TAB to jumusp between text fields.", _textField.Text);
  883. _textField.NewKeyDownEvent (Key.E); // e
  884. Assert.Equal ("TAB to jumusep between text fields.", _textField.Text);
  885. _textField.NewKeyDownEvent (Key.D); // d
  886. Assert.Equal ("TAB to jumusedp between text fields.", _textField.Text);
  887. }
  888. [Fact]
  889. [TextFieldTestsAutoInitShutdown]
  890. public void WordBackward_With_No_Selection ()
  891. {
  892. _textField.CursorPosition = _textField.Text.Length;
  893. var iteration = 0;
  894. while (_textField.CursorPosition > 0)
  895. {
  896. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  897. switch (iteration)
  898. {
  899. case 0:
  900. Assert.Equal (31, _textField.CursorPosition);
  901. Assert.Equal (-1, _textField.SelectedStart);
  902. Assert.Equal (0, _textField.SelectedLength);
  903. Assert.Null (_textField.SelectedText);
  904. break;
  905. case 1:
  906. Assert.Equal (25, _textField.CursorPosition);
  907. Assert.Equal (-1, _textField.SelectedStart);
  908. Assert.Equal (0, _textField.SelectedLength);
  909. Assert.Null (_textField.SelectedText);
  910. break;
  911. case 2:
  912. Assert.Equal (20, _textField.CursorPosition);
  913. Assert.Equal (-1, _textField.SelectedStart);
  914. Assert.Equal (0, _textField.SelectedLength);
  915. Assert.Null (_textField.SelectedText);
  916. break;
  917. case 3:
  918. Assert.Equal (12, _textField.CursorPosition);
  919. Assert.Equal (-1, _textField.SelectedStart);
  920. Assert.Equal (0, _textField.SelectedLength);
  921. Assert.Null (_textField.SelectedText);
  922. break;
  923. case 4:
  924. Assert.Equal (7, _textField.CursorPosition);
  925. Assert.Equal (-1, _textField.SelectedStart);
  926. Assert.Equal (0, _textField.SelectedLength);
  927. Assert.Null (_textField.SelectedText);
  928. break;
  929. case 5:
  930. Assert.Equal (4, _textField.CursorPosition);
  931. Assert.Equal (-1, _textField.SelectedStart);
  932. Assert.Equal (0, _textField.SelectedLength);
  933. Assert.Null (_textField.SelectedText);
  934. break;
  935. case 6:
  936. Assert.Equal (0, _textField.CursorPosition);
  937. Assert.Equal (-1, _textField.SelectedStart);
  938. Assert.Equal (0, _textField.SelectedLength);
  939. Assert.Null (_textField.SelectedText);
  940. break;
  941. }
  942. iteration++;
  943. }
  944. }
  945. [Fact]
  946. [TextFieldTestsAutoInitShutdown]
  947. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  948. {
  949. // 1 2 3 4 5
  950. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  951. _textField.Text = "TAB t o jump b etween t ext f ields .";
  952. _textField.CursorPosition = _textField.Text.Length;
  953. var iteration = 0;
  954. while (_textField.CursorPosition > 0)
  955. {
  956. _textField.NewKeyDownEvent (Key.CursorLeft.WithCtrl);
  957. switch (iteration)
  958. {
  959. case 0:
  960. Assert.Equal (54, _textField.CursorPosition);
  961. Assert.Equal (-1, _textField.SelectedStart);
  962. Assert.Equal (0, _textField.SelectedLength);
  963. Assert.Null (_textField.SelectedText);
  964. break;
  965. case 1:
  966. Assert.Equal (48, _textField.CursorPosition);
  967. Assert.Equal (-1, _textField.SelectedStart);
  968. Assert.Equal (0, _textField.SelectedLength);
  969. Assert.Null (_textField.SelectedText);
  970. break;
  971. case 2:
  972. Assert.Equal (46, _textField.CursorPosition);
  973. Assert.Equal (-1, _textField.SelectedStart);
  974. Assert.Equal (0, _textField.SelectedLength);
  975. Assert.Null (_textField.SelectedText);
  976. break;
  977. case 3:
  978. Assert.Equal (40, _textField.CursorPosition);
  979. Assert.Equal (-1, _textField.SelectedStart);
  980. Assert.Equal (0, _textField.SelectedLength);
  981. Assert.Null (_textField.SelectedText);
  982. break;
  983. case 4:
  984. Assert.Equal (38, _textField.CursorPosition);
  985. Assert.Equal (-1, _textField.SelectedStart);
  986. Assert.Equal (0, _textField.SelectedLength);
  987. Assert.Null (_textField.SelectedText);
  988. break;
  989. case 5:
  990. Assert.Equal (28, _textField.CursorPosition);
  991. Assert.Equal (-1, _textField.SelectedStart);
  992. Assert.Equal (0, _textField.SelectedLength);
  993. Assert.Null (_textField.SelectedText);
  994. break;
  995. case 6:
  996. Assert.Equal (25, _textField.CursorPosition);
  997. Assert.Equal (-1, _textField.SelectedStart);
  998. Assert.Equal (0, _textField.SelectedLength);
  999. Assert.Null (_textField.SelectedText);
  1000. break;
  1001. case 7:
  1002. Assert.Equal (12, _textField.CursorPosition);
  1003. Assert.Equal (-1, _textField.SelectedStart);
  1004. Assert.Equal (0, _textField.SelectedLength);
  1005. Assert.Null (_textField.SelectedText);
  1006. break;
  1007. case 8:
  1008. Assert.Equal (9, _textField.CursorPosition);
  1009. Assert.Equal (-1, _textField.SelectedStart);
  1010. Assert.Equal (0, _textField.SelectedLength);
  1011. Assert.Null (_textField.SelectedText);
  1012. break;
  1013. case 9:
  1014. Assert.Equal (6, _textField.CursorPosition);
  1015. Assert.Equal (-1, _textField.SelectedStart);
  1016. Assert.Equal (0, _textField.SelectedLength);
  1017. Assert.Null (_textField.SelectedText);
  1018. break;
  1019. case 10:
  1020. Assert.Equal (0, _textField.CursorPosition);
  1021. Assert.Equal (-1, _textField.SelectedStart);
  1022. Assert.Equal (0, _textField.SelectedLength);
  1023. Assert.Null (_textField.SelectedText);
  1024. break;
  1025. }
  1026. iteration++;
  1027. }
  1028. }
  1029. [Fact]
  1030. [TextFieldTestsAutoInitShutdown]
  1031. public void WordBackward_With_Selection ()
  1032. {
  1033. _textField.CursorPosition = _textField.Text.Length;
  1034. _textField.SelectedStart = _textField.Text.Length;
  1035. var iteration = 0;
  1036. while (_textField.CursorPosition > 0)
  1037. {
  1038. _textField.NewKeyDownEvent (
  1039. new (
  1040. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1041. )
  1042. );
  1043. switch (iteration)
  1044. {
  1045. case 0:
  1046. Assert.Equal (31, _textField.CursorPosition);
  1047. Assert.Equal (32, _textField.SelectedStart);
  1048. Assert.Equal (1, _textField.SelectedLength);
  1049. Assert.Equal (".", _textField.SelectedText);
  1050. break;
  1051. case 1:
  1052. Assert.Equal (25, _textField.CursorPosition);
  1053. Assert.Equal (32, _textField.SelectedStart);
  1054. Assert.Equal (7, _textField.SelectedLength);
  1055. Assert.Equal ("fields.", _textField.SelectedText);
  1056. break;
  1057. case 2:
  1058. Assert.Equal (20, _textField.CursorPosition);
  1059. Assert.Equal (32, _textField.SelectedStart);
  1060. Assert.Equal (12, _textField.SelectedLength);
  1061. Assert.Equal ("text fields.", _textField.SelectedText);
  1062. break;
  1063. case 3:
  1064. Assert.Equal (12, _textField.CursorPosition);
  1065. Assert.Equal (32, _textField.SelectedStart);
  1066. Assert.Equal (20, _textField.SelectedLength);
  1067. Assert.Equal ("between text fields.", _textField.SelectedText);
  1068. break;
  1069. case 4:
  1070. Assert.Equal (7, _textField.CursorPosition);
  1071. Assert.Equal (32, _textField.SelectedStart);
  1072. Assert.Equal (25, _textField.SelectedLength);
  1073. Assert.Equal ("jump between text fields.", _textField.SelectedText);
  1074. break;
  1075. case 5:
  1076. Assert.Equal (4, _textField.CursorPosition);
  1077. Assert.Equal (32, _textField.SelectedStart);
  1078. Assert.Equal (28, _textField.SelectedLength);
  1079. Assert.Equal ("to jump between text fields.", _textField.SelectedText);
  1080. break;
  1081. case 6:
  1082. Assert.Equal (0, _textField.CursorPosition);
  1083. Assert.Equal (32, _textField.SelectedStart);
  1084. Assert.Equal (32, _textField.SelectedLength);
  1085. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1086. break;
  1087. }
  1088. iteration++;
  1089. }
  1090. }
  1091. [Fact]
  1092. [TextFieldTestsAutoInitShutdown]
  1093. public void
  1094. WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1095. {
  1096. _textField.CursorPosition = 10;
  1097. _textField.SelectedStart = 10;
  1098. var iteration = 0;
  1099. while (_textField.CursorPosition > 0)
  1100. {
  1101. _textField.NewKeyDownEvent (
  1102. new (
  1103. KeyCode.CursorLeft | KeyCode.CtrlMask | KeyCode.ShiftMask
  1104. )
  1105. );
  1106. switch (iteration)
  1107. {
  1108. case 0:
  1109. Assert.Equal (7, _textField.CursorPosition);
  1110. Assert.Equal (10, _textField.SelectedStart);
  1111. Assert.Equal (3, _textField.SelectedLength);
  1112. Assert.Equal ("jum", _textField.SelectedText);
  1113. break;
  1114. case 1:
  1115. Assert.Equal (4, _textField.CursorPosition);
  1116. Assert.Equal (10, _textField.SelectedStart);
  1117. Assert.Equal (6, _textField.SelectedLength);
  1118. Assert.Equal ("to jum", _textField.SelectedText);
  1119. break;
  1120. case 2:
  1121. Assert.Equal (0, _textField.CursorPosition);
  1122. Assert.Equal (10, _textField.SelectedStart);
  1123. Assert.Equal (10, _textField.SelectedLength);
  1124. Assert.Equal ("TAB to jum", _textField.SelectedText);
  1125. break;
  1126. }
  1127. iteration++;
  1128. }
  1129. }
  1130. [Fact]
  1131. [TextFieldTestsAutoInitShutdown]
  1132. public void WordForward_With_No_Selection ()
  1133. {
  1134. _textField.CursorPosition = 0;
  1135. var iteration = 0;
  1136. while (_textField.CursorPosition < _textField.Text.Length)
  1137. {
  1138. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1139. switch (iteration)
  1140. {
  1141. case 0:
  1142. Assert.Equal (4, _textField.CursorPosition);
  1143. Assert.Equal (-1, _textField.SelectedStart);
  1144. Assert.Equal (0, _textField.SelectedLength);
  1145. Assert.Null (_textField.SelectedText);
  1146. break;
  1147. case 1:
  1148. Assert.Equal (7, _textField.CursorPosition);
  1149. Assert.Equal (-1, _textField.SelectedStart);
  1150. Assert.Equal (0, _textField.SelectedLength);
  1151. Assert.Null (_textField.SelectedText);
  1152. break;
  1153. case 2:
  1154. Assert.Equal (12, _textField.CursorPosition);
  1155. Assert.Equal (-1, _textField.SelectedStart);
  1156. Assert.Equal (0, _textField.SelectedLength);
  1157. Assert.Null (_textField.SelectedText);
  1158. break;
  1159. case 3:
  1160. Assert.Equal (20, _textField.CursorPosition);
  1161. Assert.Equal (-1, _textField.SelectedStart);
  1162. Assert.Equal (0, _textField.SelectedLength);
  1163. Assert.Null (_textField.SelectedText);
  1164. break;
  1165. case 4:
  1166. Assert.Equal (25, _textField.CursorPosition);
  1167. Assert.Equal (-1, _textField.SelectedStart);
  1168. Assert.Equal (0, _textField.SelectedLength);
  1169. Assert.Null (_textField.SelectedText);
  1170. break;
  1171. case 5:
  1172. Assert.Equal (31, _textField.CursorPosition);
  1173. Assert.Equal (-1, _textField.SelectedStart);
  1174. Assert.Equal (0, _textField.SelectedLength);
  1175. Assert.Null (_textField.SelectedText);
  1176. break;
  1177. case 6:
  1178. Assert.Equal (32, _textField.CursorPosition);
  1179. Assert.Equal (-1, _textField.SelectedStart);
  1180. Assert.Equal (0, _textField.SelectedLength);
  1181. Assert.Null (_textField.SelectedText);
  1182. break;
  1183. }
  1184. iteration++;
  1185. }
  1186. }
  1187. [Fact]
  1188. [TextFieldTestsAutoInitShutdown]
  1189. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  1190. {
  1191. // 1 2 3 4 5
  1192. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  1193. _textField.Text = "TAB t o jump b etween t ext f ields .";
  1194. _textField.CursorPosition = 0;
  1195. var iteration = 0;
  1196. while (_textField.CursorPosition < _textField.Text.Length)
  1197. {
  1198. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl);
  1199. switch (iteration)
  1200. {
  1201. case 0:
  1202. Assert.Equal (6, _textField.CursorPosition);
  1203. Assert.Equal (-1, _textField.SelectedStart);
  1204. Assert.Equal (0, _textField.SelectedLength);
  1205. Assert.Null (_textField.SelectedText);
  1206. break;
  1207. case 1:
  1208. Assert.Equal (9, _textField.CursorPosition);
  1209. Assert.Equal (-1, _textField.SelectedStart);
  1210. Assert.Equal (0, _textField.SelectedLength);
  1211. Assert.Null (_textField.SelectedText);
  1212. break;
  1213. case 2:
  1214. Assert.Equal (12, _textField.CursorPosition);
  1215. Assert.Equal (-1, _textField.SelectedStart);
  1216. Assert.Equal (0, _textField.SelectedLength);
  1217. Assert.Null (_textField.SelectedText);
  1218. break;
  1219. case 3:
  1220. Assert.Equal (25, _textField.CursorPosition);
  1221. Assert.Equal (-1, _textField.SelectedStart);
  1222. Assert.Equal (0, _textField.SelectedLength);
  1223. Assert.Null (_textField.SelectedText);
  1224. break;
  1225. case 4:
  1226. Assert.Equal (28, _textField.CursorPosition);
  1227. Assert.Equal (-1, _textField.SelectedStart);
  1228. Assert.Equal (0, _textField.SelectedLength);
  1229. Assert.Null (_textField.SelectedText);
  1230. break;
  1231. case 5:
  1232. Assert.Equal (38, _textField.CursorPosition);
  1233. Assert.Equal (-1, _textField.SelectedStart);
  1234. Assert.Equal (0, _textField.SelectedLength);
  1235. Assert.Null (_textField.SelectedText);
  1236. break;
  1237. case 6:
  1238. Assert.Equal (40, _textField.CursorPosition);
  1239. Assert.Equal (-1, _textField.SelectedStart);
  1240. Assert.Equal (0, _textField.SelectedLength);
  1241. Assert.Null (_textField.SelectedText);
  1242. break;
  1243. case 7:
  1244. Assert.Equal (46, _textField.CursorPosition);
  1245. Assert.Equal (-1, _textField.SelectedStart);
  1246. Assert.Equal (0, _textField.SelectedLength);
  1247. Assert.Null (_textField.SelectedText);
  1248. break;
  1249. case 8:
  1250. Assert.Equal (48, _textField.CursorPosition);
  1251. Assert.Equal (-1, _textField.SelectedStart);
  1252. Assert.Equal (0, _textField.SelectedLength);
  1253. Assert.Null (_textField.SelectedText);
  1254. break;
  1255. case 9:
  1256. Assert.Equal (54, _textField.CursorPosition);
  1257. Assert.Equal (-1, _textField.SelectedStart);
  1258. Assert.Equal (0, _textField.SelectedLength);
  1259. Assert.Null (_textField.SelectedText);
  1260. break;
  1261. case 10:
  1262. Assert.Equal (55, _textField.CursorPosition);
  1263. Assert.Equal (-1, _textField.SelectedStart);
  1264. Assert.Equal (0, _textField.SelectedLength);
  1265. Assert.Null (_textField.SelectedText);
  1266. break;
  1267. }
  1268. iteration++;
  1269. }
  1270. }
  1271. [Fact]
  1272. [TextFieldTestsAutoInitShutdown]
  1273. public void WordForward_With_Selection ()
  1274. {
  1275. _textField.CursorPosition = 0;
  1276. _textField.SelectedStart = 0;
  1277. var iteration = 0;
  1278. while (_textField.CursorPosition < _textField.Text.Length)
  1279. {
  1280. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1281. switch (iteration)
  1282. {
  1283. case 0:
  1284. Assert.Equal (4, _textField.CursorPosition);
  1285. Assert.Equal (0, _textField.SelectedStart);
  1286. Assert.Equal (4, _textField.SelectedLength);
  1287. Assert.Equal ("TAB ", _textField.SelectedText);
  1288. break;
  1289. case 1:
  1290. Assert.Equal (7, _textField.CursorPosition);
  1291. Assert.Equal (0, _textField.SelectedStart);
  1292. Assert.Equal (7, _textField.SelectedLength);
  1293. Assert.Equal ("TAB to ", _textField.SelectedText);
  1294. break;
  1295. case 2:
  1296. Assert.Equal (12, _textField.CursorPosition);
  1297. Assert.Equal (0, _textField.SelectedStart);
  1298. Assert.Equal (12, _textField.SelectedLength);
  1299. Assert.Equal ("TAB to jump ", _textField.SelectedText);
  1300. break;
  1301. case 3:
  1302. Assert.Equal (20, _textField.CursorPosition);
  1303. Assert.Equal (0, _textField.SelectedStart);
  1304. Assert.Equal (20, _textField.SelectedLength);
  1305. Assert.Equal ("TAB to jump between ", _textField.SelectedText);
  1306. break;
  1307. case 4:
  1308. Assert.Equal (25, _textField.CursorPosition);
  1309. Assert.Equal (0, _textField.SelectedStart);
  1310. Assert.Equal (25, _textField.SelectedLength);
  1311. Assert.Equal ("TAB to jump between text ", _textField.SelectedText);
  1312. break;
  1313. case 5:
  1314. Assert.Equal (31, _textField.CursorPosition);
  1315. Assert.Equal (0, _textField.SelectedStart);
  1316. Assert.Equal (31, _textField.SelectedLength);
  1317. Assert.Equal ("TAB to jump between text fields", _textField.SelectedText);
  1318. break;
  1319. case 6:
  1320. Assert.Equal (32, _textField.CursorPosition);
  1321. Assert.Equal (0, _textField.SelectedStart);
  1322. Assert.Equal (32, _textField.SelectedLength);
  1323. Assert.Equal ("TAB to jump between text fields.", _textField.SelectedText);
  1324. break;
  1325. }
  1326. iteration++;
  1327. }
  1328. }
  1329. [Fact]
  1330. [TextFieldTestsAutoInitShutdown]
  1331. public void
  1332. WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  1333. {
  1334. _textField.CursorPosition = 10;
  1335. _textField.SelectedStart = 10;
  1336. var iteration = 0;
  1337. while (_textField.CursorPosition < _textField.Text.Length)
  1338. {
  1339. _textField.NewKeyDownEvent (Key.CursorRight.WithCtrl.WithShift);
  1340. switch (iteration)
  1341. {
  1342. case 0:
  1343. Assert.Equal (12, _textField.CursorPosition);
  1344. Assert.Equal (10, _textField.SelectedStart);
  1345. Assert.Equal (2, _textField.SelectedLength);
  1346. Assert.Equal ("p ", _textField.SelectedText);
  1347. break;
  1348. case 1:
  1349. Assert.Equal (20, _textField.CursorPosition);
  1350. Assert.Equal (10, _textField.SelectedStart);
  1351. Assert.Equal (10, _textField.SelectedLength);
  1352. Assert.Equal ("p between ", _textField.SelectedText);
  1353. break;
  1354. case 2:
  1355. Assert.Equal (25, _textField.CursorPosition);
  1356. Assert.Equal (10, _textField.SelectedStart);
  1357. Assert.Equal (15, _textField.SelectedLength);
  1358. Assert.Equal ("p between text ", _textField.SelectedText);
  1359. break;
  1360. case 3:
  1361. Assert.Equal (31, _textField.CursorPosition);
  1362. Assert.Equal (10, _textField.SelectedStart);
  1363. Assert.Equal (21, _textField.SelectedLength);
  1364. Assert.Equal ("p between text fields", _textField.SelectedText);
  1365. break;
  1366. case 4:
  1367. Assert.Equal (32, _textField.CursorPosition);
  1368. Assert.Equal (10, _textField.SelectedStart);
  1369. Assert.Equal (22, _textField.SelectedLength);
  1370. Assert.Equal ("p between text fields.", _textField.SelectedText);
  1371. break;
  1372. }
  1373. iteration++;
  1374. }
  1375. }
  1376. [Fact]
  1377. [SetupFakeDriver]
  1378. public void Words_With_Accents_Incorrect_Order_Will_Result_With_Wrong_Accent_Place ()
  1379. {
  1380. var tf = new TextField { Width = 30, Text = "Les Misérables" };
  1381. tf.SetRelativeLayout (new (100, 100));
  1382. tf.Draw ();
  1383. DriverAssert.AssertDriverContentsWithFrameAre (
  1384. @"
  1385. Les Misérables",
  1386. output
  1387. );
  1388. tf.Text = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1389. tf.Draw ();
  1390. DriverAssert.AssertDriverContentsWithFrameAre (
  1391. @"
  1392. Les Misérables",
  1393. output
  1394. );
  1395. // incorrect order will result with a wrong accent place
  1396. tf.Text = "Les Mis" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "erables";
  1397. View.SetClipToScreen ();
  1398. tf.Draw ();
  1399. DriverAssert.AssertDriverContentsWithFrameAre (
  1400. @"
  1401. Les Miśerables",
  1402. output
  1403. );
  1404. }
  1405. private TextField GetTextFieldsInView ()
  1406. {
  1407. var tf = new TextField { Width = 10 };
  1408. var tf2 = new TextField { Y = 1, Width = 10 };
  1409. Toplevel top = new ();
  1410. top.Add (tf);
  1411. top.Add (tf2);
  1412. Application.Begin (top);
  1413. Assert.Same (tf, top.Focused);
  1414. return tf;
  1415. }
  1416. // This class enables test functions annotated with the [InitShutdown] attribute
  1417. // to have a function called before the test function is called and after.
  1418. //
  1419. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  1420. // as a pair, and b) all unit test functions should be atomic.
  1421. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  1422. public class TextFieldTestsAutoInitShutdown : AutoInitShutdownAttribute
  1423. {
  1424. public override void After (MethodInfo methodUnderTest)
  1425. {
  1426. _textField.Dispose ();
  1427. _textField = null;
  1428. base.After (methodUnderTest);
  1429. }
  1430. public override void Before (MethodInfo methodUnderTest)
  1431. {
  1432. base.Before (methodUnderTest);
  1433. //Application.Top.Scheme = Colors.Schemes ["Base"];
  1434. _textField = new ()
  1435. {
  1436. // 1 2 3
  1437. // 01234567890123456789012345678901=32 (Length)
  1438. Text = "TAB to jump between text fields.",
  1439. Width = 32
  1440. };
  1441. }
  1442. }
  1443. [Fact]
  1444. [AutoInitShutdown]
  1445. public void Draw_Esc_Rune ()
  1446. {
  1447. var tf = new TextField { Width = 5, Text = "\u001b" };
  1448. tf.BeginInit ();
  1449. tf.EndInit ();
  1450. tf.Draw ();
  1451. DriverAssert.AssertDriverContentsWithFrameAre ("\u241b", output);
  1452. tf.Dispose ();
  1453. }
  1454. }