using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using Avalonia.Collections; using Avalonia.Diagnostics; using Xunit; namespace Avalonia.Controls.UnitTests { public class ItemsSourceViewTests { [Fact] public void Only_Subscribes_To_Source_CollectionChanged_When_CollectionChanged_Subscribed() { var source = new AvaloniaList(); var target = new ItemsSourceView(source); var debug = (INotifyCollectionChangedDebug)source; Assert.Null(debug.GetCollectionChangedSubscribers()); void Handler(object sender, NotifyCollectionChangedEventArgs e) { } target.CollectionChanged += Handler; Assert.NotNull(debug.GetCollectionChangedSubscribers()); Assert.Equal(1, debug.GetCollectionChangedSubscribers().Length); target.CollectionChanged -= Handler; Assert.Null(debug.GetCollectionChangedSubscribers()); } [Fact] public void Cannot_Wrap_An_ItemsSourceView_In_Another() { var source = new ItemsSourceView(new string[0]); Assert.Throws(() => new ItemsSourceView(source)); } [Fact] public void Cannot_Create_ItemsSourceView_With_Collection_That_Implements_INCC_But_Not_List() { var source = new InvalidCollection(); Assert.Throws(() => new ItemsSourceView(source)); } private class InvalidCollection : INotifyCollectionChanged, IEnumerable { public event NotifyCollectionChangedEventHandler CollectionChanged { add { } remove { } } public IEnumerator GetEnumerator() { yield break; } IEnumerator IEnumerable.GetEnumerator() { yield break; } } } }