TextViewTests.cs 79 KB

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