TextFieldTests.cs 50 KB

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