SelectingItemsControlTests_Multiple.cs 48 KB

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