ItemsSourceViewTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class ItemsSourceViewTests : ScopedTestBase
  13. {
  14. [Fact]
  15. public void Only_Subscribes_To_Source_CollectionChanged_When_CollectionChanged_Subscribed()
  16. {
  17. var source = new AvaloniaList<string>();
  18. var target = ItemsSourceView.GetOrCreate(source);
  19. var debug = (INotifyCollectionChangedDebug)source;
  20. Assert.Null(debug.GetCollectionChangedSubscribers());
  21. void Handler(object sender, NotifyCollectionChangedEventArgs e) { }
  22. target.CollectionChanged += Handler;
  23. Assert.NotNull(debug.GetCollectionChangedSubscribers());
  24. Assert.Equal(1, debug.GetCollectionChangedSubscribers().Length);
  25. target.CollectionChanged -= Handler;
  26. Assert.Null(debug.GetCollectionChangedSubscribers());
  27. }
  28. [Fact]
  29. public void Cannot_Create_ItemsSourceView_With_Collection_That_Implements_INCC_But_Not_List()
  30. {
  31. var source = new InvalidCollection();
  32. Assert.Throws<ArgumentException>(() => ItemsSourceView.GetOrCreate(source));
  33. }
  34. [Fact]
  35. public void Reassigning_Source_Unsubscribes_From_Previous_Source()
  36. {
  37. var source = new AvaloniaList<string>();
  38. var target = new ReassignableItemsSourceView(source);
  39. var debug = (INotifyCollectionChangedDebug)source;
  40. target.CollectionChanged += (s, e) => { };
  41. Assert.Equal(1, debug.GetCollectionChangedSubscribers().Length);
  42. target.SetSource(new string[0]);
  43. Assert.Null(debug.GetCollectionChangedSubscribers());
  44. }
  45. [Fact]
  46. public void Reassigning_Source_Subscribes_To_New_Source()
  47. {
  48. var source = new AvaloniaList<string>();
  49. var target = new ReassignableItemsSourceView(new string[0]);
  50. var debug = (INotifyCollectionChangedDebug)source;
  51. target.CollectionChanged += (s, e) => { };
  52. target.SetSource(source);
  53. Assert.Equal(1, debug.GetCollectionChangedSubscribers().Length);
  54. }
  55. private class InvalidCollection : INotifyCollectionChanged, IEnumerable<string>
  56. {
  57. public event NotifyCollectionChangedEventHandler CollectionChanged { add { } remove { } }
  58. public IEnumerator<string> GetEnumerator()
  59. {
  60. yield break;
  61. }
  62. IEnumerator IEnumerable.GetEnumerator()
  63. {
  64. yield break;
  65. }
  66. }
  67. private class ReassignableItemsSourceView : ItemsSourceView
  68. {
  69. public ReassignableItemsSourceView(IEnumerable source)
  70. : base(source)
  71. {
  72. }
  73. public new void SetSource(IEnumerable source) => base.SetSource(source);
  74. }
  75. }
  76. }