SelectingItemsControlTests_Multiple.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. ((Control)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 static 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 static FuncControlTemplate Template()
  1253. {
  1254. return new FuncControlTemplate<SelectingItemsControl>((control, scope) =>
  1255. new ItemsPresenter
  1256. {
  1257. Name = "PART_ItemsPresenter",
  1258. [~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
  1259. }.RegisterInNameScope(scope));
  1260. }
  1261. private class TestSelector : SelectingItemsControl
  1262. {
  1263. public static readonly new AvaloniaProperty<IList> SelectedItemsProperty =
  1264. SelectingItemsControl.SelectedItemsProperty;
  1265. public static readonly new DirectProperty<SelectingItemsControl, ISelectionModel> SelectionProperty =
  1266. SelectingItemsControl.SelectionProperty;
  1267. public TestSelector()
  1268. {
  1269. SelectionMode = SelectionMode.Multiple;
  1270. }
  1271. public new IList SelectedItems
  1272. {
  1273. get { return base.SelectedItems; }
  1274. set { base.SelectedItems = value; }
  1275. }
  1276. public new ISelectionModel Selection
  1277. {
  1278. get => base.Selection;
  1279. set => base.Selection = value;
  1280. }
  1281. public new SelectionMode SelectionMode
  1282. {
  1283. get { return base.SelectionMode; }
  1284. set { base.SelectionMode = value; }
  1285. }
  1286. public void SelectAll() => Selection.SelectAll();
  1287. public void UnselectAll() => Selection.Clear();
  1288. public void SelectRange(int index) => UpdateSelection(index, true, true);
  1289. public void Toggle(int index) => UpdateSelection(index, true, false, true);
  1290. }
  1291. private class OldDataContextViewModel
  1292. {
  1293. public OldDataContextViewModel()
  1294. {
  1295. Items = new List<string> { "foo", "bar" };
  1296. SelectedItems = new List<string>();
  1297. Selection = new SelectionModel<string>();
  1298. }
  1299. public List<string> Items { get; }
  1300. public List<string> SelectedItems { get; }
  1301. public SelectionModel<string> Selection { get; }
  1302. }
  1303. private class ItemContainer : Control, ISelectable
  1304. {
  1305. public string Value { get; set; }
  1306. public bool IsSelected { get; set; }
  1307. }
  1308. }
  1309. }