ListViewTests.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. using System.Collections;
  2. using System.Collections.ObjectModel;
  3. using System.Collections.Specialized;
  4. using Moq;
  5. using UnitTests;
  6. using Xunit.Abstractions;
  7. namespace UnitTests.ViewsTests;
  8. public class ListViewTests (ITestOutputHelper output)
  9. {
  10. [Fact]
  11. public void Constructors_Defaults ()
  12. {
  13. var lv = new ListView ();
  14. Assert.Null (lv.Source);
  15. Assert.True (lv.CanFocus);
  16. Assert.Equal (-1, lv.SelectedItem);
  17. Assert.False (lv.AllowsMultipleSelection);
  18. lv = new () { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
  19. Assert.NotNull (lv.Source);
  20. Assert.Equal (-1, lv.SelectedItem);
  21. lv = new () { Source = new NewListDataSource () };
  22. Assert.NotNull (lv.Source);
  23. Assert.Equal (-1, lv.SelectedItem);
  24. lv = new ()
  25. {
  26. Y = 1, Width = 10, Height = 20, Source = new ListWrapper<string> (["One", "Two", "Three"])
  27. };
  28. Assert.NotNull (lv.Source);
  29. Assert.Equal (-1, lv.SelectedItem);
  30. Assert.Equal (new (0, 1, 10, 20), lv.Frame);
  31. lv = new () { Y = 1, Width = 10, Height = 20, Source = new NewListDataSource () };
  32. Assert.NotNull (lv.Source);
  33. Assert.Equal (-1, lv.SelectedItem);
  34. Assert.Equal (new (0, 1, 10, 20), lv.Frame);
  35. }
  36. [Fact]
  37. [AutoInitShutdown]
  38. public void Ensures_Visibility_SelectedItem_On_MoveDown_And_MoveUp ()
  39. {
  40. ObservableCollection<string> source = [];
  41. for (var i = 0; i < 20; i++)
  42. {
  43. source.Add ($"Line{i}");
  44. }
  45. var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill (), Source = new ListWrapper<string> (source) };
  46. var win = new Window ();
  47. win.Add (lv);
  48. var top = new Toplevel ();
  49. top.Add (win);
  50. SessionToken rs = Application.Begin (top);
  51. Application.Driver!.SetScreenSize (12, 12);
  52. AutoInitShutdownAttribute.RunIteration ();
  53. Assert.Equal (-1, lv.SelectedItem);
  54. DriverAssert.AssertDriverContentsWithFrameAre (
  55. @"
  56. ┌──────────┐
  57. │Line0 │
  58. │Line1 │
  59. │Line2 │
  60. │Line3 │
  61. │Line4 │
  62. │Line5 │
  63. │Line6 │
  64. │Line7 │
  65. │Line8 │
  66. │Line9 │
  67. └──────────┘",
  68. output
  69. );
  70. Assert.True (lv.ScrollVertical (10));
  71. AutoInitShutdownAttribute.RunIteration ();
  72. Assert.Equal (-1, lv.SelectedItem);
  73. DriverAssert.AssertDriverContentsWithFrameAre (
  74. @"
  75. ┌──────────┐
  76. │Line10 │
  77. │Line11 │
  78. │Line12 │
  79. │Line13 │
  80. │Line14 │
  81. │Line15 │
  82. │Line16 │
  83. │Line17 │
  84. │Line18 │
  85. │Line19 │
  86. └──────────┘",
  87. output
  88. );
  89. Assert.True (lv.MoveDown ());
  90. AutoInitShutdownAttribute.RunIteration ();
  91. Assert.Equal (0, lv.SelectedItem);
  92. DriverAssert.AssertDriverContentsWithFrameAre (
  93. @"
  94. ┌──────────┐
  95. │Line0 │
  96. │Line1 │
  97. │Line2 │
  98. │Line3 │
  99. │Line4 │
  100. │Line5 │
  101. │Line6 │
  102. │Line7 │
  103. │Line8 │
  104. │Line9 │
  105. └──────────┘",
  106. output
  107. );
  108. Assert.True (lv.MoveEnd ());
  109. AutoInitShutdownAttribute.RunIteration ();
  110. Assert.Equal (19, lv.SelectedItem);
  111. DriverAssert.AssertDriverContentsWithFrameAre (
  112. @"
  113. ┌──────────┐
  114. │Line10 │
  115. │Line11 │
  116. │Line12 │
  117. │Line13 │
  118. │Line14 │
  119. │Line15 │
  120. │Line16 │
  121. │Line17 │
  122. │Line18 │
  123. │Line19 │
  124. └──────────┘",
  125. output
  126. );
  127. Assert.True (lv.ScrollVertical (-20));
  128. AutoInitShutdownAttribute.RunIteration ();
  129. Assert.Equal (19, lv.SelectedItem);
  130. DriverAssert.AssertDriverContentsWithFrameAre (
  131. @"
  132. ┌──────────┐
  133. │Line0 │
  134. │Line1 │
  135. │Line2 │
  136. │Line3 │
  137. │Line4 │
  138. │Line5 │
  139. │Line6 │
  140. │Line7 │
  141. │Line8 │
  142. │Line9 │
  143. └──────────┘",
  144. output
  145. );
  146. Assert.True (lv.MoveDown ());
  147. AutoInitShutdownAttribute.RunIteration ();
  148. Assert.Equal (19, lv.SelectedItem);
  149. DriverAssert.AssertDriverContentsWithFrameAre (
  150. @"
  151. ┌──────────┐
  152. │Line10 │
  153. │Line11 │
  154. │Line12 │
  155. │Line13 │
  156. │Line14 │
  157. │Line15 │
  158. │Line16 │
  159. │Line17 │
  160. │Line18 │
  161. │Line19 │
  162. └──────────┘",
  163. output
  164. );
  165. Assert.True (lv.ScrollVertical (-20));
  166. AutoInitShutdownAttribute.RunIteration ();
  167. Assert.Equal (19, lv.SelectedItem);
  168. DriverAssert.AssertDriverContentsWithFrameAre (
  169. @"
  170. ┌──────────┐
  171. │Line0 │
  172. │Line1 │
  173. │Line2 │
  174. │Line3 │
  175. │Line4 │
  176. │Line5 │
  177. │Line6 │
  178. │Line7 │
  179. │Line8 │
  180. │Line9 │
  181. └──────────┘",
  182. output
  183. );
  184. Assert.True (lv.MoveDown ());
  185. AutoInitShutdownAttribute.RunIteration ();
  186. Assert.Equal (19, lv.SelectedItem);
  187. DriverAssert.AssertDriverContentsWithFrameAre (
  188. @"
  189. ┌──────────┐
  190. │Line10 │
  191. │Line11 │
  192. │Line12 │
  193. │Line13 │
  194. │Line14 │
  195. │Line15 │
  196. │Line16 │
  197. │Line17 │
  198. │Line18 │
  199. │Line19 │
  200. └──────────┘",
  201. output
  202. );
  203. Assert.True (lv.MoveHome ());
  204. AutoInitShutdownAttribute.RunIteration ();
  205. Assert.Equal (0, lv.SelectedItem);
  206. DriverAssert.AssertDriverContentsWithFrameAre (
  207. @"
  208. ┌──────────┐
  209. │Line0 │
  210. │Line1 │
  211. │Line2 │
  212. │Line3 │
  213. │Line4 │
  214. │Line5 │
  215. │Line6 │
  216. │Line7 │
  217. │Line8 │
  218. │Line9 │
  219. └──────────┘",
  220. output
  221. );
  222. Assert.True (lv.ScrollVertical (20));
  223. AutoInitShutdownAttribute.RunIteration ();
  224. Assert.Equal (0, lv.SelectedItem);
  225. DriverAssert.AssertDriverContentsWithFrameAre (
  226. @"
  227. ┌──────────┐
  228. │Line19 │
  229. │ │
  230. │ │
  231. │ │
  232. │ │
  233. │ │
  234. │ │
  235. │ │
  236. │ │
  237. │ │
  238. └──────────┘",
  239. output
  240. );
  241. Assert.True (lv.MoveUp ());
  242. AutoInitShutdownAttribute.RunIteration ();
  243. Assert.Equal (0, lv.SelectedItem);
  244. DriverAssert.AssertDriverContentsWithFrameAre (
  245. @"
  246. ┌──────────┐
  247. │Line0 │
  248. │Line1 │
  249. │Line2 │
  250. │Line3 │
  251. │Line4 │
  252. │Line5 │
  253. │Line6 │
  254. │Line7 │
  255. │Line8 │
  256. │Line9 │
  257. └──────────┘",
  258. output
  259. );
  260. top.Dispose ();
  261. }
  262. [Fact]
  263. [AutoInitShutdown]
  264. public void EnsureSelectedItemVisible_SelectedItem ()
  265. {
  266. ObservableCollection<string> source = [];
  267. for (var i = 0; i < 10; i++)
  268. {
  269. source.Add ($"Item {i}");
  270. }
  271. var lv = new ListView { Width = 10, Height = 5, Source = new ListWrapper<string> (source) };
  272. var top = new Toplevel ();
  273. top.Add (lv);
  274. Application.Begin (top);
  275. AutoInitShutdownAttribute.RunIteration ();
  276. DriverAssert.AssertDriverContentsWithFrameAre (
  277. @"
  278. Item 0
  279. Item 1
  280. Item 2
  281. Item 3
  282. Item 4",
  283. output
  284. );
  285. // EnsureSelectedItemVisible is auto enabled on the OnSelectedChanged
  286. lv.SelectedItem = 6;
  287. AutoInitShutdownAttribute.RunIteration ();
  288. DriverAssert.AssertDriverContentsWithFrameAre (
  289. @"
  290. Item 2
  291. Item 3
  292. Item 4
  293. Item 5
  294. Item 6",
  295. output
  296. );
  297. top.Dispose ();
  298. }
  299. [Fact]
  300. [AutoInitShutdown]
  301. public void EnsureSelectedItemVisible_Top ()
  302. {
  303. ObservableCollection<string> source = ["First", "Second"];
  304. var lv = new ListView { Width = Dim.Fill (), Height = 1, Source = new ListWrapper<string> (source) };
  305. lv.SelectedItem = 1;
  306. var top = new Toplevel ();
  307. top.Add (lv);
  308. Application.Begin (top);
  309. AutoInitShutdownAttribute.RunIteration ();
  310. Assert.Equal ("Second ", GetContents (0));
  311. Assert.Equal (new (' ', 7), GetContents (1));
  312. lv.MoveUp ();
  313. lv.Draw ();
  314. Assert.Equal ("First ", GetContents (0));
  315. Assert.Equal (new (' ', 7), GetContents (1));
  316. string GetContents (int line)
  317. {
  318. var item = "";
  319. for (var i = 0; i < 7; i++)
  320. {
  321. item += Application.Driver?.Contents [line, i].Rune;
  322. }
  323. return item;
  324. }
  325. top.Dispose ();
  326. }
  327. [Fact]
  328. public void KeyBindings_Command ()
  329. {
  330. ObservableCollection<string> source = ["One", "Two", "Three"];
  331. var lv = new ListView { Height = 2, AllowsMarking = true, Source = new ListWrapper<string> (source) };
  332. lv.BeginInit ();
  333. lv.EndInit ();
  334. Assert.Equal (-1, lv.SelectedItem);
  335. Assert.True (lv.NewKeyDownEvent (Key.CursorDown));
  336. Assert.Equal (0, lv.SelectedItem);
  337. Assert.True (lv.NewKeyDownEvent (Key.CursorUp));
  338. Assert.Equal (0, lv.SelectedItem);
  339. Assert.True (lv.NewKeyDownEvent (Key.PageDown));
  340. Assert.Equal (2, lv.SelectedItem);
  341. Assert.Equal (2, lv.TopItem);
  342. Assert.True (lv.NewKeyDownEvent (Key.PageUp));
  343. Assert.Equal (0, lv.SelectedItem);
  344. Assert.Equal (0, lv.TopItem);
  345. Assert.False (lv.Source.IsMarked (lv.SelectedItem));
  346. Assert.True (lv.NewKeyDownEvent (Key.Space));
  347. Assert.True (lv.Source.IsMarked (lv.SelectedItem));
  348. var opened = false;
  349. lv.OpenSelectedItem += (s, _) => opened = true;
  350. Assert.True (lv.NewKeyDownEvent (Key.Enter));
  351. Assert.True (opened);
  352. Assert.True (lv.NewKeyDownEvent (Key.End));
  353. Assert.Equal (2, lv.SelectedItem);
  354. Assert.True (lv.NewKeyDownEvent (Key.Home));
  355. Assert.Equal (0, lv.SelectedItem);
  356. }
  357. [Fact]
  358. public void HotKey_Command_SetsFocus ()
  359. {
  360. var view = new ListView ();
  361. view.CanFocus = true;
  362. Assert.False (view.HasFocus);
  363. view.InvokeCommand (Command.HotKey);
  364. Assert.True (view.HasFocus);
  365. }
  366. [Fact]
  367. public void HotKey_Command_Does_Not_Accept ()
  368. {
  369. var listView = new ListView ();
  370. var accepted = false;
  371. listView.Accepting += OnAccepted;
  372. listView.InvokeCommand (Command.HotKey);
  373. Assert.False (accepted);
  374. return;
  375. void OnAccepted (object sender, CommandEventArgs e) { accepted = true; }
  376. }
  377. [Fact]
  378. public void Accept_Command_Accepts_and_Opens_Selected_Item ()
  379. {
  380. ObservableCollection<string> source = ["One", "Two", "Three"];
  381. var listView = new ListView { Source = new ListWrapper<string> (source) };
  382. listView.SelectedItem = 0;
  383. var accepted = false;
  384. var opened = false;
  385. var selectedValue = string.Empty;
  386. listView.Accepting += Accepted;
  387. listView.OpenSelectedItem += OpenSelectedItem;
  388. listView.InvokeCommand (Command.Accept);
  389. Assert.True (accepted);
  390. Assert.True (opened);
  391. Assert.Equal (source [0], selectedValue);
  392. return;
  393. void OpenSelectedItem (object sender, ListViewItemEventArgs e)
  394. {
  395. opened = true;
  396. selectedValue = e.Value.ToString ();
  397. }
  398. void Accepted (object sender, CommandEventArgs e) { accepted = true; }
  399. }
  400. [Fact]
  401. public void Accept_Cancel_Event_Prevents_OpenSelectedItem ()
  402. {
  403. ObservableCollection<string> source = ["One", "Two", "Three"];
  404. var listView = new ListView { Source = new ListWrapper<string> (source) };
  405. listView.SelectedItem = 0;
  406. var accepted = false;
  407. var opened = false;
  408. var selectedValue = string.Empty;
  409. listView.Accepting += Accepted;
  410. listView.OpenSelectedItem += OpenSelectedItem;
  411. listView.InvokeCommand (Command.Accept);
  412. Assert.True (accepted);
  413. Assert.False (opened);
  414. Assert.Equal (string.Empty, selectedValue);
  415. return;
  416. void OpenSelectedItem (object sender, ListViewItemEventArgs e)
  417. {
  418. opened = true;
  419. selectedValue = e.Value.ToString ();
  420. }
  421. void Accepted (object sender, CommandEventArgs e)
  422. {
  423. accepted = true;
  424. e.Handled = true;
  425. }
  426. }
  427. /// <summary>
  428. /// Tests that when none of the Commands in a chained keybinding are possible the
  429. /// <see cref="View.NewKeyDownEvent"/> returns the appropriate result
  430. /// </summary>
  431. [Fact]
  432. public void ListViewProcessKeyReturnValue_WithMultipleCommands ()
  433. {
  434. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three", "Four"]) };
  435. Assert.NotNull (lv.Source);
  436. // first item should be deselected by default
  437. Assert.Equal (-1, lv.SelectedItem);
  438. // bind shift down to move down twice in control
  439. lv.KeyBindings.Add (Key.CursorDown.WithShift, Command.Down, Command.Down);
  440. Key ev = Key.CursorDown.WithShift;
  441. Assert.True (lv.NewKeyDownEvent (ev), "The first time we move down 2 it should be possible");
  442. // After moving down twice from -1 we should be at 'Two'
  443. Assert.Equal (1, lv.SelectedItem);
  444. // clear the items
  445. lv.SetSource<string> (null);
  446. // Press key combo again - return should be false this time as none of the Commands are allowable
  447. Assert.False (lv.NewKeyDownEvent (ev), "We cannot move down so will not respond to this");
  448. }
  449. [Fact]
  450. public void AllowsMarking_True_SpaceWithShift_SelectsThenDown_SingleSelection ()
  451. {
  452. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
  453. lv.AllowsMarking = true;
  454. lv.AllowsMultipleSelection = false;
  455. Assert.NotNull (lv.Source);
  456. // first item should be deselected by default
  457. Assert.Equal (-1, lv.SelectedItem);
  458. // nothing is ticked
  459. Assert.False (lv.Source.IsMarked (0));
  460. Assert.False (lv.Source.IsMarked (1));
  461. Assert.False (lv.Source.IsMarked (2));
  462. // view should indicate that it has accepted and consumed the event
  463. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  464. // first item should now be selected
  465. Assert.Equal (0, lv.SelectedItem);
  466. // none of the items should be ticked
  467. Assert.False (lv.Source.IsMarked (0));
  468. Assert.False (lv.Source.IsMarked (1));
  469. Assert.False (lv.Source.IsMarked (2));
  470. // Press key combo again
  471. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  472. // second item should now be selected
  473. Assert.Equal (1, lv.SelectedItem);
  474. // first item only should be ticked
  475. Assert.True (lv.Source.IsMarked (0));
  476. Assert.False (lv.Source.IsMarked (1));
  477. Assert.False (lv.Source.IsMarked (2));
  478. // Press key combo again
  479. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  480. Assert.Equal (2, lv.SelectedItem);
  481. Assert.False (lv.Source.IsMarked (0));
  482. Assert.True (lv.Source.IsMarked (1));
  483. Assert.False (lv.Source.IsMarked (2));
  484. // Press key combo again
  485. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  486. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  487. Assert.False (lv.Source.IsMarked (0));
  488. Assert.False (lv.Source.IsMarked (1));
  489. Assert.True (lv.Source.IsMarked (2)); // but can toggle marked
  490. // Press key combo again
  491. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  492. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  493. Assert.False (lv.Source.IsMarked (0));
  494. Assert.False (lv.Source.IsMarked (1));
  495. Assert.False (lv.Source.IsMarked (2)); // untoggle toggle marked
  496. }
  497. [Fact]
  498. public void AllowsMarking_True_SpaceWithShift_SelectsThenDown_MultipleSelection ()
  499. {
  500. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
  501. lv.AllowsMarking = true;
  502. lv.AllowsMultipleSelection = true;
  503. Assert.NotNull (lv.Source);
  504. // first item should be deselected by default
  505. Assert.Equal (-1, lv.SelectedItem);
  506. // nothing is ticked
  507. Assert.False (lv.Source.IsMarked (0));
  508. Assert.False (lv.Source.IsMarked (1));
  509. Assert.False (lv.Source.IsMarked (2));
  510. // view should indicate that it has accepted and consumed the event
  511. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  512. // first item should now be selected
  513. Assert.Equal (0, lv.SelectedItem);
  514. // none of the items should be ticked
  515. Assert.False (lv.Source.IsMarked (0));
  516. Assert.False (lv.Source.IsMarked (1));
  517. Assert.False (lv.Source.IsMarked (2));
  518. // Press key combo again
  519. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  520. // second item should now be selected
  521. Assert.Equal (1, lv.SelectedItem);
  522. // first item only should be ticked
  523. Assert.True (lv.Source.IsMarked (0));
  524. Assert.False (lv.Source.IsMarked (1));
  525. Assert.False (lv.Source.IsMarked (2));
  526. // Press key combo again
  527. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  528. Assert.Equal (2, lv.SelectedItem);
  529. Assert.True (lv.Source.IsMarked (0));
  530. Assert.True (lv.Source.IsMarked (1));
  531. Assert.False (lv.Source.IsMarked (2));
  532. // Press key combo again
  533. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  534. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  535. Assert.True (lv.Source.IsMarked (0));
  536. Assert.True (lv.Source.IsMarked (1));
  537. Assert.True (lv.Source.IsMarked (2)); // but can toggle marked
  538. // Press key combo again
  539. Assert.True (lv.NewKeyDownEvent (Key.Space.WithShift));
  540. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  541. Assert.True (lv.Source.IsMarked (0));
  542. Assert.True (lv.Source.IsMarked (1));
  543. Assert.False (lv.Source.IsMarked (2)); // untoggle toggle marked
  544. }
  545. [Fact]
  546. public void ListWrapper_StartsWith ()
  547. {
  548. var lw = new ListWrapper<string> (["One", "Two", "Three"]);
  549. Assert.Equal (1, lw.StartsWith ("t"));
  550. Assert.Equal (1, lw.StartsWith ("tw"));
  551. Assert.Equal (2, lw.StartsWith ("th"));
  552. Assert.Equal (1, lw.StartsWith ("T"));
  553. Assert.Equal (1, lw.StartsWith ("TW"));
  554. Assert.Equal (2, lw.StartsWith ("TH"));
  555. lw = new (["One", "Two", "Three"]);
  556. Assert.Equal (1, lw.StartsWith ("t"));
  557. Assert.Equal (1, lw.StartsWith ("tw"));
  558. Assert.Equal (2, lw.StartsWith ("th"));
  559. Assert.Equal (1, lw.StartsWith ("T"));
  560. Assert.Equal (1, lw.StartsWith ("TW"));
  561. Assert.Equal (2, lw.StartsWith ("TH"));
  562. }
  563. [Fact]
  564. public void OnEnter_Does_Not_Throw_Exception ()
  565. {
  566. var lv = new ListView ();
  567. var top = new View ();
  568. top.Add (lv);
  569. Exception exception = Record.Exception (() => lv.SetFocus ());
  570. Assert.Null (exception);
  571. }
  572. [Fact]
  573. [AutoInitShutdown]
  574. public void RowRender_Event ()
  575. {
  576. var rendered = false;
  577. ObservableCollection<string> source = ["one", "two", "three"];
  578. var lv = new ListView { Width = Dim.Fill (), Height = Dim.Fill () };
  579. lv.RowRender += (s, _) => rendered = true;
  580. var top = new Toplevel ();
  581. top.Add (lv);
  582. Application.Begin (top);
  583. Assert.False (rendered);
  584. lv.SetSource (source);
  585. lv.Draw ();
  586. Assert.True (rendered);
  587. top.Dispose ();
  588. }
  589. [Fact]
  590. public void SelectedItem_Get_Set ()
  591. {
  592. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
  593. Assert.Equal (-1, lv.SelectedItem);
  594. Assert.Throws<ArgumentException> (() => lv.SelectedItem = 3);
  595. Exception exception = Record.Exception (() => lv.SelectedItem = -1);
  596. Assert.Null (exception);
  597. }
  598. [Fact]
  599. public void SetSource_Preserves_ListWrapper_Instance_If_Not_Null ()
  600. {
  601. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two"]) };
  602. Assert.NotNull (lv.Source);
  603. lv.SetSource<string> (null);
  604. Assert.NotNull (lv.Source);
  605. lv.Source = null;
  606. Assert.Null (lv.Source);
  607. lv = new () { Source = new ListWrapper<string> (["One", "Two"]) };
  608. Assert.NotNull (lv.Source);
  609. lv.SetSourceAsync<string> (null);
  610. Assert.NotNull (lv.Source);
  611. }
  612. [Fact]
  613. public void SettingEmptyKeybindingThrows ()
  614. {
  615. var lv = new ListView { Source = new ListWrapper<string> (["One", "Two", "Three"]) };
  616. Assert.Throws<ArgumentException> (() => lv.KeyBindings.Add (Key.Space));
  617. }
  618. private class NewListDataSource : IListDataSource
  619. {
  620. #pragma warning disable CS0067
  621. /// <inheritdoc />
  622. public event NotifyCollectionChangedEventHandler CollectionChanged;
  623. #pragma warning restore CS0067
  624. public int Count => 0;
  625. public int Length => 0;
  626. public bool SuspendCollectionChangedEvent { get => throw new NotImplementedException (); set => throw new NotImplementedException (); }
  627. public bool IsMarked (int item) { throw new NotImplementedException (); }
  628. public void Render (
  629. ListView container,
  630. bool selected,
  631. int item,
  632. int col,
  633. int line,
  634. int width,
  635. int start = 0
  636. )
  637. {
  638. throw new NotImplementedException ();
  639. }
  640. public void SetMark (int item, bool value) { throw new NotImplementedException (); }
  641. public IList ToList () { return new List<string> { "One", "Two", "Three" }; }
  642. public void Dispose ()
  643. {
  644. throw new NotImplementedException ();
  645. }
  646. }
  647. [Fact]
  648. [AutoInitShutdown]
  649. public void Clicking_On_Border_Is_Ignored ()
  650. {
  651. var selected = "";
  652. var lv = new ListView
  653. {
  654. Height = 5,
  655. Width = 7,
  656. BorderStyle = LineStyle.Single
  657. };
  658. lv.SetSource (["One", "Two", "Three", "Four"]);
  659. lv.SelectedItemChanged += (s, e) => selected = e.Value.ToString ();
  660. var top = new Toplevel ();
  661. top.Add (lv);
  662. Application.Begin (top);
  663. AutoInitShutdownAttribute.RunIteration ();
  664. Assert.Equal (new (1), lv.Border!.Thickness);
  665. Assert.Equal (-1, lv.SelectedItem);
  666. Assert.Equal ("", lv.Text);
  667. DriverAssert.AssertDriverContentsWithFrameAre (
  668. @"
  669. ┌─────┐
  670. │One │
  671. │Two │
  672. │Three│
  673. └─────┘",
  674. output);
  675. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked });
  676. Assert.Equal ("", selected);
  677. Assert.Equal (-1, lv.SelectedItem);
  678. Application.RaiseMouseEvent (
  679. new ()
  680. {
  681. ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Clicked
  682. });
  683. Assert.Equal ("One", selected);
  684. Assert.Equal (0, lv.SelectedItem);
  685. Application.RaiseMouseEvent (
  686. new ()
  687. {
  688. ScreenPosition = new (1, 2), Flags = MouseFlags.Button1Clicked
  689. });
  690. Assert.Equal ("Two", selected);
  691. Assert.Equal (1, lv.SelectedItem);
  692. Application.RaiseMouseEvent (
  693. new ()
  694. {
  695. ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked
  696. });
  697. Assert.Equal ("Three", selected);
  698. Assert.Equal (2, lv.SelectedItem);
  699. Application.RaiseMouseEvent (
  700. new ()
  701. {
  702. ScreenPosition = new (1, 4), Flags = MouseFlags.Button1Clicked
  703. });
  704. Assert.Equal ("Three", selected);
  705. Assert.Equal (2, lv.SelectedItem);
  706. top.Dispose ();
  707. }
  708. [Fact]
  709. [AutoInitShutdown]
  710. public void LeftItem_TopItem_Tests ()
  711. {
  712. ObservableCollection<string> source = [];
  713. for (int i = 0; i < 5; i++)
  714. {
  715. source.Add ($"Item {i}");
  716. }
  717. var lv = new ListView
  718. {
  719. X = 1,
  720. Source = new ListWrapper<string> (source)
  721. };
  722. lv.Height = lv.Source.Count;
  723. lv.Width = lv.MaxLength;
  724. var top = new Toplevel ();
  725. top.Add (lv);
  726. Application.Begin (top);
  727. AutoInitShutdownAttribute.RunIteration ();
  728. DriverAssert.AssertDriverContentsWithFrameAre (
  729. @"
  730. Item 0
  731. Item 1
  732. Item 2
  733. Item 3
  734. Item 4",
  735. output);
  736. lv.LeftItem = 1;
  737. lv.TopItem = 1;
  738. AutoInitShutdownAttribute.RunIteration ();
  739. DriverAssert.AssertDriverContentsWithFrameAre (
  740. @"
  741. tem 1
  742. tem 2
  743. tem 3
  744. tem 4",
  745. output);
  746. top.Dispose ();
  747. }
  748. [Fact]
  749. public void CollectionChanged_Event ()
  750. {
  751. var added = 0;
  752. var removed = 0;
  753. ObservableCollection<string> source = [];
  754. var lv = new ListView { Source = new ListWrapper<string> (source) };
  755. lv.CollectionChanged += (sender, args) =>
  756. {
  757. if (args.Action == NotifyCollectionChangedAction.Add)
  758. {
  759. added++;
  760. }
  761. else if (args.Action == NotifyCollectionChangedAction.Remove)
  762. {
  763. removed++;
  764. }
  765. };
  766. for (int i = 0; i < 3; i++)
  767. {
  768. source.Add ($"Item{i}");
  769. }
  770. Assert.Equal (3, added);
  771. Assert.Equal (0, removed);
  772. added = 0;
  773. for (int i = 0; i < 3; i++)
  774. {
  775. source.Remove (source [0]);
  776. }
  777. Assert.Equal (0, added);
  778. Assert.Equal (3, removed);
  779. Assert.Empty (source);
  780. }
  781. [Fact]
  782. public void CollectionChanged_Event_Is_Only_Subscribed_Once ()
  783. {
  784. var added = 0;
  785. var removed = 0;
  786. var otherActions = 0;
  787. IList<string> source1 = [];
  788. var lv = new ListView { Source = new ListWrapper<string> (new (source1)) };
  789. lv.CollectionChanged += (sender, args) =>
  790. {
  791. if (args.Action == NotifyCollectionChangedAction.Add)
  792. {
  793. added++;
  794. }
  795. else if (args.Action == NotifyCollectionChangedAction.Remove)
  796. {
  797. removed++;
  798. }
  799. else
  800. {
  801. otherActions++;
  802. }
  803. };
  804. ObservableCollection<string> source2 = [];
  805. lv.Source = new ListWrapper<string> (source2);
  806. ObservableCollection<string> source3 = [];
  807. lv.Source = new ListWrapper<string> (source3);
  808. Assert.Equal (0, added);
  809. Assert.Equal (0, removed);
  810. Assert.Equal (0, otherActions);
  811. for (int i = 0; i < 3; i++)
  812. {
  813. source1.Add ($"Item{i}");
  814. source2.Add ($"Item{i}");
  815. source3.Add ($"Item{i}");
  816. }
  817. Assert.Equal (3, added);
  818. Assert.Equal (0, removed);
  819. Assert.Equal (0, otherActions);
  820. added = 0;
  821. for (int i = 0; i < 3; i++)
  822. {
  823. source1.Remove (source1 [0]);
  824. source2.Remove (source2 [0]);
  825. source3.Remove (source3 [0]);
  826. }
  827. Assert.Equal (0, added);
  828. Assert.Equal (3, removed);
  829. Assert.Equal (0, otherActions);
  830. Assert.Empty (source1);
  831. Assert.Empty (source2);
  832. Assert.Empty (source3);
  833. }
  834. [Fact]
  835. public void CollectionChanged_Event_UnSubscribe_Previous_If_New_Is_Null ()
  836. {
  837. var added = 0;
  838. var removed = 0;
  839. var otherActions = 0;
  840. ObservableCollection<string> source1 = [];
  841. var lv = new ListView { Source = new ListWrapper<string> (source1) };
  842. lv.CollectionChanged += (sender, args) =>
  843. {
  844. if (args.Action == NotifyCollectionChangedAction.Add)
  845. {
  846. added++;
  847. }
  848. else if (args.Action == NotifyCollectionChangedAction.Remove)
  849. {
  850. removed++;
  851. }
  852. else
  853. {
  854. otherActions++;
  855. }
  856. };
  857. lv.Source = new ListWrapper<string> (null);
  858. Assert.Equal (0, added);
  859. Assert.Equal (0, removed);
  860. Assert.Equal (0, otherActions);
  861. for (int i = 0; i < 3; i++)
  862. {
  863. source1.Add ($"Item{i}");
  864. }
  865. Assert.Equal (0, added);
  866. Assert.Equal (0, removed);
  867. Assert.Equal (0, otherActions);
  868. for (int i = 0; i < 3; i++)
  869. {
  870. source1.Remove (source1 [0]);
  871. }
  872. Assert.Equal (0, added);
  873. Assert.Equal (0, removed);
  874. Assert.Equal (0, otherActions);
  875. Assert.Empty (source1);
  876. }
  877. [Fact]
  878. public void ListWrapper_CollectionChanged_Event_Is_Only_Subscribed_Once ()
  879. {
  880. var added = 0;
  881. var removed = 0;
  882. var otherActions = 0;
  883. ObservableCollection<string> source1 = [];
  884. ListWrapper<string> lw = new (source1);
  885. lw.CollectionChanged += (sender, args) =>
  886. {
  887. if (args.Action == NotifyCollectionChangedAction.Add)
  888. {
  889. added++;
  890. }
  891. else if (args.Action == NotifyCollectionChangedAction.Remove)
  892. {
  893. removed++;
  894. }
  895. else
  896. {
  897. otherActions++;
  898. }
  899. };
  900. ObservableCollection<string> source2 = [];
  901. lw = new (source2);
  902. ObservableCollection<string> source3 = [];
  903. lw = new (source3);
  904. Assert.Equal (0, added);
  905. Assert.Equal (0, removed);
  906. Assert.Equal (0, otherActions);
  907. for (int i = 0; i < 3; i++)
  908. {
  909. source1.Add ($"Item{i}");
  910. source2.Add ($"Item{i}");
  911. source3.Add ($"Item{i}");
  912. }
  913. Assert.Equal (3, added);
  914. Assert.Equal (0, removed);
  915. Assert.Equal (0, otherActions);
  916. added = 0;
  917. for (int i = 0; i < 3; i++)
  918. {
  919. source1.Remove (source1 [0]);
  920. source2.Remove (source2 [0]);
  921. source3.Remove (source3 [0]);
  922. }
  923. Assert.Equal (0, added);
  924. Assert.Equal (3, removed);
  925. Assert.Equal (0, otherActions);
  926. Assert.Empty (source1);
  927. Assert.Empty (source2);
  928. Assert.Empty (source3);
  929. }
  930. [Fact]
  931. public void ListWrapper_CollectionChanged_Event_UnSubscribe_Previous_Is_Disposed ()
  932. {
  933. var added = 0;
  934. var removed = 0;
  935. var otherActions = 0;
  936. ObservableCollection<string> source1 = [];
  937. ListWrapper<string> lw = new (source1);
  938. lw.CollectionChanged += Lw_CollectionChanged;
  939. lw.Dispose ();
  940. lw = new (null);
  941. Assert.Equal (0, lw.Count);
  942. Assert.Equal (0, added);
  943. Assert.Equal (0, removed);
  944. Assert.Equal (0, otherActions);
  945. for (int i = 0; i < 3; i++)
  946. {
  947. source1.Add ($"Item{i}");
  948. }
  949. Assert.Equal (0, added);
  950. Assert.Equal (0, removed);
  951. Assert.Equal (0, otherActions);
  952. for (int i = 0; i < 3; i++)
  953. {
  954. source1.Remove (source1 [0]);
  955. }
  956. Assert.Equal (0, added);
  957. Assert.Equal (0, removed);
  958. Assert.Equal (0, otherActions);
  959. Assert.Empty (source1);
  960. void Lw_CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
  961. {
  962. if (e.Action == NotifyCollectionChangedAction.Add)
  963. {
  964. added++;
  965. }
  966. else if (e.Action == NotifyCollectionChangedAction.Remove)
  967. {
  968. removed++;
  969. }
  970. else
  971. {
  972. otherActions++;
  973. }
  974. }
  975. }
  976. [Fact]
  977. public void ListWrapper_SuspendCollectionChangedEvent_ResumeSuspendCollectionChangedEvent_Tests ()
  978. {
  979. var added = 0;
  980. ObservableCollection<string> source = [];
  981. ListWrapper<string> lw = new (source);
  982. lw.CollectionChanged += Lw_CollectionChanged;
  983. lw.SuspendCollectionChangedEvent = true;
  984. for (int i = 0; i < 3; i++)
  985. {
  986. source.Add ($"Item{i}");
  987. }
  988. Assert.Equal (0, added);
  989. Assert.Equal (3, lw.Count);
  990. Assert.Equal (3, source.Count);
  991. lw.SuspendCollectionChangedEvent = false;
  992. for (int i = 3; i < 6; i++)
  993. {
  994. source.Add ($"Item{i}");
  995. }
  996. Assert.Equal (3, added);
  997. Assert.Equal (6, lw.Count);
  998. Assert.Equal (6, source.Count);
  999. void Lw_CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
  1000. {
  1001. if (e.Action == NotifyCollectionChangedAction.Add)
  1002. {
  1003. added++;
  1004. }
  1005. }
  1006. }
  1007. [Fact]
  1008. public void ListView_SuspendCollectionChangedEvent_ResumeSuspendCollectionChangedEvent_Tests ()
  1009. {
  1010. var added = 0;
  1011. ObservableCollection<string> source = [];
  1012. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  1013. lv.CollectionChanged += Lw_CollectionChanged;
  1014. lv.SuspendCollectionChangedEvent ();
  1015. for (int i = 0; i < 3; i++)
  1016. {
  1017. source.Add ($"Item{i}");
  1018. }
  1019. Assert.Equal (0, added);
  1020. Assert.Equal (3, lv.Source.Count);
  1021. Assert.Equal (3, source.Count);
  1022. lv.ResumeSuspendCollectionChangedEvent ();
  1023. for (int i = 3; i < 6; i++)
  1024. {
  1025. source.Add ($"Item{i}");
  1026. }
  1027. Assert.Equal (3, added);
  1028. Assert.Equal (6, lv.Source.Count);
  1029. Assert.Equal (6, source.Count);
  1030. void Lw_CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
  1031. {
  1032. if (e.Action == NotifyCollectionChangedAction.Add)
  1033. {
  1034. added++;
  1035. }
  1036. }
  1037. }
  1038. }