TextViewNavigationTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using UnitTests;
  2. namespace UnitTests_Parallelizable.ViewsTests;
  3. /// <summary>
  4. /// Tests for TextView navigation, tabs, and cursor positioning.
  5. /// These replace the old non-parallelizable tests that had hard-coded viewport dimensions.
  6. /// </summary>
  7. public class TextViewNavigationTests : FakeDriverBase
  8. {
  9. [Fact]
  10. public void Tab_And_BackTab_Navigation_Without_Text ()
  11. {
  12. IDriver driver = CreateFakeDriver ();
  13. var textView = new TextView
  14. {
  15. Width = 30,
  16. Height = 10,
  17. Text = "",
  18. Driver = driver
  19. };
  20. textView.BeginInit ();
  21. textView.EndInit ();
  22. // Add 100 tabs
  23. for (var i = 0; i < 100; i++)
  24. {
  25. textView.Text += "\t";
  26. }
  27. // Move to end
  28. textView.MoveEnd ();
  29. Assert.Equal (new Point (100, 0), textView.CursorPosition);
  30. // Test BackTab (Shift+Tab) navigation backwards
  31. for (var i = 99; i >= 0; i--)
  32. {
  33. Assert.True (textView.NewKeyDownEvent (Key.Tab.WithShift));
  34. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  35. }
  36. // Test Tab navigation forwards
  37. for (var i = 1; i <= 100; i++)
  38. {
  39. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  40. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  41. }
  42. }
  43. [Fact]
  44. public void Tab_And_BackTab_Navigation_With_Text ()
  45. {
  46. IDriver driver = CreateFakeDriver ();
  47. var textView = new TextView
  48. {
  49. Width = 30,
  50. Height = 10,
  51. Text = "TAB to jump between text fields.",
  52. Driver = driver
  53. };
  54. textView.BeginInit ();
  55. textView.EndInit ();
  56. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  57. // Navigate forward with Tab
  58. for (var i = 1; i <= 100; i++)
  59. {
  60. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  61. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  62. }
  63. // Navigate backward with BackTab
  64. for (var i = 99; i >= 0; i--)
  65. {
  66. Assert.True (textView.NewKeyDownEvent (Key.Tab.WithShift));
  67. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  68. }
  69. }
  70. [Fact]
  71. public void Tab_With_CursorLeft_And_CursorRight_Without_Text ()
  72. {
  73. IDriver driver = CreateFakeDriver ();
  74. var textView = new TextView
  75. {
  76. Width = 30,
  77. Height = 10,
  78. Text = "",
  79. Driver = driver
  80. };
  81. textView.BeginInit ();
  82. textView.EndInit ();
  83. // Navigate forward with Tab
  84. for (var i = 1; i <= 100; i++)
  85. {
  86. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  87. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  88. }
  89. // Navigate backward with CursorLeft
  90. for (var i = 99; i >= 0; i--)
  91. {
  92. Assert.True (textView.NewKeyDownEvent (Key.CursorLeft));
  93. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  94. }
  95. // Navigate forward with CursorRight
  96. for (var i = 1; i <= 100; i++)
  97. {
  98. Assert.True (textView.NewKeyDownEvent (Key.CursorRight));
  99. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  100. }
  101. }
  102. [Fact]
  103. public void Tab_With_CursorLeft_And_CursorRight_With_Text ()
  104. {
  105. IDriver driver = CreateFakeDriver ();
  106. var textView = new TextView
  107. {
  108. Width = 30,
  109. Height = 10,
  110. Text = "TAB to jump between text fields.",
  111. Driver = driver
  112. };
  113. textView.BeginInit ();
  114. textView.EndInit ();
  115. Assert.Equal (32, textView.Text.Length);
  116. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  117. // Navigate forward with Tab
  118. for (var i = 1; i <= 100; i++)
  119. {
  120. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  121. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  122. }
  123. // Navigate backward with CursorLeft
  124. for (var i = 99; i >= 0; i--)
  125. {
  126. Assert.True (textView.NewKeyDownEvent (Key.CursorLeft));
  127. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  128. }
  129. // Navigate forward with CursorRight
  130. for (var i = 1; i <= 100; i++)
  131. {
  132. Assert.True (textView.NewKeyDownEvent (Key.CursorRight));
  133. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  134. }
  135. }
  136. [Fact]
  137. public void Tab_With_Home_End_And_BackTab ()
  138. {
  139. IDriver driver = CreateFakeDriver ();
  140. var textView = new TextView
  141. {
  142. Width = 30,
  143. Height = 10,
  144. Text = "TAB to jump between text fields.",
  145. Driver = driver
  146. };
  147. textView.BeginInit ();
  148. textView.EndInit ();
  149. Assert.Equal (32, textView.Text.Length);
  150. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  151. // Navigate forward with Tab to column 100
  152. for (var i = 1; i <= 100; i++)
  153. {
  154. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  155. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  156. }
  157. // Test Length increased due to tabs
  158. Assert.Equal (132, textView.Text.Length);
  159. // Press Home to go to beginning
  160. Assert.True (textView.NewKeyDownEvent (Key.Home));
  161. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  162. // Press End to go to end
  163. Assert.True (textView.NewKeyDownEvent (Key.End));
  164. Assert.Equal (132, textView.Text.Length);
  165. Assert.Equal (new Point (132, 0), textView.CursorPosition);
  166. // Find the position just before the last tab
  167. string txt = textView.Text;
  168. var col = txt.Length;
  169. while (col - 1 > 0 && txt [col - 1] != '\t')
  170. {
  171. col--;
  172. }
  173. // Set cursor to that position
  174. textView.CursorPosition = new Point (col, 0);
  175. // Navigate backward with BackTab (removes tabs, going back to original text)
  176. while (col > 0)
  177. {
  178. col--;
  179. Assert.True (textView.NewKeyDownEvent (Key.Tab.WithShift));
  180. Assert.Equal (new Point (col, 0), textView.CursorPosition);
  181. }
  182. // Should be back at the original text
  183. Assert.Equal ("TAB to jump between text fields.", textView.Text);
  184. Assert.Equal (32, textView.Text.Length);
  185. }
  186. [Fact]
  187. public void BackTab_Then_Tab_Navigation ()
  188. {
  189. IDriver driver = CreateFakeDriver ();
  190. var textView = new TextView
  191. {
  192. Width = 30,
  193. Height = 10,
  194. Text = "",
  195. Driver = driver
  196. };
  197. textView.BeginInit ();
  198. textView.EndInit ();
  199. // Add 100 tabs at end
  200. for (var i = 0; i < 100; i++)
  201. {
  202. textView.Text += "\t";
  203. }
  204. textView.MoveEnd ();
  205. Assert.Equal (new Point (100, 0), textView.CursorPosition);
  206. // Navigate backward with BackTab
  207. for (var i = 99; i >= 0; i--)
  208. {
  209. Assert.True (textView.NewKeyDownEvent (Key.Tab.WithShift));
  210. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  211. }
  212. // Navigate forward with Tab
  213. for (var i = 1; i <= 100; i++)
  214. {
  215. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  216. Assert.Equal (new Point (i, 0), textView.CursorPosition);
  217. }
  218. }
  219. [Fact]
  220. public void TabWidth_Setting_To_Zero_Keeps_AllowsTab ()
  221. {
  222. IDriver driver = CreateFakeDriver ();
  223. var textView = new TextView
  224. {
  225. Width = 30,
  226. Height = 10,
  227. Text = "TAB to jump between text fields.",
  228. Driver = driver
  229. };
  230. textView.BeginInit ();
  231. textView.EndInit ();
  232. // Verify initial state
  233. Assert.Equal (4, textView.TabWidth);
  234. Assert.True (textView.AllowsTab);
  235. Assert.True (textView.AllowsReturn);
  236. Assert.True (textView.Multiline);
  237. // Set TabWidth to -1 (should clamp to 0)
  238. textView.TabWidth = -1;
  239. Assert.Equal (0, textView.TabWidth);
  240. Assert.True (textView.AllowsTab);
  241. Assert.True (textView.AllowsReturn);
  242. Assert.True (textView.Multiline);
  243. // Insert a tab
  244. Assert.True (textView.NewKeyDownEvent (Key.Tab));
  245. Assert.Equal ("\tTAB to jump between text fields.", textView.Text);
  246. // Change TabWidth back to 4
  247. textView.TabWidth = 4;
  248. Assert.Equal (4, textView.TabWidth);
  249. // Remove the tab with BackTab
  250. Assert.True (textView.NewKeyDownEvent (Key.Tab.WithShift));
  251. Assert.Equal ("TAB to jump between text fields.", textView.Text);
  252. }
  253. [Fact]
  254. public void KeyBindings_Command_Navigation ()
  255. {
  256. IDriver driver = CreateFakeDriver ();
  257. var text = "This is the first line.\nThis is the second line.\nThis is the third line.";
  258. var textView = new TextView
  259. {
  260. Width = 10,
  261. Height = 2,
  262. Text = text,
  263. Driver = driver
  264. };
  265. textView.BeginInit ();
  266. textView.EndInit ();
  267. Assert.Equal (
  268. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  269. textView.Text
  270. );
  271. Assert.Equal (3, textView.Lines);
  272. Assert.Equal (Point.Empty, textView.CursorPosition);
  273. Assert.False (textView.ReadOnly);
  274. Assert.True (textView.CanFocus);
  275. Assert.False (textView.IsSelecting);
  276. // Test that CursorLeft doesn't move when at beginning
  277. textView.CanFocus = false;
  278. Assert.True (textView.NewKeyDownEvent (Key.CursorLeft));
  279. Assert.False (textView.IsSelecting);
  280. textView.CanFocus = true;
  281. Assert.False (textView.NewKeyDownEvent (Key.CursorLeft));
  282. Assert.False (textView.IsSelecting);
  283. // Move right
  284. Assert.True (textView.NewKeyDownEvent (Key.CursorRight));
  285. Assert.Equal (new Point (1, 0), textView.CursorPosition);
  286. Assert.False (textView.IsSelecting);
  287. // Move to end of document
  288. Assert.True (textView.NewKeyDownEvent (Key.End.WithCtrl));
  289. Assert.Equal (2, textView.CurrentRow);
  290. Assert.Equal (23, textView.CurrentColumn);
  291. Assert.Equal (textView.CurrentColumn, textView.GetCurrentLine ().Count);
  292. Assert.Equal (new Point (23, 2), textView.CursorPosition);
  293. Assert.False (textView.IsSelecting);
  294. // Try to move right (should fail, at end)
  295. Assert.False (textView.NewKeyDownEvent (Key.CursorRight));
  296. Assert.False (textView.IsSelecting);
  297. // Type a character
  298. Assert.True (textView.NewKeyDownEvent (Key.F.WithShift));
  299. Assert.Equal (
  300. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.F",
  301. textView.Text
  302. );
  303. Assert.Equal (new Point (24, 2), textView.CursorPosition);
  304. Assert.False (textView.IsSelecting);
  305. // Undo
  306. Assert.True (textView.NewKeyDownEvent (Key.Z.WithCtrl));
  307. Assert.Equal (
  308. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.",
  309. textView.Text
  310. );
  311. Assert.Equal (new Point (23, 2), textView.CursorPosition);
  312. Assert.False (textView.IsSelecting);
  313. // Redo
  314. Assert.True (textView.NewKeyDownEvent (Key.R.WithCtrl));
  315. Assert.Equal (
  316. $"This is the first line.{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third line.F",
  317. textView.Text
  318. );
  319. Assert.Equal (new Point (24, 2), textView.CursorPosition);
  320. Assert.False (textView.IsSelecting);
  321. }
  322. [Fact]
  323. public void UnwrappedCursorPosition_Event_Fires_Correctly ()
  324. {
  325. IDriver driver = CreateFakeDriver (25, 25);
  326. Point unwrappedPosition = Point.Empty;
  327. var textView = new TextView
  328. {
  329. Width = Dim.Fill (),
  330. Height = Dim.Fill (),
  331. Text = "This is the first line.\nThis is the second line.\n",
  332. Driver = driver
  333. };
  334. textView.UnwrappedCursorPosition += (s, e) => { unwrappedPosition = e; };
  335. textView.BeginInit ();
  336. textView.EndInit ();
  337. // Initially no word wrap
  338. Assert.False (textView.WordWrap);
  339. Assert.Equal (Point.Empty, textView.CursorPosition);
  340. Assert.Equal (Point.Empty, unwrappedPosition);
  341. // Enable word wrap and move cursor
  342. textView.WordWrap = true;
  343. textView.CursorPosition = new Point (12, 0);
  344. Assert.Equal (new Point (12, 0), textView.CursorPosition);
  345. Assert.Equal (new Point (12, 0), unwrappedPosition);
  346. // Resize to narrow width to force wrapping
  347. textView.Width = 6;
  348. textView.Height = 25;
  349. textView.SetRelativeLayout (new Size (6, 25));
  350. // The wrapped cursor position should differ from unwrapped
  351. // After wrap, column 12 might be on a different visual line
  352. // but unwrapped position should still be (12, 0)
  353. Assert.Equal (new Point (12, 0), unwrappedPosition);
  354. // Move right - the unwrapped position event may not fire immediately
  355. // or may be based on previous cursor position
  356. // Just verify the event mechanism works
  357. var currentUnwrapped = unwrappedPosition;
  358. Assert.True (textView.NewKeyDownEvent (Key.CursorRight));
  359. // The unwrapped position should have updated (either to 13 or still tracking properly)
  360. Assert.True (unwrappedPosition.X >= currentUnwrapped.X);
  361. }
  362. [Fact]
  363. public void Horizontal_Scrolling_Adjusts_LeftColumn ()
  364. {
  365. IDriver driver = CreateFakeDriver ();
  366. var textView = new TextView
  367. {
  368. Width = 20,
  369. Height = 5,
  370. Text = "This is a very long line that will require horizontal scrolling to see all of it",
  371. WordWrap = false,
  372. Driver = driver
  373. };
  374. textView.BeginInit ();
  375. textView.EndInit ();
  376. // Initially at the start
  377. Assert.Equal (0, textView.LeftColumn);
  378. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  379. // Move to the end of the line
  380. textView.MoveEnd ();
  381. // LeftColumn should have adjusted to show the cursor
  382. Assert.True (textView.LeftColumn > 0);
  383. Assert.Equal (textView.Text.Length, textView.CurrentColumn);
  384. // Move back to the start
  385. textView.MoveHome ();
  386. // LeftColumn should be back to 0
  387. Assert.Equal (0, textView.LeftColumn);
  388. Assert.Equal (0, textView.CurrentColumn);
  389. }
  390. [Fact]
  391. public void Vertical_Scrolling_Adjusts_TopRow ()
  392. {
  393. IDriver driver = CreateFakeDriver ();
  394. var lines = string.Join ("\n", Enumerable.Range (1, 100).Select (i => $"Line {i}"));
  395. var textView = new TextView
  396. {
  397. Width = 20,
  398. Height = 5,
  399. Text = lines,
  400. Driver = driver
  401. };
  402. textView.BeginInit ();
  403. textView.EndInit ();
  404. // Initially at the top
  405. Assert.Equal (0, textView.TopRow);
  406. Assert.Equal (new Point (0, 0), textView.CursorPosition);
  407. // Move down many lines
  408. for (var i = 0; i < 50; i++)
  409. {
  410. textView.NewKeyDownEvent (Key.CursorDown);
  411. }
  412. // TopRow should have adjusted to show the cursor
  413. Assert.True (textView.TopRow > 0);
  414. Assert.Equal (50, textView.CurrentRow);
  415. // Move back to the top
  416. textView.NewKeyDownEvent (Key.Home.WithCtrl);
  417. // TopRow should be back to 0
  418. Assert.Equal (0, textView.TopRow);
  419. Assert.Equal (0, textView.CurrentRow);
  420. }
  421. }