TextFieldTests.cs 55 KB

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