TextViewTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. using Xunit;
  2. namespace Terminal.Gui {
  3. public class TextViewTests {
  4. private TextView _textView;
  5. public TextViewTests ()
  6. {
  7. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  8. // 1 2 3
  9. // 01234567890123456789012345678901=32 (Length)
  10. var txt = "TAB to jump between text fields.";
  11. var buff = new byte [txt.Length];
  12. for (int i = 0; i < txt.Length; i++) {
  13. buff [i] = (byte)txt [i];
  14. }
  15. var ms = new System.IO.MemoryStream (buff).ToArray ();
  16. _textView = new TextView () { Width = 30, Height = 10 };
  17. _textView.Text = ms;
  18. }
  19. [Fact]
  20. public void Changing_Selection_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
  21. {
  22. _textView.SelectionStartColumn = 2;
  23. _textView.SelectionStartRow = 0;
  24. Assert.Equal (0, _textView.CursorPosition.X);
  25. Assert.Equal (0, _textView.CursorPosition.Y);
  26. Assert.Equal (2, _textView.SelectedLength);
  27. Assert.Equal ("TA", _textView.SelectedText);
  28. _textView.CursorPosition = new Point (20, 0);
  29. Assert.Equal (2, _textView.SelectionStartColumn);
  30. Assert.Equal (0, _textView.SelectionStartRow);
  31. Assert.Equal (18, _textView.SelectedLength);
  32. Assert.Equal ("B to jump between ", _textView.SelectedText);
  33. }
  34. [Fact]
  35. public void Selection_With_Value_Less_Than_Zero_Changes_To_Zero ()
  36. {
  37. _textView.SelectionStartColumn = -2;
  38. _textView.SelectionStartRow = -2;
  39. Assert.Equal (0, _textView.SelectionStartColumn);
  40. Assert.Equal (0, _textView.SelectionStartRow);
  41. Assert.Equal (0, _textView.SelectedLength);
  42. Assert.Equal ("", _textView.SelectedText);
  43. }
  44. [Fact]
  45. public void Selection_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  46. {
  47. _textView.CursorPosition = new Point (2, 0);
  48. _textView.SelectionStartColumn = 33;
  49. _textView.SelectionStartRow = 1;
  50. Assert.Equal (32, _textView.SelectionStartColumn);
  51. Assert.Equal (0, _textView.SelectionStartRow);
  52. Assert.Equal (30, _textView.SelectedLength);
  53. Assert.Equal ("B to jump between text fields.", _textView.SelectedText);
  54. }
  55. [Fact]
  56. public void Selection_With_Empty_Text ()
  57. {
  58. _textView = new TextView ();
  59. _textView.CursorPosition = new Point (2, 0);
  60. _textView.SelectionStartColumn = 33;
  61. _textView.SelectionStartRow = 1;
  62. Assert.Equal (0, _textView.SelectionStartColumn);
  63. Assert.Equal (0, _textView.SelectionStartRow);
  64. Assert.Equal (0, _textView.SelectedLength);
  65. Assert.Equal ("", _textView.SelectedText);
  66. }
  67. [Fact]
  68. public void Selection_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
  69. {
  70. _textView.CursorPosition = new Point (33, 2);
  71. _textView.SelectionStartColumn = 33;
  72. _textView.SelectionStartRow = 33;
  73. Assert.Equal (32, _textView.CursorPosition.X);
  74. Assert.Equal (0, _textView.CursorPosition.Y);
  75. Assert.Equal (32, _textView.SelectionStartColumn);
  76. Assert.Equal (0, _textView.SelectionStartRow);
  77. Assert.Equal (0, _textView.SelectedLength);
  78. Assert.Equal ("", _textView.SelectedText);
  79. }
  80. [Fact]
  81. public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
  82. {
  83. _textView.CursorPosition = new Point (-1, -1);
  84. Assert.Equal (0, _textView.CursorPosition.X);
  85. Assert.Equal (0, _textView.CursorPosition.Y);
  86. Assert.Equal (0, _textView.SelectedLength);
  87. Assert.Equal ("", _textView.SelectedText);
  88. }
  89. [Fact]
  90. public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
  91. {
  92. _textView.CursorPosition = new Point (33, 1);
  93. Assert.Equal (32, _textView.CursorPosition.X);
  94. Assert.Equal (0, _textView.CursorPosition.Y);
  95. Assert.Equal (0, _textView.SelectedLength);
  96. Assert.Equal ("", _textView.SelectedText);
  97. }
  98. [Fact]
  99. public void WordForward_With_No_Selection ()
  100. {
  101. _textView.CursorPosition = new Point (0, 0);
  102. var iteration = 0;
  103. while (_textView.CursorPosition.X < _textView.Text.Length) {
  104. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask, new KeyModifiers ()));
  105. switch (iteration) {
  106. case 0:
  107. Assert.Equal (4, _textView.CursorPosition.X);
  108. Assert.Equal (0, _textView.CursorPosition.Y);
  109. Assert.Equal (0, _textView.SelectionStartColumn);
  110. Assert.Equal (0, _textView.SelectionStartRow);
  111. Assert.Equal (0, _textView.SelectedLength);
  112. Assert.Equal ("", _textView.SelectedText);
  113. break;
  114. case 1:
  115. Assert.Equal (7, _textView.CursorPosition.X);
  116. Assert.Equal (0, _textView.CursorPosition.Y);
  117. Assert.Equal (0, _textView.SelectionStartColumn);
  118. Assert.Equal (0, _textView.SelectionStartRow);
  119. Assert.Equal (0, _textView.SelectedLength);
  120. Assert.Equal ("", _textView.SelectedText);
  121. break;
  122. case 2:
  123. Assert.Equal (12, _textView.CursorPosition.X);
  124. Assert.Equal (0, _textView.CursorPosition.Y);
  125. Assert.Equal (0, _textView.SelectionStartColumn);
  126. Assert.Equal (0, _textView.SelectionStartRow);
  127. Assert.Equal (0, _textView.SelectedLength);
  128. Assert.Equal ("", _textView.SelectedText);
  129. break;
  130. case 3:
  131. Assert.Equal (20, _textView.CursorPosition.X);
  132. Assert.Equal (0, _textView.CursorPosition.Y);
  133. Assert.Equal (0, _textView.SelectionStartColumn);
  134. Assert.Equal (0, _textView.SelectionStartRow);
  135. Assert.Equal (0, _textView.SelectedLength);
  136. Assert.Equal ("", _textView.SelectedText);
  137. break;
  138. case 4:
  139. Assert.Equal (25, _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 5:
  147. Assert.Equal (32, _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. }
  155. iteration++;
  156. }
  157. }
  158. [Fact]
  159. public void WordBackward_With_No_Selection ()
  160. {
  161. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  162. var iteration = 0;
  163. while (_textView.CursorPosition.X > 0) {
  164. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask, new KeyModifiers ()));
  165. switch (iteration) {
  166. case 0:
  167. Assert.Equal (25, _textView.CursorPosition.X);
  168. Assert.Equal (0, _textView.CursorPosition.Y);
  169. Assert.Equal (0, _textView.SelectionStartColumn);
  170. Assert.Equal (0, _textView.SelectionStartRow);
  171. Assert.Equal (0, _textView.SelectedLength);
  172. Assert.Equal ("", _textView.SelectedText);
  173. break;
  174. case 1:
  175. Assert.Equal (20, _textView.CursorPosition.X);
  176. Assert.Equal (0, _textView.CursorPosition.Y);
  177. Assert.Equal (0, _textView.SelectionStartColumn);
  178. Assert.Equal (0, _textView.SelectionStartRow);
  179. Assert.Equal (0, _textView.SelectedLength);
  180. Assert.Equal ("", _textView.SelectedText);
  181. break;
  182. case 2:
  183. Assert.Equal (12, _textView.CursorPosition.X);
  184. Assert.Equal (0, _textView.CursorPosition.Y);
  185. Assert.Equal (0, _textView.SelectionStartColumn);
  186. Assert.Equal (0, _textView.SelectionStartRow);
  187. Assert.Equal (0, _textView.SelectedLength);
  188. Assert.Equal ("", _textView.SelectedText);
  189. break;
  190. case 3:
  191. Assert.Equal (7, _textView.CursorPosition.X);
  192. Assert.Equal (0, _textView.CursorPosition.Y);
  193. Assert.Equal (0, _textView.SelectionStartColumn);
  194. Assert.Equal (0, _textView.SelectionStartRow);
  195. Assert.Equal (0, _textView.SelectedLength);
  196. Assert.Equal ("", _textView.SelectedText);
  197. break;
  198. case 4:
  199. Assert.Equal (4, _textView.CursorPosition.X);
  200. Assert.Equal (0, _textView.CursorPosition.Y);
  201. Assert.Equal (0, _textView.SelectionStartColumn);
  202. Assert.Equal (0, _textView.SelectionStartRow);
  203. Assert.Equal (0, _textView.SelectedLength);
  204. Assert.Equal ("", _textView.SelectedText);
  205. break;
  206. case 5:
  207. Assert.Equal (0, _textView.CursorPosition.X);
  208. Assert.Equal (0, _textView.CursorPosition.Y);
  209. Assert.Equal (0, _textView.SelectionStartColumn);
  210. Assert.Equal (0, _textView.SelectionStartRow);
  211. Assert.Equal (0, _textView.SelectedLength);
  212. Assert.Equal ("", _textView.SelectedText);
  213. break;
  214. }
  215. iteration++;
  216. }
  217. }
  218. [Fact]
  219. public void WordForward_With_Selection ()
  220. {
  221. _textView.CursorPosition = new Point (0, 0);
  222. _textView.SelectionStartColumn = 0;
  223. _textView.SelectionStartRow = 0;
  224. var iteration = 0;
  225. while (_textView.CursorPosition.X < _textView.Text.Length) {
  226. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  227. switch (iteration) {
  228. case 0:
  229. Assert.Equal (4, _textView.CursorPosition.X);
  230. Assert.Equal (0, _textView.CursorPosition.Y);
  231. Assert.Equal (0, _textView.SelectionStartColumn);
  232. Assert.Equal (0, _textView.SelectionStartRow);
  233. Assert.Equal (4, _textView.SelectedLength);
  234. Assert.Equal ("TAB ", _textView.SelectedText);
  235. break;
  236. case 1:
  237. Assert.Equal (7, _textView.CursorPosition.X);
  238. Assert.Equal (0, _textView.CursorPosition.Y);
  239. Assert.Equal (0, _textView.SelectionStartColumn);
  240. Assert.Equal (0, _textView.SelectionStartRow);
  241. Assert.Equal (7, _textView.SelectedLength);
  242. Assert.Equal ("TAB to ", _textView.SelectedText);
  243. break;
  244. case 2:
  245. Assert.Equal (12, _textView.CursorPosition.X);
  246. Assert.Equal (0, _textView.CursorPosition.Y);
  247. Assert.Equal (0, _textView.SelectionStartColumn);
  248. Assert.Equal (0, _textView.SelectionStartRow);
  249. Assert.Equal (12, _textView.SelectedLength);
  250. Assert.Equal ("TAB to jump ", _textView.SelectedText);
  251. break;
  252. case 3:
  253. Assert.Equal (20, _textView.CursorPosition.X);
  254. Assert.Equal (0, _textView.CursorPosition.Y);
  255. Assert.Equal (0, _textView.SelectionStartColumn);
  256. Assert.Equal (0, _textView.SelectionStartRow);
  257. Assert.Equal (20, _textView.SelectedLength);
  258. Assert.Equal ("TAB to jump between ", _textView.SelectedText);
  259. break;
  260. case 4:
  261. Assert.Equal (25, _textView.CursorPosition.X);
  262. Assert.Equal (0, _textView.CursorPosition.Y);
  263. Assert.Equal (0, _textView.SelectionStartColumn);
  264. Assert.Equal (0, _textView.SelectionStartRow);
  265. Assert.Equal (25, _textView.SelectedLength);
  266. Assert.Equal ("TAB to jump between text ", _textView.SelectedText);
  267. break;
  268. case 5:
  269. Assert.Equal (32, _textView.CursorPosition.X);
  270. Assert.Equal (0, _textView.CursorPosition.Y);
  271. Assert.Equal (0, _textView.SelectionStartColumn);
  272. Assert.Equal (0, _textView.SelectionStartRow);
  273. Assert.Equal (32, _textView.SelectedLength);
  274. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  275. break;
  276. }
  277. iteration++;
  278. }
  279. }
  280. [Fact]
  281. public void WordBackward_With_Selection ()
  282. {
  283. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  284. _textView.SelectionStartColumn = _textView.Text.Length;
  285. _textView.SelectionStartRow = 0;
  286. var iteration = 0;
  287. while (_textView.CursorPosition.X > 0) {
  288. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  289. switch (iteration) {
  290. case 0:
  291. Assert.Equal (25, _textView.CursorPosition.X);
  292. Assert.Equal (0, _textView.CursorPosition.Y);
  293. Assert.Equal (32, _textView.SelectionStartColumn);
  294. Assert.Equal (0, _textView.SelectionStartRow);
  295. Assert.Equal (7, _textView.SelectedLength);
  296. Assert.Equal ("fields.", _textView.SelectedText);
  297. break;
  298. case 1:
  299. Assert.Equal (20, _textView.CursorPosition.X);
  300. Assert.Equal (0, _textView.CursorPosition.Y);
  301. Assert.Equal (32, _textView.SelectionStartColumn);
  302. Assert.Equal (0, _textView.SelectionStartRow);
  303. Assert.Equal (12, _textView.SelectedLength);
  304. Assert.Equal ("text fields.", _textView.SelectedText);
  305. break;
  306. case 2:
  307. Assert.Equal (12, _textView.CursorPosition.X);
  308. Assert.Equal (0, _textView.CursorPosition.Y);
  309. Assert.Equal (32, _textView.SelectionStartColumn);
  310. Assert.Equal (0, _textView.SelectionStartRow);
  311. Assert.Equal (20, _textView.SelectedLength);
  312. Assert.Equal ("between text fields.", _textView.SelectedText);
  313. break;
  314. case 3:
  315. Assert.Equal (7, _textView.CursorPosition.X);
  316. Assert.Equal (0, _textView.CursorPosition.Y);
  317. Assert.Equal (32, _textView.SelectionStartColumn);
  318. Assert.Equal (0, _textView.SelectionStartRow);
  319. Assert.Equal (25, _textView.SelectedLength);
  320. Assert.Equal ("jump between text fields.", _textView.SelectedText);
  321. break;
  322. case 4:
  323. Assert.Equal (4, _textView.CursorPosition.X);
  324. Assert.Equal (0, _textView.CursorPosition.Y);
  325. Assert.Equal (32, _textView.SelectionStartColumn);
  326. Assert.Equal (0, _textView.SelectionStartRow);
  327. Assert.Equal (28, _textView.SelectedLength);
  328. Assert.Equal ("to jump between text fields.", _textView.SelectedText);
  329. break;
  330. case 5:
  331. Assert.Equal (0, _textView.CursorPosition.X);
  332. Assert.Equal (0, _textView.CursorPosition.Y);
  333. Assert.Equal (32, _textView.SelectionStartColumn);
  334. Assert.Equal (0, _textView.SelectionStartRow);
  335. Assert.Equal (32, _textView.SelectedLength);
  336. Assert.Equal ("TAB to jump between text fields.", _textView.SelectedText);
  337. break;
  338. }
  339. iteration++;
  340. }
  341. }
  342. [Fact]
  343. public void WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  344. {
  345. _textView.CursorPosition = new Point (10, 0);
  346. _textView.SelectionStartColumn = 10;
  347. _textView.SelectionStartRow = 0;
  348. var iteration = 0;
  349. while (_textView.CursorPosition.X < _textView.Text.Length) {
  350. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  351. switch (iteration) {
  352. case 0:
  353. Assert.Equal (12, _textView.CursorPosition.X);
  354. Assert.Equal (0, _textView.CursorPosition.Y);
  355. Assert.Equal (10, _textView.SelectionStartColumn);
  356. Assert.Equal (0, _textView.SelectionStartRow);
  357. Assert.Equal (2, _textView.SelectedLength);
  358. Assert.Equal ("p ", _textView.SelectedText);
  359. break;
  360. case 1:
  361. Assert.Equal (20, _textView.CursorPosition.X);
  362. Assert.Equal (0, _textView.CursorPosition.Y);
  363. Assert.Equal (10, _textView.SelectionStartColumn);
  364. Assert.Equal (0, _textView.SelectionStartRow);
  365. Assert.Equal (10, _textView.SelectedLength);
  366. Assert.Equal ("p between ", _textView.SelectedText);
  367. break;
  368. case 2:
  369. Assert.Equal (25, _textView.CursorPosition.X);
  370. Assert.Equal (0, _textView.CursorPosition.Y);
  371. Assert.Equal (10, _textView.SelectionStartColumn);
  372. Assert.Equal (0, _textView.SelectionStartRow);
  373. Assert.Equal (15, _textView.SelectedLength);
  374. Assert.Equal ("p between text ", _textView.SelectedText);
  375. break;
  376. case 3:
  377. Assert.Equal (32, _textView.CursorPosition.X);
  378. Assert.Equal (0, _textView.CursorPosition.Y);
  379. Assert.Equal (10, _textView.SelectionStartColumn);
  380. Assert.Equal (0, _textView.SelectionStartRow);
  381. Assert.Equal (22, _textView.SelectedLength);
  382. Assert.Equal ("p between text fields.", _textView.SelectedText);
  383. break;
  384. }
  385. iteration++;
  386. }
  387. }
  388. [Fact]
  389. public void WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
  390. {
  391. _textView.CursorPosition = new Point (10, 0);
  392. _textView.SelectionStartColumn = 10;
  393. _textView.SelectionStartRow = 0;
  394. var iteration = 0;
  395. while (_textView.CursorPosition.X > 0) {
  396. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask | Key.ShiftMask, new KeyModifiers ()));
  397. switch (iteration) {
  398. case 0:
  399. Assert.Equal (7, _textView.CursorPosition.X);
  400. Assert.Equal (0, _textView.CursorPosition.Y);
  401. Assert.Equal (10, _textView.SelectionStartColumn);
  402. Assert.Equal (0, _textView.SelectionStartRow);
  403. Assert.Equal (3, _textView.SelectedLength);
  404. Assert.Equal ("jum", _textView.SelectedText);
  405. break;
  406. case 1:
  407. Assert.Equal (4, _textView.CursorPosition.X);
  408. Assert.Equal (0, _textView.CursorPosition.Y);
  409. Assert.Equal (10, _textView.SelectionStartColumn);
  410. Assert.Equal (0, _textView.SelectionStartRow);
  411. Assert.Equal (6, _textView.SelectedLength);
  412. Assert.Equal ("to jum", _textView.SelectedText);
  413. break;
  414. case 2:
  415. Assert.Equal (0, _textView.CursorPosition.X);
  416. Assert.Equal (0, _textView.CursorPosition.Y);
  417. Assert.Equal (10, _textView.SelectionStartColumn);
  418. Assert.Equal (0, _textView.SelectionStartRow);
  419. Assert.Equal (10, _textView.SelectedLength);
  420. Assert.Equal ("TAB to jum", _textView.SelectedText);
  421. break;
  422. }
  423. iteration++;
  424. }
  425. }
  426. [Fact]
  427. public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  428. {
  429. // 1 2 3 4 5
  430. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  431. _textView.Text = "TAB t o jump b etween t ext f ields .";
  432. _textView.CursorPosition = new Point (0, 0);
  433. var iteration = 0;
  434. while (_textView.CursorPosition.X < _textView.Text.Length) {
  435. _textView.ProcessKey (new KeyEvent (Key.CursorRight | Key.CtrlMask, new KeyModifiers ()));
  436. switch (iteration) {
  437. case 0:
  438. Assert.Equal (6, _textView.CursorPosition.X);
  439. Assert.Equal (0, _textView.CursorPosition.Y);
  440. Assert.Equal (0, _textView.SelectionStartColumn);
  441. Assert.Equal (0, _textView.SelectionStartRow);
  442. Assert.Equal (0, _textView.SelectedLength);
  443. Assert.Equal ("", _textView.SelectedText);
  444. break;
  445. case 1:
  446. Assert.Equal (9, _textView.CursorPosition.X);
  447. Assert.Equal (0, _textView.CursorPosition.Y);
  448. Assert.Equal (0, _textView.SelectionStartColumn);
  449. Assert.Equal (0, _textView.SelectionStartRow);
  450. Assert.Equal (0, _textView.SelectedLength);
  451. Assert.Equal ("", _textView.SelectedText);
  452. break;
  453. case 2:
  454. Assert.Equal (12, _textView.CursorPosition.X);
  455. Assert.Equal (0, _textView.CursorPosition.Y);
  456. Assert.Equal (0, _textView.SelectionStartColumn);
  457. Assert.Equal (0, _textView.SelectionStartRow);
  458. Assert.Equal (0, _textView.SelectedLength);
  459. Assert.Equal ("", _textView.SelectedText);
  460. break;
  461. case 3:
  462. Assert.Equal (25, _textView.CursorPosition.X);
  463. Assert.Equal (0, _textView.CursorPosition.Y);
  464. Assert.Equal (0, _textView.SelectionStartColumn);
  465. Assert.Equal (0, _textView.SelectionStartRow);
  466. Assert.Equal (0, _textView.SelectedLength);
  467. Assert.Equal ("", _textView.SelectedText);
  468. break;
  469. case 4:
  470. Assert.Equal (28, _textView.CursorPosition.X);
  471. Assert.Equal (0, _textView.CursorPosition.Y);
  472. Assert.Equal (0, _textView.SelectionStartColumn);
  473. Assert.Equal (0, _textView.SelectionStartRow);
  474. Assert.Equal (0, _textView.SelectedLength);
  475. Assert.Equal ("", _textView.SelectedText);
  476. break;
  477. case 5:
  478. Assert.Equal (38, _textView.CursorPosition.X);
  479. Assert.Equal (0, _textView.CursorPosition.Y);
  480. Assert.Equal (0, _textView.SelectionStartColumn);
  481. Assert.Equal (0, _textView.SelectionStartRow);
  482. Assert.Equal (0, _textView.SelectedLength);
  483. Assert.Equal ("", _textView.SelectedText);
  484. break;
  485. case 6:
  486. Assert.Equal (40, _textView.CursorPosition.X);
  487. Assert.Equal (0, _textView.CursorPosition.Y);
  488. Assert.Equal (0, _textView.SelectionStartColumn);
  489. Assert.Equal (0, _textView.SelectionStartRow);
  490. Assert.Equal (0, _textView.SelectedLength);
  491. Assert.Equal ("", _textView.SelectedText);
  492. break;
  493. case 7:
  494. Assert.Equal (46, _textView.CursorPosition.X);
  495. Assert.Equal (0, _textView.CursorPosition.Y);
  496. Assert.Equal (0, _textView.SelectionStartColumn);
  497. Assert.Equal (0, _textView.SelectionStartRow);
  498. Assert.Equal (0, _textView.SelectedLength);
  499. Assert.Equal ("", _textView.SelectedText);
  500. break;
  501. case 8:
  502. Assert.Equal (48, _textView.CursorPosition.X);
  503. Assert.Equal (0, _textView.CursorPosition.Y);
  504. Assert.Equal (0, _textView.SelectionStartColumn);
  505. Assert.Equal (0, _textView.SelectionStartRow);
  506. Assert.Equal (0, _textView.SelectedLength);
  507. Assert.Equal ("", _textView.SelectedText);
  508. break;
  509. case 9:
  510. Assert.Equal (55, _textView.CursorPosition.X);
  511. Assert.Equal (0, _textView.CursorPosition.Y);
  512. Assert.Equal (0, _textView.SelectionStartColumn);
  513. Assert.Equal (0, _textView.SelectionStartRow);
  514. Assert.Equal (0, _textView.SelectedLength);
  515. Assert.Equal ("", _textView.SelectedText);
  516. break;
  517. }
  518. iteration++;
  519. }
  520. }
  521. [Fact]
  522. public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
  523. {
  524. // 1 2 3 4 5
  525. // 0123456789012345678901234567890123456789012345678901234=55 (Length)
  526. _textView.Text = "TAB t o jump b etween t ext f ields .";
  527. _textView.CursorPosition = new Point (_textView.Text.Length, 0);
  528. var iteration = 0;
  529. while (_textView.CursorPosition.X > 0) {
  530. _textView.ProcessKey (new KeyEvent (Key.CursorLeft | Key.CtrlMask, new KeyModifiers ()));
  531. switch (iteration) {
  532. case 0:
  533. Assert.Equal (54, _textView.CursorPosition.X);
  534. Assert.Equal (0, _textView.CursorPosition.Y);
  535. Assert.Equal (0, _textView.SelectionStartColumn);
  536. Assert.Equal (0, _textView.SelectionStartRow);
  537. Assert.Equal (0, _textView.SelectedLength);
  538. Assert.Equal ("", _textView.SelectedText);
  539. break;
  540. case 1:
  541. Assert.Equal (48, _textView.CursorPosition.X);
  542. Assert.Equal (0, _textView.CursorPosition.Y);
  543. Assert.Equal (0, _textView.SelectionStartColumn);
  544. Assert.Equal (0, _textView.SelectionStartRow);
  545. Assert.Equal (0, _textView.SelectedLength);
  546. Assert.Equal ("", _textView.SelectedText);
  547. break;
  548. case 2:
  549. Assert.Equal (46, _textView.CursorPosition.X);
  550. Assert.Equal (0, _textView.CursorPosition.Y);
  551. Assert.Equal (0, _textView.SelectionStartColumn);
  552. Assert.Equal (0, _textView.SelectionStartRow);
  553. Assert.Equal (0, _textView.SelectedLength);
  554. Assert.Equal ("", _textView.SelectedText);
  555. break;
  556. case 3:
  557. Assert.Equal (40, _textView.CursorPosition.X);
  558. Assert.Equal (0, _textView.CursorPosition.Y);
  559. Assert.Equal (0, _textView.SelectionStartColumn);
  560. Assert.Equal (0, _textView.SelectionStartRow);
  561. Assert.Equal (0, _textView.SelectedLength);
  562. Assert.Equal ("", _textView.SelectedText);
  563. break;
  564. case 4:
  565. Assert.Equal (38, _textView.CursorPosition.X);
  566. Assert.Equal (0, _textView.CursorPosition.Y);
  567. Assert.Equal (0, _textView.SelectionStartColumn);
  568. Assert.Equal (0, _textView.SelectionStartRow);
  569. Assert.Equal (0, _textView.SelectedLength);
  570. Assert.Equal ("", _textView.SelectedText);
  571. break;
  572. case 5:
  573. Assert.Equal (28, _textView.CursorPosition.X);
  574. Assert.Equal (0, _textView.CursorPosition.Y);
  575. Assert.Equal (0, _textView.SelectionStartColumn);
  576. Assert.Equal (0, _textView.SelectionStartRow);
  577. Assert.Equal (0, _textView.SelectedLength);
  578. Assert.Equal ("", _textView.SelectedText);
  579. break;
  580. case 6:
  581. Assert.Equal (25, _textView.CursorPosition.X);
  582. Assert.Equal (0, _textView.CursorPosition.Y);
  583. Assert.Equal (0, _textView.SelectionStartColumn);
  584. Assert.Equal (0, _textView.SelectionStartRow);
  585. Assert.Equal (0, _textView.SelectedLength);
  586. Assert.Equal ("", _textView.SelectedText);
  587. break;
  588. case 7:
  589. Assert.Equal (12, _textView.CursorPosition.X);
  590. Assert.Equal (0, _textView.CursorPosition.Y);
  591. Assert.Equal (0, _textView.SelectionStartColumn);
  592. Assert.Equal (0, _textView.SelectionStartRow);
  593. Assert.Equal (0, _textView.SelectedLength);
  594. Assert.Equal ("", _textView.SelectedText);
  595. break;
  596. case 8:
  597. Assert.Equal (9, _textView.CursorPosition.X);
  598. Assert.Equal (0, _textView.CursorPosition.Y);
  599. Assert.Equal (0, _textView.SelectionStartColumn);
  600. Assert.Equal (0, _textView.SelectionStartRow);
  601. Assert.Equal (0, _textView.SelectedLength);
  602. Assert.Equal ("", _textView.SelectedText);
  603. break;
  604. case 9:
  605. Assert.Equal (6, _textView.CursorPosition.X);
  606. Assert.Equal (0, _textView.CursorPosition.Y);
  607. Assert.Equal (0, _textView.SelectionStartColumn);
  608. Assert.Equal (0, _textView.SelectionStartRow);
  609. Assert.Equal (0, _textView.SelectedLength);
  610. Assert.Equal ("", _textView.SelectedText);
  611. break;
  612. case 10:
  613. Assert.Equal (0, _textView.CursorPosition.X);
  614. Assert.Equal (0, _textView.CursorPosition.Y);
  615. Assert.Equal (0, _textView.SelectionStartColumn);
  616. Assert.Equal (0, _textView.SelectionStartRow);
  617. Assert.Equal (0, _textView.SelectedLength);
  618. Assert.Equal ("", _textView.SelectedText);
  619. break;
  620. }
  621. iteration++;
  622. }
  623. }
  624. [Fact]
  625. public void Copy_Or_Cut_Null_If_No_Selection ()
  626. {
  627. _textView.SelectionStartColumn = 0;
  628. _textView.SelectionStartRow = 0;
  629. _textView.Copy ();
  630. Assert.Equal ("", _textView.SelectedText);
  631. _textView.Cut ();
  632. Assert.Equal ("", _textView.SelectedText);
  633. }
  634. [Fact]
  635. public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
  636. {
  637. _textView.SelectionStartColumn = 20;
  638. _textView.SelectionStartRow = 0;
  639. _textView.CursorPosition = new Point (24, 0);
  640. _textView.Copy ();
  641. Assert.Equal ("text", _textView.SelectedText);
  642. _textView.Cut ();
  643. Assert.Equal ("", _textView.SelectedText);
  644. }
  645. [Fact]
  646. public void Copy_Or_Cut_And_Paste_With_Selection ()
  647. {
  648. _textView.SelectionStartColumn = 20;
  649. _textView.SelectionStartRow = 0;
  650. _textView.CursorPosition = new Point (24, 0);
  651. _textView.Copy ();
  652. Assert.Equal ("text", _textView.SelectedText);
  653. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  654. _textView.Paste ();
  655. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  656. _textView.SelectionStartColumn = 20;
  657. _textView.SelectionStartRow = 0;
  658. _textView.Cut ();
  659. _textView.Paste ();
  660. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  661. }
  662. [Fact]
  663. public void Copy_Or_Cut_And_Paste_With_No_Selection ()
  664. {
  665. _textView.SelectionStartColumn = 20;
  666. _textView.SelectionStartRow = 0;
  667. _textView.CursorPosition = new Point (24, 0);
  668. _textView.Copy ();
  669. Assert.Equal ("text", _textView.SelectedText);
  670. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  671. _textView.SelectionStartColumn = 0;
  672. _textView.SelectionStartRow = 0;
  673. Assert.True (_textView.Selecting);
  674. _textView.Selecting = false;
  675. _textView.Paste ();
  676. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  677. _textView.SelectionStartColumn = 24;
  678. _textView.SelectionStartRow = 0;
  679. _textView.Cut ();
  680. Assert.Equal ("", _textView.SelectedText);
  681. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  682. _textView.SelectionStartColumn = 0;
  683. _textView.SelectionStartRow = 0;
  684. Assert.True (_textView.Selecting);
  685. _textView.Selecting = false;
  686. _textView.Paste ();
  687. Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
  688. }
  689. [Fact]
  690. public void Cut_Not_Allowed_If_ReadOnly_Is_True ()
  691. {
  692. _textView.ReadOnly = true;
  693. _textView.SelectionStartColumn = 20;
  694. _textView.SelectionStartRow = 0;
  695. _textView.CursorPosition = new Point (24, 0);
  696. _textView.Copy ();
  697. Assert.Equal ("text", _textView.SelectedText);
  698. _textView.Cut (); // Selecting is set to false after Cut.
  699. Assert.Equal ("", _textView.SelectedText);
  700. _textView.ReadOnly = false;
  701. Assert.False (_textView.Selecting);
  702. _textView.Selecting = true; // Needed to set Selecting to true.
  703. _textView.Copy ();
  704. Assert.Equal ("text", _textView.SelectedText);
  705. _textView.Cut ();
  706. Assert.Equal ("", _textView.SelectedText);
  707. }
  708. [Fact]
  709. public void Paste_Always_Clear_The_SelectedText ()
  710. {
  711. _textView.SelectionStartColumn = 20;
  712. _textView.SelectionStartRow = 0;
  713. _textView.CursorPosition = new Point (24, 0);
  714. _textView.Copy ();
  715. Assert.Equal ("text", _textView.SelectedText);
  716. _textView.Paste ();
  717. Assert.Equal ("", _textView.SelectedText);
  718. }
  719. [Fact]
  720. public void TextChanged_Event ()
  721. {
  722. _textView.TextChanged += () => {
  723. if (_textView.Text == "changing") {
  724. Assert.Equal ("changing", _textView.Text);
  725. _textView.Text = "changed";
  726. }
  727. };
  728. _textView.Text = "changing";
  729. Assert.Equal ("changed", _textView.Text);
  730. }
  731. [Fact]
  732. public void Used_Is_True_By_Default ()
  733. {
  734. _textView.CursorPosition = new Point (10, 0);
  735. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  736. _textView.ProcessKey (new KeyEvent ((Key)0x75, new KeyModifiers ())); // u
  737. Assert.Equal ("TAB to jumup between text fields.", _textView.Text);
  738. _textView.ProcessKey (new KeyEvent ((Key)0x73, new KeyModifiers ())); // s
  739. Assert.Equal ("TAB to jumusp between text fields.", _textView.Text);
  740. _textView.ProcessKey (new KeyEvent ((Key)0x65, new KeyModifiers ())); // e
  741. Assert.Equal ("TAB to jumusep between text fields.", _textView.Text);
  742. _textView.ProcessKey (new KeyEvent ((Key)0x64, new KeyModifiers ())); // d
  743. Assert.Equal ("TAB to jumusedp between text fields.", _textView.Text);
  744. }
  745. [Fact]
  746. public void Used_Is_False ()
  747. {
  748. _textView.Used = false;
  749. _textView.CursorPosition = new Point (10, 0);
  750. Assert.Equal ("TAB to jump between text fields.", _textView.Text);
  751. _textView.ProcessKey (new KeyEvent ((Key)0x75, new KeyModifiers ())); // u
  752. Assert.Equal ("TAB to jumu between text fields.", _textView.Text);
  753. _textView.ProcessKey (new KeyEvent ((Key)0x73, new KeyModifiers ())); // s
  754. Assert.Equal ("TAB to jumusbetween text fields.", _textView.Text);
  755. _textView.ProcessKey (new KeyEvent ((Key)0x65, new KeyModifiers ())); // e
  756. Assert.Equal ("TAB to jumuseetween text fields.", _textView.Text);
  757. _textView.ProcessKey (new KeyEvent ((Key)0x64, new KeyModifiers ())); // d
  758. Assert.Equal ("TAB to jumusedtween text fields.", _textView.Text);
  759. }
  760. }
  761. }