TextFieldTests.cs 53 KB

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