SelectingItemsControlTests_Multiple.cs 55 KB

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