2
0

TextViewTests.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. using System;
  2. using System.Reflection;
  3. using Xunit;
  4. namespace Terminal.Gui.Views {
  5. public class TextViewTests {
  6. private static TextView _textView;
  7. // This class enables test functions annotated with the [InitShutdown] attribute
  8. // to have a function called before the test function is called and after.
  9. //
  10. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  11. // as a pair, and b) all unit test functions should be atomic.
  12. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  13. public class InitShutdown : Xunit.Sdk.BeforeAfterTestAttribute {
  14. public override void Before (MethodInfo methodUnderTest)
  15. {
  16. if (_textView != null) {
  17. throw new InvalidOperationException ("After did not run.");
  18. }
  19. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  20. // 1 2 3
  21. // 01234567890123456789012345678901=32 (Length)
  22. var txt = "TAB to jump between text fields.";
  23. var buff = new byte [txt.Length];
  24. for (int i = 0; i < txt.Length; i++) {
  25. buff [i] = (byte)txt [i];
  26. }
  27. var ms = new System.IO.MemoryStream (buff).ToArray ();
  28. _textView = new TextView () { Width = 30, Height = 10 };
  29. _textView.Text = ms;
  30. }
  31. public override void After (MethodInfo methodUnderTest)
  32. {
  33. _textView = null;
  34. Application.Shutdown ();
  35. }
  36. }
  37. [Fact]
  38. [InitShutdown]
  39. public void Changing_Selection_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  40. {
  41. _textView.SelectionStartColumn = 2;
  42. _textView.SelectionStartRow = 0;
  43. Assert.Equal (0, _textView.CursorPosition.X);
  44. Assert.Equal (0, _textView.CursorPosition.Y);
  45. Assert.Equal (2, _textView.SelectedLength);
  46. Assert.Equal ("TA", _textView.SelectedText);
  47. _textView.CursorPosition = new Point (20, 0);
  48. Assert.Equal (2, _textView.SelectionStartColumn);
  49. Assert.Equal (0, _textView.SelectionStartRow);
  50. Assert.Equal (18, _textView.SelectedLength);
  51. Assert.Equal ("B to jump between ", _textView.SelectedText);
  52. }
  53. [Fact]
  54. [InitShutdown]
  55. public void Selection_With_Value_Less_Than_Zero_Changes_To_Zero ()
  56. {
  57. _textView.SelectionStartColumn = -2;
  58. _textView.SelectionStartRow = -2;
  59. Assert.Equal (0, _textView.SelectionStartColumn);
  60. Assert.Equal (0, _textView.SelectionStartRow);
  61. Assert.Equal (0, _textView.SelectedLength);
  62. Assert.Equal ("", _textView.SelectedText);
  63. }
  64. [Fact]
  65. [InitShutdown]
  66. public void Selection_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  67. {
  68. _textView.CursorPosition = new Point (2, 0);
  69. _textView.SelectionStartColumn = 33;
  70. _textView.SelectionStartRow = 1;
  71. Assert.Equal (32, _textView.SelectionStartColumn);
  72. Assert.Equal (0, _textView.SelectionStartRow);
  73. Assert.Equal (30, _textView.SelectedLength);
  74. Assert.Equal ("B to jump between text fields.", _textView.SelectedText);
  75. }
  76. [Fact]
  77. [InitShutdown]
  78. public void Selection_With_Empty_Text ()
  79. {
  80. _textView = new TextView ();
  81. _textView.CursorPosition = new Point (2, 0);
  82. _textView.SelectionStartColumn = 33;
  83. _textView.SelectionStartRow = 1;
  84. Assert.Equal (0, _textView.SelectionStartColumn);
  85. Assert.Equal (0, _textView.SelectionStartRow);
  86. Assert.Equal (0, _textView.SelectedLength);
  87. Assert.Equal ("", _textView.SelectedText);
  88. }
  89. [Fact]
  90. [InitShutdown]
  91. public void Selection_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  92. {
  93. _textView.CursorPosition = new Point (33, 2);
  94. _textView.SelectionStartColumn = 33;
  95. _textView.SelectionStartRow = 33;
  96. Assert.Equal (32, _textView.CursorPosition.X);
  97. Assert.Equal (0, _textView.CursorPosition.Y);
  98. Assert.Equal (32, _textView.SelectionStartColumn);
  99. Assert.Equal (0, _textView.SelectionStartRow);
  100. Assert.Equal (0, _textView.SelectedLength);
  101. Assert.Equal ("", _textView.SelectedText);
  102. }
  103. [Fact]
  104. [InitShutdown]
  105. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  106. {
  107. _textView.CursorPosition = new Point (-1, -1);
  108. Assert.Equal (0, _textView.CursorPosition.X);
  109. Assert.Equal (0, _textView.CursorPosition.Y);
  110. Assert.Equal (0, _textView.SelectedLength);
  111. Assert.Equal ("", _textView.SelectedText);
  112. }
  113. [Fact]
  114. [InitShutdown]
  115. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  116. {
  117. _textView.CursorPosition = new Point (33, 1);
  118. Assert.Equal (32, _textView.CursorPosition.X);
  119. Assert.Equal (0, _textView.CursorPosition.Y);
  120. Assert.Equal (0, _textView.SelectedLength);
  121. Assert.Equal ("", _textView.SelectedText);
  122. }
  123. [Fact]
  124. [InitShutdown]
  125. public void WordForward_With_No_Selection ()
  126. {
  127. _textView.CursorPosition = new Point (0, 0);
  128. var iteration = 0;
  129. while (_textView.CursorPosition.X < _textView.Text.Length) {
  130. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask, new KeyModifiers ()));
  131. switch (iteration) {
  132. case 0:
  133. Assert.Equal (4, _textView.CursorPosition.X);
  134. Assert.Equal (0, _textView.CursorPosition.Y);
  135. Assert.Equal (0, _textView.SelectionStartColumn);
  136. Assert.Equal (0, _textView.SelectionStartRow);
  137. Assert.Equal (0, _textView.SelectedLength);
  138. Assert.Equal ("", _textView.SelectedText);
  139. break;
  140. case 1:
  141. Assert.Equal (7, _textView.CursorPosition.X);
  142. Assert.Equal (0, _textView.CursorPosition.Y);
  143. Assert.Equal (0, _textView.SelectionStartColumn);
  144. Assert.Equal (0, _textView.SelectionStartRow);
  145. Assert.Equal (0, _textView.SelectedLength);
  146. Assert.Equal ("", _textView.SelectedText);
  147. break;
  148. case 2:
  149. Assert.Equal (12, _textView.CursorPosition.X);
  150. Assert.Equal (0, _textView.CursorPosition.Y);
  151. Assert.Equal (0, _textView.SelectionStartColumn);
  152. Assert.Equal (0, _textView.SelectionStartRow);
  153. Assert.Equal (0, _textView.SelectedLength);
  154. Assert.Equal ("", _textView.SelectedText);
  155. break;
  156. case 3:
  157. Assert.Equal (20, _textView.CursorPosition.X);
  158. Assert.Equal (0, _textView.CursorPosition.Y);
  159. Assert.Equal (0, _textView.SelectionStartColumn);
  160. Assert.Equal (0, _textView.SelectionStartRow);
  161. Assert.Equal (0, _textView.SelectedLength);
  162. Assert.Equal ("", _textView.SelectedText);
  163. break;
  164. case 4:
  165. Assert.Equal (25, _textView.CursorPosition.X);
  166. Assert.Equal (0, _textView.CursorPosition.Y);
  167. Assert.Equal (0, _textView.SelectionStartColumn);
  168. Assert.Equal (0, _textView.SelectionStartRow);
  169. Assert.Equal (0, _textView.SelectedLength);
  170. Assert.Equal ("", _textView.SelectedText);
  171. break;
  172. case 5:
  173. Assert.Equal (32, _textView.CursorPosition.X);
  174. Assert.Equal (0, _textView.CursorPosition.Y);
  175. Assert.Equal (0, _textView.SelectionStartColumn);
  176. Assert.Equal (0, _textView.SelectionStartRow);
  177. Assert.Equal (0, _textView.SelectedLength);
  178. Assert.Equal ("", _textView.SelectedText);
  179. break;
  180. }
  181. iteration++;
  182. }
  183. }
  184. [Fact]
  185. [InitShutdown]
  186. public void WordBackward_With_No_Selection ()
  187. {
  188. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  189. var iteration = 0;
  190. while (_textView.CursorPosition.X > 0) {
  191. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask, new KeyModifiers ()));
  192. switch (iteration) {
  193. case 0:
  194. Assert.Equal (25, _textView.CursorPosition.X);
  195. Assert.Equal (0, _textView.CursorPosition.Y);
  196. Assert.Equal (0, _textView.SelectionStartColumn);
  197. Assert.Equal (0, _textView.SelectionStartRow);
  198. Assert.Equal (0, _textView.SelectedLength);
  199. Assert.Equal ("", _textView.SelectedText);
  200. break;
  201. case 1:
  202. Assert.Equal (20, _textView.CursorPosition.X);
  203. Assert.Equal (0, _textView.CursorPosition.Y);
  204. Assert.Equal (0, _textView.SelectionStartColumn);
  205. Assert.Equal (0, _textView.SelectionStartRow);
  206. Assert.Equal (0, _textView.SelectedLength);
  207. Assert.Equal ("", _textView.SelectedText);
  208. break;
  209. case 2:
  210. Assert.Equal (12, _textView.CursorPosition.X);
  211. Assert.Equal (0, _textView.CursorPosition.Y);
  212. Assert.Equal (0, _textView.SelectionStartColumn);
  213. Assert.Equal (0, _textView.SelectionStartRow);
  214. Assert.Equal (0, _textView.SelectedLength);
  215. Assert.Equal ("", _textView.SelectedText);
  216. break;
  217. case 3:
  218. Assert.Equal (7, _textView.CursorPosition.X);
  219. Assert.Equal (0, _textView.CursorPosition.Y);
  220. Assert.Equal (0, _textView.SelectionStartColumn);
  221. Assert.Equal (0, _textView.SelectionStartRow);
  222. Assert.Equal (0, _textView.SelectedLength);
  223. Assert.Equal ("", _textView.SelectedText);
  224. break;
  225. case 4:
  226. Assert.Equal (4, _textView.CursorPosition.X);
  227. Assert.Equal (0, _textView.CursorPosition.Y);
  228. Assert.Equal (0, _textView.SelectionStartColumn);
  229. Assert.Equal (0, _textView.SelectionStartRow);
  230. Assert.Equal (0, _textView.SelectedLength);
  231. Assert.Equal ("", _textView.SelectedText);
  232. break;
  233. case 5:
  234. Assert.Equal (0, _textView.CursorPosition.X);
  235. Assert.Equal (0, _textView.CursorPosition.Y);
  236. Assert.Equal (0, _textView.SelectionStartColumn);
  237. Assert.Equal (0, _textView.SelectionStartRow);
  238. Assert.Equal (0, _textView.SelectedLength);
  239. Assert.Equal ("", _textView.SelectedText);
  240. break;
  241. }
  242. iteration++;
  243. }
  244. }
  245. [Fact]
  246. [InitShutdown]
  247. public void WordForward_With_Selection ()
  248. {
  249. _textView.CursorPosition = new Point (0, 0);
  250. _textView.SelectionStartColumn = 0;
  251. _textView.SelectionStartRow = 0;
  252. var iteration = 0;
  253. while (_textView.CursorPosition.X < _textView.Text.Length) {
  254. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  255. switch (iteration) {
  256. case 0:
  257. Assert.Equal (4, _textView.CursorPosition.X);
  258. Assert.Equal (0, _textView.CursorPosition.Y);
  259. Assert.Equal (0, _textView.SelectionStartColumn);
  260. Assert.Equal (0, _textView.SelectionStartRow);
  261. Assert.Equal (4, _textView.SelectedLength);
  262. Assert.Equal ("TAB ", _textView.SelectedText);
  263. break;
  264. case 1:
  265. Assert.Equal (7, _textView.CursorPosition.X);
  266. Assert.Equal (0, _textView.CursorPosition.Y);
  267. Assert.Equal (0, _textView.SelectionStartColumn);
  268. Assert.Equal (0, _textView.SelectionStartRow);
  269. Assert.Equal (7, _textView.SelectedLength);
  270. Assert.Equal ("TAB to ", _textView.SelectedText);
  271. break;
  272. case 2:
  273. Assert.Equal (12, _textView.CursorPosition.X);
  274. Assert.Equal (0, _textView.CursorPosition.Y);
  275. Assert.Equal (0, _textView.SelectionStartColumn);
  276. Assert.Equal (0, _textView.SelectionStartRow);
  277. Assert.Equal (12, _textView.SelectedLength);
  278. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  279. break;
  280. case 3:
  281. Assert.Equal (20, _textView.CursorPosition.X);
  282. Assert.Equal (0, _textView.CursorPosition.Y);
  283. Assert.Equal (0, _textView.SelectionStartColumn);
  284. Assert.Equal (0, _textView.SelectionStartRow);
  285. Assert.Equal (20, _textView.SelectedLength);
  286. Assert.Equal ("TAB to jump between ", _textView.SelectedText);
  287. break;
  288. case 4:
  289. Assert.Equal (25, _textView.CursorPosition.X);
  290. Assert.Equal (0, _textView.CursorPosition.Y);
  291. Assert.Equal (0, _textView.SelectionStartColumn);
  292. Assert.Equal (0, _textView.SelectionStartRow);
  293. Assert.Equal (25, _textView.SelectedLength);
  294. Assert.Equal ("TAB to jump between text ", _textView.SelectedText);
  295. break;
  296. case 5:
  297. Assert.Equal (32, _textView.CursorPosition.X);
  298. Assert.Equal (0, _textView.CursorPosition.Y);
  299. Assert.Equal (0, _textView.SelectionStartColumn);
  300. Assert.Equal (0, _textView.SelectionStartRow);
  301. Assert.Equal (32, _textView.SelectedLength);
  302. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  303. break;
  304. }
  305. iteration++;
  306. }
  307. }
  308. [Fact]
  309. [InitShutdown]
  310. public void WordBackward_With_Selection ()
  311. {
  312. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  313. _textView.SelectionStartColumn = _textView.Text.Length;
  314. _textView.SelectionStartRow = 0;
  315. var iteration = 0;
  316. while (_textView.CursorPosition.X > 0) {
  317. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  318. switch (iteration) {
  319. case 0:
  320. Assert.Equal (25, _textView.CursorPosition.X);
  321. Assert.Equal (0, _textView.CursorPosition.Y);
  322. Assert.Equal (32, _textView.SelectionStartColumn);
  323. Assert.Equal (0, _textView.SelectionStartRow);
  324. Assert.Equal (7, _textView.SelectedLength);
  325. Assert.Equal ("fields.", _textView.SelectedText);
  326. break;
  327. case 1:
  328. Assert.Equal (20, _textView.CursorPosition.X);
  329. Assert.Equal (0, _textView.CursorPosition.Y);
  330. Assert.Equal (32, _textView.SelectionStartColumn);
  331. Assert.Equal (0, _textView.SelectionStartRow);
  332. Assert.Equal (12, _textView.SelectedLength);
  333. Assert.Equal ("text fields.", _textView.SelectedText);
  334. break;
  335. case 2:
  336. Assert.Equal (12, _textView.CursorPosition.X);
  337. Assert.Equal (0, _textView.CursorPosition.Y);
  338. Assert.Equal (32, _textView.SelectionStartColumn);
  339. Assert.Equal (0, _textView.SelectionStartRow);
  340. Assert.Equal (20, _textView.SelectedLength);
  341. Assert.Equal ("between text fields.", _textView.SelectedText);
  342. break;
  343. case 3:
  344. Assert.Equal (7, _textView.CursorPosition.X);
  345. Assert.Equal (0, _textView.CursorPosition.Y);
  346. Assert.Equal (32, _textView.SelectionStartColumn);
  347. Assert.Equal (0, _textView.SelectionStartRow);
  348. Assert.Equal (25, _textView.SelectedLength);
  349. Assert.Equal ("jump between text fields.", _textView.SelectedText);
  350. break;
  351. case 4:
  352. Assert.Equal (4, _textView.CursorPosition.X);
  353. Assert.Equal (0, _textView.CursorPosition.Y);
  354. Assert.Equal (32, _textView.SelectionStartColumn);
  355. Assert.Equal (0, _textView.SelectionStartRow);
  356. Assert.Equal (28, _textView.SelectedLength);
  357. Assert.Equal ("to jump between text fields.", _textView.SelectedText);
  358. break;
  359. case 5:
  360. Assert.Equal (0, _textView.CursorPosition.X);
  361. Assert.Equal (0, _textView.CursorPosition.Y);
  362. Assert.Equal (32, _textView.SelectionStartColumn);
  363. Assert.Equal (0, _textView.SelectionStartRow);
  364. Assert.Equal (32, _textView.SelectedLength);
  365. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  366. break;
  367. }
  368. iteration++;
  369. }
  370. }
  371. [Fact]
  372. [InitShutdown]
  373. public void WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  374. {
  375. _textView.CursorPosition = new Point (10, 0);
  376. _textView.SelectionStartColumn = 10;
  377. _textView.SelectionStartRow = 0;
  378. var iteration = 0;
  379. while (_textView.CursorPosition.X < _textView.Text.Length) {
  380. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  381. switch (iteration) {
  382. case 0:
  383. Assert.Equal (12, _textView.CursorPosition.X);
  384. Assert.Equal (0, _textView.CursorPosition.Y);
  385. Assert.Equal (10, _textView.SelectionStartColumn);
  386. Assert.Equal (0, _textView.SelectionStartRow);
  387. Assert.Equal (2, _textView.SelectedLength);
  388. Assert.Equal ("p ", _textView.SelectedText);
  389. break;
  390. case 1:
  391. Assert.Equal (20, _textView.CursorPosition.X);
  392. Assert.Equal (0, _textView.CursorPosition.Y);
  393. Assert.Equal (10, _textView.SelectionStartColumn);
  394. Assert.Equal (0, _textView.SelectionStartRow);
  395. Assert.Equal (10, _textView.SelectedLength);
  396. Assert.Equal ("p between ", _textView.SelectedText);
  397. break;
  398. case 2:
  399. Assert.Equal (25, _textView.CursorPosition.X);
  400. Assert.Equal (0, _textView.CursorPosition.Y);
  401. Assert.Equal (10, _textView.SelectionStartColumn);
  402. Assert.Equal (0, _textView.SelectionStartRow);
  403. Assert.Equal (15, _textView.SelectedLength);
  404. Assert.Equal ("p between text ", _textView.SelectedText);
  405. break;
  406. case 3:
  407. Assert.Equal (32, _textView.CursorPosition.X);
  408. Assert.Equal (0, _textView.CursorPosition.Y);
  409. Assert.Equal (10, _textView.SelectionStartColumn);
  410. Assert.Equal (0, _textView.SelectionStartRow);
  411. Assert.Equal (22, _textView.SelectedLength);
  412. Assert.Equal ("p between text fields.", _textView.SelectedText);
  413. break;
  414. }
  415. iteration++;
  416. }
  417. }
  418. [Fact]
  419. [InitShutdown]
  420. public void WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  421. {
  422. _textView.CursorPosition = new Point (10, 0);
  423. _textView.SelectionStartColumn = 10;
  424. _textView.SelectionStartRow = 0;
  425. var iteration = 0;
  426. while (_textView.CursorPosition.X > 0) {
  427. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  428. switch (iteration) {
  429. case 0:
  430. Assert.Equal (7, _textView.CursorPosition.X);
  431. Assert.Equal (0, _textView.CursorPosition.Y);
  432. Assert.Equal (10, _textView.SelectionStartColumn);
  433. Assert.Equal (0, _textView.SelectionStartRow);
  434. Assert.Equal (3, _textView.SelectedLength);
  435. Assert.Equal ("jum", _textView.SelectedText);
  436. break;
  437. case 1:
  438. Assert.Equal (4, _textView.CursorPosition.X);
  439. Assert.Equal (0, _textView.CursorPosition.Y);
  440. Assert.Equal (10, _textView.SelectionStartColumn);
  441. Assert.Equal (0, _textView.SelectionStartRow);
  442. Assert.Equal (6, _textView.SelectedLength);
  443. Assert.Equal ("to jum", _textView.SelectedText);
  444. break;
  445. case 2:
  446. Assert.Equal (0, _textView.CursorPosition.X);
  447. Assert.Equal (0, _textView.CursorPosition.Y);
  448. Assert.Equal (10, _textView.SelectionStartColumn);
  449. Assert.Equal (0, _textView.SelectionStartRow);
  450. Assert.Equal (10, _textView.SelectedLength);
  451. Assert.Equal ("TAB to jum", _textView.SelectedText);
  452. break;
  453. }
  454. iteration++;
  455. }
  456. }
  457. [Fact]
  458. [InitShutdown]
  459. public void WordForward_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. _textView.Text = "TAB t o jump b etween t ext f ields .";
  464. _textView.CursorPosition = new Point (0, 0);
  465. var iteration = 0;
  466. while (_textView.CursorPosition.X < _textView.Text.Length) {
  467. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask, new KeyModifiers ()));
  468. switch (iteration) {
  469. case 0:
  470. Assert.Equal (6, _textView.CursorPosition.X);
  471. Assert.Equal (0, _textView.CursorPosition.Y);
  472. Assert.Equal (0, _textView.SelectionStartColumn);
  473. Assert.Equal (0, _textView.SelectionStartRow);
  474. Assert.Equal (0, _textView.SelectedLength);
  475. Assert.Equal ("", _textView.SelectedText);
  476. break;
  477. case 1:
  478. Assert.Equal (9, _textView.CursorPosition.X);
  479. Assert.Equal (0, _textView.CursorPosition.Y);
  480. Assert.Equal (0, _textView.SelectionStartColumn);
  481. Assert.Equal (0, _textView.SelectionStartRow);
  482. Assert.Equal (0, _textView.SelectedLength);
  483. Assert.Equal ("", _textView.SelectedText);
  484. break;
  485. case 2:
  486. Assert.Equal (12, _textView.CursorPosition.X);
  487. Assert.Equal (0, _textView.CursorPosition.Y);
  488. Assert.Equal (0, _textView.SelectionStartColumn);
  489. Assert.Equal (0, _textView.SelectionStartRow);
  490. Assert.Equal (0, _textView.SelectedLength);
  491. Assert.Equal ("", _textView.SelectedText);
  492. break;
  493. case 3:
  494. Assert.Equal (25, _textView.CursorPosition.X);
  495. Assert.Equal (0, _textView.CursorPosition.Y);
  496. Assert.Equal (0, _textView.SelectionStartColumn);
  497. Assert.Equal (0, _textView.SelectionStartRow);
  498. Assert.Equal (0, _textView.SelectedLength);
  499. Assert.Equal ("", _textView.SelectedText);
  500. break;
  501. case 4:
  502. Assert.Equal (28, _textView.CursorPosition.X);
  503. Assert.Equal (0, _textView.CursorPosition.Y);
  504. Assert.Equal (0, _textView.SelectionStartColumn);
  505. Assert.Equal (0, _textView.SelectionStartRow);
  506. Assert.Equal (0, _textView.SelectedLength);
  507. Assert.Equal ("", _textView.SelectedText);
  508. break;
  509. case 5:
  510. Assert.Equal (38, _textView.CursorPosition.X);
  511. Assert.Equal (0, _textView.CursorPosition.Y);
  512. Assert.Equal (0, _textView.SelectionStartColumn);
  513. Assert.Equal (0, _textView.SelectionStartRow);
  514. Assert.Equal (0, _textView.SelectedLength);
  515. Assert.Equal ("", _textView.SelectedText);
  516. break;
  517. case 6:
  518. Assert.Equal (40, _textView.CursorPosition.X);
  519. Assert.Equal (0, _textView.CursorPosition.Y);
  520. Assert.Equal (0, _textView.SelectionStartColumn);
  521. Assert.Equal (0, _textView.SelectionStartRow);
  522. Assert.Equal (0, _textView.SelectedLength);
  523. Assert.Equal ("", _textView.SelectedText);
  524. break;
  525. case 7:
  526. Assert.Equal (46, _textView.CursorPosition.X);
  527. Assert.Equal (0, _textView.CursorPosition.Y);
  528. Assert.Equal (0, _textView.SelectionStartColumn);
  529. Assert.Equal (0, _textView.SelectionStartRow);
  530. Assert.Equal (0, _textView.SelectedLength);
  531. Assert.Equal ("", _textView.SelectedText);
  532. break;
  533. case 8:
  534. Assert.Equal (48, _textView.CursorPosition.X);
  535. Assert.Equal (0, _textView.CursorPosition.Y);
  536. Assert.Equal (0, _textView.SelectionStartColumn);
  537. Assert.Equal (0, _textView.SelectionStartRow);
  538. Assert.Equal (0, _textView.SelectedLength);
  539. Assert.Equal ("", _textView.SelectedText);
  540. break;
  541. case 9:
  542. Assert.Equal (55, _textView.CursorPosition.X);
  543. Assert.Equal (0, _textView.CursorPosition.Y);
  544. Assert.Equal (0, _textView.SelectionStartColumn);
  545. Assert.Equal (0, _textView.SelectionStartRow);
  546. Assert.Equal (0, _textView.SelectedLength);
  547. Assert.Equal ("", _textView.SelectedText);
  548. break;
  549. }
  550. iteration++;
  551. }
  552. }
  553. [Fact]
  554. [InitShutdown]
  555. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  556. {
  557. // 1 2 3 4 5
  558. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  559. _textView.Text = "TAB t o jump b etween t ext f ields .";
  560. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  561. var iteration = 0;
  562. while (_textView.CursorPosition.X > 0) {
  563. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask, new KeyModifiers ()));
  564. switch (iteration) {
  565. case 0:
  566. Assert.Equal (54, _textView.CursorPosition.X);
  567. Assert.Equal (0, _textView.CursorPosition.Y);
  568. Assert.Equal (0, _textView.SelectionStartColumn);
  569. Assert.Equal (0, _textView.SelectionStartRow);
  570. Assert.Equal (0, _textView.SelectedLength);
  571. Assert.Equal ("", _textView.SelectedText);
  572. break;
  573. case 1:
  574. Assert.Equal (48, _textView.CursorPosition.X);
  575. Assert.Equal (0, _textView.CursorPosition.Y);
  576. Assert.Equal (0, _textView.SelectionStartColumn);
  577. Assert.Equal (0, _textView.SelectionStartRow);
  578. Assert.Equal (0, _textView.SelectedLength);
  579. Assert.Equal ("", _textView.SelectedText);
  580. break;
  581. case 2:
  582. Assert.Equal (46, _textView.CursorPosition.X);
  583. Assert.Equal (0, _textView.CursorPosition.Y);
  584. Assert.Equal (0, _textView.SelectionStartColumn);
  585. Assert.Equal (0, _textView.SelectionStartRow);
  586. Assert.Equal (0, _textView.SelectedLength);
  587. Assert.Equal ("", _textView.SelectedText);
  588. break;
  589. case 3:
  590. Assert.Equal (40, _textView.CursorPosition.X);
  591. Assert.Equal (0, _textView.CursorPosition.Y);
  592. Assert.Equal (0, _textView.SelectionStartColumn);
  593. Assert.Equal (0, _textView.SelectionStartRow);
  594. Assert.Equal (0, _textView.SelectedLength);
  595. Assert.Equal ("", _textView.SelectedText);
  596. break;
  597. case 4:
  598. Assert.Equal (38, _textView.CursorPosition.X);
  599. Assert.Equal (0, _textView.CursorPosition.Y);
  600. Assert.Equal (0, _textView.SelectionStartColumn);
  601. Assert.Equal (0, _textView.SelectionStartRow);
  602. Assert.Equal (0, _textView.SelectedLength);
  603. Assert.Equal ("", _textView.SelectedText);
  604. break;
  605. case 5:
  606. Assert.Equal (28, _textView.CursorPosition.X);
  607. Assert.Equal (0, _textView.CursorPosition.Y);
  608. Assert.Equal (0, _textView.SelectionStartColumn);
  609. Assert.Equal (0, _textView.SelectionStartRow);
  610. Assert.Equal (0, _textView.SelectedLength);
  611. Assert.Equal ("", _textView.SelectedText);
  612. break;
  613. case 6:
  614. Assert.Equal (25, _textView.CursorPosition.X);
  615. Assert.Equal (0, _textView.CursorPosition.Y);
  616. Assert.Equal (0, _textView.SelectionStartColumn);
  617. Assert.Equal (0, _textView.SelectionStartRow);
  618. Assert.Equal (0, _textView.SelectedLength);
  619. Assert.Equal ("", _textView.SelectedText);
  620. break;
  621. case 7:
  622. Assert.Equal (12, _textView.CursorPosition.X);
  623. Assert.Equal (0, _textView.CursorPosition.Y);
  624. Assert.Equal (0, _textView.SelectionStartColumn);
  625. Assert.Equal (0, _textView.SelectionStartRow);
  626. Assert.Equal (0, _textView.SelectedLength);
  627. Assert.Equal ("", _textView.SelectedText);
  628. break;
  629. case 8:
  630. Assert.Equal (9, _textView.CursorPosition.X);
  631. Assert.Equal (0, _textView.CursorPosition.Y);
  632. Assert.Equal (0, _textView.SelectionStartColumn);
  633. Assert.Equal (0, _textView.SelectionStartRow);
  634. Assert.Equal (0, _textView.SelectedLength);
  635. Assert.Equal ("", _textView.SelectedText);
  636. break;
  637. case 9:
  638. Assert.Equal (6, _textView.CursorPosition.X);
  639. Assert.Equal (0, _textView.CursorPosition.Y);
  640. Assert.Equal (0, _textView.SelectionStartColumn);
  641. Assert.Equal (0, _textView.SelectionStartRow);
  642. Assert.Equal (0, _textView.SelectedLength);
  643. Assert.Equal ("", _textView.SelectedText);
  644. break;
  645. case 10:
  646. Assert.Equal (0, _textView.CursorPosition.X);
  647. Assert.Equal (0, _textView.CursorPosition.Y);
  648. Assert.Equal (0, _textView.SelectionStartColumn);
  649. Assert.Equal (0, _textView.SelectionStartRow);
  650. Assert.Equal (0, _textView.SelectedLength);
  651. Assert.Equal ("", _textView.SelectedText);
  652. break;
  653. }
  654. iteration++;
  655. }
  656. }
  657. [Fact]
  658. [InitShutdown]
  659. public void WordBackward_Multiline_With_Selection ()
  660. {
  661. // 4 3 2 1
  662. // 87654321098765432109876 54321098765432109876543210-Length
  663. // 1 2 1 2
  664. // 01234567890123456789012 0123456789012345678901234
  665. _textView.Text = "This is the first line.\nThis is the second line.";
  666. _textView.MoveEnd ();
  667. _textView.SelectionStartColumn = _textView.CurrentColumn;
  668. _textView.SelectionStartRow = _textView.CurrentRow;
  669. var iteration = 0;
  670. bool iterationsFinished = false;
  671. while (!iterationsFinished) {
  672. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  673. switch (iteration) {
  674. case 0:
  675. Assert.Equal (19, _textView.CursorPosition.X);
  676. Assert.Equal (1, _textView.CursorPosition.Y);
  677. Assert.Equal (24, _textView.SelectionStartColumn);
  678. Assert.Equal (1, _textView.SelectionStartRow);
  679. Assert.Equal (5, _textView.SelectedLength);
  680. Assert.Equal ("line.", _textView.SelectedText);
  681. break;
  682. case 1:
  683. Assert.Equal (12, _textView.CursorPosition.X);
  684. Assert.Equal (1, _textView.CursorPosition.Y);
  685. Assert.Equal (24, _textView.SelectionStartColumn);
  686. Assert.Equal (1, _textView.SelectionStartRow);
  687. Assert.Equal (12, _textView.SelectedLength);
  688. Assert.Equal ("second line.", _textView.SelectedText);
  689. break;
  690. case 2:
  691. Assert.Equal (8, _textView.CursorPosition.X);
  692. Assert.Equal (1, _textView.CursorPosition.Y);
  693. Assert.Equal (24, _textView.SelectionStartColumn);
  694. Assert.Equal (1, _textView.SelectionStartRow);
  695. Assert.Equal (16, _textView.SelectedLength);
  696. Assert.Equal ("the second line.", _textView.SelectedText);
  697. break;
  698. case 3:
  699. Assert.Equal (5, _textView.CursorPosition.X);
  700. Assert.Equal (1, _textView.CursorPosition.Y);
  701. Assert.Equal (24, _textView.SelectionStartColumn);
  702. Assert.Equal (1, _textView.SelectionStartRow);
  703. Assert.Equal (19, _textView.SelectedLength);
  704. Assert.Equal ("is the second line.", _textView.SelectedText);
  705. break;
  706. case 4:
  707. Assert.Equal (0, _textView.CursorPosition.X);
  708. Assert.Equal (1, _textView.CursorPosition.Y);
  709. Assert.Equal (24, _textView.SelectionStartColumn);
  710. Assert.Equal (1, _textView.SelectionStartRow);
  711. Assert.Equal (24, _textView.SelectedLength);
  712. Assert.Equal ("This is the second line.", _textView.SelectedText);
  713. break;
  714. case 5:
  715. Assert.Equal (23, _textView.CursorPosition.X);
  716. Assert.Equal (0, _textView.CursorPosition.Y);
  717. Assert.Equal (24, _textView.SelectionStartColumn);
  718. Assert.Equal (1, _textView.SelectionStartRow);
  719. Assert.Equal (25, _textView.SelectedLength);
  720. Assert.Equal ("\nThis is the second line.", _textView.SelectedText);
  721. break;
  722. case 6:
  723. Assert.Equal (18, _textView.CursorPosition.X);
  724. Assert.Equal (0, _textView.CursorPosition.Y);
  725. Assert.Equal (24, _textView.SelectionStartColumn);
  726. Assert.Equal (1, _textView.SelectionStartRow);
  727. Assert.Equal (30, _textView.SelectedLength);
  728. Assert.Equal ("line.\nThis is the second line.", _textView.SelectedText);
  729. break;
  730. case 7:
  731. Assert.Equal (12, _textView.CursorPosition.X);
  732. Assert.Equal (0, _textView.CursorPosition.Y);
  733. Assert.Equal (24, _textView.SelectionStartColumn);
  734. Assert.Equal (1, _textView.SelectionStartRow);
  735. Assert.Equal (36, _textView.SelectedLength);
  736. Assert.Equal ("first line.\nThis is the second line.", _textView.SelectedText);
  737. break;
  738. case 8:
  739. Assert.Equal (8, _textView.CursorPosition.X);
  740. Assert.Equal (0, _textView.CursorPosition.Y);
  741. Assert.Equal (24, _textView.SelectionStartColumn);
  742. Assert.Equal (1, _textView.SelectionStartRow);
  743. Assert.Equal (40, _textView.SelectedLength);
  744. Assert.Equal ("the first line.\nThis is the second line.", _textView.SelectedText);
  745. break;
  746. case 9:
  747. Assert.Equal (5, _textView.CursorPosition.X);
  748. Assert.Equal (0, _textView.CursorPosition.Y);
  749. Assert.Equal (24, _textView.SelectionStartColumn);
  750. Assert.Equal (1, _textView.SelectionStartRow);
  751. Assert.Equal (43, _textView.SelectedLength);
  752. Assert.Equal ("is the first line.\nThis is the second line.", _textView.SelectedText);
  753. break;
  754. case 10:
  755. Assert.Equal (0, _textView.CursorPosition.X);
  756. Assert.Equal (0, _textView.CursorPosition.Y);
  757. Assert.Equal (24, _textView.SelectionStartColumn);
  758. Assert.Equal (1, _textView.SelectionStartRow);
  759. Assert.Equal (48, _textView.SelectedLength);
  760. Assert.Equal ("This is the first line.\nThis is the second line.", _textView.SelectedText);
  761. break;
  762. default:
  763. iterationsFinished = true;
  764. break;
  765. }
  766. iteration++;
  767. }
  768. }
  769. [Fact]
  770. [InitShutdown]
  771. public void WordForward_Multiline_With_Selection ()
  772. {
  773. // 1 2 3 4
  774. // 01234567890123456789012 34567890123456789012345678-Length
  775. // 1 2 1 2
  776. // 01234567890123456789012 0123456789012345678901234
  777. _textView.Text = "This is the first line.\nThis is the second line.";
  778. _textView.SelectionStartColumn = _textView.CurrentColumn;
  779. _textView.SelectionStartRow = _textView.CurrentRow;
  780. var iteration = 0;
  781. bool iterationsFinished = false;
  782. while (!iterationsFinished) {
  783. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  784. switch (iteration) {
  785. case 0:
  786. Assert.Equal (5, _textView.CursorPosition.X);
  787. Assert.Equal (0, _textView.CursorPosition.Y);
  788. Assert.Equal (0, _textView.SelectionStartColumn);
  789. Assert.Equal (0, _textView.SelectionStartRow);
  790. Assert.Equal (5, _textView.SelectedLength);
  791. Assert.Equal ("This ", _textView.SelectedText);
  792. break;
  793. case 1:
  794. Assert.Equal (8, _textView.CursorPosition.X);
  795. Assert.Equal (0, _textView.CursorPosition.Y);
  796. Assert.Equal (0, _textView.SelectionStartColumn);
  797. Assert.Equal (0, _textView.SelectionStartRow);
  798. Assert.Equal (8, _textView.SelectedLength);
  799. Assert.Equal ("This is ", _textView.SelectedText);
  800. break;
  801. case 2:
  802. Assert.Equal (12, _textView.CursorPosition.X);
  803. Assert.Equal (0, _textView.CursorPosition.Y);
  804. Assert.Equal (0, _textView.SelectionStartColumn);
  805. Assert.Equal (0, _textView.SelectionStartRow);
  806. Assert.Equal (12, _textView.SelectedLength);
  807. Assert.Equal ("This is the ", _textView.SelectedText);
  808. break;
  809. case 3:
  810. Assert.Equal (18, _textView.CursorPosition.X);
  811. Assert.Equal (0, _textView.CursorPosition.Y);
  812. Assert.Equal (0, _textView.SelectionStartColumn);
  813. Assert.Equal (0, _textView.SelectionStartRow);
  814. Assert.Equal (18, _textView.SelectedLength);
  815. Assert.Equal ("This is the first ", _textView.SelectedText);
  816. break;
  817. case 4:
  818. Assert.Equal (23, _textView.CursorPosition.X);
  819. Assert.Equal (0, _textView.CursorPosition.Y);
  820. Assert.Equal (0, _textView.SelectionStartColumn);
  821. Assert.Equal (0, _textView.SelectionStartRow);
  822. Assert.Equal (23, _textView.SelectedLength);
  823. Assert.Equal ("This is the first line.", _textView.SelectedText);
  824. break;
  825. case 5:
  826. Assert.Equal (0, _textView.CursorPosition.X);
  827. Assert.Equal (1, _textView.CursorPosition.Y);
  828. Assert.Equal (0, _textView.SelectionStartColumn);
  829. Assert.Equal (0, _textView.SelectionStartRow);
  830. Assert.Equal (24, _textView.SelectedLength);
  831. Assert.Equal ("This is the first line.\n", _textView.SelectedText);
  832. break;
  833. case 6:
  834. Assert.Equal (5, _textView.CursorPosition.X);
  835. Assert.Equal (1, _textView.CursorPosition.Y);
  836. Assert.Equal (0, _textView.SelectionStartColumn);
  837. Assert.Equal (0, _textView.SelectionStartRow);
  838. Assert.Equal (29, _textView.SelectedLength);
  839. Assert.Equal ("This is the first line.\nThis ", _textView.SelectedText);
  840. break;
  841. case 7:
  842. Assert.Equal (8, _textView.CursorPosition.X);
  843. Assert.Equal (1, _textView.CursorPosition.Y);
  844. Assert.Equal (0, _textView.SelectionStartColumn);
  845. Assert.Equal (0, _textView.SelectionStartRow);
  846. Assert.Equal (32, _textView.SelectedLength);
  847. Assert.Equal ("This is the first line.\nThis is ", _textView.SelectedText);
  848. break;
  849. case 8:
  850. Assert.Equal (12, _textView.CursorPosition.X);
  851. Assert.Equal (1, _textView.CursorPosition.Y);
  852. Assert.Equal (0, _textView.SelectionStartColumn);
  853. Assert.Equal (0, _textView.SelectionStartRow);
  854. Assert.Equal (36, _textView.SelectedLength);
  855. Assert.Equal ("This is the first line.\nThis is the ", _textView.SelectedText);
  856. break;
  857. case 9:
  858. Assert.Equal (19, _textView.CursorPosition.X);
  859. Assert.Equal (1, _textView.CursorPosition.Y);
  860. Assert.Equal (0, _textView.SelectionStartColumn);
  861. Assert.Equal (0, _textView.SelectionStartRow);
  862. Assert.Equal (43, _textView.SelectedLength);
  863. Assert.Equal ("This is the first line.\nThis is the second ", _textView.SelectedText);
  864. break;
  865. case 10:
  866. Assert.Equal (24, _textView.CursorPosition.X);
  867. Assert.Equal (1, _textView.CursorPosition.Y);
  868. Assert.Equal (0, _textView.SelectionStartColumn);
  869. Assert.Equal (0, _textView.SelectionStartRow);
  870. Assert.Equal (48, _textView.SelectedLength);
  871. Assert.Equal ("This is the first line.\nThis is the second line.", _textView.SelectedText);
  872. break;
  873. default:
  874. iterationsFinished = true;
  875. break;
  876. }
  877. iteration++;
  878. }
  879. }
  880. [Fact]
  881. [InitShutdown]
  882. public void Kill_To_End_Delete_Forwards_And_Copy_To_The_Clipboard ()
  883. {
  884. _textView.Text = "This is the first line.\nThis is the second line.";
  885. var iteration = 0;
  886. bool iterationsFinished = false;
  887. while (!iterationsFinished) {
  888. _textView.ProcessKey (new KeyEvent (Key.DeleteChar | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  889. switch (iteration) {
  890. case 0:
  891. Assert.Equal (0, _textView.CursorPosition.X);
  892. Assert.Equal (0, _textView.CursorPosition.Y);
  893. Assert.Equal ($"{System.Environment.NewLine}This is the second line.", _textView.Text);
  894. break;
  895. case 1:
  896. Assert.Equal (0, _textView.CursorPosition.X);
  897. Assert.Equal (0, _textView.CursorPosition.Y);
  898. Assert.Equal ("This is the second line.", _textView.Text);
  899. break;
  900. case 2:
  901. Assert.Equal (0, _textView.CursorPosition.X);
  902. Assert.Equal (0, _textView.CursorPosition.Y);
  903. Assert.Equal ("", _textView.Text);
  904. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  905. Assert.Equal ("This is the second line.", _textView.Text);
  906. break;
  907. default:
  908. iterationsFinished = true;
  909. break;
  910. }
  911. iteration++;
  912. }
  913. }
  914. [Fact]
  915. [InitShutdown]
  916. public void Kill_To_Start_Delete_Backwards_And_Copy_To_The_Clipboard ()
  917. {
  918. _textView.Text = "This is the first line.\nThis is the second line.";
  919. _textView.MoveEnd ();
  920. var iteration = 0;
  921. bool iterationsFinished = false;
  922. while (!iterationsFinished) {
  923. _textView.ProcessKey (new KeyEvent (Key.Backspace | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  924. switch (iteration) {
  925. case 0:
  926. Assert.Equal (0, _textView.CursorPosition.X);
  927. Assert.Equal (1, _textView.CursorPosition.Y);
  928. Assert.Equal ($"This is the first line.{System.Environment.NewLine}", _textView.Text);
  929. break;
  930. case 1:
  931. Assert.Equal (23, _textView.CursorPosition.X);
  932. Assert.Equal (0, _textView.CursorPosition.Y);
  933. Assert.Equal ("This is the first line.", _textView.Text);
  934. break;
  935. case 2:
  936. Assert.Equal (0, _textView.CursorPosition.X);
  937. Assert.Equal (0, _textView.CursorPosition.Y);
  938. Assert.Equal ("", _textView.Text);
  939. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  940. Assert.Equal ("This is the first line.", _textView.Text);
  941. break;
  942. default:
  943. iterationsFinished = true;
  944. break;
  945. }
  946. iteration++;
  947. }
  948. }
  949. [Fact]
  950. [InitShutdown]
  951. public void Kill_Delete_WordForward ()
  952. {
  953. _textView.Text = "This is the first line.";
  954. var iteration = 0;
  955. bool iterationsFinished = false;
  956. while (!iterationsFinished) {
  957. _textView.ProcessKey (new KeyEvent (Key.DeleteChar | Key.CtrlMask, new KeyModifiers ()));
  958. switch (iteration) {
  959. case 0:
  960. Assert.Equal (0, _textView.CursorPosition.X);
  961. Assert.Equal (0, _textView.CursorPosition.Y);
  962. Assert.Equal ("is the first line.", _textView.Text);
  963. break;
  964. case 1:
  965. Assert.Equal (0, _textView.CursorPosition.X);
  966. Assert.Equal (0, _textView.CursorPosition.Y);
  967. Assert.Equal ("the first line.", _textView.Text);
  968. break;
  969. case 2:
  970. Assert.Equal (0, _textView.CursorPosition.X);
  971. Assert.Equal (0, _textView.CursorPosition.Y);
  972. Assert.Equal ("first line.", _textView.Text);
  973. break;
  974. case 3:
  975. Assert.Equal (0, _textView.CursorPosition.X);
  976. Assert.Equal (0, _textView.CursorPosition.Y);
  977. Assert.Equal ("line.", _textView.Text);
  978. break;
  979. case 4:
  980. Assert.Equal (0, _textView.CursorPosition.X);
  981. Assert.Equal (0, _textView.CursorPosition.Y);
  982. Assert.Equal ("", _textView.Text);
  983. break;
  984. default:
  985. iterationsFinished = true;
  986. break;
  987. }
  988. iteration++;
  989. }
  990. }
  991. [Fact]
  992. [InitShutdown]
  993. public void Kill_Delete_WordBackward ()
  994. {
  995. _textView.Text = "This is the first line.";
  996. _textView.MoveEnd ();
  997. var iteration = 0;
  998. bool iterationsFinished = false;
  999. while (!iterationsFinished) {
  1000. _textView.ProcessKey (new KeyEvent (Key.Backspace | Key.CtrlMask, new KeyModifiers ()));
  1001. switch (iteration) {
  1002. case 0:
  1003. Assert.Equal (18, _textView.CursorPosition.X);
  1004. Assert.Equal (0, _textView.CursorPosition.Y);
  1005. Assert.Equal ("This is the first ", _textView.Text);
  1006. break;
  1007. case 1:
  1008. Assert.Equal (12, _textView.CursorPosition.X);
  1009. Assert.Equal (0, _textView.CursorPosition.Y);
  1010. Assert.Equal ("This is the ", _textView.Text);
  1011. break;
  1012. case 2:
  1013. Assert.Equal (8, _textView.CursorPosition.X);
  1014. Assert.Equal (0, _textView.CursorPosition.Y);
  1015. Assert.Equal ("This is ", _textView.Text);
  1016. break;
  1017. case 3:
  1018. Assert.Equal (5, _textView.CursorPosition.X);
  1019. Assert.Equal (0, _textView.CursorPosition.Y);
  1020. Assert.Equal ("This ", _textView.Text);
  1021. break;
  1022. case 4:
  1023. Assert.Equal (0, _textView.CursorPosition.X);
  1024. Assert.Equal (0, _textView.CursorPosition.Y);
  1025. Assert.Equal ("", _textView.Text);
  1026. break;
  1027. default:
  1028. iterationsFinished = true;
  1029. break;
  1030. }
  1031. iteration++;
  1032. }
  1033. }
  1034. [Fact]
  1035. [InitShutdown]
  1036. public void Kill_Delete_WordForward_Multiline ()
  1037. {
  1038. _textView.Text = "This is the first line.\nThis is the second line.";
  1039. _textView.Width = 4;
  1040. var iteration = 0;
  1041. bool iterationsFinished = false;
  1042. while (!iterationsFinished) {
  1043. _textView.ProcessKey (new KeyEvent (Key.DeleteChar | Key.CtrlMask, new KeyModifiers ()));
  1044. switch (iteration) {
  1045. case 0:
  1046. Assert.Equal (0, _textView.CursorPosition.X);
  1047. Assert.Equal (0, _textView.CursorPosition.Y);
  1048. Assert.Equal ("is the first line." + Environment.NewLine
  1049. + "This is the second line.", _textView.Text);
  1050. break;
  1051. case 1:
  1052. Assert.Equal (0, _textView.CursorPosition.X);
  1053. Assert.Equal (0, _textView.CursorPosition.Y);
  1054. Assert.Equal ("the first line." + Environment.NewLine
  1055. + "This is the second line.", _textView.Text);
  1056. break;
  1057. case 2:
  1058. Assert.Equal (0, _textView.CursorPosition.X);
  1059. Assert.Equal (0, _textView.CursorPosition.Y);
  1060. Assert.Equal ("first line." + Environment.NewLine
  1061. + "This is the second line.", _textView.Text);
  1062. break;
  1063. case 3:
  1064. Assert.Equal (0, _textView.CursorPosition.X);
  1065. Assert.Equal (0, _textView.CursorPosition.Y);
  1066. Assert.Equal ("line." + Environment.NewLine
  1067. + "This is the second line.", _textView.Text);
  1068. break;
  1069. case 4:
  1070. Assert.Equal (0, _textView.CursorPosition.X);
  1071. Assert.Equal (0, _textView.CursorPosition.Y);
  1072. Assert.Equal ("" + Environment.NewLine
  1073. + "This is the second line.", _textView.Text);
  1074. break;
  1075. case 5:
  1076. Assert.Equal (0, _textView.CursorPosition.X);
  1077. Assert.Equal (0, _textView.CursorPosition.Y);
  1078. Assert.Equal ("This is the second line.", _textView.Text);
  1079. break;
  1080. case 6:
  1081. Assert.Equal (0, _textView.CursorPosition.X);
  1082. Assert.Equal (0, _textView.CursorPosition.Y);
  1083. Assert.Equal ("is the second line.", _textView.Text);
  1084. break;
  1085. case 7:
  1086. Assert.Equal (0, _textView.CursorPosition.X);
  1087. Assert.Equal (0, _textView.CursorPosition.Y);
  1088. Assert.Equal ("the second line.", _textView.Text);
  1089. break;
  1090. case 8:
  1091. Assert.Equal (0, _textView.CursorPosition.X);
  1092. Assert.Equal (0, _textView.CursorPosition.Y);
  1093. Assert.Equal ("second line.", _textView.Text);
  1094. break;
  1095. case 9:
  1096. Assert.Equal (0, _textView.CursorPosition.X);
  1097. Assert.Equal (0, _textView.CursorPosition.Y);
  1098. Assert.Equal ("line.", _textView.Text);
  1099. break;
  1100. case 10:
  1101. Assert.Equal (0, _textView.CursorPosition.X);
  1102. Assert.Equal (0, _textView.CursorPosition.Y);
  1103. Assert.Equal ("", _textView.Text);
  1104. break;
  1105. default:
  1106. iterationsFinished = true;
  1107. break;
  1108. }
  1109. iteration++;
  1110. }
  1111. }
  1112. [Fact]
  1113. [InitShutdown]
  1114. public void Kill_Delete_WordBackward_Multiline ()
  1115. {
  1116. _textView.Text = "This is the first line.\nThis is the second line.";
  1117. _textView.Width = 4;
  1118. _textView.MoveEnd ();
  1119. var iteration = 0;
  1120. bool iterationsFinished = false;
  1121. while (!iterationsFinished) {
  1122. _textView.ProcessKey (new KeyEvent (Key.Backspace | Key.CtrlMask, new KeyModifiers ()));
  1123. switch (iteration) {
  1124. case 0:
  1125. Assert.Equal (19, _textView.CursorPosition.X);
  1126. Assert.Equal (1, _textView.CursorPosition.Y);
  1127. Assert.Equal ("This is the first line." + Environment.NewLine
  1128. + "This is the second ", _textView.Text);
  1129. break;
  1130. case 1:
  1131. Assert.Equal (12, _textView.CursorPosition.X);
  1132. Assert.Equal (1, _textView.CursorPosition.Y);
  1133. Assert.Equal ("This is the first line." + Environment.NewLine
  1134. + "This is the ", _textView.Text);
  1135. break;
  1136. case 2:
  1137. Assert.Equal (8, _textView.CursorPosition.X);
  1138. Assert.Equal (1, _textView.CursorPosition.Y);
  1139. Assert.Equal ("This is the first line." + Environment.NewLine
  1140. + "This is ", _textView.Text);
  1141. break;
  1142. case 3:
  1143. Assert.Equal (5, _textView.CursorPosition.X);
  1144. Assert.Equal (1, _textView.CursorPosition.Y);
  1145. Assert.Equal ("This is the first line." + Environment.NewLine
  1146. + "This ", _textView.Text);
  1147. break;
  1148. case 4:
  1149. Assert.Equal (0, _textView.CursorPosition.X);
  1150. Assert.Equal (1, _textView.CursorPosition.Y);
  1151. Assert.Equal ("This is the first line." + Environment.NewLine, _textView.Text);
  1152. break;
  1153. case 5:
  1154. Assert.Equal (23, _textView.CursorPosition.X);
  1155. Assert.Equal (0, _textView.CursorPosition.Y);
  1156. Assert.Equal ("This is the first line.", _textView.Text);
  1157. break;
  1158. case 6:
  1159. Assert.Equal (18, _textView.CursorPosition.X);
  1160. Assert.Equal (0, _textView.CursorPosition.Y);
  1161. Assert.Equal ("This is the first ", _textView.Text);
  1162. break;
  1163. case 7:
  1164. Assert.Equal (12, _textView.CursorPosition.X);
  1165. Assert.Equal (0, _textView.CursorPosition.Y);
  1166. Assert.Equal ("This is the ", _textView.Text);
  1167. break;
  1168. case 8:
  1169. Assert.Equal (8, _textView.CursorPosition.X);
  1170. Assert.Equal (0, _textView.CursorPosition.Y);
  1171. Assert.Equal ("This is ", _textView.Text);
  1172. break;
  1173. case 9:
  1174. Assert.Equal (5, _textView.CursorPosition.X);
  1175. Assert.Equal (0, _textView.CursorPosition.Y);
  1176. Assert.Equal ("This ", _textView.Text);
  1177. break;
  1178. case 10:
  1179. Assert.Equal (0, _textView.CursorPosition.X);
  1180. Assert.Equal (0, _textView.CursorPosition.Y);
  1181. Assert.Equal ("", _textView.Text);
  1182. break;
  1183. default:
  1184. iterationsFinished = true;
  1185. break;
  1186. }
  1187. iteration++;
  1188. }
  1189. }
  1190. [Fact]
  1191. [InitShutdown]
  1192. public void Copy_Or_Cut_Null_If_No_Selection ()
  1193. {
  1194. _textView.SelectionStartColumn = 0;
  1195. _textView.SelectionStartRow = 0;
  1196. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1197. Assert.Equal ("", _textView.SelectedText);
  1198. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Cut
  1199. Assert.Equal ("", _textView.SelectedText);
  1200. }
  1201. [Fact]
  1202. [InitShutdown]
  1203. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  1204. {
  1205. _textView.SelectionStartColumn = 20;
  1206. _textView.SelectionStartRow = 0;
  1207. _textView.CursorPosition = new Point (24, 0);
  1208. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1209. Assert.Equal ("text", _textView.SelectedText);
  1210. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Cut
  1211. Assert.Equal ("", _textView.SelectedText);
  1212. }
  1213. [Fact]
  1214. [InitShutdown]
  1215. public void Copy_Or_Cut_And_Paste_With_Selection ()
  1216. {
  1217. _textView.SelectionStartColumn = 20;
  1218. _textView.SelectionStartRow = 0;
  1219. _textView.CursorPosition = new Point (24, 0);
  1220. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1221. Assert.Equal ("text", _textView.SelectedText);
  1222. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1223. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1224. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1225. _textView.SelectionStartColumn = 20;
  1226. _textView.SelectionStartRow = 0;
  1227. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Cut
  1228. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1229. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1230. }
  1231. [Fact]
  1232. [InitShutdown]
  1233. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  1234. {
  1235. _textView.SelectionStartColumn = 20;
  1236. _textView.SelectionStartRow = 0;
  1237. _textView.CursorPosition = new Point (24, 0);
  1238. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1239. Assert.Equal ("text", _textView.SelectedText);
  1240. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1241. _textView.SelectionStartColumn = 0;
  1242. _textView.SelectionStartRow = 0;
  1243. Assert.True (_textView.Selecting);
  1244. _textView.Selecting = false;
  1245. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1246. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  1247. _textView.SelectionStartColumn = 24;
  1248. _textView.SelectionStartRow = 0;
  1249. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Cut
  1250. Assert.Equal ("", _textView.SelectedText);
  1251. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1252. _textView.SelectionStartColumn = 0;
  1253. _textView.SelectionStartRow = 0;
  1254. Assert.True (_textView.Selecting);
  1255. _textView.Selecting = false;
  1256. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1257. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  1258. }
  1259. [Fact]
  1260. [InitShutdown]
  1261. public void Cut_Not_Allowed_If_ReadOnly_Is_True ()
  1262. {
  1263. _textView.ReadOnly = true;
  1264. _textView.SelectionStartColumn = 20;
  1265. _textView.SelectionStartRow = 0;
  1266. _textView.CursorPosition = new Point (24, 0);
  1267. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1268. Assert.Equal ("text", _textView.SelectedText);
  1269. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Selecting is set to false after Cut.
  1270. Assert.Equal ("", _textView.SelectedText);
  1271. _textView.ReadOnly = false;
  1272. Assert.False (_textView.Selecting);
  1273. _textView.Selecting = true; // Needed to set Selecting to true.
  1274. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1275. Assert.Equal ("text", _textView.SelectedText);
  1276. _textView.ProcessKey (new KeyEvent (Key.W | Key.CtrlMask, new KeyModifiers ())); // Cut
  1277. Assert.Equal ("", _textView.SelectedText);
  1278. }
  1279. [Fact]
  1280. [InitShutdown]
  1281. public void Paste_Always_Clear_The_SelectedText ()
  1282. {
  1283. _textView.SelectionStartColumn = 20;
  1284. _textView.SelectionStartRow = 0;
  1285. _textView.CursorPosition = new Point (24, 0);
  1286. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1287. Assert.Equal ("text", _textView.SelectedText);
  1288. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1289. Assert.Equal ("", _textView.SelectedText);
  1290. }
  1291. [Fact]
  1292. [InitShutdown]
  1293. public void TextChanged_Event ()
  1294. {
  1295. _textView.TextChanged += () => {
  1296. if (_textView.Text == "changing") {
  1297. Assert.Equal ("changing", _textView.Text);
  1298. _textView.Text = "changed";
  1299. }
  1300. };
  1301. _textView.Text = "changing";
  1302. Assert.Equal ("changed", _textView.Text);
  1303. }
  1304. [Fact]
  1305. [InitShutdown]
  1306. public void Used_Is_True_By_Default ()
  1307. {
  1308. _textView.CursorPosition = new Point (10, 0);
  1309. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1310. _textView.ProcessKey (new KeyEvent ((Key)0x75, new KeyModifiers ())); // u
  1311. Assert.Equal ("TAB to jumup between text fields.", _textView.Text);
  1312. _textView.ProcessKey (new KeyEvent ((Key)0x73, new KeyModifiers ())); // s
  1313. Assert.Equal ("TAB to jumusp between text fields.", _textView.Text);
  1314. _textView.ProcessKey (new KeyEvent ((Key)0x65, new KeyModifiers ())); // e
  1315. Assert.Equal ("TAB to jumusep between text fields.", _textView.Text);
  1316. _textView.ProcessKey (new KeyEvent ((Key)0x64, new KeyModifiers ())); // d
  1317. Assert.Equal ("TAB to jumusedp between text fields.", _textView.Text);
  1318. }
  1319. [Fact]
  1320. [InitShutdown]
  1321. public void Used_Is_False ()
  1322. {
  1323. _textView.Used = false;
  1324. _textView.CursorPosition = new Point (10, 0);
  1325. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1326. _textView.ProcessKey (new KeyEvent ((Key)0x75, new KeyModifiers ())); // u
  1327. Assert.Equal ("TAB to jumu between text fields.", _textView.Text);
  1328. _textView.ProcessKey (new KeyEvent ((Key)0x73, new KeyModifiers ())); // s
  1329. Assert.Equal ("TAB to jumusbetween text fields.", _textView.Text);
  1330. _textView.ProcessKey (new KeyEvent ((Key)0x65, new KeyModifiers ())); // e
  1331. Assert.Equal ("TAB to jumuseetween text fields.", _textView.Text);
  1332. _textView.ProcessKey (new KeyEvent ((Key)0x64, new KeyModifiers ())); // d
  1333. Assert.Equal ("TAB to jumusedtween text fields.", _textView.Text);
  1334. }
  1335. [Fact]
  1336. [InitShutdown]
  1337. public void Copy_Without_Selection ()
  1338. {
  1339. _textView.Text = "This is the first line.\nThis is the second line.\n";
  1340. _textView.CursorPosition = new Point (0, _textView.Lines - 1);
  1341. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1342. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1343. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}", _textView.Text);
  1344. _textView.CursorPosition = new Point (3, 1);
  1345. _textView.ProcessKey (new KeyEvent (Key.C | Key.CtrlMask, new KeyModifiers ())); // Copy
  1346. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1347. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}", _textView.Text);
  1348. Assert.Equal (new Point (3, 2), _textView.CursorPosition);
  1349. _textView.ProcessKey (new KeyEvent (Key.Y | Key.CtrlMask, new KeyModifiers ())); // Paste
  1350. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the second line.{Environment.NewLine}{Environment.NewLine}", _textView.Text);
  1351. Assert.Equal (new Point (3, 3), _textView.CursorPosition);
  1352. }
  1353. [Fact]
  1354. [InitShutdown]
  1355. public void TabWidth_Setting_To_Zero_Changes_AllowsTab_To_False_If_True ()
  1356. {
  1357. Assert.Equal (4, _textView.TabWidth);
  1358. Assert.True (_textView.AllowsTab);
  1359. Assert.True (_textView.AllowsReturn);
  1360. Assert.True (_textView.Multiline);
  1361. _textView.TabWidth = -1;
  1362. Assert.Equal (0, _textView.TabWidth);
  1363. Assert.False (_textView.AllowsTab);
  1364. Assert.False (_textView.AllowsReturn);
  1365. Assert.False (_textView.Multiline);
  1366. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1367. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1368. _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
  1369. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1370. }
  1371. [Fact]
  1372. [InitShutdown]
  1373. public void AllowsTab_Setting_To_True_Changes_TabWidth_To_Default_If_It_Is_Zero ()
  1374. {
  1375. _textView.TabWidth = 0;
  1376. Assert.Equal (0, _textView.TabWidth);
  1377. Assert.False (_textView.AllowsTab);
  1378. Assert.False (_textView.AllowsReturn);
  1379. Assert.False (_textView.Multiline);
  1380. _textView.AllowsTab = true;
  1381. Assert.True (_textView.AllowsTab);
  1382. Assert.Equal (4, _textView.TabWidth);
  1383. Assert.True (_textView.AllowsReturn);
  1384. Assert.True (_textView.Multiline);
  1385. }
  1386. [Fact]
  1387. [InitShutdown]
  1388. public void AllowsReturn_Setting_To_True_Changes_Multiline_To_True_If_It_Is_False ()
  1389. {
  1390. Assert.True (_textView.AllowsReturn);
  1391. Assert.True (_textView.Multiline);
  1392. Assert.Equal (4, _textView.TabWidth);
  1393. Assert.True (_textView.AllowsTab);
  1394. _textView.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1395. Assert.Equal (Environment.NewLine +
  1396. "TAB to jump between text fields.", _textView.Text);
  1397. _textView.AllowsReturn = false;
  1398. Assert.False (_textView.AllowsReturn);
  1399. Assert.False (_textView.Multiline);
  1400. Assert.Equal (0, _textView.TabWidth);
  1401. Assert.False (_textView.AllowsTab);
  1402. _textView.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1403. Assert.Equal (Environment.NewLine +
  1404. "TAB to jump between text fields.", _textView.Text);
  1405. }
  1406. [Fact]
  1407. [InitShutdown]
  1408. public void Multiline_Setting_Changes_AllowsReturn_And_AllowsTab_And_Height ()
  1409. {
  1410. Assert.True (_textView.Multiline);
  1411. Assert.True (_textView.AllowsReturn);
  1412. Assert.Equal (4, _textView.TabWidth);
  1413. Assert.True (_textView.AllowsTab);
  1414. Assert.Equal ("Dim.Absolute(30)", _textView.Width.ToString ());
  1415. Assert.Equal ("Dim.Absolute(10)", _textView.Height.ToString ());
  1416. _textView.Multiline = false;
  1417. Assert.False (_textView.Multiline);
  1418. Assert.False (_textView.AllowsReturn);
  1419. Assert.Equal (0, _textView.TabWidth);
  1420. Assert.False (_textView.AllowsTab);
  1421. Assert.Equal ("Dim.Absolute(30)", _textView.Width.ToString ());
  1422. Assert.Equal ("Dim.Absolute(1)", _textView.Height.ToString ());
  1423. _textView.Multiline = true;
  1424. Assert.True (_textView.Multiline);
  1425. Assert.True (_textView.AllowsReturn);
  1426. Assert.Equal (4, _textView.TabWidth);
  1427. Assert.True (_textView.AllowsTab);
  1428. Assert.Equal ("Dim.Absolute(30)", _textView.Width.ToString ());
  1429. Assert.Equal ("Dim.Absolute(10)", _textView.Height.ToString ());
  1430. }
  1431. [Fact]
  1432. [InitShutdown]
  1433. public void Tab_Test_Follow_By_BackTab ()
  1434. {
  1435. Application.Top.Add (_textView);
  1436. Application.Iteration += () => {
  1437. var width = _textView.Bounds.Width - 1;
  1438. Assert.Equal (30, width + 1);
  1439. Assert.Equal (10, _textView.Height);
  1440. _textView.Text = "";
  1441. var col = 0;
  1442. var leftCol = 0;
  1443. var tabWidth = _textView.TabWidth;
  1444. while (col < 100) {
  1445. col++;
  1446. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1447. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1448. leftCol = GetLeftCol (leftCol);
  1449. Assert.Equal (leftCol, _textView.LeftColumn);
  1450. }
  1451. while (col > 0) {
  1452. col--;
  1453. _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
  1454. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1455. leftCol = GetLeftCol (leftCol);
  1456. Assert.Equal (leftCol, _textView.LeftColumn);
  1457. }
  1458. Application.Top.Remove (_textView);
  1459. Application.RequestStop ();
  1460. };
  1461. Application.Run ();
  1462. }
  1463. [Fact]
  1464. [InitShutdown]
  1465. public void BackTab_Test_Follow_By_Tab ()
  1466. {
  1467. Application.Top.Add (_textView);
  1468. Application.Iteration += () => {
  1469. var width = _textView.Bounds.Width - 1;
  1470. Assert.Equal (30, width + 1);
  1471. Assert.Equal (10, _textView.Height);
  1472. _textView.Text = "";
  1473. for (int i = 0; i < 100; i++) {
  1474. _textView.Text += "\t";
  1475. }
  1476. var col = 100;
  1477. var tabWidth = _textView.TabWidth;
  1478. var leftCol = _textView.LeftColumn;
  1479. _textView.MoveEnd ();
  1480. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1481. leftCol = GetLeftCol (leftCol);
  1482. Assert.Equal (leftCol, _textView.LeftColumn);
  1483. while (col > 0) {
  1484. col--;
  1485. _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
  1486. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1487. leftCol = GetLeftCol (leftCol);
  1488. Assert.Equal (leftCol, _textView.LeftColumn);
  1489. }
  1490. while (col < 100) {
  1491. col++;
  1492. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1493. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1494. leftCol = GetLeftCol (leftCol);
  1495. Assert.Equal (leftCol, _textView.LeftColumn);
  1496. }
  1497. Application.Top.Remove (_textView);
  1498. Application.RequestStop ();
  1499. };
  1500. Application.Run ();
  1501. }
  1502. [Fact]
  1503. [InitShutdown]
  1504. public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight ()
  1505. {
  1506. Application.Top.Add (_textView);
  1507. Application.Iteration += () => {
  1508. var width = _textView.Bounds.Width - 1;
  1509. Assert.Equal (30, width + 1);
  1510. Assert.Equal (10, _textView.Height);
  1511. _textView.Text = "";
  1512. var col = 0;
  1513. var leftCol = 0;
  1514. var tabWidth = _textView.TabWidth;
  1515. while (col < 100) {
  1516. col++;
  1517. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1518. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1519. leftCol = GetLeftCol (leftCol);
  1520. Assert.Equal (leftCol, _textView.LeftColumn);
  1521. }
  1522. while (col > 0) {
  1523. col--;
  1524. _textView.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  1525. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1526. leftCol = GetLeftCol (leftCol);
  1527. Assert.Equal (leftCol, _textView.LeftColumn);
  1528. }
  1529. while (col < 100) {
  1530. col++;
  1531. _textView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  1532. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1533. leftCol = GetLeftCol (leftCol);
  1534. Assert.Equal (leftCol, _textView.LeftColumn);
  1535. }
  1536. Application.Top.Remove (_textView);
  1537. Application.RequestStop ();
  1538. };
  1539. Application.Run ();
  1540. }
  1541. [Fact]
  1542. [InitShutdown]
  1543. public void Tab_Test_Follow_By_BackTab_With_Text ()
  1544. {
  1545. Application.Top.Add (_textView);
  1546. Application.Iteration += () => {
  1547. var width = _textView.Bounds.Width - 1;
  1548. Assert.Equal (30, width + 1);
  1549. Assert.Equal (10, _textView.Height);
  1550. var col = 0;
  1551. var leftCol = 0;
  1552. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1553. Assert.Equal (leftCol, _textView.LeftColumn);
  1554. while (col < 100) {
  1555. col++;
  1556. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1557. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1558. leftCol = GetLeftCol (leftCol);
  1559. Assert.Equal (leftCol, _textView.LeftColumn);
  1560. }
  1561. while (col > 0) {
  1562. col--;
  1563. _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
  1564. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1565. leftCol = GetLeftCol (leftCol);
  1566. Assert.Equal (leftCol, _textView.LeftColumn);
  1567. }
  1568. Application.Top.Remove (_textView);
  1569. Application.RequestStop ();
  1570. };
  1571. Application.Run ();
  1572. }
  1573. [Fact]
  1574. [InitShutdown]
  1575. public void Tab_Test_Follow_By_Home_And_Then_Follow_By_End_And_Then_Follow_By_BackTab_With_Text ()
  1576. {
  1577. Application.Top.Add (_textView);
  1578. Application.Iteration += () => {
  1579. var width = _textView.Bounds.Width - 1;
  1580. Assert.Equal (30, width + 1);
  1581. Assert.Equal (10, _textView.Height);
  1582. var col = 0;
  1583. var leftCol = 0;
  1584. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1585. Assert.Equal (leftCol, _textView.LeftColumn);
  1586. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1587. Assert.Equal (32, _textView.Text.Length);
  1588. while (col < 100) {
  1589. col++;
  1590. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1591. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1592. leftCol = GetLeftCol (leftCol);
  1593. Assert.Equal (leftCol, _textView.LeftColumn);
  1594. }
  1595. _textView.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ()));
  1596. col = 0;
  1597. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1598. leftCol = 0;
  1599. Assert.Equal (leftCol, _textView.LeftColumn);
  1600. _textView.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ()));
  1601. col = _textView.Text.Length;
  1602. Assert.Equal (132, _textView.Text.Length);
  1603. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1604. leftCol = GetLeftCol (leftCol);
  1605. Assert.Equal (leftCol, _textView.LeftColumn);
  1606. var txt = _textView.Text;
  1607. while (col - 1 > 0 && txt [col - 1] != '\t') {
  1608. col--;
  1609. }
  1610. _textView.CursorPosition = new Point (col, 0);
  1611. leftCol = GetLeftCol (leftCol);
  1612. while (col > 0) {
  1613. col--;
  1614. _textView.ProcessKey (new KeyEvent (Key.BackTab, new KeyModifiers ()));
  1615. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1616. leftCol = GetLeftCol (leftCol);
  1617. Assert.Equal (leftCol, _textView.LeftColumn);
  1618. }
  1619. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1620. Assert.Equal (32, _textView.Text.Length);
  1621. Application.Top.Remove (_textView);
  1622. Application.RequestStop ();
  1623. };
  1624. Application.Run ();
  1625. }
  1626. [Fact]
  1627. [InitShutdown]
  1628. public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight_With_Text ()
  1629. {
  1630. Application.Top.Add (_textView);
  1631. Application.Iteration += () => {
  1632. var width = _textView.Bounds.Width - 1;
  1633. Assert.Equal (30, width + 1);
  1634. Assert.Equal (10, _textView.Height);
  1635. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  1636. var col = 0;
  1637. var leftCol = 0;
  1638. var tabWidth = _textView.TabWidth;
  1639. while (col < 100) {
  1640. col++;
  1641. _textView.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  1642. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1643. leftCol = GetLeftCol (leftCol);
  1644. Assert.Equal (leftCol, _textView.LeftColumn);
  1645. }
  1646. Assert.Equal (132, _textView.Text.Length);
  1647. while (col > 0) {
  1648. col--;
  1649. _textView.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  1650. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1651. leftCol = GetLeftCol (leftCol);
  1652. Assert.Equal (leftCol, _textView.LeftColumn);
  1653. }
  1654. while (col < 100) {
  1655. col++;
  1656. _textView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  1657. Assert.Equal (new Point (col, 0), _textView.CursorPosition);
  1658. leftCol = GetLeftCol (leftCol);
  1659. Assert.Equal (leftCol, _textView.LeftColumn);
  1660. }
  1661. Application.Top.Remove (_textView);
  1662. Application.RequestStop ();
  1663. };
  1664. Application.Run ();
  1665. }
  1666. private int GetLeftCol (int start)
  1667. {
  1668. var lines = _textView.Text.Split (Environment.NewLine);
  1669. if (lines == null || lines.Length == 0) {
  1670. return 0;
  1671. }
  1672. if (start == _textView.LeftColumn) {
  1673. return start;
  1674. }
  1675. if (_textView.LeftColumn == _textView.CurrentColumn) {
  1676. return _textView.CurrentColumn;
  1677. }
  1678. var cCol = _textView.CurrentColumn;
  1679. var line = lines [_textView.CurrentRow];
  1680. var lCount = cCol > line.Length - 1 ? line.Length - 1 : cCol;
  1681. var width = _textView.Frame.Width;
  1682. var tabWidth = _textView.TabWidth;
  1683. var sumLength = 0;
  1684. var col = 0;
  1685. for (int i = lCount; i >= 0; i--) {
  1686. var r = line [i];
  1687. sumLength += Rune.ColumnWidth (r);
  1688. if (r == '\t') {
  1689. sumLength += tabWidth + 1;
  1690. }
  1691. if (sumLength > width) {
  1692. if (cCol == line.Length) {
  1693. col++;
  1694. }
  1695. break;
  1696. } else if (cCol < line.Length && col > 0 && start < cCol && col == start) {
  1697. break;
  1698. }
  1699. col = i;
  1700. }
  1701. return col;
  1702. }
  1703. [Fact]
  1704. public void LoadFile_Throws_If_File_Is_Null ()
  1705. {
  1706. var tv = new TextView ();
  1707. Assert.Throws<ArgumentNullException> (() => tv.LoadFile (null));
  1708. }
  1709. [Fact]
  1710. public void LoadFile_Throws_If_File_Is_Empty ()
  1711. {
  1712. var tv = new TextView ();
  1713. Assert.Throws<ArgumentException> (() => tv.LoadFile (""));
  1714. }
  1715. [Fact]
  1716. public void LoadFile_Throws_If_File_Not_Exist ()
  1717. {
  1718. var tv = new TextView ();
  1719. Assert.Throws<System.IO.FileNotFoundException> (() => tv.LoadFile ("blabla"));
  1720. }
  1721. [Fact]
  1722. public void LoadStream_Throws_If_Stream_Is_Null ()
  1723. {
  1724. var tv = new TextView ();
  1725. Assert.Throws<ArgumentNullException> (() => tv.LoadStream (null));
  1726. }
  1727. [Fact]
  1728. public void LoadStream_Stream_Is_Empty ()
  1729. {
  1730. var tv = new TextView ();
  1731. tv.LoadStream (new System.IO.MemoryStream ());
  1732. Assert.Equal ("", tv.Text);
  1733. }
  1734. [Fact]
  1735. public void LoadStream_CRLF ()
  1736. {
  1737. var text = "This is the first line.\r\nThis is the second line.\r\n";
  1738. var tv = new TextView ();
  1739. tv.LoadStream (new System.IO.MemoryStream (System.Text.Encoding.ASCII.GetBytes (text)));
  1740. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
  1741. }
  1742. [Fact]
  1743. public void LoadStream_LF ()
  1744. {
  1745. var text = "This is the first line.\nThis is the second line.\n";
  1746. var tv = new TextView ();
  1747. tv.LoadStream (new System.IO.MemoryStream (System.Text.Encoding.ASCII.GetBytes (text)));
  1748. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
  1749. }
  1750. [Fact]
  1751. public void StringToRunes_Slipts_CRLF ()
  1752. {
  1753. var text = "This is the first line.\r\nThis is the second line.\r\n";
  1754. var tv = new TextView ();
  1755. tv.Text = text;
  1756. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
  1757. }
  1758. [Fact]
  1759. public void StringToRunes_Slipts_LF ()
  1760. {
  1761. var text = "This is the first line.\nThis is the second line.\n";
  1762. var tv = new TextView ();
  1763. tv.Text = text;
  1764. Assert.Equal ($"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}", tv.Text);
  1765. }
  1766. [Fact]
  1767. public void CloseFile_Throws_If_FilePath_Is_Null ()
  1768. {
  1769. var tv = new TextView ();
  1770. Assert.Throws<ArgumentNullException> (() => tv.CloseFile ());
  1771. }
  1772. }
  1773. }