PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/test/2.0/moon-unit/System.Windows.Data/CollectionViewTest.cs

http://github.com/mono/moon
C# | 850 lines | 686 code | 124 blank | 40 comment | 6 complexity | a75b1f95e9a55ca390a692483e25b1e8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. //
  2. // CollectionViewTest.cs
  3. //
  4. // Contact:
  5. // Moonlight List (moonlight-list@lists.ximian.com)
  6. //
  7. // Copyright 2010 Novell, Inc.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. using System.Linq;
  33. using System.Windows.Data;
  34. using Microsoft.VisualStudio.TestTools.UnitTesting;
  35. using Mono.Moonlight.UnitTesting;
  36. using System.Windows.Shapes;
  37. using System.Globalization;
  38. using System.Collections.ObjectModel;
  39. using System.Windows.Controls;
  40. using System.Collections;
  41. namespace MoonTest.System.Windows.Data {
  42. [TestClass]
  43. public class CollectionViewTest {
  44. public int CurrentChanged { get; set; }
  45. public int CurrentChanging { get; set; }
  46. public List<NotifyCollectionChangedEventArgs> CollectionChanged;
  47. public List<object> Items
  48. {
  49. get;
  50. set;
  51. }
  52. ObservableCollection<Rectangle> Rectangles
  53. {
  54. get;
  55. set;
  56. }
  57. public CollectionViewSource Source
  58. {
  59. get;
  60. set;
  61. }
  62. public ICollectionView View
  63. {
  64. get;
  65. set;
  66. }
  67. [TestInitialize]
  68. public void Setup ()
  69. {
  70. Items = new List<object> (new [] {
  71. new object (),
  72. new object (),
  73. new object (),
  74. new object (),
  75. new object (),
  76. });
  77. Rectangles = new ObservableCollection<Rectangle> {
  78. new Rectangle { Width= 1 },
  79. new Rectangle { Width= 2 },
  80. new Rectangle { Width= 3 },
  81. new Rectangle { Width= 4 },
  82. new Rectangle { Width= 5 },
  83. };
  84. Source = new CollectionViewSource { };
  85. SetSource(Items);
  86. ResetCounters();
  87. }
  88. void SetSource(IEnumerable items)
  89. {
  90. Source.Source = items;
  91. View = Source.View;
  92. View.CurrentChanged += (o, e) => {
  93. CurrentChanged++;
  94. };
  95. View.CurrentChanging += (o, e) => {
  96. CurrentChanging++;
  97. };
  98. View.CollectionChanged += (o, e) => CollectionChanged.Add (e);
  99. }
  100. void ResetCounters()
  101. {
  102. CurrentChanged = 0;
  103. CurrentChanging = 0;
  104. CollectionChanged = new List<NotifyCollectionChangedEventArgs>();
  105. }
  106. [TestMethod]
  107. public void BindDirectlyToCVS_ICVProperty ()
  108. {
  109. var HostPanel = new StackPanel ();
  110. var collection = new ObservableCollection<int> () { 1, 2, 5, 0 };
  111. var cvs = new CollectionViewSource () { Source = collection };
  112. var binding = new Binding ("IsCurrentAfterLast") {
  113. Source = cvs,
  114. BindsDirectlyToSource = true
  115. };
  116. HostPanel.SetBinding (Panel.TagProperty, binding);
  117. Assert.IsNull (HostPanel.Tag, "#1");
  118. }
  119. [TestMethod]
  120. public void BindDirectlyToICV_ICVProperty ()
  121. {
  122. var HostPanel = new StackPanel ();
  123. var collection = new ObservableCollection<int> () { 1, 2, 5, 0 };
  124. var cvs = new CollectionViewSource () { Source = collection };
  125. var binding = new Binding ("IsCurrentAfterLast") {
  126. Source = cvs.View,
  127. BindsDirectlyToSource = true
  128. };
  129. HostPanel.SetBinding (Panel.TagProperty, binding);
  130. Assert.IsNotNull (HostPanel.Tag, "#2");
  131. }
  132. [TestMethod]
  133. public void BindToCVS_ICVProperty ()
  134. {
  135. var HostPanel = new StackPanel ();
  136. var collection = new ObservableCollection<int> () { 1, 2, 5, 0 };
  137. var cvs = new CollectionViewSource () { Source = collection };
  138. var binding = new Binding ("IsCurrentAfterLast") {
  139. Source = cvs,
  140. BindsDirectlyToSource = false
  141. };
  142. HostPanel.SetBinding (Panel.TagProperty, binding);
  143. Assert.IsNotNull (HostPanel.Tag, "#3");
  144. }
  145. [TestMethod]
  146. public void BindToICV__ICVProperty ()
  147. {
  148. var HostPanel = new StackPanel ();
  149. var collection = new ObservableCollection<int> () { 1, 2, 5, 0 };
  150. var cvs = new CollectionViewSource () { Source = collection };
  151. var binding = new Binding ("IsCurrentAfterLast") {
  152. Source = cvs.View,
  153. BindsDirectlyToSource = false
  154. };
  155. HostPanel.SetBinding (Panel.TagProperty, binding);
  156. Assert.IsNotNull (HostPanel.Tag, "#4");
  157. }
  158. [TestMethod]
  159. public void ChangingGroupsClearsFilter ()
  160. {
  161. View.Filter = o => true;
  162. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  163. Assert.IsNull (View.Filter, "#1");
  164. View.Filter = o => true;
  165. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription ("Test"));
  166. Assert.IsNull (View.Filter, "#2");
  167. }
  168. [TestMethod]
  169. public void ChangeGroupsOnView ()
  170. {
  171. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription {
  172. GroupNameFromItemFunc = (item, depth, culture) => "A",
  173. });
  174. Assert.IsNotNull(View.Groups, "#1");
  175. Assert.IsInstanceOfType<CollectionViewGroup>(View.Groups[0], "#2");
  176. }
  177. [TestMethod]
  178. public void ChangeGroupsOnSource_EventsOnView ()
  179. {
  180. var list = new List<NotifyCollectionChangedEventArgs> ();
  181. View.GroupDescriptions.CollectionChanged += (o, e) => list.Add (e);
  182. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  183. Assert.AreEqual (2, list.Count, "#1");
  184. Assert.AreEqual (NotifyCollectionChangedAction.Reset, list [0].Action, "#2");
  185. Assert.AreEqual (NotifyCollectionChangedAction.Add, list [1].Action, "#3");
  186. list.Clear ();
  187. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  188. Assert.AreEqual (3, list.Count, "#4");
  189. Assert.AreEqual (NotifyCollectionChangedAction.Reset, list [0].Action, "#5");
  190. Assert.AreEqual (NotifyCollectionChangedAction.Add, list [1].Action, "#6");
  191. Assert.AreEqual (NotifyCollectionChangedAction.Add, list [2].Action, "#7");
  192. }
  193. [TestMethod]
  194. public void Contains()
  195. {
  196. Assert.IsTrue(View.Contains(Items[0]), "#1");
  197. }
  198. [TestMethod]
  199. public void CulturePropagatesToDescriptions ()
  200. {
  201. CultureInfo culture = null;
  202. Source.Culture = CultureInfo.InvariantCulture;
  203. Assert.AreSame (Source.Culture, Source.View.Culture, "#1");
  204. var group = new ConcretePropertyGroupDescription {
  205. GroupNameFromItemFunc = (item, level, cult) => {
  206. culture = cult;
  207. return "A";
  208. }
  209. };
  210. // Verify that the method was called and we passed in the right cultureinfo
  211. Source.GroupDescriptions.Add (group);
  212. Assert.AreSame (Source.Culture, culture, "#2");
  213. }
  214. [TestMethod]
  215. public void DefaultImplIsINPC ()
  216. {
  217. Assert.IsTrue (View is INotifyPropertyChanged, "#1");
  218. }
  219. [TestMethod]
  220. public void INPCEvents ()
  221. {
  222. INotifyPropertyChanged source = (INotifyPropertyChanged)View;
  223. VerifyPropertyChanged ("#1", source, () => View.Culture = CultureInfo.InvariantCulture, "Culture");
  224. VerifyPropertyChanged ("#3", source, () => View.MoveCurrentToNext (), "CurrentPosition", "CurrentItem");
  225. View.MoveCurrentToFirst ();
  226. VerifyPropertyChanged ("#4", source, () => View.MoveCurrentToPrevious (), "IsCurrentBeforeFirst", "CurrentPosition", "CurrentItem");
  227. }
  228. [TestMethod]
  229. public void HiddenINPCEvents ()
  230. {
  231. // These INPC events don't have publicly visible properties so I don't know why/how they're being raised.
  232. INotifyPropertyChanged source = (INotifyPropertyChanged)View;
  233. VerifyPropertyChanged ("#2", source, () => View.Filter = delegate { return true; }, "Count");
  234. }
  235. [TestMethod]
  236. public void HiddenINPCEvents_Remove ()
  237. {
  238. SetSource(Rectangles);
  239. // These INPC events don't have publicly visible properties so I don't know why/how they're being raised.
  240. INotifyPropertyChanged source = (INotifyPropertyChanged)View;
  241. VerifyPropertyChanged("#2", source, () => Rectangles.RemoveAt (1), "Count");
  242. }
  243. [TestMethod]
  244. public void HiddenINPCEvents_Add()
  245. {
  246. SetSource(Rectangles);
  247. // These INPC events don't have publicly visible properties so I don't know why/how they're being raised.
  248. INotifyPropertyChanged source = (INotifyPropertyChanged)View;
  249. VerifyPropertyChanged("#2", source, () => Rectangles.Add(new Rectangle ()), "Count");
  250. }
  251. [TestMethod]
  252. public void HiddenINPCEvents_Replace()
  253. {
  254. SetSource(Rectangles);
  255. // These INPC events don't have publicly visible properties so I don't know why/how they're being raised.
  256. INotifyPropertyChanged source = (INotifyPropertyChanged)View;
  257. VerifyPropertyChanged("#2", source, () => Rectangles[1] = new Rectangle ());
  258. }
  259. void VerifyPropertyChanged (string message, INotifyPropertyChanged source, Action action, params string[] expectedProperties)
  260. {
  261. List<string> props = new List<string> (expectedProperties);
  262. PropertyChangedEventHandler h = (o, e) => {
  263. Assert.AreEqual (props.First (), e.PropertyName, message + ": Incorrect property name received");
  264. props.RemoveAt (0);
  265. };
  266. source.PropertyChanged += h;
  267. action ();
  268. source.PropertyChanged -= h;
  269. Assert.AreEqual (0, props.Count, message + ": All expected PropertyChanged events were not raised for");
  270. }
  271. [TestMethod]
  272. public void DeferAndAddGroup ()
  273. {
  274. using (View.DeferRefresh ()) {
  275. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  276. Assert.IsNull (View.Groups, "#1");
  277. }
  278. Assert.IsNotNull (View.Groups, "#2");
  279. }
  280. [TestMethod]
  281. public void DeferTwiceAndAddGroup ()
  282. {
  283. using (View.DeferRefresh ()) {
  284. using (View.DeferRefresh ()) {
  285. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  286. Assert.IsNull (View.Groups, "#1");
  287. }
  288. Assert.IsNull (View.Groups, "#2");
  289. }
  290. Assert.IsNotNull (View.Groups, "#3");
  291. }
  292. [TestMethod]
  293. public void DeferSameOneTwiceAndAddGroup ()
  294. {
  295. using (var deferrer = View.DeferRefresh ()) {
  296. using (View.DeferRefresh ()) {
  297. deferrer.Dispose ();
  298. deferrer.Dispose ();
  299. deferrer.Dispose ();
  300. deferrer.Dispose ();
  301. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  302. Assert.IsNull (View.Groups, "#1");
  303. }
  304. Assert.IsNotNull (View.Groups, "#3");
  305. }
  306. }
  307. [TestMethod]
  308. public void EmptyList ()
  309. {
  310. View = new CollectionViewSource { Source = new object [0] }.View;
  311. Assert.IsTrue (View.IsCurrentBeforeFirst, "#1");
  312. Assert.IsTrue (View.IsCurrentAfterLast, "#2");
  313. }
  314. [TestMethod]
  315. public void EventOrdering_SelectNewItem ()
  316. {
  317. int count = 0;
  318. ((INotifyPropertyChanged) View).PropertyChanged += (o, e) => {
  319. Assert.AreSame (Items [1], View.CurrentItem, "#1");
  320. Assert.AreEqual (1, View.CurrentPosition, "#2");
  321. if (count == 0)
  322. Assert.AreEqual ("CurrentPosition", e.PropertyName, "position");
  323. else if (count == 1)
  324. Assert.AreEqual ("CurrentItem", e.PropertyName, "#item");
  325. else
  326. Assert.Fail ("Too many events");
  327. count++;
  328. };
  329. View.MoveCurrentToNext ();
  330. Assert.AreEqual (2, count, "#two events should fire");
  331. }
  332. [TestMethod]
  333. public void FilterAndGroup_FilterUpper_GroupBySelf ()
  334. {
  335. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  336. View.Filter = o => Items.IndexOf (o) < 2;
  337. Assert.AreEqual (Items.Count - 3, View.Groups.Count, "#1");
  338. }
  339. [TestMethod]
  340. public void FilterAll ()
  341. {
  342. Check (Items [0], 0, false, false, "#1");
  343. View.Filter = o => false;
  344. Check (null, -1, true, true, "#2");
  345. }
  346. [TestMethod]
  347. public void FilterSome_LowerHalf ()
  348. {
  349. Check (Items [0], 0, false, false, "#1");
  350. View.Filter = o => Items.IndexOf (o) >= 2;
  351. Check (Items [2], 0, false, false, "#2");
  352. }
  353. [TestMethod]
  354. public void FilterSome_UpperHalf ()
  355. {
  356. Check (Items [0], 0, false, false, "#1");
  357. View.Filter = o => Items.IndexOf (o) < 2;
  358. Check (Items [0], 0, false, false, "#2");
  359. }
  360. [TestMethod]
  361. public void Group_TwoDescriptions_CanGroup ()
  362. {
  363. Assert.IsTrue (View.CanGroup, "#1");
  364. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  365. Assert.IsTrue (View.CanGroup, "#2");
  366. }
  367. [TestMethod]
  368. public void Group_GroupBySelf ()
  369. {
  370. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  371. Assert.AreEqual (Items.Count, View.Groups.Count, "#1");
  372. for (int i = 0; i < View.Groups.Count; i++) {
  373. var g = (CollectionViewGroup) View.Groups [i];
  374. Assert.AreEqual (1, g.ItemCount, "#2." + i);
  375. Assert.IsTrue (g.IsBottomLevel, "#3." + i);
  376. Assert.AreSame (Items [i], g.Name, "#5." + i);
  377. }
  378. }
  379. [TestMethod]
  380. public void Group_IndexOf()
  381. {
  382. List<Rectangle> rects = new List<Rectangle> {
  383. new Rectangle { Width = 10, Height = 10 },
  384. new Rectangle { Width = 10, Height = 20 },
  385. new Rectangle { Width = 20, Height = 10 },
  386. new Rectangle { Width = 20, Height = 20 }
  387. };
  388. SetSource(rects);
  389. using (Source.DeferRefresh ()) {
  390. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription {
  391. GroupNameFromItemFunc = (item, level, culture) => rects.IndexOf ((Rectangle)item) < 2 ? "A" : "B"
  392. });
  393. Source.SortDescriptions.Add (new SortDescription ("Width", ListSortDirection.Descending));
  394. Source.SortDescriptions.Add (new SortDescription ("Height", ListSortDirection.Ascending));
  395. }
  396. Assert.AreSame (rects [2], View.Cast<object>().ElementAt(0), "#1");
  397. Assert.AreSame(rects[3], View.Cast<object>().ElementAt(1), "#2");
  398. Assert.AreSame(rects[0], View.Cast<object>().ElementAt(2), "#3");
  399. Assert.AreSame(rects[1], View.Cast<object>().ElementAt(3), "#4");
  400. }
  401. [TestMethod]
  402. public void GroupDescriptions_SourceForwardsToView ()
  403. {
  404. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  405. Assert.AreEqual (1, View.GroupDescriptions.Count, "#1");
  406. Assert.AreSame (Source.GroupDescriptions [0], View.GroupDescriptions [0], "#2");
  407. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ("Test"));
  408. Assert.AreEqual (1, Source.GroupDescriptions.Count, "#3");
  409. View.GroupDescriptions.Clear ();
  410. Assert.AreEqual (1, Source.GroupDescriptions.Count, "#4");
  411. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ("aaa"));
  412. Source.GroupDescriptions.Clear ();
  413. Assert.AreEqual (0, View.GroupDescriptions.Count, "#5");
  414. }
  415. [TestMethod]
  416. public void GroupsIsSameCollection ()
  417. {
  418. Assert.IsNull (View.Groups, "#1");
  419. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription (""));
  420. Assert.IsNotNull (View.Groups, "#2");
  421. var g = View.Groups;
  422. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription ("Tester"));
  423. Assert.AreSame (g, View.Groups, "#3");
  424. Source.GroupDescriptions.Clear ();
  425. Assert.IsNull (View.Groups, "#4");
  426. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription ());
  427. Assert.AreSame (g, View.Groups, "#5");
  428. }
  429. [TestMethod]
  430. public void ImplicitGroupDoesNotExist ()
  431. {
  432. Assert.IsNull (View.Groups, "#1");
  433. }
  434. [TestMethod]
  435. public void InsertItemBeforeSelection ()
  436. {
  437. var index = 2;
  438. var item = Items [index];
  439. var incc = new ObservableCollection<object>(Items);
  440. SetSource(incc);
  441. View.MoveCurrentToPosition(index);
  442. ResetCounters();
  443. incc.Insert(0, new object());
  444. Assert.AreEqual(index + 1, View.CurrentPosition, "#1");
  445. Assert.AreEqual(item, View.CurrentItem, "#2");
  446. Assert.AreEqual(0, CurrentChanged, "#3");
  447. Assert.AreEqual(0, CurrentChanging, "#4");
  448. }
  449. [TestMethod]
  450. public void InsertItem_AfterSelection_SortedSecond ()
  451. {
  452. // This should be second in our list no matter where we insert it
  453. var selection = Rectangles[1];
  454. var rect = new Rectangle { Width = 1.5 };
  455. SetSource(Rectangles);
  456. View.SortDescriptions.Add(new SortDescription("Width", ListSortDirection.Ascending));
  457. View.MoveCurrentTo(selection);
  458. ResetCounters();
  459. Rectangles.Insert(0, rect);
  460. Assert.AreSame(selection, View.CurrentItem, "#1");
  461. Assert.AreEqual(2, View.CurrentPosition, "#2");
  462. Assert.AreEqual(0, CurrentChanged, "#3");
  463. Assert.AreEqual(0, CurrentChanging, "#4");
  464. }
  465. [TestMethod]
  466. public void InsertItem_BeforeSelection_SortedSecond()
  467. {
  468. // This should be second in our list no matter where we insert it
  469. var selection = Rectangles[1];
  470. var rect = new Rectangle { Width = 1.5 };
  471. SetSource(Rectangles);
  472. View.SortDescriptions.Add(new SortDescription("Width", ListSortDirection.Ascending));
  473. View.MoveCurrentTo(selection);
  474. ResetCounters();
  475. Rectangles.Insert(4, rect);
  476. Assert.AreSame(selection, View.CurrentItem, "#1");
  477. Assert.AreEqual(2, View.CurrentPosition, "#2");
  478. Assert.AreEqual(0, CurrentChanged, "#3");
  479. Assert.AreEqual(0, CurrentChanging, "#4");
  480. }
  481. [TestMethod]
  482. public void InsertItem_AfterSelection_SortedLast()
  483. {
  484. // This should be second in our list no matter where we insert it
  485. var selection = Rectangles[1];
  486. var rect = new Rectangle { Width = 6 };
  487. SetSource(Rectangles);
  488. View.SortDescriptions.Add(new SortDescription("Width", ListSortDirection.Ascending));
  489. View.MoveCurrentTo(selection);
  490. ResetCounters();
  491. Rectangles.Insert(0, rect);
  492. Assert.AreSame(selection, View.CurrentItem, "#1");
  493. Assert.AreEqual(1, View.CurrentPosition, "#2");
  494. Assert.AreEqual(0, CurrentChanged, "#3");
  495. Assert.AreEqual(0, CurrentChanging, "#4");
  496. }
  497. [TestMethod]
  498. public void InsertItem_BeforeSelection_SortedLast()
  499. {
  500. // This should be second in our list no matter where we insert it
  501. var selection = Rectangles[1];
  502. var rect = new Rectangle { Width = 6 };
  503. SetSource(Rectangles);
  504. View.SortDescriptions.Add(new SortDescription("Width", ListSortDirection.Ascending));
  505. View.MoveCurrentTo(selection);
  506. ResetCounters();
  507. Rectangles.Insert(4, rect);
  508. Assert.AreSame(selection, View.CurrentItem, "#1");
  509. Assert.AreEqual(1, View.CurrentPosition, "#2");
  510. Assert.AreEqual(0, CurrentChanged, "#3");
  511. Assert.AreEqual(0, CurrentChanging, "#4");
  512. }
  513. [TestMethod]
  514. public void InsertItem_Grouped_BeforeSelection ()
  515. {
  516. // This should be second in our list no matter where we insert it
  517. var rect = new Rectangle { Width = 1.5 };
  518. SetSource (Rectangles);
  519. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription ("Width") {
  520. GroupNameFromItemFunc = (item, level, culture) => ((Rectangle) item).Width < 2 ? "A" : "B"
  521. });
  522. View.SortDescriptions.Add (new SortDescription ("Width", ListSortDirection.Ascending));
  523. var selection = View.Cast<object> ().Last ();
  524. View.MoveCurrentTo (selection);
  525. Assert.AreEqual (View.CurrentPosition, 4, "#1");
  526. ResetCounters ();
  527. Rectangles.Insert (4, rect);
  528. Assert.AreSame (selection, View.CurrentItem, "#2");
  529. Assert.AreEqual (5, View.CurrentPosition, "#3");
  530. Assert.AreEqual (0, CurrentChanged, "#4");
  531. Assert.AreEqual (0, CurrentChanging, "#5");
  532. }
  533. [TestMethod]
  534. public void DifferentGroupDescriptions ()
  535. {
  536. Assert.AreNotSame (View.GroupDescriptions, Source.GroupDescriptions, "#1");
  537. }
  538. [TestMethod]
  539. public void MoveTo_LowerFiltered ()
  540. {
  541. Check (Items [0], 0, false, false, "#1");
  542. View.Filter = o => Items.IndexOf (o) >= 2;
  543. View.MoveCurrentToPosition (1);
  544. Check (Items [3], 1, false, false, "#2");
  545. }
  546. [TestMethod]
  547. public void MoveTo_UpperFiltered ()
  548. {
  549. Check (Items [0], 0, false, false, "#1");
  550. View.Filter = o => Items.IndexOf (o) < 2;
  551. View.MoveCurrentToPosition (1);
  552. Check (Items [1], 1, false, false, "#2");
  553. }
  554. [TestMethod]
  555. public void MoveEventsRaised ()
  556. {
  557. View.MoveCurrentToNext ();
  558. Check (Items [1], 1, false, false, "#2");
  559. Assert.AreEqual (1, CurrentChanging, "#3");
  560. Assert.AreEqual (1, CurrentChanged, "#4");
  561. }
  562. [TestMethod]
  563. public void MoveEventsRaised_Cancelled ()
  564. {
  565. View.CurrentChanging += (o, e) => {
  566. Assert.IsTrue (e.IsCancelable, "#1");
  567. e.Cancel = true;
  568. };
  569. // It will not move
  570. Assert.IsTrue (View.MoveCurrentToNext (), "#2");
  571. Check (Items [0], 0, false, false, "#3");
  572. Assert.AreEqual (1, CurrentChanging, "#4");
  573. Assert.AreEqual (0, CurrentChanged, "#5");
  574. }
  575. [TestMethod]
  576. public void MoveToPrev ()
  577. {
  578. Assert.IsTrue (View.MoveCurrentToPosition (2), "#a");
  579. Assert.IsTrue (View.MoveCurrentToPrevious (), "#b");
  580. Check (Items [1], 1, false, false, "#1");
  581. }
  582. [TestMethod]
  583. public void MoveToPrev_AtStart ()
  584. {
  585. Check (Items [0], 0, false, false, "#1");
  586. Assert.IsTrue (View.MoveCurrentToFirst (), "#a");
  587. Assert.IsFalse (View.MoveCurrentToPrevious (), "#b");
  588. Check (null, -1, true, false, "#2");
  589. }
  590. [TestMethod]
  591. public void MoveToPrev_BeforeFirst ()
  592. {
  593. Check (Items [0], 0, false, false, "#1");
  594. Assert.IsTrue (View.MoveCurrentToFirst (), "#a");
  595. Assert.IsFalse (View.MoveCurrentToPrevious (), "#b");
  596. Assert.IsFalse (View.MoveCurrentToPrevious (), "#c");
  597. Check (null, -1, true, false, "#2");
  598. }
  599. [TestMethod]
  600. public void MoveToFirst ()
  601. {
  602. Check (Items [0], 0, false, false, "#1");
  603. Assert.IsTrue (View.MoveCurrentToFirst (), "#a");
  604. Check (Items [0], 0, false, false, "#2");
  605. }
  606. [TestMethod]
  607. public void MoveToNext ()
  608. {
  609. Check (Items [0], 0, false, false, "#1");
  610. Assert.IsTrue (View.MoveCurrentToNext (), "#a");
  611. Check (Items [1], 1, false, false, "#2");
  612. }
  613. [TestMethod]
  614. public void MoveToNext_AtEnd ()
  615. {
  616. Assert.IsTrue (View.MoveCurrentToLast (), "#1");
  617. Assert.IsFalse (View.MoveCurrentToNext (), "#2");
  618. Check (null, Items.Count, false, true, "#3");
  619. }
  620. [TestMethod]
  621. public void MoveToNext_AfterLast ()
  622. {
  623. Assert.IsTrue (View.MoveCurrentToLast (), "#1");
  624. Check (Items.Last (), Items.Count - 1, false, false, "#2");
  625. Assert.IsFalse (View.MoveCurrentToNext (), "#3");
  626. Check (null, Items.Count, false, true, "#4");
  627. Assert.IsFalse (View.MoveCurrentToNext (), "#5");
  628. Check (null, Items.Count, false, true, "#6");
  629. }
  630. [TestMethod]
  631. public void MoveToLast ()
  632. {
  633. Check (Items [0], 0, false, false, "#1");
  634. Assert.IsTrue (View.MoveCurrentToLast (), "#a");
  635. Check (Items.Last (), Items.Count - 1, false, false, "#2");
  636. }
  637. [TestMethod]
  638. public void MoveToItem ()
  639. {
  640. Check (Items [0], 0, false, false, "#1");
  641. Assert.IsTrue (View.MoveCurrentTo (Items [2]), "#a");
  642. Check (Items [2], 2, false, false, "#2");
  643. }
  644. [TestMethod]
  645. public void MoveToItem_NotThere ()
  646. {
  647. Check (Items [0], 0, false, false, "#1");
  648. Assert.IsFalse (View.MoveCurrentTo (new object ()), "#a");
  649. Check (null, -1, true, false, "#2");
  650. }
  651. [TestMethod]
  652. public void RefreshEmitsCurrentChangingEvents()
  653. {
  654. var events = new List<CurrentChangingEventArgs>();
  655. var data = new List<object> {
  656. "First",
  657. "Second",
  658. "Third",
  659. "Fourth",
  660. "Fifth",
  661. };
  662. var view = new CollectionViewSource() { Source = data }.View;
  663. view.CurrentChanging += (o, e) => events.Add(e);
  664. view.Refresh();
  665. Assert.AreEqual(1, events.Count, "#1");
  666. view.Refresh();
  667. Assert.AreEqual(2, events.Count, "#2");
  668. }
  669. [TestMethod]
  670. public void Sort_OneDescription ()
  671. {
  672. Source.Source = new [] { 1, 2, 3, 4, 5 };
  673. Source.SortDescriptions.Add (new SortDescription ("", ListSortDirection.Descending));
  674. Source.View.MoveCurrentToFirst ();
  675. Assert.AreEqual (5, (int) Source.View.CurrentItem, "#1");
  676. Source.View.MoveCurrentToLast ();
  677. Assert.AreEqual (1, (int) Source.View.CurrentItem, "#2");
  678. }
  679. [TestMethod]
  680. public void Sort_TwoDescription ()
  681. {
  682. Source.Source = new [] { 1, 2, 3, 4, 5 };
  683. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription { GroupNameFromItemFunc = (item, level, culture) => (int) item < 3 ? "A" : "B" });
  684. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription { GroupNameFromItemFunc = (item, level, culture) => (int) item < 3 ? "A" : "B" });
  685. Source.SortDescriptions.Add (new SortDescription ("", ListSortDirection.Descending));
  686. Source.SortDescriptions.Add (new SortDescription ("", ListSortDirection.Ascending));
  687. Source.View.MoveCurrentToFirst ();
  688. Assert.AreEqual (5, (int) Source.View.CurrentItem, "#1");
  689. Source.View.MoveCurrentToLast ();
  690. Assert.AreEqual (1, (int) Source.View.CurrentItem, "#2");
  691. }
  692. [TestMethod]
  693. public void Sort_TwoDescription_ActualProperties ()
  694. {
  695. List<Rectangle> rects = new List<Rectangle> {
  696. new Rectangle { Width = 10, Height = 10 },
  697. new Rectangle { Width = 10, Height = 20 },
  698. new Rectangle { Width = 20, Height = 10 },
  699. new Rectangle { Width = 20, Height = 20 }
  700. };
  701. using (Source.DeferRefresh ()) {
  702. Source.Source = rects;
  703. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription {
  704. GroupNameFromItemFunc = (item, level, culture) => rects.IndexOf ((Rectangle)item) < 2 ? "A" : "B"
  705. });
  706. Source.SortDescriptions.Add (new SortDescription ("Width", ListSortDirection.Descending));
  707. Source.SortDescriptions.Add (new SortDescription ("Height", ListSortDirection.Ascending));
  708. }
  709. // Check the first group
  710. var group = (CollectionViewGroup) Source.View.Groups [0];
  711. Assert.AreSame (rects [2], group.Items [0], "#1");
  712. Assert.AreSame (rects [3], group.Items [1], "#2");
  713. // Check the second group
  714. group = (CollectionViewGroup) Source.View.Groups [1];
  715. Assert.AreSame (rects [0], group.Items [0], "#3");
  716. Assert.AreSame (rects [1], group.Items [1], "#4");
  717. }
  718. void Check (object item, int position, bool beforeFirst, bool afterLast, string message)
  719. {
  720. Assert.AreEqual (item, View.CurrentItem, message + ".1");
  721. Assert.AreEqual (position, View.CurrentPosition, message + ".2");
  722. Assert.AreEqual (beforeFirst, View.IsCurrentBeforeFirst, message + ".3");
  723. Assert.AreEqual (afterLast, View.IsCurrentAfterLast, message + ".4");
  724. }
  725. }
  726. }