ListViewTests.cs 34 KB

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