TextFieldTests.cs 53 KB

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