ItemsSourceViewTests.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. using Avalonia.Collections;
  7. using Avalonia.Diagnostics;
  8. using Xunit;
  9. namespace Avalonia.Controls.UnitTests
  10. {
  11. public class ItemsSourceViewTests
  12. {
  13. [Fact]
  14. public void Only_Subscribes_To_Source_CollectionChanged_When_CollectionChanged_Subscribed()
  15. {
  16. var source = new AvaloniaList<string>();
  17. var target = new ItemsSourceView<string>(source);
  18. var debug = (INotifyCollectionChangedDebug)source;
  19. Assert.Null(debug.GetCollectionChangedSubscribers());
  20. void Handler(object sender, NotifyCollectionChangedEventArgs e) { }
  21. target.CollectionChanged += Handler;
  22. Assert.NotNull(debug.GetCollectionChangedSubscribers());
  23. Assert.Equal(1, debug.GetCollectionChangedSubscribers().Length);
  24. target.CollectionChanged -= Handler;
  25. Assert.Null(debug.GetCollectionChangedSubscribers());
  26. }
  27. [Fact]
  28. public void Cannot_Wrap_An_ItemsSourceView_In_Another()
  29. {
  30. var source = new ItemsSourceView<string>(new string[0]);
  31. Assert.Throws<ArgumentException>(() => new ItemsSourceView<string>(source));
  32. }
  33. [Fact]
  34. public void Cannot_Create_ItemsSourceView_With_Collection_That_Implements_INCC_But_Not_List()
  35. {
  36. var source = new InvalidCollection();
  37. Assert.Throws<ArgumentException>(() => new ItemsSourceView<string>(source));
  38. }
  39. private class InvalidCollection : INotifyCollectionChanged, IEnumerable<string>
  40. {
  41. public event NotifyCollectionChangedEventHandler CollectionChanged { add { } remove { } }
  42. public IEnumerator<string> GetEnumerator()
  43. {
  44. yield break;
  45. }
  46. IEnumerator IEnumerable.GetEnumerator()
  47. {
  48. yield break;
  49. }
  50. }
  51. }
  52. }