SelectingItemsControlTests_Multiple.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using Avalonia.Collections;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Controls.Selection;
  10. using Avalonia.Controls.Templates;
  11. using Avalonia.Data;
  12. using Avalonia.Input;
  13. using Avalonia.Input.Platform;
  14. using Avalonia.Interactivity;
  15. using Avalonia.UnitTests;
  16. using Moq;
  17. using Xunit;
  18. namespace Avalonia.Controls.UnitTests.Primitives
  19. {
  20. public class SelectingItemsControlTests_Multiple
  21. {
  22. private MouseTestHelper _helper = new MouseTestHelper();
  23. [Fact]
  24. public void Setting_SelectedIndex_Should_Add_To_SelectedItems()
  25. {
  26. var target = new TestSelector
  27. {
  28. Items = new[] { "foo", "bar" },
  29. Template = Template(),
  30. };
  31. target.ApplyTemplate();
  32. target.SelectedIndex = 1;
  33. Assert.Equal(new[] { "bar" }, target.SelectedItems.Cast<object>().ToList());
  34. }
  35. [Fact]
  36. public void Adding_SelectedItems_Should_Set_SelectedIndex()
  37. {
  38. var target = new TestSelector
  39. {
  40. Items = new[] { "foo", "bar" },
  41. Template = Template(),
  42. };
  43. target.ApplyTemplate();
  44. target.SelectedItems.Add("bar");
  45. Assert.Equal(1, target.SelectedIndex);
  46. }
  47. [Fact]
  48. public void Assigning_Single_SelectedItems_Should_Set_SelectedIndex()
  49. {
  50. var target = new TestSelector
  51. {
  52. Items = new[] { "foo", "bar" },
  53. Template = Template(),
  54. };
  55. target.ApplyTemplate();
  56. target.Presenter.ApplyTemplate();
  57. target.SelectedItems = new AvaloniaList<object>("bar");
  58. Assert.Equal(1, target.SelectedIndex);
  59. Assert.Equal(new[] { "bar" }, target.SelectedItems);
  60. Assert.Equal(new[] { 1 }, SelectedContainers(target));
  61. }
  62. [Fact]
  63. public void Assigning_Multiple_SelectedItems_Should_Set_SelectedIndex()
  64. {
  65. var target = new TestSelector
  66. {
  67. Items = new[] { "foo", "bar", "baz" },
  68. Template = Template(),
  69. };
  70. target.ApplyTemplate();
  71. target.Presenter.ApplyTemplate();
  72. target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
  73. Assert.Equal(0, target.SelectedIndex);
  74. Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
  75. Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
  76. }
  77. [Fact]
  78. public void Selected_Items_Should_Be_Marked_When_Panel_Created_After_SelectedItems_Is_Set()
  79. {
  80. // Issue #2565.
  81. var target = new TestSelector
  82. {
  83. Items = new[] { "foo", "bar", "baz" },
  84. Template = Template(),
  85. };
  86. target.ApplyTemplate();
  87. target.SelectedItems = new AvaloniaList<string>("foo", "bar", "baz");
  88. target.Presenter.ApplyTemplate();
  89. Assert.Equal(0, target.SelectedIndex);
  90. Assert.Equal(new[] { "foo", "bar", "baz" }, target.SelectedItems);
  91. Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
  92. }
  93. [Fact]
  94. public void Reassigning_SelectedItems_Should_Clear_Selection()
  95. {
  96. var target = new TestSelector
  97. {
  98. Items = new[] { "foo", "bar" },
  99. Template = Template(),
  100. };
  101. target.ApplyTemplate();
  102. target.SelectedItems.Add("bar");
  103. target.SelectedItems = new AvaloniaList<object>();
  104. Assert.Equal(-1, target.SelectedIndex);
  105. Assert.Null(target.SelectedItem);
  106. }
  107. [Fact]
  108. public void Adding_First_SelectedItem_Should_Raise_SelectedIndex_SelectedItem_Changed()
  109. {
  110. var target = new TestSelector
  111. {
  112. Items = new[] { "foo", "bar" },
  113. Template = Template(),
  114. };
  115. bool indexRaised = false;
  116. bool itemRaised = false;
  117. target.PropertyChanged += (s, e) =>
  118. {
  119. indexRaised |= e.Property.Name == "SelectedIndex" &&
  120. (int)e.OldValue == -1 &&
  121. (int)e.NewValue == 1;
  122. itemRaised |= e.Property.Name == "SelectedItem" &&
  123. (string)e.OldValue == null &&
  124. (string)e.NewValue == "bar";
  125. };
  126. target.ApplyTemplate();
  127. target.SelectedItems.Add("bar");
  128. Assert.True(indexRaised);
  129. Assert.True(itemRaised);
  130. }
  131. [Fact]
  132. public void Adding_Subsequent_SelectedItems_Should_Not_Raise_SelectedIndex_SelectedItem_Changed()
  133. {
  134. var target = new TestSelector
  135. {
  136. Items = new[] { "foo", "bar" },
  137. Template = Template(),
  138. };
  139. target.ApplyTemplate();
  140. target.SelectedItems.Add("foo");
  141. bool raised = false;
  142. target.PropertyChanged += (s, e) =>
  143. raised |= e.Property.Name == "SelectedIndex" ||
  144. e.Property.Name == "SelectedItem";
  145. target.SelectedItems.Add("bar");
  146. Assert.False(raised);
  147. }
  148. [Fact]
  149. public void Removing_Last_SelectedItem_Should_Raise_SelectedIndex_Changed()
  150. {
  151. var target = new TestSelector
  152. {
  153. Items = new[] { "foo", "bar" },
  154. Template = Template(),
  155. };
  156. target.ApplyTemplate();
  157. target.SelectedItems.Add("foo");
  158. bool raised = false;
  159. target.PropertyChanged += (s, e) =>
  160. raised |= e.Property.Name == "SelectedIndex" &&
  161. (int)e.OldValue == 0 &&
  162. (int)e.NewValue == -1;
  163. target.SelectedItems.RemoveAt(0);
  164. Assert.True(raised);
  165. }
  166. [Fact]
  167. public void Adding_SelectedItems_Should_Set_Item_IsSelected()
  168. {
  169. var items = new[]
  170. {
  171. new ListBoxItem(),
  172. new ListBoxItem(),
  173. new ListBoxItem(),
  174. };
  175. var target = new TestSelector
  176. {
  177. Items = items,
  178. Template = Template(),
  179. };
  180. target.ApplyTemplate();
  181. target.Presenter.ApplyTemplate();
  182. target.SelectedItems.Add(items[0]);
  183. target.SelectedItems.Add(items[1]);
  184. var foo = target.Presenter.Panel.Children[0];
  185. Assert.True(items[0].IsSelected);
  186. Assert.True(items[1].IsSelected);
  187. Assert.False(items[2].IsSelected);
  188. }
  189. [Fact]
  190. public void Assigning_SelectedItems_Should_Set_Item_IsSelected()
  191. {
  192. var items = new[]
  193. {
  194. new ListBoxItem(),
  195. new ListBoxItem(),
  196. new ListBoxItem(),
  197. };
  198. var target = new TestSelector
  199. {
  200. Items = items,
  201. Template = Template(),
  202. };
  203. target.ApplyTemplate();
  204. target.Presenter.ApplyTemplate();
  205. target.SelectedItems = new AvaloniaList<object> { items[0], items[1] };
  206. Assert.True(items[0].IsSelected);
  207. Assert.True(items[1].IsSelected);
  208. Assert.False(items[2].IsSelected);
  209. }
  210. [Fact]
  211. public void Removing_SelectedItems_Should_Clear_Item_IsSelected()
  212. {
  213. var items = new[]
  214. {
  215. new ListBoxItem(),
  216. new ListBoxItem(),
  217. new ListBoxItem(),
  218. };
  219. var target = new TestSelector
  220. {
  221. Items = items,
  222. Template = Template(),
  223. };
  224. target.ApplyTemplate();
  225. target.Presenter.ApplyTemplate();
  226. target.SelectedItems.Add(items[0]);
  227. target.SelectedItems.Add(items[1]);
  228. target.SelectedItems.Remove(items[1]);
  229. Assert.True(items[0].IsSelected);
  230. Assert.False(items[1].IsSelected);
  231. }
  232. [Fact]
  233. public void Reassigning_SelectedItems_Should_Clear_Item_IsSelected()
  234. {
  235. var items = new[]
  236. {
  237. new ListBoxItem(),
  238. new ListBoxItem(),
  239. new ListBoxItem(),
  240. };
  241. var target = new TestSelector
  242. {
  243. Items = items,
  244. Template = Template(),
  245. };
  246. target.ApplyTemplate();
  247. target.SelectedItems.Add(items[0]);
  248. target.SelectedItems.Add(items[1]);
  249. target.SelectedItems = new AvaloniaList<object> { items[0], items[1] };
  250. Assert.False(items[0].IsSelected);
  251. Assert.False(items[1].IsSelected);
  252. }
  253. [Fact]
  254. public void Setting_SelectedIndex_Should_Unmark_Previously_Selected_Containers()
  255. {
  256. var target = new TestSelector
  257. {
  258. Items = new[] { "foo", "bar", "baz" },
  259. Template = Template(),
  260. };
  261. target.ApplyTemplate();
  262. target.Presenter.ApplyTemplate();
  263. target.SelectedItems.Add("foo");
  264. target.SelectedItems.Add("bar");
  265. Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
  266. target.SelectedIndex = 2;
  267. Assert.Equal(new[] { 2 }, SelectedContainers(target));
  268. }
  269. [Fact]
  270. public void Range_Select_Should_Select_Range()
  271. {
  272. var target = new TestSelector
  273. {
  274. Items = new[]
  275. {
  276. "foo",
  277. "bar",
  278. "baz",
  279. "qux",
  280. "qiz",
  281. "lol",
  282. },
  283. Template = Template(),
  284. };
  285. target.ApplyTemplate();
  286. target.SelectedIndex = 1;
  287. target.SelectRange(3);
  288. Assert.Equal(new[] { "bar", "baz", "qux" }, target.SelectedItems.Cast<object>().ToList());
  289. }
  290. [Fact]
  291. public void Range_Select_Backwards_Should_Select_Range()
  292. {
  293. var target = new TestSelector
  294. {
  295. Items = new[]
  296. {
  297. "foo",
  298. "bar",
  299. "baz",
  300. "qux",
  301. "qiz",
  302. "lol",
  303. },
  304. SelectionMode = SelectionMode.Multiple,
  305. Template = Template(),
  306. };
  307. target.ApplyTemplate();
  308. target.SelectedIndex = 3;
  309. target.SelectRange(1);
  310. Assert.Equal(new[] { "qux", "bar", "baz" }, target.SelectedItems.Cast<object>().ToList());
  311. }
  312. [Fact]
  313. public void Second_Range_Select_Backwards_Should_Select_From_Original_Selection()
  314. {
  315. var target = new TestSelector
  316. {
  317. Items = new[]
  318. {
  319. "foo",
  320. "bar",
  321. "baz",
  322. "qux",
  323. "qiz",
  324. "lol",
  325. },
  326. SelectionMode = SelectionMode.Multiple,
  327. Template = Template(),
  328. };
  329. target.ApplyTemplate();
  330. target.SelectedIndex = 2;
  331. target.SelectRange(5);
  332. target.SelectRange(4);
  333. Assert.Equal(new[] { "baz", "qux", "qiz" }, target.SelectedItems.Cast<object>().ToList());
  334. }
  335. [Fact]
  336. public void Setting_SelectedIndex_After_Range_Should_Unmark_Previously_Selected_Containers()
  337. {
  338. var target = new TestSelector
  339. {
  340. Items = new[] { "foo", "bar", "baz", "qux" },
  341. Template = Template(),
  342. SelectedIndex = 0,
  343. SelectionMode = SelectionMode.Multiple,
  344. };
  345. target.ApplyTemplate();
  346. target.Presenter.ApplyTemplate();
  347. target.SelectRange(2);
  348. Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
  349. target.SelectedIndex = 3;
  350. Assert.Equal(new[] { 3 }, SelectedContainers(target));
  351. }
  352. [Fact]
  353. public void Toggling_Selection_After_Range_Should_Work()
  354. {
  355. var target = new TestSelector
  356. {
  357. Items = new[] { "foo", "bar", "baz", "foo", "bar", "baz" },
  358. Template = Template(),
  359. SelectedIndex = 0,
  360. SelectionMode = SelectionMode.Multiple,
  361. };
  362. target.ApplyTemplate();
  363. target.Presenter.ApplyTemplate();
  364. target.SelectRange(3);
  365. Assert.Equal(new[] { 0, 1, 2, 3 }, SelectedContainers(target));
  366. target.Toggle(4);
  367. Assert.Equal(new[] { 0, 1, 2, 3, 4 }, SelectedContainers(target));
  368. }
  369. [Fact]
  370. public void Suprious_SelectedIndex_Changes_Should_Not_Be_Triggered()
  371. {
  372. var target = new TestSelector
  373. {
  374. Items = new[] { "foo", "bar", "baz" },
  375. Template = Template(),
  376. };
  377. target.ApplyTemplate();
  378. var selectedIndexes = new List<int>();
  379. target.GetObservable(TestSelector.SelectedIndexProperty).Subscribe(x => selectedIndexes.Add(x));
  380. target.SelectedItems = new AvaloniaList<object> { "bar", "baz" };
  381. target.SelectedItem = "foo";
  382. Assert.Equal(0, target.SelectedIndex);
  383. Assert.Equal(new[] { -1, 1, 0 }, selectedIndexes);
  384. }
  385. [Fact]
  386. public void Can_Set_SelectedIndex_To_Another_Selected_Item()
  387. {
  388. var target = new TestSelector
  389. {
  390. Items = new[] { "foo", "bar", "baz" },
  391. Template = Template(),
  392. };
  393. target.ApplyTemplate();
  394. target.Presenter.ApplyTemplate();
  395. target.SelectedItems.Add("foo");
  396. target.SelectedItems.Add("bar");
  397. Assert.Equal(0, target.SelectedIndex);
  398. Assert.Equal(new[] { "foo", "bar" }, target.SelectedItems);
  399. Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
  400. var raised = false;
  401. target.SelectionChanged += (s, e) =>
  402. {
  403. raised = true;
  404. Assert.Empty(e.AddedItems);
  405. Assert.Equal(new[] { "foo" }, e.RemovedItems);
  406. };
  407. target.SelectedIndex = 1;
  408. Assert.True(raised);
  409. Assert.Equal(1, target.SelectedIndex);
  410. Assert.Equal(new[] { "bar" }, target.SelectedItems);
  411. Assert.Equal(new[] { 1 }, SelectedContainers(target));
  412. }
  413. /// <summary>
  414. /// Tests a problem discovered with ListBox with selection.
  415. /// </summary>
  416. /// <remarks>
  417. /// - Items is bound to DataContext first, followed by say SelectedIndex
  418. /// - When the ListBox is removed from the visual tree, DataContext becomes null (as it's
  419. /// inherited)
  420. /// - This changes Items to null, which changes SelectedIndex to null as there are no
  421. /// longer any items
  422. /// - However, the news that DataContext is now null hasn't yet reached the SelectedItems
  423. /// binding and so the unselection is sent back to the ViewModel
  424. ///
  425. /// This is a similar problem to that tested by XamlBindingTest.Should_Not_Write_To_Old_DataContext.
  426. /// However, that tests a general property binding problem: here we are writing directly
  427. /// to the SelectedItems collection - not via a binding - so it's something that the
  428. /// binding system cannot solve. Instead we solve it by not clearing SelectedItems when
  429. /// DataContext is in the process of changing.
  430. /// </remarks>
  431. [Fact]
  432. public void Should_Not_Write_SelectedItems_To_Old_DataContext()
  433. {
  434. var vm = new OldDataContextViewModel();
  435. var target = new TestSelector();
  436. var itemsBinding = new Binding
  437. {
  438. Path = "Items",
  439. Mode = BindingMode.OneWay,
  440. };
  441. var selectedItemsBinding = new Binding
  442. {
  443. Path = "SelectedItems",
  444. Mode = BindingMode.OneWay,
  445. };
  446. // Bind Items and SelectedItems to the VM.
  447. target.Bind(TestSelector.ItemsProperty, itemsBinding);
  448. target.Bind(TestSelector.SelectedItemsProperty, selectedItemsBinding);
  449. // Set DataContext and SelectedIndex
  450. target.DataContext = vm;
  451. target.SelectedIndex = 1;
  452. // Make sure SelectedItems are written back to VM.
  453. Assert.Equal(new[] { "bar" }, vm.SelectedItems);
  454. // Clear DataContext and ensure that SelectedItems is still set in the VM.
  455. target.DataContext = null;
  456. Assert.Equal(new[] { "bar" }, vm.SelectedItems);
  457. // Ensure target's SelectedItems is now clear.
  458. Assert.Empty(target.SelectedItems);
  459. }
  460. /// <summary>
  461. /// See <see cref="Should_Not_Write_SelectedItems_To_Old_DataContext"/>.
  462. /// </summary>
  463. [Fact]
  464. public void Should_Not_Write_SelectionModel_To_Old_DataContext()
  465. {
  466. var vm = new OldDataContextViewModel();
  467. var target = new TestSelector();
  468. var itemsBinding = new Binding
  469. {
  470. Path = "Items",
  471. Mode = BindingMode.OneWay,
  472. };
  473. var selectionBinding = new Binding
  474. {
  475. Path = "Selection",
  476. Mode = BindingMode.OneWay,
  477. };
  478. // Bind Items and Selection to the VM.
  479. target.Bind(TestSelector.ItemsProperty, itemsBinding);
  480. target.Bind(TestSelector.SelectionProperty, selectionBinding);
  481. // Set DataContext and SelectedIndex
  482. target.DataContext = vm;
  483. target.SelectedIndex = 1;
  484. // Make sure selection is written to selection model
  485. Assert.Equal(1, vm.Selection.SelectedIndex);
  486. // Clear DataContext and ensure that selection is still set in model.
  487. target.DataContext = null;
  488. Assert.Equal(1, vm.Selection.SelectedIndex);
  489. // Ensure target's SelectedItems is now clear.
  490. Assert.Empty(target.SelectedItems);
  491. }
  492. [Fact]
  493. public void Unbound_SelectedItems_Should_Be_Cleared_When_DataContext_Cleared()
  494. {
  495. var data = new
  496. {
  497. Items = new[] { "foo", "bar", "baz" },
  498. };
  499. var target = new TestSelector
  500. {
  501. DataContext = data,
  502. Template = Template(),
  503. };
  504. var itemsBinding = new Binding { Path = "Items" };
  505. target.Bind(TestSelector.ItemsProperty, itemsBinding);
  506. Assert.Same(data.Items, target.Items);
  507. target.SelectedItems.Add("bar");
  508. target.DataContext = null;
  509. Assert.Empty(target.SelectedItems);
  510. }
  511. [Fact]
  512. public void Adding_To_SelectedItems_Should_Raise_SelectionChanged()
  513. {
  514. var items = new[] { "foo", "bar", "baz" };
  515. var target = new TestSelector
  516. {
  517. DataContext = items,
  518. Template = Template(),
  519. Items = items,
  520. };
  521. var called = false;
  522. target.SelectionChanged += (s, e) =>
  523. {
  524. Assert.Equal(new[] { "bar" }, e.AddedItems.Cast<object>().ToList());
  525. Assert.Empty(e.RemovedItems);
  526. called = true;
  527. };
  528. target.SelectedItems.Add("bar");
  529. Assert.True(called);
  530. }
  531. [Fact]
  532. public void Removing_From_SelectedItems_Should_Raise_SelectionChanged()
  533. {
  534. var items = new[] { "foo", "bar", "baz" };
  535. var target = new TestSelector
  536. {
  537. Items = items,
  538. Template = Template(),
  539. SelectedItem = "bar",
  540. };
  541. var called = false;
  542. target.SelectionChanged += (s, e) =>
  543. {
  544. Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>().ToList());
  545. Assert.Empty(e.AddedItems);
  546. called = true;
  547. };
  548. target.SelectedItems.Remove("bar");
  549. Assert.True(called);
  550. }
  551. [Fact]
  552. public void Assigning_SelectedItems_Should_Raise_SelectionChanged()
  553. {
  554. var items = new[] { "foo", "bar", "baz" };
  555. var target = new TestSelector
  556. {
  557. Items = items,
  558. Template = Template(),
  559. SelectedItem = "bar",
  560. };
  561. var called = false;
  562. target.SelectionChanged += (s, e) =>
  563. {
  564. Assert.Equal(new[] { "foo", "baz" }, e.AddedItems.Cast<object>());
  565. Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>());
  566. called = true;
  567. };
  568. target.ApplyTemplate();
  569. target.Presenter.ApplyTemplate();
  570. target.SelectedItems = new AvaloniaList<object>("foo", "baz");
  571. Assert.True(called);
  572. }
  573. [Fact]
  574. public void Shift_Selecting_From_No_Selection_Selects_From_Start()
  575. {
  576. using (UnitTestApplication.Start())
  577. {
  578. var target = new ListBox
  579. {
  580. Template = Template(),
  581. Items = new[] { "Foo", "Bar", "Baz" },
  582. SelectionMode = SelectionMode.Multiple,
  583. };
  584. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  585. target.ApplyTemplate();
  586. target.Presenter.ApplyTemplate();
  587. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Shift);
  588. var panel = target.Presenter.Panel;
  589. Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
  590. Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
  591. }
  592. }
  593. [Fact]
  594. public void Ctrl_Selecting_Raises_SelectionChanged_Events()
  595. {
  596. using (UnitTestApplication.Start())
  597. {
  598. var target = new ListBox
  599. {
  600. Template = Template(),
  601. Items = new[] { "Foo", "Bar", "Baz", "Qux" },
  602. SelectionMode = SelectionMode.Multiple,
  603. };
  604. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  605. target.ApplyTemplate();
  606. target.Presenter.ApplyTemplate();
  607. SelectionChangedEventArgs receivedArgs = null;
  608. target.SelectionChanged += (_, args) => receivedArgs = args;
  609. void VerifyAdded(string selection)
  610. {
  611. Assert.NotNull(receivedArgs);
  612. Assert.Equal(new[] { selection }, receivedArgs.AddedItems);
  613. Assert.Empty(receivedArgs.RemovedItems);
  614. }
  615. void VerifyRemoved(string selection)
  616. {
  617. Assert.NotNull(receivedArgs);
  618. Assert.Equal(new[] { selection }, receivedArgs.RemovedItems);
  619. Assert.Empty(receivedArgs.AddedItems);
  620. }
  621. _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
  622. VerifyAdded("Bar");
  623. receivedArgs = null;
  624. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
  625. VerifyAdded("Baz");
  626. receivedArgs = null;
  627. _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
  628. VerifyAdded("Qux");
  629. receivedArgs = null;
  630. _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
  631. VerifyRemoved("Bar");
  632. }
  633. }
  634. [Fact]
  635. public void Ctrl_Selecting_SelectedItem_With_Multiple_Selection_Active_Sets_SelectedItem_To_Next_Selection()
  636. {
  637. using (UnitTestApplication.Start())
  638. {
  639. var target = new ListBox
  640. {
  641. Template = Template(),
  642. Items = new[] { "Foo", "Bar", "Baz", "Qux" },
  643. SelectionMode = SelectionMode.Multiple,
  644. };
  645. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  646. target.ApplyTemplate();
  647. target.Presenter.ApplyTemplate();
  648. _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
  649. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
  650. _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
  651. Assert.Equal(1, target.SelectedIndex);
  652. Assert.Equal("Bar", target.SelectedItem);
  653. Assert.Equal(new[] { "Bar", "Baz", "Qux" }, target.SelectedItems);
  654. _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
  655. Assert.Equal(2, target.SelectedIndex);
  656. Assert.Equal("Baz", target.SelectedItem);
  657. Assert.Equal(new[] { "Baz", "Qux" }, target.SelectedItems);
  658. }
  659. }
  660. [Fact]
  661. public void Ctrl_Selecting_Non_SelectedItem_With_Multiple_Selection_Active_Leaves_SelectedItem_The_Same()
  662. {
  663. using (UnitTestApplication.Start())
  664. {
  665. var target = new ListBox
  666. {
  667. Template = Template(),
  668. Items = new[] { "Foo", "Bar", "Baz" },
  669. SelectionMode = SelectionMode.Multiple,
  670. };
  671. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  672. target.ApplyTemplate();
  673. target.Presenter.ApplyTemplate();
  674. _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
  675. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
  676. Assert.Equal(1, target.SelectedIndex);
  677. Assert.Equal("Bar", target.SelectedItem);
  678. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Control);
  679. Assert.Equal(1, target.SelectedIndex);
  680. Assert.Equal("Bar", target.SelectedItem);
  681. }
  682. }
  683. [Fact]
  684. public void Should_Ctrl_Select_Correct_Item_When_Duplicate_Items_Are_Present()
  685. {
  686. using (UnitTestApplication.Start())
  687. {
  688. var target = new ListBox
  689. {
  690. Template = Template(),
  691. Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
  692. SelectionMode = SelectionMode.Multiple,
  693. };
  694. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  695. target.ApplyTemplate();
  696. target.Presenter.ApplyTemplate();
  697. _helper.Click((Interactive)target.Presenter.Panel.Children[3]);
  698. _helper.Click((Interactive)target.Presenter.Panel.Children[4], modifiers: KeyModifiers.Control);
  699. var panel = target.Presenter.Panel;
  700. Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
  701. Assert.Equal(new[] { 3, 4 }, SelectedContainers(target));
  702. }
  703. }
  704. [Fact]
  705. public void Should_Shift_Select_Correct_Item_When_Duplicates_Are_Present()
  706. {
  707. using (UnitTestApplication.Start())
  708. {
  709. var target = new ListBox
  710. {
  711. Template = Template(),
  712. Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
  713. SelectionMode = SelectionMode.Multiple,
  714. };
  715. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  716. target.ApplyTemplate();
  717. target.Presenter.ApplyTemplate();
  718. _helper.Click((Interactive)target.Presenter.Panel.Children[3]);
  719. _helper.Click((Interactive)target.Presenter.Panel.Children[5], modifiers: KeyModifiers.Shift);
  720. var panel = target.Presenter.Panel;
  721. Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
  722. Assert.Equal(new[] { 3, 4, 5 }, SelectedContainers(target));
  723. }
  724. }
  725. [Fact]
  726. public void Can_Shift_Select_All_Items_When_Duplicates_Are_Present()
  727. {
  728. using (UnitTestApplication.Start())
  729. {
  730. var target = new ListBox
  731. {
  732. Template = Template(),
  733. Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
  734. SelectionMode = SelectionMode.Multiple,
  735. };
  736. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  737. target.ApplyTemplate();
  738. target.Presenter.ApplyTemplate();
  739. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  740. _helper.Click((Interactive)target.Presenter.Panel.Children[5], modifiers: KeyModifiers.Shift);
  741. var panel = target.Presenter.Panel;
  742. Assert.Equal(new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" }, target.SelectedItems);
  743. Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, SelectedContainers(target));
  744. }
  745. }
  746. [Fact]
  747. public void Shift_Selecting_Raises_SelectionChanged_Events()
  748. {
  749. using (UnitTestApplication.Start())
  750. {
  751. var target = new ListBox
  752. {
  753. Template = Template(),
  754. Items = new[] { "Foo", "Bar", "Baz", "Qux" },
  755. SelectionMode = SelectionMode.Multiple,
  756. };
  757. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  758. target.ApplyTemplate();
  759. target.Presenter.ApplyTemplate();
  760. SelectionChangedEventArgs receivedArgs = null;
  761. target.SelectionChanged += (_, args) => receivedArgs = args;
  762. void VerifyAdded(params string[] selection)
  763. {
  764. Assert.NotNull(receivedArgs);
  765. Assert.Equal(selection, receivedArgs.AddedItems);
  766. Assert.Empty(receivedArgs.RemovedItems);
  767. }
  768. void VerifyRemoved(string selection)
  769. {
  770. Assert.NotNull(receivedArgs);
  771. Assert.Equal(new[] { selection }, receivedArgs.RemovedItems);
  772. Assert.Empty(receivedArgs.AddedItems);
  773. }
  774. _helper.Click((Interactive)target.Presenter.Panel.Children[1]);
  775. VerifyAdded("Bar");
  776. receivedArgs = null;
  777. _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Shift);
  778. VerifyAdded("Baz", "Qux");
  779. receivedArgs = null;
  780. _helper.Click((Interactive)target.Presenter.Panel.Children[2], modifiers: KeyModifiers.Shift);
  781. VerifyRemoved("Qux");
  782. }
  783. }
  784. [Fact]
  785. public void Duplicate_Items_Are_Added_To_SelectedItems_In_Order()
  786. {
  787. using (UnitTestApplication.Start())
  788. {
  789. var target = new ListBox
  790. {
  791. Template = Template(),
  792. Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
  793. SelectionMode = SelectionMode.Multiple,
  794. };
  795. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  796. target.ApplyTemplate();
  797. target.Presenter.ApplyTemplate();
  798. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  799. Assert.Equal(new[] { "Foo" }, target.SelectedItems);
  800. _helper.Click((Interactive)target.Presenter.Panel.Children[4], modifiers: KeyModifiers.Control);
  801. Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
  802. _helper.Click((Interactive)target.Presenter.Panel.Children[3], modifiers: KeyModifiers.Control);
  803. Assert.Equal(new[] { "Foo", "Bar", "Foo" }, target.SelectedItems);
  804. _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Control);
  805. Assert.Equal(new[] { "Foo", "Bar", "Foo", "Bar" }, target.SelectedItems);
  806. }
  807. }
  808. [Fact]
  809. public void SelectAll_Sets_SelectedIndex_And_SelectedItem()
  810. {
  811. var target = new TestSelector
  812. {
  813. Template = Template(),
  814. Items = new[] { "Foo", "Bar", "Baz" },
  815. SelectionMode = SelectionMode.Multiple,
  816. };
  817. target.ApplyTemplate();
  818. target.Presenter.ApplyTemplate();
  819. target.SelectAll();
  820. Assert.Equal(0, target.SelectedIndex);
  821. Assert.Equal("Foo", target.SelectedItem);
  822. }
  823. [Fact]
  824. public void SelectAll_Raises_SelectionChanged_Event()
  825. {
  826. var target = new TestSelector
  827. {
  828. Template = Template(),
  829. Items = new[] { "Foo", "Bar", "Baz" },
  830. SelectionMode = SelectionMode.Multiple,
  831. };
  832. target.ApplyTemplate();
  833. target.Presenter.ApplyTemplate();
  834. SelectionChangedEventArgs receivedArgs = null;
  835. target.SelectionChanged += (_, args) => receivedArgs = args;
  836. target.SelectAll();
  837. Assert.NotNull(receivedArgs);
  838. Assert.Equal(target.Items, receivedArgs.AddedItems);
  839. Assert.Empty(receivedArgs.RemovedItems);
  840. }
  841. [Fact]
  842. public void UnselectAll_Clears_SelectedIndex_And_SelectedItem()
  843. {
  844. var target = new TestSelector
  845. {
  846. Template = Template(),
  847. Items = new[] { "Foo", "Bar", "Baz" },
  848. SelectionMode = SelectionMode.Multiple,
  849. SelectedIndex = 0,
  850. };
  851. target.ApplyTemplate();
  852. target.Presenter.ApplyTemplate();
  853. target.UnselectAll();
  854. Assert.Equal(-1, target.SelectedIndex);
  855. Assert.Equal(null, target.SelectedItem);
  856. }
  857. [Fact]
  858. public void SelectAll_Handles_Duplicate_Items()
  859. {
  860. var target = new TestSelector
  861. {
  862. Template = Template(),
  863. Items = new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" },
  864. SelectionMode = SelectionMode.Multiple,
  865. };
  866. target.ApplyTemplate();
  867. target.Presenter.ApplyTemplate();
  868. target.SelectAll();
  869. Assert.Equal(new[] { "Foo", "Bar", "Baz", "Foo", "Bar", "Baz" }, target.SelectedItems);
  870. }
  871. [Fact]
  872. public void Adding_Item_Before_SelectedItems_Should_Update_Selection()
  873. {
  874. var items = new ObservableCollection<string>
  875. {
  876. "Foo",
  877. "Bar",
  878. "Baz"
  879. };
  880. var target = new ListBox
  881. {
  882. Template = Template(),
  883. Items = items,
  884. SelectionMode = SelectionMode.Multiple,
  885. };
  886. target.ApplyTemplate();
  887. target.Presenter.ApplyTemplate();
  888. target.SelectAll();
  889. items.Insert(0, "Qux");
  890. Assert.Equal(1, target.SelectedIndex);
  891. Assert.Equal("Foo", target.SelectedItem);
  892. Assert.Equal(new[] { "Foo", "Bar", "Baz" }, target.SelectedItems);
  893. Assert.Equal(new[] { 1, 2, 3 }, SelectedContainers(target));
  894. }
  895. [Fact]
  896. public void Removing_Item_Before_SelectedItem_Should_Update_Selection()
  897. {
  898. var items = new ObservableCollection<string>
  899. {
  900. "Foo",
  901. "Bar",
  902. "Baz"
  903. };
  904. var target = new TestSelector
  905. {
  906. Template = Template(),
  907. Items = items,
  908. SelectionMode = SelectionMode.Multiple,
  909. };
  910. target.ApplyTemplate();
  911. target.Presenter.ApplyTemplate();
  912. target.SelectedIndex = 1;
  913. target.SelectRange(2);
  914. Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
  915. items.RemoveAt(0);
  916. Assert.Equal(0, target.SelectedIndex);
  917. Assert.Equal("Bar", target.SelectedItem);
  918. Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
  919. Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
  920. }
  921. [Fact]
  922. public void Removing_SelectedItem_With_Multiple_Selection_Active_Should_Update_Selection()
  923. {
  924. var items = new ObservableCollection<string>
  925. {
  926. "Foo",
  927. "Bar",
  928. "Baz"
  929. };
  930. var target = new ListBox
  931. {
  932. Template = Template(),
  933. Items = items,
  934. SelectionMode = SelectionMode.Multiple,
  935. };
  936. target.ApplyTemplate();
  937. target.Presenter.ApplyTemplate();
  938. target.SelectAll();
  939. items.RemoveAt(0);
  940. Assert.Equal(0, target.SelectedIndex);
  941. Assert.Equal("Bar", target.SelectedItem);
  942. Assert.Equal(new[] { "Bar", "Baz" }, target.SelectedItems);
  943. Assert.Equal(new[] { 0, 1 }, SelectedContainers(target));
  944. }
  945. [Fact]
  946. public void Replacing_Selected_Item_Should_Update_SelectedItems()
  947. {
  948. var items = new ObservableCollection<string>
  949. {
  950. "Foo",
  951. "Bar",
  952. "Baz"
  953. };
  954. var target = new ListBox
  955. {
  956. Template = Template(),
  957. Items = items,
  958. SelectionMode = SelectionMode.Multiple,
  959. };
  960. target.ApplyTemplate();
  961. target.Presenter.ApplyTemplate();
  962. target.SelectAll();
  963. items[1] = "Qux";
  964. Assert.Equal(new[] { "Foo", "Baz" }, target.SelectedItems);
  965. }
  966. [Fact]
  967. public void Left_Click_On_SelectedItem_Should_Clear_Existing_Selection()
  968. {
  969. using (UnitTestApplication.Start())
  970. {
  971. var target = new ListBox
  972. {
  973. Template = Template(),
  974. Items = new[] { "Foo", "Bar", "Baz" },
  975. ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
  976. SelectionMode = SelectionMode.Multiple,
  977. };
  978. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  979. target.ApplyTemplate();
  980. target.Presenter.ApplyTemplate();
  981. target.SelectAll();
  982. Assert.Equal(3, target.SelectedItems.Count);
  983. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  984. Assert.Equal(1, target.SelectedItems.Count);
  985. Assert.Equal(new[] { "Foo", }, target.SelectedItems);
  986. Assert.Equal(new[] { 0 }, SelectedContainers(target));
  987. }
  988. }
  989. [Fact]
  990. public void Right_Click_On_SelectedItem_Should_Not_Clear_Existing_Selection()
  991. {
  992. using (UnitTestApplication.Start())
  993. {
  994. var target = new ListBox
  995. {
  996. Template = Template(),
  997. Items = new[] { "Foo", "Bar", "Baz" },
  998. ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
  999. SelectionMode = SelectionMode.Multiple,
  1000. };
  1001. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  1002. target.ApplyTemplate();
  1003. target.Presenter.ApplyTemplate();
  1004. target.SelectAll();
  1005. Assert.Equal(3, target.SelectedItems.Count);
  1006. _helper.Click((Interactive)target.Presenter.Panel.Children[0], MouseButton.Right);
  1007. Assert.Equal(3, target.SelectedItems.Count);
  1008. }
  1009. }
  1010. [Fact]
  1011. public void Right_Click_On_UnselectedItem_Should_Clear_Existing_Selection()
  1012. {
  1013. using (UnitTestApplication.Start())
  1014. {
  1015. var target = new ListBox
  1016. {
  1017. Template = Template(),
  1018. Items = new[] { "Foo", "Bar", "Baz" },
  1019. ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
  1020. SelectionMode = SelectionMode.Multiple,
  1021. };
  1022. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  1023. target.ApplyTemplate();
  1024. target.Presenter.ApplyTemplate();
  1025. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  1026. _helper.Click((Interactive)target.Presenter.Panel.Children[1], modifiers: KeyModifiers.Shift);
  1027. Assert.Equal(2, target.SelectedItems.Count);
  1028. _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right);
  1029. Assert.Equal(1, target.SelectedItems.Count);
  1030. }
  1031. }
  1032. [Fact]
  1033. public void Adding_Selected_ItemContainers_Should_Update_Selection()
  1034. {
  1035. var items = new AvaloniaList<ItemContainer>(new[]
  1036. {
  1037. new ItemContainer(),
  1038. new ItemContainer(),
  1039. });
  1040. var target = new TestSelector
  1041. {
  1042. Items = items,
  1043. SelectionMode = SelectionMode.Multiple,
  1044. Template = Template(),
  1045. };
  1046. target.ApplyTemplate();
  1047. target.Presenter.ApplyTemplate();
  1048. items.Add(new ItemContainer { IsSelected = true });
  1049. items.Add(new ItemContainer { IsSelected = true });
  1050. Assert.Equal(2, target.SelectedIndex);
  1051. Assert.Equal(items[2], target.SelectedItem);
  1052. Assert.Equal(new[] { items[2], items[3] }, target.SelectedItems);
  1053. }
  1054. [Fact]
  1055. public void Shift_Right_Click_Should_Not_Select_Multiple()
  1056. {
  1057. using (UnitTestApplication.Start())
  1058. {
  1059. var target = new ListBox
  1060. {
  1061. Template = Template(),
  1062. Items = new[] { "Foo", "Bar", "Baz" },
  1063. ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
  1064. SelectionMode = SelectionMode.Multiple,
  1065. };
  1066. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  1067. target.ApplyTemplate();
  1068. target.Presenter.ApplyTemplate();
  1069. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  1070. _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right, modifiers: KeyModifiers.Shift);
  1071. Assert.Equal(1, target.SelectedItems.Count);
  1072. }
  1073. }
  1074. [Fact]
  1075. public void Ctrl_Right_Click_Should_Not_Select_Multiple()
  1076. {
  1077. using (UnitTestApplication.Start())
  1078. {
  1079. var target = new ListBox
  1080. {
  1081. Template = Template(),
  1082. Items = new[] { "Foo", "Bar", "Baz" },
  1083. ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Width = 20, Height = 10 }),
  1084. SelectionMode = SelectionMode.Multiple,
  1085. };
  1086. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  1087. target.ApplyTemplate();
  1088. target.Presenter.ApplyTemplate();
  1089. _helper.Click((Interactive)target.Presenter.Panel.Children[0]);
  1090. _helper.Click((Interactive)target.Presenter.Panel.Children[2], MouseButton.Right, modifiers: KeyModifiers.Control);
  1091. Assert.Equal(1, target.SelectedItems.Count);
  1092. }
  1093. }
  1094. [Fact]
  1095. public void Adding_To_Selection_Should_Set_SelectedIndex()
  1096. {
  1097. var target = new TestSelector
  1098. {
  1099. Items = new[] { "foo", "bar" },
  1100. Template = Template(),
  1101. };
  1102. target.ApplyTemplate();
  1103. target.SelectedItems.Add("bar");
  1104. Assert.Equal(1, target.SelectedIndex);
  1105. }
  1106. [Fact]
  1107. public void Assigning_Null_To_Selection_Should_Create_New_SelectionModel()
  1108. {
  1109. var target = new TestSelector
  1110. {
  1111. Items = new[] { "foo", "bar" },
  1112. Template = Template(),
  1113. };
  1114. var oldSelection = target.Selection;
  1115. target.Selection = null;
  1116. Assert.NotNull(target.Selection);
  1117. Assert.NotSame(oldSelection, target.Selection);
  1118. }
  1119. [Fact]
  1120. public void Assigning_SelectionModel_With_Different_Source_To_Selection_Should_Fail()
  1121. {
  1122. var target = new TestSelector
  1123. {
  1124. Items = new[] { "foo", "bar" },
  1125. Template = Template(),
  1126. };
  1127. var selection = new SelectionModel<string> { Source = new[] { "baz" } };
  1128. Assert.Throws<ArgumentException>(() => target.Selection = selection);
  1129. }
  1130. [Fact]
  1131. public void Assigning_SelectionModel_With_Null_Source_To_Selection_Should_Set_Source()
  1132. {
  1133. var target = new TestSelector
  1134. {
  1135. Items = new[] { "foo", "bar" },
  1136. Template = Template(),
  1137. };
  1138. var selection = new SelectionModel<string>();
  1139. target.Selection = selection;
  1140. Assert.Same(target.Items, selection.Source);
  1141. }
  1142. [Fact]
  1143. public void Assigning_Single_Selected_Item_To_Selection_Should_Set_SelectedIndex()
  1144. {
  1145. var target = new TestSelector
  1146. {
  1147. Items = new[] { "foo", "bar" },
  1148. Template = Template(),
  1149. };
  1150. target.ApplyTemplate();
  1151. target.Presenter.ApplyTemplate();
  1152. var selection = new SelectionModel<string> { SingleSelect = false };
  1153. selection.Select(1);
  1154. target.Selection = selection;
  1155. Assert.Equal(1, target.SelectedIndex);
  1156. Assert.Equal(new[] { "bar" }, target.Selection.SelectedItems);
  1157. Assert.Equal(new[] { 1 }, SelectedContainers(target));
  1158. }
  1159. [Fact]
  1160. public void Assigning_Multiple_Selected_Items_To_Selection_Should_Set_SelectedIndex()
  1161. {
  1162. var target = new TestSelector
  1163. {
  1164. Items = new[] { "foo", "bar", "baz" },
  1165. Template = Template(),
  1166. };
  1167. target.ApplyTemplate();
  1168. target.Presenter.ApplyTemplate();
  1169. var selection = new SelectionModel<string> { SingleSelect = false };
  1170. selection.SelectRange(0, 2);
  1171. target.Selection = selection;
  1172. Assert.Equal(0, target.SelectedIndex);
  1173. Assert.Equal(new[] { "foo", "bar", "baz" }, target.Selection.SelectedItems);
  1174. Assert.Equal(new[] { 0, 1, 2 }, SelectedContainers(target));
  1175. }
  1176. [Fact]
  1177. public void Reassigning_Selection_Should_Clear_Selection()
  1178. {
  1179. var target = new TestSelector
  1180. {
  1181. Items = new[] { "foo", "bar" },
  1182. Template = Template(),
  1183. };
  1184. target.ApplyTemplate();
  1185. target.Selection.Select(1);
  1186. target.Selection = new SelectionModel<string>();
  1187. Assert.Equal(-1, target.SelectedIndex);
  1188. Assert.Null(target.SelectedItem);
  1189. }
  1190. [Fact]
  1191. public void Assigning_Selection_Should_Set_Item_IsSelected()
  1192. {
  1193. var items = new[]
  1194. {
  1195. new ListBoxItem(),
  1196. new ListBoxItem(),
  1197. new ListBoxItem(),
  1198. };
  1199. var target = new TestSelector
  1200. {
  1201. Items = items,
  1202. Template = Template(),
  1203. };
  1204. target.ApplyTemplate();
  1205. target.Presenter.ApplyTemplate();
  1206. var selection = new SelectionModel<object> { SingleSelect = false };
  1207. selection.SelectRange(0, 1);
  1208. target.Selection = selection;
  1209. Assert.True(items[0].IsSelected);
  1210. Assert.True(items[1].IsSelected);
  1211. Assert.False(items[2].IsSelected);
  1212. }
  1213. [Fact]
  1214. public void Assigning_Selection_Should_Raise_SelectionChanged()
  1215. {
  1216. var items = new[] { "foo", "bar", "baz" };
  1217. var target = new TestSelector
  1218. {
  1219. Items = items,
  1220. Template = Template(),
  1221. SelectedItem = "bar",
  1222. };
  1223. var raised = 0;
  1224. target.SelectionChanged += (s, e) =>
  1225. {
  1226. if (raised == 0)
  1227. {
  1228. Assert.Empty(e.AddedItems.Cast<object>());
  1229. Assert.Equal(new[] { "bar" }, e.RemovedItems.Cast<object>());
  1230. }
  1231. else
  1232. {
  1233. Assert.Equal(new[] { "foo", "baz" }, e.AddedItems.Cast<object>());
  1234. Assert.Empty(e.RemovedItems.Cast<object>());
  1235. }
  1236. ++raised;
  1237. };
  1238. target.ApplyTemplate();
  1239. target.Presenter.ApplyTemplate();
  1240. var selection = new SelectionModel<string> { Source = items, SingleSelect = false };
  1241. selection.Select(0);
  1242. selection.Select(2);
  1243. target.Selection = selection;
  1244. Assert.Equal(2, raised);
  1245. }
  1246. private IEnumerable<int> SelectedContainers(SelectingItemsControl target)
  1247. {
  1248. return target.Presenter.Panel.Children
  1249. .Select((x, i) => x.Classes.Contains(":selected") ? i : -1)
  1250. .Where(x => x != -1);
  1251. }
  1252. private FuncControlTemplate Template()
  1253. {
  1254. return new FuncControlTemplate<SelectingItemsControl>((control, scope) =>
  1255. new ItemsPresenter
  1256. {
  1257. Name = "PART_ItemsPresenter",
  1258. [~ItemsPresenter.ItemsProperty] = control[~ItemsControl.ItemsProperty],
  1259. [~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
  1260. }.RegisterInNameScope(scope));
  1261. }
  1262. private class TestSelector : SelectingItemsControl
  1263. {
  1264. public static readonly new AvaloniaProperty<IList> SelectedItemsProperty =
  1265. SelectingItemsControl.SelectedItemsProperty;
  1266. public static readonly new DirectProperty<SelectingItemsControl, ISelectionModel> SelectionProperty =
  1267. SelectingItemsControl.SelectionProperty;
  1268. public TestSelector()
  1269. {
  1270. SelectionMode = SelectionMode.Multiple;
  1271. }
  1272. public new IList SelectedItems
  1273. {
  1274. get { return base.SelectedItems; }
  1275. set { base.SelectedItems = value; }
  1276. }
  1277. public new ISelectionModel Selection
  1278. {
  1279. get => base.Selection;
  1280. set => base.Selection = value;
  1281. }
  1282. public new SelectionMode SelectionMode
  1283. {
  1284. get { return base.SelectionMode; }
  1285. set { base.SelectionMode = value; }
  1286. }
  1287. public void SelectAll() => Selection.SelectAll();
  1288. public void UnselectAll() => Selection.Clear();
  1289. public void SelectRange(int index) => UpdateSelection(index, true, true);
  1290. public void Toggle(int index) => UpdateSelection(index, true, false, true);
  1291. }
  1292. private class OldDataContextViewModel
  1293. {
  1294. public OldDataContextViewModel()
  1295. {
  1296. Items = new List<string> { "foo", "bar" };
  1297. SelectedItems = new List<string>();
  1298. Selection = new SelectionModel<string>();
  1299. }
  1300. public List<string> Items { get; }
  1301. public List<string> SelectedItems { get; }
  1302. public SelectionModel<string> Selection { get; }
  1303. }
  1304. private class ItemContainer : Control, ISelectable
  1305. {
  1306. public string Value { get; set; }
  1307. public bool IsSelected { get; set; }
  1308. }
  1309. }
  1310. }