TextFieldTests.cs 53 KB

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