public void IListImplementation_Properties_ShoudReturnExpectedValues()
        {
            var groups = new List <IGrouping <string, int> >
            {
                new IntGroup("A", new[] { 1, 3, 5 }),
                new IntGroup("B", new[] { 2, 4, 6 }),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);
            var list          = (IList)readOnlyGroup;

            list.Count.Should().Be(2);
            var group0 = (ReadOnlyObservableGroup <string, int>)list[0];

            group0.Key.Should().Be("A");
            group0.Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
            var group1 = (ReadOnlyObservableGroup <string, int>)list[1];

            group1.Key.Should().Be("B");
            group1.Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());

            list.SyncRoot.Should().NotBeNull();
            list.IsFixedSize.Should().BeTrue();
            list.IsReadOnly.Should().BeTrue();
            list.IsSynchronized.Should().BeFalse();
        }
        public void ClearSource_ShoudClear()
        {
            NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
            var collectionChangedEventsCount      = 0;
            var isCountPropertyChangedEventRaised = false;
            var aItemsList = new[] { 1, 2, 3 };
            var bItemsList = new[] { 2, 4, 6 };
            var groups     = new List <IGrouping <string, int> >
            {
                new IntGroup("A", aItemsList),
                new IntGroup("B", bItemsList),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            ((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
            {
                collectionChangedEventArgs = e;
                collectionChangedEventsCount++;
            };
            ((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);

            source.Clear();

            readOnlyGroup.Should().BeEmpty();
            readOnlyGroup.Count.Should().Be(0);

            isCountPropertyChangedEventRaised.Should().BeTrue();
            collectionChangedEventArgs.Should().NotBeNull();
            collectionChangedEventsCount.Should().Be(1);
            IsResetEventValid(collectionChangedEventArgs).Should().BeTrue();
        }
        public void InsertGroupInSource_ShouldAddGroup(int insertionIndex)
        {
            NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
            var collectionChangedEventsCount      = 0;
            var isCountPropertyChangedEventRaised = false;
            var itemsList = new[] { 1, 2, 3 };
            var source    = new ObservableGroupedCollection <string, int>
            {
                new ObservableGroup <string, int>("Group0", new[] { 10, 20, 30 }),
                new ObservableGroup <string, int>("Group1", new[] { 40, 50, 60 })
            };
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            ((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
            {
                collectionChangedEventArgs = e;
                collectionChangedEventsCount++;
            };
            ((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);

            source.Insert(insertionIndex, new ObservableGroup <string, int>("Add", itemsList));

            readOnlyGroup.Should().HaveCount(3);
            readOnlyGroup.Count.Should().Be(3);
            readOnlyGroup.ElementAt(insertionIndex).Key.Should().Be("Add");
            readOnlyGroup.ElementAt(insertionIndex).Should().BeEquivalentTo(itemsList, o => o.WithoutStrictOrdering());

            isCountPropertyChangedEventRaised.Should().BeTrue();
            collectionChangedEventArgs.Should().NotBeNull();
            collectionChangedEventsCount.Should().Be(1);
            IsAddEventValid(collectionChangedEventArgs, itemsList, addIndex: insertionIndex).Should().BeTrue();
        }
        public void RemoveGroupInSource_ShoudRemoveGroup()
        {
            NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
            var collectionChangedEventsCount      = 0;
            var isCountPropertyChangedEventRaised = false;
            var aItemsList = new[] { 1, 2, 3 };
            var bItemsList = new[] { 2, 4, 6 };
            var groups     = new List <IGrouping <string, int> >
            {
                new IntGroup("A", aItemsList),
                new IntGroup("B", bItemsList),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            ((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
            {
                collectionChangedEventArgs = e;
                collectionChangedEventsCount++;
            };
            ((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);

            source.RemoveAt(1);

            readOnlyGroup.Should().ContainSingle();
            readOnlyGroup.Count.Should().Be(1);
            readOnlyGroup.ElementAt(0).Key.Should().Be("A");
            readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(aItemsList, o => o.WithoutStrictOrdering());

            isCountPropertyChangedEventRaised.Should().BeTrue();
            collectionChangedEventArgs.Should().NotBeNull();
            collectionChangedEventsCount.Should().Be(1);
            IsRemoveEventValid(collectionChangedEventArgs, bItemsList, 1).Should().BeTrue();
        }
        public void Ctor_WithEmptySource_ShoudInitializeObject()
        {
            var source        = new ObservableGroupedCollection <string, int>();
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            readOnlyGroup.Should().BeEmpty();
            readOnlyGroup.Count.Should().Be(0);
        }
        public void Ctor_WithListOfReadOnlyObservableGroupSource_ShoudInitializeObject()
        {
            var source = new List <ReadOnlyObservableGroup <string, int> >
            {
                new ReadOnlyObservableGroup <string, int>("A", new[] { 1, 3, 5 }),
                new ReadOnlyObservableGroup <string, int>("B", new[] { 2, 4, 6 }),
            };
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            readOnlyGroup.Should().HaveCount(2);
            readOnlyGroup.Count.Should().Be(2);
            readOnlyGroup.ElementAt(0).Key.Should().Be("A");
            readOnlyGroup.ElementAt(0).Should().BeEquivalentTo(new[] { 1, 3, 5 }, o => o.WithoutStrictOrdering());
            readOnlyGroup.ElementAt(1).Key.Should().Be("B");
            readOnlyGroup.ElementAt(1).Should().BeEquivalentTo(new[] { 2, 4, 6 }, o => o.WithoutStrictOrdering());
        }
        public void IListImplementation_Contains_ShoudReturnExpectedValue(int groupIndex, bool expectedResult)
        {
            var groups = new List <IGrouping <string, int> >
            {
                new IntGroup("A", new[] { 1, 3, 5 }),
                new IntGroup("B", new[] { 2, 4, 6 }),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);
            var list          = (IList)readOnlyGroup;

            var groupToSearch = groupIndex >= 0 ? list[groupIndex] : null;

            var result = list.Contains(groupToSearch);

            result.Should().Be(expectedResult);
        }
        public void IListImplementation_IndexOf_ShoudReturnExpectedValue(int groupIndex)
        {
            var groups = new List <IGrouping <string, int> >
            {
                new IntGroup("A", new[] { 1, 3, 5 }),
                new IntGroup("B", new[] { 2, 4, 6 }),
                new IntGroup("C", new[] { 7, 8, 9 }),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);
            var list          = (IList)readOnlyGroup;

            var groupToSearch = groupIndex >= 0 ? list[groupIndex] : null;

            var index = list.IndexOf(groupToSearch);

            index.Should().Be(groupIndex);
        }
Пример #9
0
        public ObservableGroupPage()
        {
            var contacts = new[]
            {
                new Person {
                    Name = "Staff"
                },
                new Person {
                    Name = "Swan"
                },
                new Person {
                    Name = "Orchid"
                },
                new Person {
                    Name = "Flame"
                },
                new Person {
                    Name = "Arrow"
                },
                new Person {
                    Name = "Tempest"
                },
                new Person {
                    Name = "Pearl"
                },
                new Person {
                    Name = "Hydra"
                },
                new Person {
                    Name = "Lamp Post"
                },
                new Person {
                    Name = "Looking Glass"
                },
            };
            var grouped = contacts.GroupBy(GetGroupName).OrderBy(g => g.Key);

            _contactsSource = new ObservableGroupedCollection <string, Person>(grouped);
            Contacts        = new ReadOnlyObservableGroupedCollection <string, Person>(_contactsSource);

            InitializeComponent();
        }
        public void IListImplementation_MutableMethods_ShoudThrow()
        {
            var groups = new List <IGrouping <string, int> >
            {
                new IntGroup("A", new[] { 1, 3, 5 }),
                new IntGroup("B", new[] { 2, 4, 6 }),
            };
            var source        = new ObservableGroupedCollection <string, int>(groups);
            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);
            var list          = (IList)readOnlyGroup;

            var    testGroup = new ReadOnlyObservableGroup <string, int>("test", new ObservableCollection <int>());
            Action add       = () => list.Add(testGroup);

            add.Should().Throw <NotSupportedException>();

            Action clear = () => list.Clear();

            clear.Should().Throw <NotSupportedException>();

            Action insert = () => list.Insert(2, testGroup);

            insert.Should().Throw <NotSupportedException>();

            Action remove = () => list.Remove(testGroup);

            remove.Should().Throw <NotSupportedException>();

            Action removeAt = () => list.RemoveAt(2);

            removeAt.Should().Throw <NotSupportedException>();

            Action set = () => list[2] = testGroup;

            set.Should().Throw <NotSupportedException>();

            var    array  = new object[5];
            Action copyTo = () => list.CopyTo(array, 0);

            copyTo.Should().NotThrow();
        }
        public void AddGroupInSource_ShouldAddGroup(int sourceInitialItemsCount, int expectedInsertionIndex)
        {
            NotifyCollectionChangedEventArgs collectionChangedEventArgs = null;
            var collectionChangedEventsCount      = 0;
            var isCountPropertyChangedEventRaised = false;
            var itemsList = new[] { 1, 2, 3 };
            var source    = new ObservableGroupedCollection <string, int>();

            for (var i = 0; i < sourceInitialItemsCount; i++)
            {
                source.Add(new ObservableGroup <string, int>($"group {i}", Enumerable.Empty <int>()));
            }

            var readOnlyGroup = new ReadOnlyObservableGroupedCollection <string, int>(source);

            ((INotifyCollectionChanged)readOnlyGroup).CollectionChanged += (s, e) =>
            {
                collectionChangedEventArgs = e;
                collectionChangedEventsCount++;
            };
            ((INotifyPropertyChanged)readOnlyGroup).PropertyChanged += (s, e) => isCountPropertyChangedEventRaised = isCountPropertyChangedEventRaised || e.PropertyName == nameof(readOnlyGroup.Count);

            source.Add(new ObservableGroup <string, int>("Add", itemsList));

            var expectedReadOnlyGroupCount = sourceInitialItemsCount + 1;

            readOnlyGroup.Should().HaveCount(expectedReadOnlyGroupCount);
            readOnlyGroup.Count.Should().Be(expectedReadOnlyGroupCount);
            readOnlyGroup.Last().Key.Should().Be("Add");
            readOnlyGroup.Last().Should().BeEquivalentTo(itemsList, o => o.WithoutStrictOrdering());

            isCountPropertyChangedEventRaised.Should().BeTrue();
            collectionChangedEventArgs.Should().NotBeNull();
            collectionChangedEventsCount.Should().Be(1);
            IsAddEventValid(collectionChangedEventArgs, itemsList, expectedInsertionIndex).Should().BeTrue();
        }